Ejemplo n.º 1
0
void testCheckDuplicates2(void) {
    bson b, b2;
    bson_iterator it, sit;
    bson_type bt;

    bson_init(&b);
    bson_append_start_array(&b, "array");
    bson_append_int(&b, "0", 1);
    bson_append_finish_array(&b);
    bson_append_start_array(&b, "array");
    bson_append_int(&b, "0", 3);
    bson_append_int(&b, "1", 4);
    bson_append_finish_array(&b);
    bson_finish(&b);
    CU_ASSERT_FALSE_FATAL(b.err);

    CU_ASSERT_TRUE_FATAL(bson_check_duplicate_keys(&b));

    bson_init(&b2);
    bson_fix_duplicate_keys(&b, &b2);
    bson_finish(&b2);
    CU_ASSERT_FALSE_FATAL(b2.err);

    CU_ASSERT_FALSE_FATAL(bson_check_duplicate_keys(&b2));

    BSON_ITERATOR_INIT(&it, &b2);
    bt = bson_iterator_next(&it);
    CU_ASSERT_EQUAL_FATAL(bt, BSON_ARRAY);
    CU_ASSERT_STRING_EQUAL_FATAL(BSON_ITERATOR_KEY(&it), "array");
    BSON_ITERATOR_SUBITERATOR(&it, &sit);
    
    bt = bson_iterator_next(&sit);
    CU_ASSERT_STRING_EQUAL_FATAL(BSON_ITERATOR_KEY(&sit), "0");
    CU_ASSERT_TRUE_FATAL(BSON_IS_NUM_TYPE(bt));
    CU_ASSERT_EQUAL_FATAL(bson_iterator_int(&sit), 1);
    bt = bson_iterator_next(&sit);
    CU_ASSERT_STRING_EQUAL_FATAL(BSON_ITERATOR_KEY(&sit), "1");
    CU_ASSERT_TRUE_FATAL(BSON_IS_NUM_TYPE(bt));
    CU_ASSERT_EQUAL_FATAL(bson_iterator_int(&sit), 3);
    bt = bson_iterator_next(&sit);
    CU_ASSERT_STRING_EQUAL_FATAL(BSON_ITERATOR_KEY(&sit), "2");
    CU_ASSERT_TRUE_FATAL(BSON_IS_NUM_TYPE(bt));
    CU_ASSERT_EQUAL_FATAL(bson_iterator_int(&sit), 4);
    
    bt = bson_iterator_next(&sit);
    CU_ASSERT_EQUAL_FATAL(bt, BSON_EOO);

    bson_destroy(&b2);
    bson_destroy(&b);
}
Ejemplo n.º 2
0
/**
* @brief fo_tableExists() tests:
*   - Check for an existing table
*   - Check for table that does not exist
*   - Check for a non table entities (sequence, constraint, ...)
* @return void
*/
void test_fo_tableExists()
{
  PGconn* pgConn;
  int nonexistant_table;
  int existing_table;
  char* DBConfFile = dbConf;
  char* ErrorBuf;

  pgConn = fo_dbconnect(DBConfFile, &ErrorBuf);

  CU_ASSERT_PTR_NOT_NULL(pgConn);

  nonexistant_table = fo_tableExists(pgConn, "nonexistanttable");
  CU_ASSERT_FALSE(nonexistant_table);

  PGresult* result = PQexec(pgConn, "CREATE table exists()");
  CU_ASSERT_PTR_NOT_NULL_FATAL(result);
  CU_ASSERT_FALSE_FATAL(fo_checkPQcommand(pgConn, result, "create", __FILE__, __LINE__));

  existing_table = fo_tableExists(pgConn, "exists");
  CU_ASSERT_TRUE(existing_table);

  PQfinish(pgConn);
  return;
}
Ejemplo n.º 3
0
void test_cancel()
{
  T1 t1, t2;
  CU_ASSERT_TRUE_FATAL(t1.cancel());
  t2.start();
  CU_ASSERT_FALSE_FATAL(t2.cancel());
  t2.stop();
}
Ejemplo n.º 4
0
static void test_open_on_dev_null_if_closed(
    void)
{
    int status = close(STDERR_FILENO);
    CU_ASSERT_EQUAL_FATAL(status, 0);
    status = open_on_dev_null_if_closed(STDERR_FILENO, O_RDWR);
    CU_ASSERT_EQUAL_FATAL(status, 0);
    CU_ASSERT_FALSE_FATAL(log_is_stderr_useful());
    CU_ASSERT_TRUE_FATAL(fcntl(STDERR_FILENO, F_GETFD) >= 0);
}
Ejemplo n.º 5
0
void test_initialize_record(void) {
    record = NULL;

    initialize_record(&record, SIZE);
    int i;
    for (i = 0; i < SIZE; i++) {
        CU_ASSERT_EQUAL_FATAL(record->members[i], i + 1);
        CU_ASSERT_EQUAL_FATAL(record->index[i], i);
        CU_ASSERT_FALSE_FATAL(record->direction[i]);
    }
}
Ejemplo n.º 6
0
void test_current()
{
  StdThreadRepository& tr =
    StdThreadRepository::instance();

  // this thread is not registered;
  CU_ASSERT_PTR_NULL_FATAL(RT::current());

  CU_ASSERT_EQUAL_FATAL(tr.size(), 0);
  T1* thread1 = T1::create<T1>("thread1");
  T1* thread2 = T1::create<T1>("thread2");
  thread1->start();
  thread2->start();
  thread1->is_terminal_state().wait();
  thread2->is_terminal_state().wait();
  CU_ASSERT_TRUE_FATAL(thread1->is_check_passed().signalled());
  CU_ASSERT_FALSE_FATAL(thread1->is_check_failed().signalled());
  CU_ASSERT_TRUE_FATAL(thread2->is_check_passed().signalled());
  CU_ASSERT_FALSE_FATAL(thread2->is_check_failed().signalled());
  thread1->remove(); // implies stop()
  thread2->remove();
}
Ejemplo n.º 7
0
static int numLines(
        const char* const pathname)
{
    FILE* stream = fopen(pathname, "r");
    CU_ASSERT_PTR_NOT_NULL_FATAL(stream);
    int n = 0;
    char line[_POSIX_MAX_INPUT];
    for (; fgets(line, sizeof(line), stream) != NULL; ++n)
        ;
    CU_ASSERT_TRUE_FATAL(feof(stream));
    CU_ASSERT_FALSE_FATAL(ferror(stream));
    CU_ASSERT_EQUAL_FATAL(fclose(stream), 0);
    return n;
}
Ejemplo n.º 8
0
Archivo: t1.c Proyecto: CowanSM/ejdb
void testBuildQuery1() {
    CU_ASSERT_PTR_NOT_NULL_FATAL(jb);
    /*
     Query = {
        "name" : Петров Петр,
        "age"  : 33,
        "family" : {
            "wife" : {
                "name"  : "Jeniffer",
                "age"   : {"$gt" : 25},
                "phone" : "444-111"
            },
            "children" : [
                {
                    "name" : "Dasha",
                    "age" : {"$in" : [1, 4, 10]}
                }
            ]
         }
     */
    bson q1;
    bson_init_as_query(&q1);
    bson_append_string(&q1, "name", "Петров Петр");
    bson_append_int(&q1, "age", 33);

    bson q1family_wife;
    bson_init_as_query(&q1family_wife);
    bson_append_string(&q1family_wife, "name", "Jeniffer");
    bson_append_start_object(&q1family_wife, "age");
    bson_append_int(&q1family_wife, "$gt", 25);
    bson_append_finish_object(&q1family_wife);

    bson_append_string(&q1family_wife, "phone", "444-111");
    bson_finish(&q1family_wife);

    bson q1family_child;
    bson_init_as_query(&q1family_child);
    bson_append_string(&q1family_child, "name", "Dasha");

    //"age" : {"$in" : [1, 4, 10]}
    bson q1family_child_age_IN;
    bson_init_as_query(&q1family_child_age_IN);
    bson_append_start_array(&q1family_child_age_IN, "$in");
    bson_append_int(&q1family_child_age_IN, "0", 1);
    bson_append_int(&q1family_child_age_IN, "1", 4);
    bson_append_int(&q1family_child_age_IN, "2", 10);
    bson_append_finish_array(&q1family_child_age_IN);
    bson_finish(&q1family_child_age_IN);
    bson_append_bson(&q1family_child, "age", &q1family_child_age_IN);
    bson_finish(&q1family_child);

    bson q1family;
    bson_init_as_query(&q1family);
    bson_append_bson(&q1family, "wife", &q1family_wife);
    bson_append_start_array(&q1family, "children");
    bson_append_bson(&q1family, "0", &q1family_child);
    bson_append_finish_array(&q1family);
    bson_finish(&q1family);

    bson_append_bson(&q1, "family", &q1family);
    bson_finish(&q1);

    CU_ASSERT_FALSE_FATAL(q1.err);
    CU_ASSERT_FALSE_FATAL(q1family.err);
    CU_ASSERT_FALSE_FATAL(q1family_wife.err);
    CU_ASSERT_FALSE_FATAL(q1family_child.err);
    CU_ASSERT_FALSE_FATAL(q1family_child_age_IN.err);

    EJQ *ejq = ejdbcreatequery(jb, &q1, NULL, 0, NULL);
    CU_ASSERT_PTR_NOT_NULL_FATAL(ejq);

    bson_destroy(&q1);
    bson_destroy(&q1family);
    bson_destroy(&q1family_wife);
    bson_destroy(&q1family_child);
    bson_destroy(&q1family_child_age_IN);

    CU_ASSERT_PTR_NOT_NULL_FATAL(ejq->qobjlist);
    TCLIST *qmap = ejq->qobjlist;
    CU_ASSERT_EQUAL(qmap->num, 7);

    for (int i = 0; i < TCLISTNUM(qmap); ++i) {

        const EJQF *qf = TCLISTVALPTR(qmap, i);
        CU_ASSERT_PTR_NOT_NULL_FATAL(qf);
        const char* key = qf->fpath;

        switch (i) {
            case 0:
            {
                CU_ASSERT_STRING_EQUAL(key, "name");
                CU_ASSERT_PTR_NOT_NULL(qf);
                CU_ASSERT_STRING_EQUAL(qf->expr, "Петров Петр");
                CU_ASSERT_EQUAL(qf->tcop, TDBQCSTREQ);
                break;
            }
            case 1:
            {
                CU_ASSERT_STRING_EQUAL(key, "age");
                CU_ASSERT_PTR_NOT_NULL(qf);
                CU_ASSERT_STRING_EQUAL(qf->expr, "33");
                CU_ASSERT_EQUAL(qf->tcop, TDBQCNUMEQ);
                break;
            }
            case 2:
            {
                CU_ASSERT_STRING_EQUAL(key, "family.wife.name");
                CU_ASSERT_PTR_NOT_NULL(qf);
                CU_ASSERT_STRING_EQUAL(qf->expr, "Jeniffer");
                CU_ASSERT_EQUAL(qf->tcop, TDBQCSTREQ);
                break;
            }
            case 3:
            {
                CU_ASSERT_STRING_EQUAL(key, "family.wife.age");
                CU_ASSERT_PTR_NOT_NULL(qf);
                CU_ASSERT_STRING_EQUAL(qf->expr, "25");
                CU_ASSERT_EQUAL(qf->tcop, TDBQCNUMGT);
                break;
            }
            case 4:
            {
                CU_ASSERT_STRING_EQUAL(key, "family.wife.phone");
                CU_ASSERT_PTR_NOT_NULL(qf);
                CU_ASSERT_STRING_EQUAL(qf->expr, "444-111");
                CU_ASSERT_EQUAL(qf->tcop, TDBQCSTREQ);
                break;
            }
            case 5:
            {
                CU_ASSERT_STRING_EQUAL(key, "family.children.0.name");
                CU_ASSERT_PTR_NOT_NULL(qf);
                CU_ASSERT_STRING_EQUAL(qf->expr, "Dasha");
                CU_ASSERT_EQUAL(qf->tcop, TDBQCSTREQ);
                break;
            }
            case 6:
            {
                CU_ASSERT_STRING_EQUAL(key, "family.children.0.age");
                CU_ASSERT_PTR_NOT_NULL(qf);
                CU_ASSERT_EQUAL(qf->ftype, BSON_ARRAY);
                TCLIST *al = tclistload(qf->expr, qf->exprsz);
                char* als = tcstrjoin(al, ',');
                CU_ASSERT_STRING_EQUAL(als, "1,4,10");
                TCFREE(als);
                tclistdel(al);
                CU_ASSERT_EQUAL(qf->tcop, TDBQCNUMOREQ);
                break;
            }
        }
    }

    ejdbquerydel(ejq);
}
Ejemplo n.º 9
0
void testCheckDuplicates(void) {
    bson bs, bs2;
    bson_iterator it;
    bson_type bt;
    
    bson_init(&bs);
    bson_append_string(&bs, "a", "a");
    bson_append_int(&bs, "b", 2);
    bson_append_null(&bs, "c");
    bson_append_start_object(&bs, "d");
    bson_append_string(&bs, "a", "a");
    bson_append_int(&bs, "e", 0);
    bson_append_int(&bs, "d", 1);
    bson_append_finish_object(&bs);
    bson_finish(&bs);
    CU_ASSERT_FALSE_FATAL(bs.err);

    CU_ASSERT_FALSE(bson_check_duplicate_keys(&bs));

    bson_destroy(&bs);

    bson_init(&bs);
    bson_append_string(&bs, "a", "a");
    bson_append_int(&bs, "b", 2);
    bson_append_null(&bs, "c");
    bson_append_start_object(&bs, "d");
    bson_append_string(&bs, "a", "a");
    bson_append_int(&bs, "e", 0);
    bson_append_int(&bs, "e", 1);
    bson_append_finish_object(&bs);
    bson_finish(&bs);
    CU_ASSERT_FALSE_FATAL(bs.err);

    CU_ASSERT_TRUE(bson_check_duplicate_keys(&bs));

    bson_init(&bs2);
    bson_fix_duplicate_keys(&bs, &bs2);
    bson_finish(&bs2);

    CU_ASSERT_FALSE(bson_check_duplicate_keys(&bs2));
    BSON_ITERATOR_INIT(&it, &bs2);
    bt = bson_find_fieldpath_value("d.e", &it);
    CU_ASSERT_TRUE(BSON_IS_NUM_TYPE(bt));
    CU_ASSERT_EQUAL(bson_iterator_int(&it), 1);

    bson_destroy(&bs2);

    bson_init(&bs);
    bson_append_string(&bs, "a", "a");
    bson_append_int(&bs, "b", 2);
    bson_append_null(&bs, "c");
    bson_append_start_object(&bs, "d");
    bson_append_string(&bs, "a", "a");
    bson_append_int(&bs, "e", 0);
    bson_append_int(&bs, "d", 1);
    bson_append_finish_object(&bs);
    bson_append_start_array(&bs, "f");
    bson_append_start_object(&bs, "0");
    bson_append_string(&bs, "a", "a");
    bson_append_string(&bs, "b", "b");
    bson_append_int(&bs, "c", 1);
    bson_append_finish_object(&bs);
    bson_append_start_object(&bs, "1");
    bson_append_string(&bs, "a", "a");
    bson_append_string(&bs, "b", "b");
    bson_append_int(&bs, "c", 1);
    bson_append_finish_object(&bs);
    bson_append_finish_array(&bs);
    bson_finish(&bs);
    CU_ASSERT_FALSE_FATAL(bs.err);

    CU_ASSERT_FALSE(bson_check_duplicate_keys(&bs));

    bson_init(&bs);
    bson_append_string(&bs, "a", "a");
    bson_append_int(&bs, "b", 2);
    bson_append_null(&bs, "c");
    bson_append_start_object(&bs, "d");
    bson_append_string(&bs, "a", "a");
    bson_append_int(&bs, "e", 0);
    bson_append_int(&bs, "d", 1);
    bson_append_start_object(&bs, "q");
    bson_append_int(&bs, "w", 0);
    bson_append_finish_object(&bs);
    bson_append_finish_object(&bs);
    bson_append_start_array(&bs, "f");
    bson_append_start_object(&bs, "0");
    bson_append_string(&bs, "a", "a");
    bson_append_string(&bs, "b", "b");
    bson_append_int(&bs, "a", 1);
    bson_append_finish_object(&bs);
    bson_append_start_object(&bs, "1");
    bson_append_string(&bs, "a", "a");
    bson_append_string(&bs, "b", "b");
    bson_append_int(&bs, "c", 1);
    bson_append_finish_object(&bs);
    bson_append_finish_array(&bs);
    bson_append_start_object(&bs, "a");
    bson_append_finish_object(&bs);
    bson_append_start_object(&bs, "d");
    bson_append_start_object(&bs, "q");
    bson_append_int(&bs, "e", 1);
    bson_append_finish_object(&bs);
    bson_append_finish_object(&bs);
    bson_finish(&bs);
    CU_ASSERT_FALSE_FATAL(bs.err);

    CU_ASSERT_TRUE(bson_check_duplicate_keys(&bs));

    bson_init(&bs2);
    bson_fix_duplicate_keys(&bs, &bs2);
    bson_finish(&bs2);

    CU_ASSERT_FALSE(bson_check_duplicate_keys(&bs2));
    BSON_ITERATOR_INIT(&it, &bs2);
    bt = bson_find_fieldpath_value("f.0.a", &it);
    CU_ASSERT_TRUE(BSON_IS_NUM_TYPE(bt));
    CU_ASSERT_EQUAL(bson_iterator_int(&it), 1);
    BSON_ITERATOR_INIT(&it, &bs2);
    bt = bson_find_fieldpath_value("f.1.a", &it);
    CU_ASSERT_TRUE(BSON_IS_STRING_TYPE(bt));
    CU_ASSERT_FALSE(strcmp(bson_iterator_string(&it), "a"));

    BSON_ITERATOR_INIT(&it, &bs2);
    bt = bson_find_fieldpath_value("a", &it);
    CU_ASSERT_EQUAL(bt, BSON_OBJECT);

    BSON_ITERATOR_INIT(&it, &bs2);
    bt = bson_find_fieldpath_value("d.q.w", &it);
    CU_ASSERT_TRUE(BSON_IS_NUM_TYPE(bt));
    CU_ASSERT_EQUAL(bson_iterator_int(&it), 0);
    BSON_ITERATOR_INIT(&it, &bs2);
    bt = bson_find_fieldpath_value("d.q.e", &it);
    CU_ASSERT_TRUE(BSON_IS_NUM_TYPE(bt));
    CU_ASSERT_EQUAL(bson_iterator_int(&it), 1);
}
Ejemplo n.º 10
0
void test_fill_binkeys(void) {
    const uint samples = 10000;
    typedef struct {
        uchar data[SHA_DIGEST_LENGTH] __attribute__((aligned(JUDY_key_size)));
    } _key_t __attribute__((aligned(JUDY_key_size)));
    unsigned int offset = 0xffff;
    uint *seen;
    Judy *j;
    JudySlot *slot;
    _key_t *keys;
    _key_t last;
    uint idx;

    j = judy_open_bin(sizeof(_key_t));
    CU_ASSERT_PTR_NOT_NULL_FATAL(j);

    seen = malloc(samples*sizeof(uint));
    CU_ASSERT_PTR_NOT_NULL_FATAL(seen);
    memset(seen, 0, samples*sizeof(uint));

    keys = malloc(samples*sizeof(_key_t));
    CU_ASSERT_PTR_NOT_NULL_FATAL(keys);
    memset(keys, 0, samples*sizeof(_key_t));
    for (idx=0; idx<samples; ++idx) {
        unsigned char r[128];
        unsigned char d[SHA_DIGEST_LENGTH];
        CU_ASSERT_EQUAL(RAND_bytes(r, sizeof(r)), 1);
        SHA1(r, sizeof(r), d);
        memcpy(keys[idx].data, d, sizeof(keys[idx].data));
    }

    CU_ASSERT_FALSE_FATAL(judy_key_bin(j, &last));

    idx = 0;
    slot = judy_strt(j, NULL, 0);
    while (slot) {
        ++idx;
        slot = judy_nxt(j);
    }
    CU_ASSERT_EQUAL_FATAL(idx, 0);

    for (idx=0; idx<samples; ++idx) {
        slot = judy_slot_bin(j, keys[idx].data);
        CU_ASSERT_PTR_NULL_FATAL(slot);
        slot = judy_cell_bin(j, keys[idx].data);
        CU_ASSERT_PTR_NOT_NULL_FATAL(slot);
        *slot = idx + offset;
        slot = judy_slot_bin(j, keys[idx].data);
        CU_ASSERT_PTR_NOT_NULL_FATAL(slot);
        CU_ASSERT_EQUAL_FATAL(*slot, idx + offset);
        CU_ASSERT_TRUE_FATAL(judy_key_bin(j, &last));
        CU_ASSERT_EQUAL_FATAL(memcmp(&keys[idx], &last, sizeof(_key_t)), 0);
    }

    for (idx=0; idx<samples; ++idx) {
        slot = judy_slot_bin(j, keys[idx].data);
        CU_ASSERT_PTR_NOT_NULL_FATAL(slot);
        CU_ASSERT_EQUAL_FATAL(*slot, idx + offset);
        CU_ASSERT_TRUE_FATAL(judy_key_bin(j, &last));
        CU_ASSERT_EQUAL_FATAL(memcmp(&keys[idx], &last, sizeof(_key_t)), 0);
    }

    idx = 0;
    slot = judy_strt(j, NULL, 0);
    CU_ASSERT_PTR_NOT_NULL_FATAL(slot);
    while (slot) {
        ++idx;
        if (slot) {
            ++seen[*slot - offset];
            CU_ASSERT_TRUE_FATAL(judy_key_bin(j, &last));
            CU_ASSERT_EQUAL_FATAL(memcmp(&keys[*slot - offset], &last, sizeof(_key_t)), 0);
        }
        slot = judy_nxt(j);
    }
    CU_ASSERT_EQUAL_FATAL(idx, samples);

    for (idx=0; idx<samples; ++idx) {
        slot = judy_slot_bin(j, keys[idx].data);
        CU_ASSERT_PTR_NOT_NULL_FATAL(slot);
        CU_ASSERT_EQUAL_FATAL(*slot, idx + offset);
        CU_ASSERT_EQUAL_FATAL(seen[idx], 1);
        judy_del(j);
        slot = judy_slot_bin(j, keys[idx].data);
        CU_ASSERT_PTR_NULL_FATAL(slot);
    }

    for (idx=0; idx<samples; ++idx) {
        slot = judy_slot_bin(j, keys[idx].data);
        CU_ASSERT_PTR_NULL_FATAL(slot);
    }

    free(keys);
    free(seen);

    judy_close(j);
}