예제 #1
0
TEST_F(TestIBUtilField, AliasBytestr)
{
    const char s1[] = "hello";
    const char s2[] = "bye";
    ib_field_t *f;
    const ib_bytestr_t *obs;
    ib_status_t rc;
    ib_bytestr_t *bs;
    uint8_t *copy;

    copy = (uint8_t *)MemPoolMemDup("x", 1);
    rc = ib_field_create_bytestr_alias(&f, MemPool(),
                                       IB_FIELD_NAME("foo"), copy, 0);
    ASSERT_EQ(IB_OK, rc);

    rc = ib_bytestr_dup_nulstr(&bs, MemPool(), s1);
    ASSERT_EQ(IB_OK, rc);
    rc = ib_field_setv(f, bs);
    ASSERT_EQ(IB_OK, rc);
    rc = ib_field_value(f, ib_ftype_bytestr_out(&obs));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(strlen(s1), ib_bytestr_length(obs));
    ASSERT_EQ(0, memcmp(s1,
                        ib_bytestr_const_ptr(obs), ib_bytestr_length(obs)) );

    rc = ib_bytestr_dup_nulstr(&bs, MemPool(), s2);
    ASSERT_EQ(IB_OK, rc);
    rc = ib_field_setv(f, bs);
    ASSERT_EQ(IB_OK, rc);
    rc = ib_field_value(f, ib_ftype_bytestr_out(&obs));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(strlen(s2), ib_bytestr_length(obs));
    ASSERT_EQ(0, memcmp(s2,
                        ib_bytestr_const_ptr(obs), ib_bytestr_length(obs)) );
}
예제 #2
0
TEST_F(TestIBUtilField, Alias)
{
    ib_num_t   num1;
    ib_num_t   num2;
    ib_float_t flt1;
    ib_float_t flt2;
    char *s = NULL;
    const char *v;
    ib_field_t *f;
    ib_status_t rc;

    rc = ib_field_create_alias(&f, MemPool(), "foo", 3,
                               IB_FTYPE_NULSTR,
                               ib_ftype_nulstr_mutable_out(&s));
    ASSERT_EQ(IB_OK, rc);
    v = "hello";
    rc = ib_field_setv(f, ib_ftype_nulstr_in(v));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_STREQ(v, s);

    /*
     * Alias a numeric field
     */
    num1 = 1;
    rc = ib_field_create_alias(&f, MemPool(), "num", 3,
                               IB_FTYPE_NUM,
                               ib_ftype_num_in(&num1));
    ASSERT_EQ(IB_OK, rc);

    rc = ib_field_value(f, ib_ftype_num_out(&num2));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(num1, num2);

    num1 = 3;
    rc = ib_field_value(f, ib_ftype_num_out(&num2));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(num1, num2);


    /*
     * Alias a floating point field
     */
    flt1 = 1.1;
    rc = ib_field_create_alias(&f, MemPool(), "flt", 3,
                               IB_FTYPE_FLOAT,
                               ib_ftype_float_in(&flt1));
    ASSERT_EQ(IB_OK, rc);

    rc = ib_field_value(f, ib_ftype_float_out(&flt2));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(flt1, flt2);

    flt1 = 1.5;
    rc = ib_field_value(f, ib_ftype_float_out(&flt2));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(flt1, flt2);
}
예제 #3
0
/// @test Test util array library - IB_ARRAY_LOOP()
TEST_F(TestIBUtilArray, test_array_loop)
{
    ib_array_t *arr;
    size_t nelts;
    size_t i;
    int *val;
    ib_status_t rc;
    int init[20] = {
         0,  1,  2,  3,  4,  5,  6,  7,  8,  9,
        10, 11, 12, 13, 14, 15, 16, 17, 18, 19
    };
    const size_t count = (sizeof(init)/sizeof(int));
    size_t prev;

    rc = ib_array_create(&arr, MemPool(), 16, 8);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(arr);
    ASSERT_EQ(16UL, ib_array_size(arr));
    ASSERT_EQ(0UL, ib_array_elements(arr));

    for (i = 0; i < count; i++) {
        rc = ib_array_setn(arr, i, init + i);
        ASSERT_EQ(IB_OK, rc);
    }
    ASSERT_EQ(32UL, ib_array_size(arr));
    ASSERT_EQ(20UL, ib_array_elements(arr));
    rc = ib_array_get(arr, 1, &val);
    ASSERT_EQ(IB_OK, rc);

    prev = -1;
    IB_ARRAY_LOOP(arr, nelts, i, val) {
        ASSERT_EQ(i, prev+1);
        prev = i;
        ASSERT_EQ(init[i], *val);
    }
예제 #4
0
/// @test Test util field library - ib_field_format() with numeric types
TEST_F(TestIBUtilField, test_field_format_num)
{
    ib_field_t *f;
    ib_status_t rc;
    char fmtbuf[32];
    ib_num_t num;
    const char *tname;
    const char *buf;

    num = -10;
    rc = ib_field_create(&f, MemPool(), IB_FIELD_NAME("test_num"),
                         IB_FTYPE_NUM, ib_ftype_num_in(&num));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);

    buf = ib_field_format(f, false, false, &tname, fmtbuf, sizeof(fmtbuf));
    ASSERT_STREQ("-10", fmtbuf);
    ASSERT_STREQ("NUM", tname);
    ASSERT_EQ(buf, fmtbuf);

    buf = ib_field_format(f, true, true, &tname, fmtbuf, sizeof(fmtbuf));
    ASSERT_STREQ("-10", fmtbuf);
    ASSERT_STREQ("NUM", tname);
    ASSERT_EQ(buf, fmtbuf);
}
예제 #5
0
/// @test Test util list library - IB_LIST_LOOP()
TEST_F(TestIBUtilList, test_list_loop)
{
    ib_list_t *list;
    ib_list_node_t *node;
    ib_status_t rc;
    int init[] = { 0, 1, 2, 3, 4 };
    int *val;
    int i;

    rc = ib_list_create(&list, MemPool());
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(list);
    ASSERT_EQ(0UL, ib_list_elements(list));

    for (i = 0; i < 5; i++) {
        rc = ib_list_push(list, &init[i]);
        ASSERT_EQ(IB_OK, rc);
    }
    ASSERT_EQ(5UL, ib_list_elements(list));

    i = 0;
    IB_LIST_LOOP(list, node) {
        val = (int *)ib_list_node_data(node);
        ASSERT_EQ(init[i], *val);
        i++;
    }
예제 #6
0
/// @test Test util list library - ib_htree_list() and ib_list_destroy()
TEST_F(TestIBUtilList, test_list_create_and_destroy)
{
    ib_list_t *list;
    ib_status_t rc;

    rc = ib_list_create(&list, MemPool());
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(list);
    ASSERT_EQ(0UL, ib_list_elements(list));
}
예제 #7
0
/// @test Test util field library - ib_field_create() ib_field_create()
TEST_F(TestIBUtilField, test_field_create)
{
    ib_field_t *f;
    ib_status_t rc;
    const char *nulstrval = "TestValue";
    ib_num_t numval = 5;
    ib_bytestr_t *bytestrval;
    const char *nulout;
    const char *nulcopy;

    nulcopy = MemPoolStrDup(nulstrval);
    ASSERT_STRNE(NULL, nulcopy);
    rc = ib_field_create(&f, MemPool(), IB_FIELD_NAME("test_nulstr"),
                         IB_FTYPE_NULSTR, ib_ftype_nulstr_in(nulcopy));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);
    ASSERT_EQ(11UL, f->nlen);
    ASSERT_EQ(0, memcmp("test_nulstr", f->name, 11));

    rc = ib_field_value(f, ib_ftype_nulstr_out(&nulout));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_STREQ(nulstrval, nulout);

    rc = ib_field_create(&f, MemPool(), IB_FIELD_NAME("test_num"),
                         IB_FTYPE_NUM, ib_ftype_num_in(&numval));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);
    ASSERT_EQ(8UL, f->nlen);
    ASSERT_EQ(0, memcmp("test_num", f->name, 8));

    rc = ib_bytestr_dup_mem(&bytestrval, MemPool(),
                            (uint8_t *)nulstrval, strlen(nulstrval));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);

    rc = ib_field_create(&f, MemPool(), IB_FIELD_NAME("test_bytestr"),
                         IB_FTYPE_BYTESTR, ib_ftype_bytestr_in(bytestrval));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);
    ASSERT_EQ(12UL, f->nlen);
    ASSERT_EQ(0, memcmp("test_bytestr", f->name, 12));

    rc = ib_field_create(&f, MemPool(), IB_FIELD_NAME("test_nulstr_ex"),
                         IB_FTYPE_NULSTR, ib_ftype_nulstr_in(nulstrval));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);

    rc = ib_field_create(&f, MemPool(), IB_FIELD_NAME("test_num_ex"),
                         IB_FTYPE_NUM, ib_ftype_num_in(&numval));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);

    rc = ib_field_create(&f, MemPool(), IB_FIELD_NAME("test_bytestr_ex"),
                         IB_FTYPE_BYTESTR, ib_ftype_bytestr_in(bytestrval));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);
}
예제 #8
0
/// @test Test util array library - ib_htree_array() and ib_array_destroy()
TEST_F(TestIBUtilArray, test_array_create_and_destroy)
{
    ib_array_t *arr;
    ib_status_t rc;

    rc = ib_array_create(&arr, MemPool(), 10, 10);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(arr);
    ASSERT_EQ(10UL, ib_array_size(arr));
    ASSERT_EQ(0UL, ib_array_elements(arr));
}
예제 #9
0
/// @test Test util path functions - ib_util_relative_path()
TEST_F(TestIBUtilPath, relative_path)
{
    struct test_path_data_t *test;

    for (test = &test_rel_file[0]; test->in1 != NULL; ++test) {
        const char *out;
        out = ib_util_relative_file(MemPool(), test->in1, test->in2);
        EXPECT_STREQ(test->out, out)
            << "Test: in1 = '" << test->in1 << "'"
            << ", in2 = '" << test->in2 << "'";
    }
}
예제 #10
0
TEST_F(TestIBUtilField, Alias)
{
    char *s = NULL;
    const char *v;
    ib_field_t *f;
    ib_status_t rc;

    rc = ib_field_create_alias(&f, MemPool(), "foo", 3, IB_FTYPE_NULSTR,
        ib_ftype_nulstr_mutable_out(&s));
    ASSERT_EQ(IB_OK, rc);
    v = "hello";
    rc = ib_field_setv(f, ib_ftype_nulstr_in(v));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(std::string(v), std::string(s));
}
예제 #11
0
/// @test Test util field library - ib_field_from_string()
TEST_F(TestIBUtilField, test_field_from_string)
{
    ib_field_t *f;
    ib_status_t rc;
    ib_num_t fnum;
    ib_float_t ffloat;
    const char *fnulstr;

    rc = ib_field_from_string(MemPool(), IB_FIELD_NAME("test_num"),
                              "11", &f);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);
    ASSERT_EQ(IB_FTYPE_NUM, f->type);
    ASSERT_EQ(IB_OK, ib_field_value(f, ib_ftype_num_out(&fnum)));
    ASSERT_EQ(11, fnum);

    rc = ib_field_from_string(MemPool(), IB_FIELD_NAME("test_num"),
                              "-11", &f);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);
    ASSERT_EQ(IB_FTYPE_NUM, f->type);
    ASSERT_EQ(IB_OK, ib_field_value(f, ib_ftype_num_out(&fnum)));
    ASSERT_EQ(-11, fnum);

    rc = ib_field_from_string(MemPool(), IB_FIELD_NAME("test_float"),
                              "1.0", &f);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);
    ASSERT_EQ(IB_FTYPE_FLOAT, f->type);
    ASSERT_EQ(IB_OK, ib_field_value(f, ib_ftype_float_out(&ffloat)));
    ASSERT_EQ(1.0, ffloat);

    rc = ib_field_from_string(MemPool(), IB_FIELD_NAME("test_num"),
                              "-1.0", &f);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);
    ASSERT_EQ(IB_FTYPE_FLOAT, f->type);
    ASSERT_EQ(IB_OK, ib_field_value(f, ib_ftype_float_out(&ffloat)));
    ASSERT_EQ(-1.0, ffloat);

    rc = ib_field_from_string(MemPool(), IB_FIELD_NAME("test_str"),
                              "x", &f);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);
    ASSERT_EQ(IB_FTYPE_NULSTR, f->type);
    ASSERT_EQ(IB_OK, ib_field_value(f, ib_ftype_nulstr_out(&fnulstr)));
    ASSERT_STREQ("x", fnulstr);

    rc = ib_field_from_string(MemPool(), IB_FIELD_NAME("test_str"),
                              "-1.1x", &f);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);
    ASSERT_EQ(IB_FTYPE_NULSTR, f->type);
    ASSERT_EQ(IB_OK, ib_field_value(f, ib_ftype_nulstr_out(&fnulstr)));
    ASSERT_STREQ("-1.1x", fnulstr);
}
예제 #12
0
    ib_status_t RunTestStr(const BaseTestDatum &test, ib_flags_t &result)
    {
        char *out = NULL;
        char *in;
        ib_status_t rc;

        m_inbuf.Set(test.InBuf());
        in = const_cast<char *>(m_inbuf.GetBuf());
        assert(in != NULL);
        assert(m_strmod_fn != NULL);
        rc = m_strmod_fn(m_op, MemPool(), in, &out, &result);
        if (rc == IB_OK) {
            m_outbuf.SetStr(out);
        }
        return rc;
    }
