summaryrefslogtreecommitdiff
path: root/tests/test-util.c
diff options
context:
space:
mode:
authorBen Pfaff <blp@nicira.com>2011-08-17 10:55:15 -0700
committerBen Pfaff <blp@nicira.com>2011-09-13 11:46:08 -0700
commitaad29cd1a1fb76aa68a9c404a47b66ac516149b5 (patch)
tree698546190e980993cff186029b5dd3ca66a8b09f /tests/test-util.c
parente7ed3a3a5f87d20f0cc632ae23adba6f30a4e19d (diff)
downloadopenvswitch-aad29cd1a1fb76aa68a9c404a47b66ac516149b5.tar.gz
packets: Add more utility functions for IPv4 and IPv6 addresses.
We had these functions scattered around the source tree anyway. packets.h is a good place to centralize them. I do plan to introduce some additional callers.
Diffstat (limited to 'tests/test-util.c')
-rw-r--r--tests/test-util.c31
1 files changed, 25 insertions, 6 deletions
diff --git a/tests/test-util.c b/tests/test-util.c
index e9a827a4c..bc4af2356 100644
--- a/tests/test-util.c
+++ b/tests/test-util.c
@@ -17,6 +17,7 @@
#include <config.h>
#include <inttypes.h>
+#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
@@ -24,7 +25,7 @@
#include "util.h"
static void
-check(uint32_t x, int n)
+check_log_2_floor(uint32_t x, int n)
{
if (log_2_floor(x) != n) {
fprintf(stderr, "log_2_floor(%"PRIu32") is %d but should be %d\n",
@@ -33,20 +34,38 @@ check(uint32_t x, int n)
}
}
+static void
+check_ctz(uint32_t x, int n)
+{
+ if (ctz(x) != n) {
+ fprintf(stderr, "ctz(%"PRIu32") is %d but should be %d\n",
+ x, ctz(x), n);
+ abort();
+ }
+}
+
int
main(void)
{
int n;
for (n = 0; n < 32; n++) {
- /* Check minimum x that has log2(x) == n. */
- check(1 << n, n);
+ /* Check minimum x such that f(x) == n. */
+ check_log_2_floor(1 << n, n);
+ check_ctz(1 << n, n);
- /* Check maximum x that has log2(x) == n. */
- check((1 << n) | ((1 << n) - 1), n);
+ /* Check maximum x such that f(x) == n. */
+ check_log_2_floor((1 << n) | ((1 << n) - 1), n);
+ check_ctz(UINT32_MAX << n, n);
/* Check a random value in the middle. */
- check((random_uint32() & ((1 << n) - 1)) | (1 << n), n);
+ check_log_2_floor((random_uint32() & ((1 << n) - 1)) | (1 << n), n);
+ check_ctz((random_uint32() | 1) << n, n);
}
+
+ /* Check ctz(0).
+ * (log_2_floor(0) is undefined.) */
+ check_ctz(0, 32);
+
return 0;
}