X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=tests%2Ftest-util.c;h=bc4af2356049155b3ec241445e131c844a7add51;hb=b17a80ee405da3843710f428b4f83015dc35c342;hp=e9a827a4ccc5a7a31e0cd6ce9c5fd26f90d19e01;hpb=711e0157cf345f1473247ec90c6ff39eb9f7743c;p=openvswitch diff --git a/tests/test-util.c b/tests/test-util.c index e9a827a4..bc4af235 100644 --- a/tests/test-util.c +++ b/tests/test-util.c @@ -17,6 +17,7 @@ #include #include +#include #include #include @@ -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; }