static void
test_Grow(TestBatchRunner *runner) {
    ByteBuf *bb = BB_new(1);
    TEST_UINT_EQ(runner, BB_Get_Capacity(bb), 8,
                "Allocate in 8-byte increments");
    BB_Grow(bb, 9);
    TEST_UINT_EQ(runner, BB_Get_Capacity(bb), 16,
                "Grow in 8-byte increments");
    DECREF(bb);
}
Example #2
0
static void
test_To_Array(TestBatchRunner *runner) {
    uint64_t  *source_ints = TestUtils_random_u64s(NULL, 20, 0, 200);
    BitVector *bit_vec = BitVec_new(0);
    I32Array  *array;
    unsigned   num_unique = 0;

    // Unique the random ints.
    qsort(source_ints, 20, sizeof(uint64_t), S_compare_u64s);
    for (unsigned i = 0; i < 19; i++) {
        if (source_ints[i] != source_ints[i + 1]) {
            source_ints[num_unique] = source_ints[i];
            num_unique++;
        }
    }

    // Set bits.
    for (unsigned i = 0; i < num_unique; i++) {
        BitVec_Set(bit_vec, (size_t)source_ints[i]);
    }

    // Create the array and compare it to the source.
    array = BitVec_To_Array(bit_vec);
    unsigned i;
    for (i = 0; i < num_unique; i++) {
        if (I32Arr_Get(array, (size_t)i) != (int32_t)source_ints[i]) { break; }
    }
    TEST_UINT_EQ(runner, i, num_unique, "To_Array (%u == %u)", i,
                 num_unique);

    DECREF(array);
    DECREF(bit_vec);
    FREEMEM(source_ints);
}
static void
test_Equals(TestBatchRunner *runner) {
    ByteBuf *bb = BB_new_bytes("foo", 4); // Include terminating NULL.

    {
        ByteBuf *other = BB_new_bytes("foo", 4);
        TEST_TRUE(runner, BB_Equals(bb, (Obj*)other), "Equals");
        DECREF(other);
    }

    TEST_TRUE(runner, BB_Equals_Bytes(bb, "foo", 4), "Equals_Bytes");
    TEST_FALSE(runner, BB_Equals_Bytes(bb, "foo", 3),
               "Equals_Bytes spoiled by different size");
    TEST_FALSE(runner, BB_Equals_Bytes(bb, "bar", 4),
               "Equals_Bytes spoiled by different content");

    {
        ByteBuf *other = BB_new_bytes("foo", 3);
        TEST_FALSE(runner, BB_Equals(bb, (Obj*)other),
                   "Different size spoils Equals");
        DECREF(other);
    }

    {
        ByteBuf *other = BB_new_bytes("bar", 4);
        TEST_UINT_EQ(runner, BB_Get_Size(bb), BB_Get_Size(other),
                     "same length");
        TEST_FALSE(runner, BB_Equals(bb, (Obj*)other),
                   "Different content spoils Equals");
        DECREF(other);
    }

    DECREF(bb);
}
Example #4
0
static void
test_And(TestBatchRunner *runner) {
    BitVector *smaller = S_create_set(1);
    BitVector *larger  = S_create_set(2);
    BitVector *set_1   = S_create_set(1);
    BitVector *set_2   = S_create_set(2);

    BitVec_And(smaller, set_2);
    TEST_UINT_EQ(runner, S_verify_logical_op(smaller, set_1, set_2, OP_AND),
                 50, "AND with self smaller than other");
    BitVec_And(larger, set_1);
    TEST_UINT_EQ(runner, S_verify_logical_op(larger, set_1, set_2, OP_AND),
                 50, "AND with other smaller than self");

    DECREF(smaller);
    DECREF(larger);
    DECREF(set_1);
    DECREF(set_2);
}
Example #5
0
static void
test_Clone(TestBatchRunner *runner) {
    BitVector *self = BitVec_new(30);
    BitVector *twin;

    BitVec_Set(self, 2);
    BitVec_Set(self, 3);
    BitVec_Set(self, 10);
    BitVec_Set(self, 20);

    twin = BitVec_Clone(self);
    size_t i;
    for (i = 0; i < 50; i++) {
        if (BitVec_Get(self, i) != BitVec_Get(twin, i)) { break; }
    }
    TEST_UINT_EQ(runner, i, 50, "Clone");
    TEST_UINT_EQ(runner, BitVec_Count(twin), 4, "clone Count");

    DECREF(self);
    DECREF(twin);
}
Example #6
0
static void
test_overlap(TestBatchRunner *runner) {
    size_t result;
    result = StrHelp_overlap("", "", 0, 0);
    TEST_UINT_EQ(runner, result, 0, "two empty strings");
    result = StrHelp_overlap("", "foo", 0, 3);
    TEST_UINT_EQ(runner, result, 0, "first string is empty");
    result = StrHelp_overlap("foo", "", 3, 0);
    TEST_UINT_EQ(runner, result, 0, "second string is empty");
    result = StrHelp_overlap("foo", "foo", 3, 3);
    TEST_UINT_EQ(runner, result, 3, "equal strings");
    result = StrHelp_overlap("foo bar", "foo", 7, 3);
    TEST_UINT_EQ(runner, result, 3, "first string is longer");
    result = StrHelp_overlap("foo", "foo bar", 3, 7);
    TEST_UINT_EQ(runner, result, 3, "second string is longer");
    result = StrHelp_overlap("bar", "baz", 3, 3);
    TEST_UINT_EQ(runner, result, 2, "different byte");
}
Example #7
0
static void
test_Count(TestBatchRunner *runner) {
    unsigned shuffled[64];
    BitVector *bit_vec = BitVec_new(64);

    for (unsigned i = 0; i < 64; i++) { shuffled[i] = i; }
    for (unsigned i = 0; i < 64; i++) {
        unsigned shuffle_pos = (unsigned)rand() % 64;
        unsigned temp = shuffled[shuffle_pos];
        shuffled[shuffle_pos] = shuffled[i];
        shuffled[i] = temp;
    }
    unsigned i;
    for (i = 0; i < 64; i++) {
        BitVec_Set(bit_vec, shuffled[i]);
        if (BitVec_Count(bit_vec) != (uint32_t)(i + 1)) { break; }
    }
    TEST_UINT_EQ(runner, i, 64, "Count() returns the right number of bits");

    DECREF(bit_vec);
}
Example #8
0
static void
test_Mimic(TestBatchRunner *runner) {
    for (unsigned foo = 0; foo <= 17; foo++) {
        for (unsigned bar = 0; bar <= 17; bar++) {
            BitVector *foo_vec = BitVec_new(0);
            BitVector *bar_vec = BitVec_new(0);

            BitVec_Set(foo_vec, foo);
            BitVec_Set(bar_vec, bar);
            BitVec_Mimic(foo_vec, (Obj*)bar_vec);

            unsigned i;
            for (i = 0; i <= 17; i++) {
                if (BitVec_Get(foo_vec, i) && i != bar) { break; }
            }
            TEST_UINT_EQ(runner, i, 18, "Mimic(%u, %u)", foo, bar);

            DECREF(foo_vec);
            DECREF(bar_vec);
        }
    }
}
Example #9
0
static void
test_Flip_Block_bulk(TestBatchRunner *runner) {
    for (unsigned offset = 0; offset <= 17; offset++) {
        for (unsigned len = 0; len <= 17; len++) {
            int upper = (int)offset + (int)len - 1;
            BitVector *bit_vec = BitVec_new(0);

            BitVec_Flip_Block(bit_vec, offset, len);
            unsigned i;
            for (i = 0; i <= 17; i++) {
                if (i >= offset && (int)i <= upper) {
                    if (!BitVec_Get(bit_vec, i)) { break; }
                }
                else {
                    if (BitVec_Get(bit_vec, i)) { break; }
                }
            }
            TEST_UINT_EQ(runner, i, 18, "Flip_Block(%u, %u)", offset, len);

            DECREF(bit_vec);
        }
    }
}
Example #10
0
static void
S_do_test_matrix(TestBatchRunner *runner, int32_t doc_max, int32_t first_doc_id,
                 int32_t doc_inc, int32_t offset_inc) {
    I32Array *doc_ids
        = S_generate_match_list(first_doc_id, doc_max, doc_inc);
    I32Array *offsets
        = S_generate_match_list(0, doc_max, offset_inc);
    SeriesMatcher *series_matcher
        = S_make_series_matcher(doc_ids, offsets, doc_max);
    size_t num_in_agreement = 0;
    int32_t got;

    while (0 != (got = SeriesMatcher_Next(series_matcher))) {
        if (got != I32Arr_Get(doc_ids, num_in_agreement)) { break; }
        num_in_agreement++;
    }
    TEST_UINT_EQ(runner, num_in_agreement, I32Arr_Get_Size(doc_ids),
                 "doc_max=%d first_doc_id=%d doc_inc=%d offset_inc=%d",
                 doc_max, first_doc_id, doc_inc, offset_inc);

    DECREF(doc_ids);
    DECREF(offsets);
    DECREF(series_matcher);
}