X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=tests%2Ftest-util.c;h=ffd4dcefd8ef0110f1131518164fd1d97883eb86;hb=0ef165ecb57943e17a8ee8270df68ffb8d032e29;hp=e66987d667d646ae183c64fecc47195f315865cd;hpb=2a4ca27c2659dbe5b07f7c1faa301bce17704ec3;p=sliver-openvswitch.git diff --git a/tests/test-util.c b/tests/test-util.c index e66987d66..ffd4dcefd 100644 --- a/tests/test-util.c +++ b/tests/test-util.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2012, 2013 Nicira, Inc. + * Copyright (c) 2011, 2012, 2013, 2014 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -27,6 +27,7 @@ #include "random.h" #include "util.h" #include "vlog.h" +#include "ovstest.h" #undef NDEBUG #include @@ -61,11 +62,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 +88,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,10 +110,62 @@ 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); } +static void +check_clz32(uint32_t x, int n) +{ + if (clz32(x) != n) { + fprintf(stderr, "clz32(%"PRIu32") is %d but should be %d\n", + x, clz32(x), n); + abort(); + } +} + +static void +check_clz64(uint64_t x, int n) +{ + if (clz64(x) != n) { + fprintf(stderr, "clz64(%"PRIu64") is %d but should be %d\n", + x, clz64(x), n); + abort(); + } +} + +static void +test_clz(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +{ + int n; + + for (n = 0; n < 32; n++) { + /* Check minimum x such that f(x) == n. */ + check_clz32((1u << 31) >> n, n); + + /* Check maximum x such that f(x) == n. */ + check_clz32(UINT32_MAX >> n, n); + + /* Check a random value in the middle. */ + check_clz32((random_uint32() | 1u << 31) >> n, n); + } + + for (n = 0; n < 64; n++) { + /* Check minimum x such that f(x) == n. */ + check_clz64((UINT64_C(1) << 63) >> n, n); + + /* Check maximum x such that f(x) == n. */ + check_clz64(UINT64_MAX >> n, n); + + /* Check a random value in the middle. */ + check_clz64((random_uint64() | UINT64_C(1) << 63) >> n, n); + } + + /* Check clz(0). */ + check_clz32(0, 32); + check_clz64(0, 64); +} + /* Returns a random number in the range 'min'...'max' inclusive. */ static uint32_t random_in_range(uint32_t min, uint32_t max) @@ -188,17 +241,17 @@ shuffle(uint64_t *p, size_t n) } static void -check_popcount(uint64_t x, int n) +check_count_1bits(uint64_t x, int n) { - if (popcount(x) != n) { - fprintf(stderr, "popcount(%#"PRIx64") 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 -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; @@ -207,7 +260,7 @@ test_popcount(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) bits[i] = UINT64_C(1) << i; } - check_popcount(0, 0); + check_count_1bits(0, 0); for (i = 0; i < 1000; i++) { uint64_t x = 0; @@ -216,14 +269,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_popcount(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_popcount(x, j); + check_count_1bits(x, j); } assert(x == 0); } @@ -961,12 +1014,30 @@ test_ovs_scan(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) ovs_assert(sscanf("0x12-3]xyz", "%[^-a-f]", str)); ovs_assert(!strcmp(str, "0x12")); } + +static void +test_snprintf(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) +{ + char s[16]; + + ovs_assert(snprintf(s, 4, "abcde") == 5); + ovs_assert(!strcmp(s, "abc")); + + ovs_assert(snprintf(s, 5, "abcde") == 5); + ovs_assert(!strcmp(s, "abcd")); + + ovs_assert(snprintf(s, 6, "abcde") == 5); + ovs_assert(!strcmp(s, "abcde")); + + ovs_assert(snprintf(NULL, 0, "abcde") == 5); +} static const struct command commands[] = { {"ctz", 0, 0, test_ctz}, + {"clz", 0, 0, test_clz}, {"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}, @@ -975,6 +1046,7 @@ static const struct command commands[] = { {"follow-symlinks", 1, INT_MAX, test_follow_symlinks}, {"assert", 0, 0, test_assert}, {"ovs_scan", 0, 0, test_ovs_scan}, + {"snprintf", 0, 0, test_snprintf}, {NULL, 0, 0, NULL}, }; @@ -1009,11 +1081,12 @@ parse_options(int argc, char *argv[]) free(short_options); } -int -main(int argc, char *argv[]) +static void +test_util_main(int argc, char *argv[]) { set_program_name(argv[0]); parse_options(argc, argv); run_command(argc - optind, argv + optind, commands); - return 0; } + +OVSTEST_REGISTER("test-util", test_util_main);