Rework BitVector tests for index width change.

Changing indexes from uint32_t to size_t requires updating a fair amount
of test code.


Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/e03b8c71
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/e03b8c71
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/e03b8c71

Branch: refs/heads/master
Commit: e03b8c7160bca49bb212bdf027a7f9ca2d2c4524
Parents: 0a6ea88
Author: Marvin Humphrey <[email protected]>
Authored: Wed Apr 6 17:56:36 2016 -0700
Committer: Marvin Humphrey <[email protected]>
Committed: Tue Apr 12 21:38:55 2016 -0700

----------------------------------------------------------------------
 core/Lucy/Test/Object/TestBitVector.c | 169 +++++++++++++----------------
 1 file changed, 78 insertions(+), 91 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/e03b8c71/core/Lucy/Test/Object/TestBitVector.c
----------------------------------------------------------------------
diff --git a/core/Lucy/Test/Object/TestBitVector.c 
b/core/Lucy/Test/Object/TestBitVector.c
index c8c4203..d6080b7 100644
--- a/core/Lucy/Test/Object/TestBitVector.c
+++ b/core/Lucy/Test/Object/TestBitVector.c
@@ -33,10 +33,9 @@ TestBitVector_new() {
 
 static void
 test_Set_and_Get(TestBatchRunner *runner) {
-    unsigned i, max;
-    const uint32_t  three     = 3;
-    const uint32_t  seventeen = 17;
-    BitVector      *bit_vec   = BitVec_new(8);
+    const size_t  three     = 3;
+    const size_t  seventeen = 17;
+    BitVector    *bit_vec   = BitVec_new(8);
 
     BitVec_Set(bit_vec, three);
     TEST_TRUE(runner, BitVec_Get_Capacity(bit_vec) < seventeen,
@@ -45,12 +44,13 @@ test_Set_and_Get(TestBatchRunner *runner) {
     TEST_TRUE(runner, BitVec_Get_Capacity(bit_vec) > seventeen,
               "set above cap causes BitVector to grow");
 
+    size_t i, max;
     for (i = 0, max = BitVec_Get_Capacity(bit_vec); i < max; i++) {
         if (i == three || i == seventeen) {
-            TEST_TRUE(runner, BitVec_Get(bit_vec, i), "set/get %d", i);
+            TEST_TRUE(runner, BitVec_Get(bit_vec, i), "set/get %u", 
(unsigned)i);
         }
         else {
-            TEST_FALSE(runner, BitVec_Get(bit_vec, i), "get %d", i);
+            TEST_FALSE(runner, BitVec_Get(bit_vec, i), "get %u", (unsigned)i);
         }
     }
     TEST_FALSE(runner, BitVec_Get(bit_vec, i), "out of range get");
@@ -61,18 +61,18 @@ test_Set_and_Get(TestBatchRunner *runner) {
 static void
 test_Flip(TestBatchRunner *runner) {
     BitVector *bit_vec = BitVec_new(0);
-    int i;
 
+    unsigned i;
     for (i = 0; i <= 20; i++) { BitVec_Flip(bit_vec, i); }
     for (i = 0; i <= 20; i++) {
-        TEST_TRUE(runner, BitVec_Get(bit_vec, i), "flip on %d", i);
+        TEST_TRUE(runner, BitVec_Get(bit_vec, i), "flip on %u", i);
     }
-    TEST_FALSE(runner, BitVec_Get(bit_vec, i), "no flip %d", i);
+    TEST_FALSE(runner, BitVec_Get(bit_vec, i), "no flip %u", i);
     for (i = 0; i <= 20; i++) { BitVec_Flip(bit_vec, i); }
     for (i = 0; i <= 20; i++) {
-        TEST_FALSE(runner, BitVec_Get(bit_vec, i), "flip off %d", i);
+        TEST_FALSE(runner, BitVec_Get(bit_vec, i), "flip off %u", i);
     }
-    TEST_FALSE(runner, BitVec_Get(bit_vec, i), "still no flip %d", i);
+    TEST_FALSE(runner, BitVec_Get(bit_vec, 21), "still no flip %u", i);
 
     DECREF(bit_vec);
 }
@@ -80,20 +80,19 @@ test_Flip(TestBatchRunner *runner) {
 static void
 test_Flip_Block_ascending(TestBatchRunner *runner) {
     BitVector *bit_vec = BitVec_new(0);
-    int i;
 
-    for (i = 0; i <= 20; i++) {
+    for (unsigned i = 0; i <= 20; i++) {
         BitVec_Flip_Block(bit_vec, i, 21 - i);
     }
 
-    for (i = 0; i <= 20; i++) {
+    for (unsigned i = 0; i <= 20; i++) {
         if (i % 2 == 0) {
             TEST_TRUE(runner, BitVec_Get(bit_vec, i),
-                      "Flip_Block ascending %d", i);
+                      "Flip_Block ascending %u", i);
         }
         else {
             TEST_FALSE(runner, BitVec_Get(bit_vec, i),
-                       "Flip_Block ascending %d", i);
+                       "Flip_Block ascending %u", i);
         }
     }
 
@@ -103,20 +102,19 @@ test_Flip_Block_ascending(TestBatchRunner *runner) {
 static void
 test_Flip_Block_descending(TestBatchRunner *runner) {
     BitVector *bit_vec = BitVec_new(0);
-    int i;
 
-    for (i = 19; i >= 0; i--) {
-        BitVec_Flip_Block(bit_vec, 1, i);
+    for (int i = 19; i >= 0; i--) {
+        BitVec_Flip_Block(bit_vec, 1, (size_t)i);
     }
 
-    for (i = 0; i <= 20; i++) {
+    for (unsigned i = 0; i <= 20; i++) {
         if (i % 2) {
             TEST_TRUE(runner, BitVec_Get(bit_vec, i),
-                      "Flip_Block descending %d", i);
+                      "Flip_Block descending %u", i);
         }
         else {
             TEST_FALSE(runner, BitVec_Get(bit_vec, i),
-                       "Flip_Block descending %d", i);
+                       "Flip_Block descending %u", i);
         }
     }
 
@@ -125,25 +123,22 @@ test_Flip_Block_descending(TestBatchRunner *runner) {
 
 static void
 test_Flip_Block_bulk(TestBatchRunner *runner) {
-    int32_t offset;
-
-    for (offset = 0; offset <= 17; offset++) {
-        int32_t len;
-        for (len = 0; len <= 17; len++) {
-            int i;
-            int upper = offset + len - 1;
+    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 && i <= upper) {
+                if (i >= offset && (int)i <= upper) {
                     if (!BitVec_Get(bit_vec, i)) { break; }
                 }
                 else {
                     if (BitVec_Get(bit_vec, i)) { break; }
                 }
             }
-            TEST_INT_EQ(runner, i, 18, "Flip_Block(%d, %d)", offset, len);
+            TEST_UINT_EQ(runner, i, 18, "Flip_Block(%u, %u)", offset, len);
 
             DECREF(bit_vec);
         }
@@ -152,12 +147,8 @@ test_Flip_Block_bulk(TestBatchRunner *runner) {
 
 static void
 test_Mimic(TestBatchRunner *runner) {
-    int foo;
-
-    for (foo = 0; foo <= 17; foo++) {
-        int bar;
-        for (bar = 0; bar <= 17; bar++) {
-            int i;
+    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);
 
@@ -165,10 +156,11 @@ test_Mimic(TestBatchRunner *runner) {
             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_INT_EQ(runner, i, 18, "Mimic(%d, %d)", foo, bar);
+            TEST_UINT_EQ(runner, i, 18, "Mimic(%u, %u)", foo, bar);
 
             DECREF(foo_vec);
             DECREF(bar_vec);
@@ -178,14 +170,13 @@ test_Mimic(TestBatchRunner *runner) {
 
 static BitVector*
 S_create_set(int set_num) {
-    int i;
-    int nums_1[] = { 1, 2, 3, 10, 20, 30, 0 };
-    int nums_2[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10,
+    unsigned nums_1[] = { 1, 2, 3, 10, 20, 30, 0 };
+    unsigned nums_2[] = { 2, 3, 4, 5, 6, 7, 8, 9, 10,
                      25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 0
                    };
-    int *nums = set_num == 1 ? nums_1 : nums_2;
+    unsigned *nums = set_num == 1 ? nums_1 : nums_2;
     BitVector *bit_vec = BitVec_new(31);
-    for (i = 0; nums[i] != 0; i++) {
+    for (unsigned i = 0; nums[i] != 0; i++) {
         BitVec_Set(bit_vec, nums[i]);
     }
     return bit_vec;
@@ -195,10 +186,10 @@ S_create_set(int set_num) {
 #define OP_XOR 2
 #define OP_AND 3
 #define OP_AND_NOT 4
-static int
+static unsigned
 S_verify_logical_op(BitVector *bit_vec, BitVector *set_1, BitVector *set_2,
                     int op) {
-    int i;
+    unsigned i;
 
     for (i = 0; i < 50; i++) {
         bool wanted;
@@ -235,11 +226,11 @@ test_Or(TestBatchRunner *runner) {
     BitVector *set_2   = S_create_set(2);
 
     BitVec_Or(smaller, set_2);
-    TEST_INT_EQ(runner, S_verify_logical_op(smaller, set_1, set_2, OP_OR),
-                50, "OR with self smaller than other");
+    TEST_UINT_EQ(runner, S_verify_logical_op(smaller, set_1, set_2, OP_OR),
+                 50, "OR with self smaller than other");
     BitVec_Or(larger, set_1);
-    TEST_INT_EQ(runner, S_verify_logical_op(larger, set_1, set_2, OP_OR),
-                50, "OR with other smaller than self");
+    TEST_UINT_EQ(runner, S_verify_logical_op(larger, set_1, set_2, OP_OR),
+                 50, "OR with other smaller than self");
 
     DECREF(smaller);
     DECREF(larger);
@@ -255,11 +246,11 @@ test_Xor(TestBatchRunner *runner) {
     BitVector *set_2   = S_create_set(2);
 
     BitVec_Xor(smaller, set_2);
-    TEST_INT_EQ(runner, S_verify_logical_op(smaller, set_1, set_2, OP_XOR),
-                50, "XOR with self smaller than other");
+    TEST_UINT_EQ(runner, S_verify_logical_op(smaller, set_1, set_2, OP_XOR),
+                 50, "XOR with self smaller than other");
     BitVec_Xor(larger, set_1);
-    TEST_INT_EQ(runner, S_verify_logical_op(larger, set_1, set_2, OP_XOR),
-                50, "XOR with other smaller than self");
+    TEST_UINT_EQ(runner, S_verify_logical_op(larger, set_1, set_2, OP_XOR),
+                 50, "XOR with other smaller than self");
 
     DECREF(smaller);
     DECREF(larger);
@@ -275,11 +266,11 @@ test_And(TestBatchRunner *runner) {
     BitVector *set_2   = S_create_set(2);
 
     BitVec_And(smaller, set_2);
-    TEST_INT_EQ(runner, S_verify_logical_op(smaller, set_1, set_2, OP_AND),
-                50, "AND with self smaller than other");
+    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_INT_EQ(runner, S_verify_logical_op(larger, set_1, set_2, OP_AND),
-                50, "AND with other smaller than self");
+    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);
@@ -295,13 +286,13 @@ test_And_Not(TestBatchRunner *runner) {
     BitVector *set_2   = S_create_set(2);
 
     BitVec_And_Not(smaller, set_2);
-    TEST_INT_EQ(runner,
-                S_verify_logical_op(smaller, set_1, set_2, OP_AND_NOT),
-                50, "AND_NOT with self smaller than other");
+    TEST_UINT_EQ(runner,
+                 S_verify_logical_op(smaller, set_1, set_2, OP_AND_NOT),
+                 50, "AND_NOT with self smaller than other");
     BitVec_And_Not(larger, set_1);
-    TEST_INT_EQ(runner,
-                S_verify_logical_op(larger, set_2, set_1, OP_AND_NOT),
-                50, "AND_NOT with other smaller than self");
+    TEST_UINT_EQ(runner,
+                 S_verify_logical_op(larger, set_2, set_1, OP_AND_NOT),
+                 50, "AND_NOT with other smaller than self");
 
     DECREF(smaller);
     DECREF(larger);
@@ -311,44 +302,41 @@ test_And_Not(TestBatchRunner *runner) {
 
 static void
 test_Count(TestBatchRunner *runner) {
-    int i;
-    int shuffled[64];
+    unsigned shuffled[64];
     BitVector *bit_vec = BitVec_new(64);
 
-    for (i = 0; i < 64; i++) { shuffled[i] = i; }
-    for (i = 0; i < 64; i++) {
-        int shuffle_pos = rand() % 64;
-        int temp = shuffled[shuffle_pos];
+    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_INT_EQ(runner, i, 64, "Count() returns the right number of bits");
+    TEST_UINT_EQ(runner, i, 64, "Count() returns the right number of bits");
 
     DECREF(bit_vec);
 }
 
 static void
 test_Next_Hit(TestBatchRunner *runner) {
-    int i;
-
-    for (i = 24; i <= 33; i++) {
-        int probe;
+    for (int i = 24; i <= 33; i++) {
         BitVector *bit_vec = BitVec_new(64);
-        BitVec_Set(bit_vec, i);
+        BitVec_Set(bit_vec, (size_t)i);
         TEST_INT_EQ(runner, BitVec_Next_Hit(bit_vec, 0), i,
                     "Next_Hit for 0 is %d", i);
         TEST_INT_EQ(runner, BitVec_Next_Hit(bit_vec, 0), i,
                     "Next_Hit for 1 is %d", i);
-        for (probe = 15; probe <= i; probe++) {
-            TEST_INT_EQ(runner, BitVec_Next_Hit(bit_vec, probe), i,
+        for (int probe = 15; probe <= i; probe++) {
+            TEST_INT_EQ(runner, BitVec_Next_Hit(bit_vec, (size_t)probe), i,
                         "Next_Hit for %d is %d", probe, i);
         }
-        for (probe = i + 1; probe <= i + 9; probe++) {
-            TEST_INT_EQ(runner, BitVec_Next_Hit(bit_vec, probe), -1,
+        for (int probe = i + 1; probe <= i + 9; probe++) {
+            TEST_INT_EQ(runner, BitVec_Next_Hit(bit_vec, (size_t)probe), -1,
                         "no Next_Hit for %d when max is %d", probe, i);
         }
         DECREF(bit_vec);
@@ -366,7 +354,6 @@ test_Clear_All(TestBatchRunner *runner) {
 
 static void
 test_Clone(TestBatchRunner *runner) {
-    int i;
     BitVector *self = BitVec_new(30);
     BitVector *twin;
 
@@ -376,11 +363,12 @@ test_Clone(TestBatchRunner *runner) {
     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_INT_EQ(runner, i, 50, "Clone");
-    TEST_INT_EQ(runner, BitVec_Count(twin), 4, "clone Count");
+    TEST_UINT_EQ(runner, i, 50, "Clone");
+    TEST_UINT_EQ(runner, BitVec_Count(twin), 4, "clone Count");
 
     DECREF(self);
     DECREF(twin);
@@ -398,12 +386,11 @@ test_To_Array(TestBatchRunner *runner) {
     uint64_t  *source_ints = TestUtils_random_u64s(NULL, 20, 0, 200);
     BitVector *bit_vec = BitVec_new(0);
     I32Array  *array;
-    long       num_unique = 0;
-    long       i;
+    unsigned   num_unique = 0;
 
     // Unique the random ints.
     qsort(source_ints, 20, sizeof(uint64_t), S_compare_u64s);
-    for (i = 0; i < 19; i++) {
+    for (unsigned i = 0; i < 19; i++) {
         if (source_ints[i] != source_ints[i + 1]) {
             source_ints[num_unique] = source_ints[i];
             num_unique++;
@@ -411,17 +398,18 @@ test_To_Array(TestBatchRunner *runner) {
     }
 
     // Set bits.
-    for (i = 0; i < num_unique; i++) {
-        BitVec_Set(bit_vec, (uint32_t)source_ints[i]);
+    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_INT_EQ(runner, i, num_unique, "To_Array (%ld == %ld)", i,
-                num_unique);
+    TEST_UINT_EQ(runner, i, num_unique, "To_Array (%u == %u)", i,
+                 num_unique);
 
     DECREF(array);
     DECREF(bit_vec);
@@ -432,8 +420,7 @@ test_To_Array(TestBatchRunner *runner) {
 // Valgrind only - detect off-by-one error.
 static void
 test_off_by_one_error() {
-    int cap;
-    for (cap = 5; cap <= 24; cap++) {
+    for (unsigned cap = 5; cap <= 24; cap++) {
         BitVector *bit_vec = BitVec_new(cap);
         BitVec_Set(bit_vec, cap - 2);
         DECREF(bit_vec);

Reply via email to