예제 #13
0
    virtual ib_status_t RunTestEx(const BaseTestDatum &test, ib_flags_t &result)
    {
        uint8_t *out;
        size_t outlen;
        ib_status_t rc;

        m_inbuf.Set(test.InBuf());

        rc = m_strmod_ex_fn(m_op, MemPool(),
                            const_cast<uint8_t *>(m_inbuf.GetText()),
                            m_inbuf.GetLen(),
                            &out, &outlen,
                            &result);
        if (rc == IB_OK) {
            m_outbuf.SetText((char *)out, outlen);
        }
        return rc;
    }
예제 #14
0
/// @test Test util list library - ib_list_push() and ib_list_pop()
TEST_F(TestIBUtilList, test_list_push_and_pop)
{
    ib_list_t *list;
    ib_status_t rc;
    int v0 = 0;
    int v1 = 1;
    int v2 = 2;
    int v3 = 3;
    int v4 = 4;
    int *val;

    rc = ib_list_create(&list, MemPool());
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(list);
    ASSERT_EQ(0UL, ib_list_elements(list));

    /* Pop invalid. */
    rc = ib_list_pop(list,(void *)&val);
    ASSERT_EQ(IB_ENOENT, rc);
    ASSERT_FALSE(val);
    ASSERT_EQ(0UL, ib_list_elements(list));

    /* Simple pushes followed by pops. */
    rc = ib_list_push(list, &v0);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(1UL, ib_list_elements(list));
    rc = ib_list_push(list, &v1);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(2UL, ib_list_elements(list));
    rc = ib_list_push(list, &v2);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(3UL, ib_list_elements(list));
    rc = ib_list_push(list, &v3);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(4UL, ib_list_elements(list));
    rc = ib_list_push(list, &v4);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(5UL, ib_list_elements(list));
    ASSERT_EQ(v0, *(int *)(ib_list_node_data(ib_list_first(list))));
    ASSERT_EQ(v4, *(int *)(ib_list_node_data(ib_list_last(list))));
    rc = ib_list_pop(list, (void *)&val);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(val);
    ASSERT_EQ(v4, *val);
    ASSERT_EQ(4UL, ib_list_elements(list));
    rc = ib_list_pop(list, (void *)&val);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(val);
    ASSERT_EQ(v3, *val);
    ASSERT_EQ(3UL, ib_list_elements(list));
    rc = ib_list_pop(list, (void *)&val);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(val);
    ASSERT_EQ(v2, *val);
    ASSERT_EQ(2UL, ib_list_elements(list));
    rc = ib_list_pop(list, (void *)&val);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(val);
    ASSERT_EQ(v1, *val);
    ASSERT_EQ(1UL, ib_list_elements(list));
    rc = ib_list_pop(list, (void *)&val);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(val);
    ASSERT_EQ(v0, *val);
    ASSERT_EQ(0UL, ib_list_elements(list));
}
예제 #15
0
///@test Test util field library - ib_field_dyn_register_get()
TEST_F(TestIBUtilField, test_dyn_field)
{
    ib_field_t *dynf;
    ib_field_t *cdynf;
    ib_status_t rc;
    const char *fval;

    /* Create a field with no initial value. */
    rc = ib_field_create_dynamic(
        &dynf, MemPool(),
        IB_FIELD_NAME("test_dynf"), IB_FTYPE_NULSTR,
        dyn_get, (void *)"dynf_get",
        dyn_set, (void *)"dynf_set"
    );
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(dynf);
    ASSERT_EQ(9UL, dynf->nlen);
    ASSERT_EQ(0, memcmp("test_dynf", dynf->name, 9));

    /* Get the value from the dynamic field. */
    rc = ib_field_value_ex(dynf,
        ib_ftype_nulstr_out(&fval),
        (void *)"fetch1", 6
    );
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(fval);
    ASSERT_EQ(
        std::string("testval_dynf_get_fetch1_call01"),
        fval
    );

    /* Get the value from the dynamic field again. */
    rc = ib_field_value_ex(dynf,
        ib_ftype_nulstr_out(&fval),
        (void *)"fetch2", 6
    );
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(fval);
    ASSERT_EQ(
        std::string("testval_dynf_get_fetch2_call02"),
        fval
    );

    /* Set */
    rc = ib_field_setv_ex(dynf, (void *)"val1", (void *)"set1", 4);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(std::string("testval_dynf_set_set1_val1_call03"), g_dyn_call_val);

    /* Reset call counter. */
    g_dyn_call_count = 0;

    /* Create another field with no initial value. */
    rc = ib_field_create_dynamic(
        &cdynf, MemPool(),
        IB_FIELD_NAME("test_cdynf"), IB_FTYPE_NULSTR,
        dyn_get_cached, (void *)("cdynf_get"),
        dyn_set, NULL
    );
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(cdynf);
    ASSERT_EQ(10UL, cdynf->nlen);
    ASSERT_EQ(0, memcmp("test_cdynf", cdynf->name, 10));

    /* Get the value from the dynamic field. */
    rc = ib_field_value_ex(cdynf,
        ib_ftype_nulstr_out(&fval),
        (void *)"fetch1", 6
    );
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(fval);
    ASSERT_EQ(
        std::string("testval_cdynf_get_fetch1_call01"),
        fval
    );

    /* Get the value from the dynamic field again. */
    rc = ib_field_value_ex(cdynf,
        ib_ftype_nulstr_out(&fval),
        NULL, 0
    );
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(fval);
    ASSERT_EQ(
        std::string("testval_cdynf_get_fetch1_call01"),
        fval
    );
}
예제 #16
0
/// @test Test util field library - ib_field_format() with bytestr
TEST_F(TestIBUtilField, test_field_format_bytestr)
{
    ib_field_t *f;
    ib_status_t rc;
    char fmtbuf[32];
    const uint8_t in1[] = "a\0b";
    const uint8_t in2[] = "\fabcd\0efghijk\t";
    size_t size;
    ib_bytestr_t *bs;
    const char *tname;
    const char *buf;

    size = sizeof(in1) - 1;
    rc = ib_bytestr_dup_mem(&bs, MemPool(), in1, size);
    ASSERT_EQ(IB_OK, rc);

    rc = ib_field_create(&f, MemPool(), IB_FIELD_NAME("test_bytestr"),
                         IB_FTYPE_BYTESTR, ib_ftype_bytestr_in(bs));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f != NULL);

    buf = ib_field_format(f, false, false, &tname, fmtbuf, sizeof(fmtbuf));
    ASSERT_STREQ((const char *)in1, fmtbuf);
    ASSERT_STREQ("BYTESTR", tname);
    ASSERT_EQ(buf, fmtbuf);

    buf = ib_field_format(f, false, true, &tname, fmtbuf, sizeof(fmtbuf));
    ASSERT_STREQ("a\\u0000b", fmtbuf);
    ASSERT_STREQ("BYTESTR", tname);
    ASSERT_EQ(buf, fmtbuf);

    buf = ib_field_format(f, true, true, &tname, fmtbuf, sizeof(fmtbuf));
    ASSERT_STREQ("\"a\\u0000b\"", fmtbuf);
    ASSERT_STREQ("BYTESTR", tname);
    ASSERT_EQ(buf, fmtbuf);


    size = sizeof(in2) - 1;
    rc = ib_bytestr_dup_mem(&bs, MemPool(), in2, size);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f != NULL);

    rc = ib_field_create(&f, MemPool(), IB_FIELD_NAME("test_bytestr"),
                         IB_FTYPE_BYTESTR, ib_ftype_bytestr_in(bs));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);

    buf = ib_field_format(f, false, false, NULL, fmtbuf, sizeof(fmtbuf));
    ASSERT_STREQ((const char *)in2, fmtbuf);
    ASSERT_EQ(buf, fmtbuf);

    buf = ib_field_format(f, true, false, NULL, fmtbuf, sizeof(fmtbuf));
    ASSERT_STREQ("\"\fabcd\"", fmtbuf);
    ASSERT_EQ(buf, fmtbuf);

    buf = ib_field_format(f, false, true, NULL, fmtbuf, sizeof(fmtbuf));
    ASSERT_STREQ("\\fabcd\\u0000efghijk\\t", fmtbuf);
    ASSERT_EQ(buf, fmtbuf);

    buf = ib_field_format(f, true, true, NULL, fmtbuf, sizeof(fmtbuf));
    ASSERT_STREQ("\"\\fabcd\\u0000efghijk\\t\"", fmtbuf);
    ASSERT_EQ(buf, fmtbuf);
}
예제 #17
0
/// @test Test util field library - ib_field_format() with nulstr
TEST_F(TestIBUtilField, test_field_format_nulstr)
{
    ib_field_t *f;
    ib_status_t rc;
    char fmtbuf1[8];
    char fmtbuf2[32];
    const char *nul1 = "\"a\n\"";
    const char *nul2 = "\fabcdefghijk\t";
    const char *tname;
    const char *buf;
    char *nulcopy;

    nulcopy = MemPoolStrDup(nul1);
    ASSERT_STRNE(NULL, nulcopy);
    rc = ib_field_create(&f, MemPool(), IB_FIELD_NAME("test_nulstr"),
                         IB_FTYPE_NULSTR, ib_ftype_nulstr_in(nulcopy));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);

    buf = ib_field_format(f, false, false, &tname, fmtbuf1, sizeof(fmtbuf1));
    ASSERT_STREQ(nul1, fmtbuf1);
    ASSERT_STREQ("NULSTR", tname);
    ASSERT_EQ(buf, fmtbuf1);

    buf = ib_field_format(f, true, false, &tname, fmtbuf1, sizeof(fmtbuf1));
    ASSERT_STREQ("\"\"a\n\"\"", fmtbuf1);
    ASSERT_STREQ("NULSTR", tname);
    ASSERT_EQ(buf, fmtbuf1);

    buf = ib_field_format(f, true, true, &tname, fmtbuf1, sizeof(fmtbuf1));
    ASSERT_STREQ("\"\\\"a\\n\"", fmtbuf1);
    ASSERT_STREQ("NULSTR", tname);
    ASSERT_EQ(buf, fmtbuf1);


    nulcopy = MemPoolStrDup(nul2);
    ASSERT_STRNE(NULL, nulcopy);
    rc = ib_field_create(&f, MemPool(), IB_FIELD_NAME("test_nulstr"),
                         IB_FTYPE_NULSTR, ib_ftype_nulstr_in(nulcopy));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(f);

    buf = ib_field_format(f, false, false, NULL, fmtbuf1, sizeof(fmtbuf1));
    ASSERT_STREQ("\fabcdef", fmtbuf1);
    ASSERT_EQ(buf, fmtbuf1);

    buf = ib_field_format(f, false, false, NULL, fmtbuf2, sizeof(fmtbuf2));
    ASSERT_STREQ(nul2, fmtbuf2);
    ASSERT_EQ(buf, fmtbuf2);

    buf = ib_field_format(f, true, false, NULL, fmtbuf2, sizeof(fmtbuf2));
    ASSERT_STREQ("\"\fabcdefghijk\t\"", fmtbuf2);
    ASSERT_EQ(buf, fmtbuf2);

    buf = ib_field_format(f, false, true, NULL, fmtbuf2, sizeof(fmtbuf2));
    ASSERT_STREQ("\\fabcdefghijk\\t", fmtbuf2);
    ASSERT_EQ(buf, fmtbuf2);

    buf = ib_field_format(f, true, true, NULL, fmtbuf2, sizeof(fmtbuf2));
    ASSERT_STREQ("\"\\fabcdefghijk\\t\"", fmtbuf2);
    ASSERT_EQ(buf, fmtbuf2);

}
예제 #18
0
/// @test Test util array library - ib_htree_setn() and ib_array_get()
TEST_F(TestIBUtilArray, test_array_set_and_get)
{
    ib_array_t *arr;
    ib_status_t rc;
    int v0 = 0;
    int v9 = 9;
    int v10 = 10;
    int v99 = 99;
    int v100 = 100;
    int v1000 = 1000;
    int v1000000 = 1000000;
    int *val;

    rc = ib_array_create(&arr, MemPool(), 10, 10);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(arr);
    ASSERT_EQ(10UL, ib_array_size(arr));
    ASSERT_EQ(0UL, ib_array_elements(arr));

    /* Get invalid. */
    rc = ib_array_get(arr, 10, &val);
    ASSERT_EQ(IB_EINVAL, rc);
    ASSERT_FALSE(val);
    ASSERT_EQ(10UL, ib_array_size(arr));
    ASSERT_EQ(0UL, ib_array_elements(arr));

    /* Simple set. */
    rc = ib_array_setn(arr, 0, &v0);
    ASSERT_EQ(IB_OK, rc);
    rc = ib_array_get(arr, 0, &val);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(val);
    ASSERT_EQ(v0, *val);
    ASSERT_EQ(10UL, ib_array_size(arr));
    ASSERT_EQ(1UL, ib_array_elements(arr));

    /* Should not extend. */
    rc = ib_array_setn(arr, 9, &v9);
    ASSERT_EQ(IB_OK, rc);
    rc = ib_array_get(arr, 9, &val);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(val);
    ASSERT_EQ(v9, *val);
    ASSERT_EQ(10UL, ib_array_size(arr));
    ASSERT_EQ(10UL, ib_array_elements(arr));

    /* Should be null if unset. */
    rc = ib_array_get(arr, 5, &val);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_FALSE(val);
    ASSERT_EQ(10UL, ib_array_size(arr));
    ASSERT_EQ(10UL, ib_array_elements(arr));

    /* Should extend once. */
    rc = ib_array_setn(arr, 10, &v10);
    ASSERT_EQ(IB_OK, rc);
    rc = ib_array_get(arr, 10, &val);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(val);
    ASSERT_EQ(v10, *val);
    ASSERT_EQ(20UL, ib_array_size(arr));
    ASSERT_EQ(11UL, ib_array_elements(arr));

    /* Should extend to max. */
    rc = ib_array_setn(arr, 99, &v99);
    ASSERT_EQ(IB_OK, rc);
    rc = ib_array_get(arr, 99, &val);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(val);
    ASSERT_EQ(v99, *val);
    ASSERT_EQ(100UL, ib_array_size(arr));
    ASSERT_EQ(100UL, ib_array_elements(arr));

    /* Should reallocate extents. */
    rc = ib_array_setn(arr, 100, &v100);
    ASSERT_EQ(IB_OK, rc);
    rc = ib_array_get(arr, 100, &val);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(val);
    ASSERT_EQ(v100, *val);
    ASSERT_EQ(110UL, ib_array_size(arr));
    ASSERT_EQ(101UL, ib_array_elements(arr));

    /* Should reallocate extents 2 more times. */
    rc = ib_array_setn(arr, 1000, &v1000);
    ASSERT_EQ(IB_OK, rc);
    rc = ib_array_get(arr, 1000, &val);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(val);
    ASSERT_EQ(v1000, *val);
    ASSERT_EQ(1010UL, ib_array_size(arr));
    ASSERT_EQ(1001UL, ib_array_elements(arr));

    /* Should reallocate extents many more times. */
    rc = ib_array_setn(arr, 1000000, &v1000000);
    ASSERT_EQ(IB_OK, rc);
    rc = ib_array_get(arr, 1000000, &val);
    ASSERT_EQ(IB_OK, rc);
    ASSERT_TRUE(val);
    ASSERT_EQ(v1000000, *val);
    ASSERT_EQ(1000010UL, ib_array_size(arr));
    ASSERT_EQ(1000001UL, ib_array_elements(arr));
}
예제 #19
0
TEST_F(TestIBUtilField, ConvertString)
{
    ib_field_t *f1;
    ib_field_t *f2;
    ib_status_t rc;
    ib_num_t    num;
    ib_float_t  flt;

    /*
     * Convert numeric string to number
     */

    /* Create the field */
    rc = ib_field_create(&f1, MemPool(), "one", 3,
                         IB_FTYPE_NULSTR, ib_ftype_nulstr_in("1"));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MemPool(), IB_FTYPE_NUM, f1, &f2);
    ASSERT_EQ(IB_OK, rc);

    /* Pull out param value for check. */
    rc = ib_field_value(f2, ib_ftype_num_out(&num));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(1, num);

    /*
     * Convert floating-point string to float
     */

    /* Create the field */
    rc = ib_field_create(&f1, MemPool(), "one", 3,
                         IB_FTYPE_NULSTR, ib_ftype_nulstr_in("1.1"));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MemPool(), IB_FTYPE_FLOAT, f1, &f2);
    ASSERT_EQ(IB_OK, rc);

    /* Pull out param value for check. */
    rc = ib_field_value(f2, ib_ftype_float_out(&flt));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_FLOAT_EQ(1.1, flt);

    /*
     * Convert non-numeric string to number
     */

    /* Create the field */
    rc = ib_field_create(&f1, MemPool(), "one", 3,
                         IB_FTYPE_NULSTR, ib_ftype_nulstr_in("x1"));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MemPool(), IB_FTYPE_NUM, f1, &f2);
    ASSERT_EQ(IB_EINVAL, rc);

    /*
     * Convert floating-point string to number
     */

    /* Create the field */
    rc = ib_field_create(&f1, MemPool(), "one", 3,
                         IB_FTYPE_NULSTR, ib_ftype_nulstr_in("1.1"));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MemPool(), IB_FTYPE_NUM, f1, &f2);
    ASSERT_EQ(IB_EINVAL, rc);


    /*
     * Convert non-numeric string to float
     */

    /* Create the field */
    rc = ib_field_create(&f1, MemPool(), "one", 3,
                         IB_FTYPE_NULSTR, ib_ftype_nulstr_in("x1.1"));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MemPool(), IB_FTYPE_FLOAT, f1, &f2);
    ASSERT_EQ(IB_EINVAL, rc);
}
예제 #20
0
TEST_F(TestIBUtilField, ConvertNumbers)
{
    ib_field_t *f1;
    ib_field_t *f2;
    ib_status_t rc;
    ib_num_t    num;
    ib_float_t  flt;

    /*
     * Convert numeric to a float
     */

    /* Create the field */
    num = 1;
    rc = ib_field_create(&f1, MemPool(), "one", 3,
                         IB_FTYPE_NUM, ib_ftype_num_in(&num));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MemPool(), IB_FTYPE_FLOAT, f1, &f2);
    ASSERT_EQ(IB_OK, rc);

    /* Pull out param value for check. */
    rc = ib_field_value(f2, ib_ftype_float_out(&flt));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_FLOAT_EQ(1.0, flt);

    /*
     * Convert floating-point to num
     */

    /* Create the field */
    flt = 1.0;
    rc = ib_field_create(&f1, MemPool(), "one", 3,
                         IB_FTYPE_FLOAT, ib_ftype_float_in(&flt));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MemPool(), IB_FTYPE_NUM, f1, &f2);
    ASSERT_EQ(IB_OK, rc);

    /* Pull out param value for check. */
    rc = ib_field_value(f2, ib_ftype_num_in(&num));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(1, num);

    /*
     * Convert floating-point to num
     */

    /* Create the field */
    flt = 1.1;
    rc = ib_field_create(&f1, MemPool(), "one", 3,
                         IB_FTYPE_FLOAT, ib_ftype_float_in(&flt));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MemPool(), IB_FTYPE_NUM, f1, &f2);
    ASSERT_EQ(IB_OK, rc);

    /* Pull out param value for check. */
    rc = ib_field_value(f2, ib_ftype_num_in(&num));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(1, num);
}
예제 #21
0
TEST_F(TestIBUtilField, AliasConvert)
{
    char       *str;
    ib_field_t *f1;
    ib_field_t *f2;
    ib_status_t rc;
    ib_num_t    num;
    ib_float_t  flt;

    /*
     * Convert numeric string to number
     */

    /* Copy a number into the string */
    str = ib_mpool_strdup(MemPool(), "1");
    ASSERT_TRUE(str != NULL);

    /* Create the aliased field */
    rc = ib_field_create_alias(&f1, MemPool(), "one", 3,
                               IB_FTYPE_NULSTR,
                               ib_ftype_nulstr_mutable_out(&str));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MemPool(), IB_FTYPE_NUM, f1, &f2);
    ASSERT_EQ(IB_OK, rc);

    /* Pull out param value for check. */
    rc = ib_field_value(f2, ib_ftype_num_out(&num));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_EQ(1, num);

    /*
     * Convert floating-point string to float
     */

    /* Copy a number into the string */
    str = ib_mpool_strdup(MemPool(), "1.1");
    ASSERT_TRUE(str != NULL);

    /* Create the aliased field */
    rc = ib_field_create_alias(&f1, MemPool(), "one", 3,
                               IB_FTYPE_NULSTR,
                               ib_ftype_nulstr_mutable_out(&str));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MemPool(), IB_FTYPE_FLOAT, f1, &f2);
    ASSERT_EQ(IB_OK, rc);

    /* Pull out param value for check. */
    rc = ib_field_value(f2, ib_ftype_float_out(&flt));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_FLOAT_EQ(1.1, flt);

    /*
     * Convert non-numeric string to number
     */

    /* Copy a number into the string */
    str = ib_mpool_strdup(MemPool(), "x1");
    ASSERT_TRUE(str != NULL);

    /* Create the aliased field */
    rc = ib_field_create_alias(&f1, MemPool(), "one", 3,
                               IB_FTYPE_NULSTR,
                               ib_ftype_nulstr_mutable_out(&str));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MemPool(), IB_FTYPE_NUM, f1, &f2);
    ASSERT_EQ(IB_EINVAL, rc);

    /*
     * Convert floating-point string to number
     */

    /* Copy a number into the string */
    str = ib_mpool_strdup(MemPool(), "1.1");
    ASSERT_TRUE(str != NULL);

    /* Create the aliased field */
    rc = ib_field_create_alias(&f1, MemPool(), "one", 3,
                               IB_FTYPE_NULSTR,
                               ib_ftype_nulstr_mutable_out(&str));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MemPool(), IB_FTYPE_NUM, f1, &f2);
    ASSERT_EQ(IB_EINVAL, rc);


    /*
     * Convert non-numeric string to float
     */

    /* Copy a number into the string */
    str = ib_mpool_strdup(MemPool(), "1.1");
    ASSERT_TRUE(str != NULL);

    /* Create the aliased field */
    rc = ib_field_create_alias(&f1, MemPool(), "one", 3,
                               IB_FTYPE_NULSTR,
                               ib_ftype_nulstr_mutable_out(&str));
    ASSERT_EQ(IB_OK, rc);

    /* Attempt a numeric conversion. */
    rc = ib_field_convert(MemPool(), IB_FTYPE_FLOAT, f1, &f2);
    ASSERT_EQ(IB_OK, rc);

    /* Pull out param value for check. */
    rc = ib_field_value(f2, ib_ftype_float_out(&flt));
    ASSERT_EQ(IB_OK, rc);
    ASSERT_FLOAT_EQ(1.1, flt);
}