Example #1
0
static void
test_Mimic(TestBatch *batch) {
    Float32   *f32 = Float32_new(1.33f);
    Float64   *f64 = Float64_new(1.33);
    Integer32 *i32 = Int32_new(I32_MAX);
    Integer64 *i64 = Int64_new(I64_MAX);
    Float32   *f32_dupe = Float32_new(0.0f);
    Float64   *f64_dupe = Float64_new(0.0);
    Integer32 *i32_dupe = Int32_new(0);
    Integer64 *i64_dupe = Int64_new(0);
    Float32_Mimic(f32_dupe, (Obj*)f32);
    Float64_Mimic(f64_dupe, (Obj*)f64);
    Int32_Mimic(i32_dupe, (Obj*)i32);
    Int64_Mimic(i64_dupe, (Obj*)i64);
    TEST_TRUE(batch, Float32_Equals(f32, (Obj*)f32_dupe),
              "Float32 Mimic");
    TEST_TRUE(batch, Float64_Equals(f64, (Obj*)f64_dupe),
              "Float64 Mimic");
    TEST_TRUE(batch, Int32_Equals(i32, (Obj*)i32_dupe),
              "Integer32 Mimic");
    TEST_TRUE(batch, Int64_Equals(i64, (Obj*)i64_dupe),
              "Integer64 Mimic");
    DECREF(i64_dupe);
    DECREF(i32_dupe);
    DECREF(f64_dupe);
    DECREF(f32_dupe);
    DECREF(i64);
    DECREF(i32);
    DECREF(f64);
    DECREF(f32);
}
Example #2
0
static void
test_Clone(TestBatch *batch) {
    Float32   *f32 = Float32_new(1.33f);
    Float64   *f64 = Float64_new(1.33);
    Integer32 *i32 = Int32_new(I32_MAX);
    Integer64 *i64 = Int64_new(I64_MAX);
    Float32   *f32_dupe = Float32_Clone(f32);
    Float64   *f64_dupe = Float64_Clone(f64);
    Integer32 *i32_dupe = Int32_Clone(i32);
    Integer64 *i64_dupe = Int64_Clone(i64);
    TEST_TRUE(batch, Float32_Equals(f32, (Obj*)f32_dupe),
              "Float32 Clone");
    TEST_TRUE(batch, Float64_Equals(f64, (Obj*)f64_dupe),
              "Float64 Clone");
    TEST_TRUE(batch, Int32_Equals(i32, (Obj*)i32_dupe),
              "Integer32 Clone");
    TEST_TRUE(batch, Int64_Equals(i64, (Obj*)i64_dupe),
              "Integer64 Clone");
    TEST_TRUE(batch, Bool_Equals(CFISH_TRUE, (Obj*)Bool_Clone(CFISH_TRUE)),
              "BoolNum Clone");
    DECREF(i64_dupe);
    DECREF(i32_dupe);
    DECREF(f64_dupe);
    DECREF(f32_dupe);
    DECREF(i64);
    DECREF(i32);
    DECREF(f64);
    DECREF(f32);
}
Example #3
0
InverterEntry*
InvEntry_init(InverterEntry *self, Schema *schema, String *field,
              int32_t field_num) {
    InverterEntryIVARS *const ivars = InvEntry_IVARS(self);
    ivars->field_num  = field_num;
    ivars->field      = field ? Str_Clone(field) : NULL;
    ivars->inversion  = NULL;

    if (schema) {
        ivars->analyzer
            = (Analyzer*)INCREF(Schema_Fetch_Analyzer(schema, field));
        ivars->sim  = (Similarity*)INCREF(Schema_Fetch_Sim(schema, field));
        ivars->type = (FieldType*)INCREF(Schema_Fetch_Type(schema, field));
        if (!ivars->type) { THROW(ERR, "Unknown field: '%o'", field); }

        uint8_t prim_id = FType_Primitive_ID(ivars->type);
        switch (prim_id & FType_PRIMITIVE_ID_MASK) {
            case FType_TEXT:
                ivars->value = NULL;
                break;
            case FType_BLOB:
                ivars->value = (Obj*)ViewBB_new(NULL, 0);
                break;
            case FType_INT32:
                ivars->value = (Obj*)Int32_new(0);
                break;
            case FType_INT64:
                ivars->value = (Obj*)Int64_new(0);
                break;
            case FType_FLOAT32:
                ivars->value = (Obj*)Float32_new(0);
                break;
            case FType_FLOAT64:
                ivars->value = (Obj*)Float64_new(0);
                break;
            default:
                THROW(ERR, "Unrecognized primitive id: %i8", prim_id);
        }

        ivars->indexed = FType_Indexed(ivars->type);
        if (ivars->indexed && FType_Is_A(ivars->type, NUMERICTYPE)) {
            THROW(ERR, "Field '%o' spec'd as indexed, but numerical types cannot "
                  "be indexed yet", field);
        }
        if (FType_Is_A(ivars->type, FULLTEXTTYPE)) {
            ivars->highlightable
                = FullTextType_Highlightable((FullTextType*)ivars->type);
        }
    }
    return self;
}
Example #4
0
static void
test_illegal_keys(TestBatch *batch) {
    Hash *hash = Hash_new(0);
    Float64 *key = Float64_new(1.1);
    Hash_Store(hash, (Obj*)key, (Obj*)CB_newf("blah"));
    Err_set_error(NULL);
    CharBuf *not_json = Json_to_json((Obj*)hash);
    TEST_TRUE(batch, not_json == NULL,
              "to_json returns NULL when fed an illegal key");
    TEST_TRUE(batch, Err_get_error() != NULL,
              "to_json sets Err_error when fed an illegal key");
    DECREF(key);
    DECREF(hash);
}
Example #5
0
static void
S_round_trip_float(TestBatch *batch, double value, double max_diff) {
    Float64 *num = Float64_new(value);
    VArray *array = VA_new(1);
    VA_Store(array, 0, (Obj*)num);
    CharBuf *json = Json_to_json((Obj*)array);
    Obj *dump = CERTIFY(Json_from_json(json), VARRAY);
    Float64 *got = (Float64*)CERTIFY(VA_Fetch((VArray*)dump, 0), FLOAT64);
    double diff = Float64_Get_Value(num) - Float64_Get_Value(got);
    if (diff < 0) { diff = 0 - diff; }
    TEST_TRUE(batch, diff <= max_diff, "Round trip float %f", value);
    DECREF(dump);
    DECREF(json);
    DECREF(array);
}
Example #6
0
static void
test_To_String(TestBatch *batch) {
    Float32   *f32 = Float32_new(1.33f);
    Float64   *f64 = Float64_new(1.33);
    Integer32 *i32 = Int32_new(I32_MAX);
    Integer64 *i64 = Int64_new(I64_MAX);
    CharBuf *f32_string = Float32_To_String(f32);
    CharBuf *f64_string = Float64_To_String(f64);
    CharBuf *i32_string = Int32_To_String(i32);
    CharBuf *i64_string = Int64_To_String(i64);
    CharBuf *true_string  = Bool_To_String(CFISH_TRUE);
    CharBuf *false_string = Bool_To_String(CFISH_FALSE);

    TEST_TRUE(batch, CB_Starts_With_Str(f32_string, "1.3", 3),
              "Float32_To_String");
    TEST_TRUE(batch, CB_Starts_With_Str(f64_string, "1.3", 3),
              "Float64_To_String");
    TEST_TRUE(batch, CB_Equals_Str(i32_string, "2147483647", 10),
              "Int32_To_String");
    TEST_TRUE(batch, CB_Equals_Str(i64_string, "9223372036854775807", 19),
              "Int64_To_String");
    TEST_TRUE(batch, CB_Equals_Str(true_string, "true", 4),
              "Bool_To_String [true]");
    TEST_TRUE(batch, CB_Equals_Str(false_string, "false", 5),
              "Bool_To_String [false]");

    DECREF(false_string);
    DECREF(true_string);
    DECREF(i64_string);
    DECREF(i32_string);
    DECREF(f64_string);
    DECREF(f32_string);
    DECREF(i64);
    DECREF(i32);
    DECREF(f64);
    DECREF(f32);
}
Example #7
0
static Float64*
S_parse_number(char **json_ptr, char *const limit) {
    char *top = *json_ptr;
    char *end = top;
    bool_t terminated = false;

    // We can't assume NULL termination for the JSON string, so we need to
    // ensure that strtod() cannot overrun and access invalid memory.
    for (; end < limit; end++) {
        switch (*end) {
                // Only these characters may legally follow a number in
                // Javascript.  If we don't find one before the end of the JSON,
                // it's a parse error.
            case ' ': case '\n': case '\r': case '\t':
            case ']':
            case '}':
            case ':':
            case ',':
                terminated = true;
                break;
        }
    }

    Float64 *result = NULL;
    if (terminated) {
        char *terminus;
        double number = strtod(top, &terminus);
        if (terminus != top) {
            *json_ptr = terminus;
            result = Float64_new(number);
        }
    }
    if (!result) {
        SET_ERROR(CB_newf("JSON syntax error"), top, limit);
    }
    return result;
}
Example #8
0
static void
test_numbers(TestBatch *batch) {
    Integer64 *i64  = Int64_new(33);
    CharBuf   *json = Json_to_json((Obj*)i64);
    CB_Trim(json);
    TEST_TRUE(batch, json && CB_Equals_Str(json, "33", 2), "Integer");
    DECREF(json);

    Float64 *f64 = Float64_new(33.33);
    json = Json_to_json((Obj*)f64);
    if (json) {
        double value = CB_To_F64(json);
        double diff = 33.33 - value;
        if (diff < 0.0) { diff = 0.0 - diff; }
        TEST_TRUE(batch, diff < 0.0001, "Float");
        DECREF(json);
    }
    else {
        FAIL(batch, "Float conversion to  json  failed.");
    }

    DECREF(i64);
    DECREF(f64);
}
Example #9
0
Float64*
F64SortCache_make_blank(Float64SortCache *self) {
    UNUSED_VAR(self);
    return Float64_new(0.0);
}
Example #10
0
HitDoc*
DefDocReader_Fetch_Doc_IMP(DefaultDocReader *self, int32_t doc_id) {
    DefaultDocReaderIVARS *const ivars = DefDocReader_IVARS(self);
    Schema   *const schema = ivars->schema;
    InStream *const dat_in = ivars->dat_in;
    InStream *const ix_in  = ivars->ix_in;
    Hash     *const fields = Hash_new(1);
    int64_t   start;
    uint32_t  num_fields;
    uint32_t  field_name_cap = 31;
    char     *field_name = (char*)MALLOCATE(field_name_cap + 1);

    // Get data file pointer from index, read number of fields.
    InStream_Seek(ix_in, (int64_t)doc_id * 8);
    start = InStream_Read_U64(ix_in);
    InStream_Seek(dat_in, start);
    num_fields = InStream_Read_C32(dat_in);

    // Decode stored data and build up the doc field by field.
    while (num_fields--) {
        uint32_t        field_name_len;
        Obj       *value;
        FieldType *type;

        // Read field name.
        field_name_len = InStream_Read_C32(dat_in);
        if (field_name_len > field_name_cap) {
            field_name_cap = field_name_len;
            field_name     = (char*)REALLOCATE(field_name,
                                                    field_name_cap + 1);
        }
        InStream_Read_Bytes(dat_in, field_name, field_name_len);

        // Find the Field's FieldType.
        StackString *field_name_str
            = SSTR_WRAP_UTF8(field_name, field_name_len);
        type = Schema_Fetch_Type(schema, (String*)field_name_str);

        // Read the field value.
        switch (FType_Primitive_ID(type) & FType_PRIMITIVE_ID_MASK) {
            case FType_TEXT: {
                    uint32_t value_len = InStream_Read_C32(dat_in);
                    char *buf = (char*)MALLOCATE(value_len + 1);
                    InStream_Read_Bytes(dat_in, buf, value_len);
                    buf[value_len] = '\0'; 
                    value = (Obj*)Str_new_steal_utf8(buf, value_len);
                    break;
                }
            case FType_BLOB: {
                    uint32_t value_len = InStream_Read_C32(dat_in);
                    char *buf = (char*)MALLOCATE(value_len);
                    InStream_Read_Bytes(dat_in, buf, value_len);
                    value = (Obj*)BB_new_steal_bytes(
                                buf, value_len, value_len);
                    break;
                }
            case FType_FLOAT32:
                value = (Obj*)Float32_new(
                                InStream_Read_F32(dat_in));
                break;
            case FType_FLOAT64:
                value = (Obj*)Float64_new(
                                InStream_Read_F64(dat_in));
                break;
            case FType_INT32:
                value = (Obj*)Int32_new(
                                (int32_t)InStream_Read_C32(dat_in));
                break;
            case FType_INT64:
                value = (Obj*)Int64_new(
                                (int64_t)InStream_Read_C64(dat_in));
                break;
            default:
                value = NULL;
                THROW(ERR, "Unrecognized type: %o", type);
        }

        // Store the value.
        Hash_Store_Utf8(fields, field_name, field_name_len, value);
    }
    FREEMEM(field_name);

    HitDoc *retval = HitDoc_new(fields, doc_id, 0.0);
    DECREF(fields);
    return retval;
}
Example #11
0
static void
test_accessors(TestBatch *batch) {
    Float32   *f32 = Float32_new(1.0);
    Float64   *f64 = Float64_new(1.0);
    Integer32 *i32 = Int32_new(1);
    Integer64 *i64 = Int64_new(1);
    float  wanted32 = 1.33f;
    double wanted64 = 1.33;
    float  got32;
    double got64;

    Float32_Set_Value(f32, 1.33f);
    TEST_FLOAT_EQ(batch, Float32_Get_Value(f32), 1.33f,
                  "F32 Set_Value Get_Value");

    Float64_Set_Value(f64, 1.33);
    got64 = Float64_Get_Value(f64);
    TEST_TRUE(batch, *(int64_t*)&got64 == *(int64_t*)&wanted64,
              "F64 Set_Value Get_Value");

    TEST_TRUE(batch, Float32_To_I64(f32) == 1, "Float32_To_I64");
    TEST_TRUE(batch, Float64_To_I64(f64) == 1, "Float64_To_I64");

    got32 = (float)Float32_To_F64(f32);
    TEST_TRUE(batch, *(int32_t*)&got32 == *(int32_t*)&wanted32,
              "Float32_To_F64");

    got64 = Float64_To_F64(f64);
    TEST_TRUE(batch, *(int64_t*)&got64 == *(int64_t*)&wanted64,
              "Float64_To_F64");

    Int32_Set_Value(i32, I32_MIN);
    TEST_INT_EQ(batch, Int32_Get_Value(i32), I32_MIN,
                "I32 Set_Value Get_Value");

    Int64_Set_Value(i64, I64_MIN);
    TEST_TRUE(batch, Int64_Get_Value(i64) == I64_MIN,
              "I64 Set_Value Get_Value");

    Int32_Set_Value(i32, -1);
    Int64_Set_Value(i64, -1);
    TEST_TRUE(batch, Int32_To_F64(i32) == -1, "Int32_To_F64");
    TEST_TRUE(batch, Int64_To_F64(i64) == -1, "Int64_To_F64");

    TEST_INT_EQ(batch, Bool_Get_Value(CFISH_TRUE), true,
                "Bool_Get_Value [true]");
    TEST_INT_EQ(batch, Bool_Get_Value(CFISH_FALSE), false,
                "Bool_Get_Value [false]");
    TEST_TRUE(batch, Bool_To_I64(CFISH_TRUE) == true,
              "Bool_To_I64 [true]");
    TEST_TRUE(batch, Bool_To_I64(CFISH_FALSE) == false,
              "Bool_To_I64 [false]");
    TEST_TRUE(batch, Bool_To_F64(CFISH_TRUE) == 1.0,
              "Bool_To_F64 [true]");
    TEST_TRUE(batch, Bool_To_F64(CFISH_FALSE) == 0.0,
              "Bool_To_F64 [false]");

    DECREF(i64);
    DECREF(i32);
    DECREF(f64);
    DECREF(f32);
}
Example #12
0
static void
test_Equals_and_Compare_To(TestBatch *batch) {
    Float32   *f32 = Float32_new(1.0);
    Float64   *f64 = Float64_new(1.0);
    Integer32 *i32 = Int32_new(I32_MAX);
    Integer64 *i64 = Int64_new(I64_MAX);

    TEST_TRUE(batch, Float32_Compare_To(f32, (Obj*)f64) == 0,
              "F32_Compare_To equal");
    TEST_TRUE(batch, Float32_Equals(f32, (Obj*)f64),
              "F32_Equals equal");

    Float64_Set_Value(f64, 2.0);
    TEST_TRUE(batch, Float32_Compare_To(f32, (Obj*)f64) < 0,
              "F32_Compare_To less than");
    TEST_FALSE(batch, Float32_Equals(f32, (Obj*)f64),
               "F32_Equals less than");

    Float64_Set_Value(f64, 0.0);
    TEST_TRUE(batch, Float32_Compare_To(f32, (Obj*)f64) > 0,
              "F32_Compare_To greater than");
    TEST_FALSE(batch, Float32_Equals(f32, (Obj*)f64),
               "F32_Equals greater than");

    Float64_Set_Value(f64, 1.0);
    Float32_Set_Value(f32, 1.0);
    TEST_TRUE(batch, Float64_Compare_To(f64, (Obj*)f32) == 0,
              "F64_Compare_To equal");
    TEST_TRUE(batch, Float64_Equals(f64, (Obj*)f32),
              "F64_Equals equal");

    Float32_Set_Value(f32, 2.0);
    TEST_TRUE(batch, Float64_Compare_To(f64, (Obj*)f32) < 0,
              "F64_Compare_To less than");
    TEST_FALSE(batch, Float64_Equals(f64, (Obj*)f32),
               "F64_Equals less than");

    Float32_Set_Value(f32, 0.0);
    TEST_TRUE(batch, Float64_Compare_To(f64, (Obj*)f32) > 0,
              "F64_Compare_To greater than");
    TEST_FALSE(batch, Float64_Equals(f64, (Obj*)f32),
               "F64_Equals greater than");

    Float64_Set_Value(f64, I64_MAX * 2.0);
    TEST_TRUE(batch, Float64_Compare_To(f64, (Obj*)i64) > 0,
              "Float64 comparison to Integer64");
    TEST_TRUE(batch, Int64_Compare_To(i64, (Obj*)f64) < 0,
              "Integer64 comparison to Float64");

    Float32_Set_Value(f32, I32_MAX * 2.0f);
    TEST_TRUE(batch, Float32_Compare_To(f32, (Obj*)i32) > 0,
              "Float32 comparison to Integer32");
    TEST_TRUE(batch, Int32_Compare_To(i32, (Obj*)f32) < 0,
              "Integer32 comparison to Float32");

    Int64_Set_Value(i64, 0x6666666666666666LL);
    Integer64 *i64_copy = Int64_new(0x6666666666666666LL);
    TEST_TRUE(batch, Int64_Compare_To(i64, (Obj*)i64_copy) == 0,
              "Integer64 comparison to same number");

    TEST_TRUE(batch, Bool_Equals(CFISH_TRUE, (Obj*)CFISH_TRUE),
              "CFISH_TRUE Equals itself");
    TEST_TRUE(batch, Bool_Equals(CFISH_FALSE, (Obj*)CFISH_FALSE),
              "CFISH_FALSE Equals itself");
    TEST_FALSE(batch, Bool_Equals(CFISH_FALSE, (Obj*)CFISH_TRUE),
               "CFISH_FALSE not Equals CFISH_TRUE ");
    TEST_FALSE(batch, Bool_Equals(CFISH_TRUE, (Obj*)CFISH_FALSE),
               "CFISH_TRUE not Equals CFISH_FALSE ");
    TEST_FALSE(batch, Bool_Equals(CFISH_TRUE, (Obj*)CHARBUF),
               "CFISH_TRUE not Equals random other object ");

    DECREF(i64_copy);
    DECREF(i64);
    DECREF(i32);
    DECREF(f64);
    DECREF(f32);
}
Example #13
0
File: Num.c Project: theory/lucy
Float64*
Float64_clone(Float64 *self) {
    return Float64_new(self->value);
}
Example #14
0
static void
S_insert_num(NumPriorityQueue *pq, int32_t value) {
    NumPriQ_Insert(pq, (Obj*)Float64_new((double)value));
}
Example #15
0
File: Num.c Project: hernan604/lucy
Float64*
Float64_Clone_IMP(Float64 *self) {
    return Float64_new(self->value);
}