lib/util: Rename ctz() as ctz32().
[sliver-openvswitch.git] / tests / test-util.c
index e59adf7..8dbc98b 100644 (file)
@@ -61,11 +61,11 @@ test_log_2_floor(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 }
 
 static void
-check_ctz(uint32_t x, int n)
+check_ctz32(uint32_t x, int n)
 {
-    if (ctz(x) != n) {
-        fprintf(stderr, "ctz(%"PRIu32") is %d but should be %d\n",
-                x, ctz(x), n);
+    if (ctz32(x) != n) {
+        fprintf(stderr, "ctz32(%"PRIu32") is %d but should be %d\n",
+                x, ctz32(x), n);
         abort();
     }
 }
@@ -87,13 +87,13 @@ test_ctz(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 
     for (n = 0; n < 32; n++) {
         /* Check minimum x such that f(x) == n. */
-        check_ctz(1 << n, n);
+        check_ctz32(1 << n, n);
 
         /* Check maximum x such that f(x) == n. */
-        check_ctz(UINT32_MAX << n, n);
+        check_ctz32(UINT32_MAX << n, n);
 
         /* Check a random value in the middle. */
-        check_ctz((random_uint32() | 1) << n, n);
+        check_ctz32((random_uint32() | 1) << n, n);
     }
 
 
@@ -109,7 +109,7 @@ test_ctz(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
     }
 
     /* Check ctz(0). */
-    check_ctz(0, 32);
+    check_ctz32(0, 32);
     check_ctz64(0, 64);
 }
 
@@ -188,27 +188,17 @@ shuffle(uint64_t *p, size_t n)
 }
 
 static void
-check_popcount(uint32_t x, int n)
+check_count_1bits(uint64_t x, int n)
 {
-    if (popcount(x) != n) {
-        fprintf(stderr, "popcount(%#"PRIx32") is %d but should be %d\n",
-                x, popcount(x), n);
+    if (count_1bits(x) != n) {
+        fprintf(stderr, "count_1bits(%#"PRIx64") is %d but should be %d\n",
+                x, count_1bits(x), n);
         abort();
     }
 }
 
 static void
-check_popcount64(uint64_t x, int n)
-{
-    if (popcount64(x) != n) {
-        fprintf(stderr, "popcount64(%#"PRIx64") is %d but should be %d\n",
-                x, popcount64(x), n);
-        abort();
-    }
-}
-
-static void
-test_popcount(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+test_count_1bits(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 {
     uint64_t bits[64];
     int i;
@@ -217,27 +207,7 @@ test_popcount(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
         bits[i] = UINT64_C(1) << i;
     }
 
-    check_popcount(0, 0);
-    check_popcount64(0, 0);
-
-    for (i = 0; i < 1000; i++) {
-        uint32_t x = 0;
-        int j;
-
-        shuffle(bits, ARRAY_SIZE(bits)/2);
-        for (j = 0; j < 32; j++) {
-            x |= bits[j];
-            check_popcount(x, j + 1);
-        }
-        assert(x == UINT32_MAX);
-
-        shuffle(bits, ARRAY_SIZE(bits)/2);
-        for (j = 31; j >= 0; j--) {
-            x &= ~bits[j];
-            check_popcount(x, j);
-        }
-        assert(x == 0);
-    }
+    check_count_1bits(0, 0);
 
     for (i = 0; i < 1000; i++) {
         uint64_t x = 0;
@@ -246,14 +216,14 @@ test_popcount(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
         shuffle(bits, ARRAY_SIZE(bits));
         for (j = 0; j < 64; j++) {
             x |= bits[j];
-            check_popcount64(x, j + 1);
+            check_count_1bits(x, j + 1);
         }
         assert(x == UINT64_MAX);
 
         shuffle(bits, ARRAY_SIZE(bits));
         for (j = 63; j >= 0; j--) {
             x &= ~bits[j];
-            check_popcount64(x, j);
+            check_count_1bits(x, j);
         }
         assert(x == 0);
     }
@@ -996,7 +966,7 @@ static const struct command commands[] = {
     {"ctz", 0, 0, test_ctz},
     {"round_up_pow2", 0, 0, test_round_up_pow2},
     {"round_down_pow2", 0, 0, test_round_down_pow2},
-    {"popcount", 0, 0, test_popcount},
+    {"count_1bits", 0, 0, test_count_1bits},
     {"log_2_floor", 0, 0, test_log_2_floor},
     {"bitwise_copy", 0, 0, test_bitwise_copy},
     {"bitwise_zero", 0, 0, test_bitwise_zero},