Merge branch 'mainstream'
[sliver-openvswitch.git] / tests / test-util.c
index b98fc79..6bf8ed5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2011, 2012 Nicira, Inc.
+ * Copyright (c) 2011, 2012, 2013 Nicira, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
 
 #include <config.h>
 
+#include <getopt.h>
 #include <inttypes.h>
 #include <limits.h>
 #include <stdio.h>
@@ -25,6 +26,7 @@
 #include "command-line.h"
 #include "random.h"
 #include "util.h"
+#include "vlog.h"
 
 #undef NDEBUG
 #include <assert.h>
@@ -88,11 +90,74 @@ test_ctz(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
     check_ctz(0, 32);
 }
 
+/* Returns a random number in the range 'min'...'max' inclusive. */
+static uint32_t
+random_in_range(uint32_t min, uint32_t max)
+{
+    return min == max ? min : min + random_range(max - min + 1);
+}
+
+static void
+check_rup2(uint32_t x, int n)
+{
+    uint32_t rup2 = ROUND_UP_POW2(x);
+    if (rup2 != n) {
+        fprintf(stderr, "ROUND_UP_POW2(%#"PRIx32") is %#"PRIx32" "
+                "but should be %#"PRIx32"\n", x, rup2, n);
+        abort();
+    }
+}
+
+static void
+test_round_up_pow2(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+{
+    int n;
+
+    for (n = 0; n < 32; n++) {
+        /* Min, max value for which ROUND_UP_POW2 should yield (1 << n). */
+        uint32_t min = ((1u << n) >> 1) + 1;
+        uint32_t max = 1u << n;
+
+        check_rup2(min, 1u << n);
+        check_rup2(max, 1u << n);
+        check_rup2(random_in_range(min, max), 1u << n);
+    }
+    check_rup2(0, 0);
+}
+
+static void
+check_rdp2(uint32_t x, int n)
+{
+    uint32_t rdp2 = ROUND_DOWN_POW2(x);
+    if (rdp2 != n) {
+        fprintf(stderr, "ROUND_DOWN_POW2(%#"PRIx32") is %#"PRIx32" "
+                "but should be %#"PRIx32"\n", x, rdp2, n);
+        abort();
+    }
+}
+
+static void
+test_round_down_pow2(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+{
+    int n;
+
+    for (n = 0; n < 32; n++) {
+        /* Min, max value for which ROUND_DOWN_POW2 should yield (1 << n). */
+        uint32_t min = 1u << n;
+        uint32_t max = ((1u << n) << 1) - 1;
+
+        check_rdp2(min, 1u << n);
+        check_rdp2(max, 1u << n);
+        check_rdp2(random_in_range(min, max), 1u << n);
+    }
+    check_rdp2(0, 0);
+}
+
 static void
 shuffle(unsigned int *p, size_t n)
 {
     for (; n > 1; n--, p++) {
-        unsigned int *q = &p[rand() % n];
+        unsigned int *q = &p[random_range(n)];
         unsigned int tmp = *p;
         *p = *q;
         *q = tmp;
@@ -295,7 +360,7 @@ test_bitwise_is_all_zeros(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 
             /* Change a random 0-bit into a 1-bit. */
             do {
-                bit = htonll(UINT64_C(1) << (random_uint32() % 64));
+                bit = htonll(UINT64_C(1) << (random_range(64)));
             } while (x & bit);
             x |= bit;
 
@@ -335,9 +400,17 @@ test_follow_symlinks(int argc, char *argv[])
         free(target);
     }
 }
+
+static void
+test_assert(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+{
+    ovs_assert(false);
+}
 \f
 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},
     {"log_2_floor", 0, 0, test_log_2_floor},
     {"bitwise_copy", 0, 0, test_bitwise_copy},
@@ -345,13 +418,46 @@ static const struct command commands[] = {
     {"bitwise_one", 0, 0, test_bitwise_one},
     {"bitwise_is_all_zeros", 0, 0, test_bitwise_is_all_zeros},
     {"follow-symlinks", 1, INT_MAX, test_follow_symlinks},
+    {"assert", 0, 0, test_assert},
     {NULL, 0, 0, NULL},
 };
 
+static void
+parse_options(int argc, char *argv[])
+{
+    enum {
+        VLOG_OPTION_ENUMS
+    };
+    static const struct option long_options[] = {
+        VLOG_LONG_OPTIONS,
+        {NULL, 0, NULL, 0},
+    };
+    char *short_options = long_options_to_short_options(long_options);
+
+    for (;;) {
+        int c = getopt_long(argc, argv, short_options, long_options, NULL);
+        if (c == -1) {
+            break;
+        }
+
+        switch (c) {
+        VLOG_OPTION_HANDLERS
+
+        case '?':
+            exit(EXIT_FAILURE);
+
+        default:
+            abort();
+        }
+    }
+    free(short_options);
+}
+
 int
 main(int argc, char *argv[])
 {
     set_program_name(argv[0]);
-    run_command(argc - 1, argv + 1, commands);
+    parse_options(argc, argv);
+    run_command(argc - optind, argv + optind, commands);
     return 0;
 }