command-line: add ovs_cmdl_context
[cascardo/ovs.git] / tests / test-util.c
index 4256509..1d63d68 100644 (file)
@@ -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.
  */
 
 #include <config.h>
-
+#undef NDEBUG
+#include "util.h"
+#include <assert.h>
 #include <getopt.h>
 #include <inttypes.h>
 #include <limits.h>
 #include <stdio.h>
 #include <stdlib.h>
-
 #include "byte-order.h"
 #include "command-line.h"
+#include "ovstest.h"
 #include "random.h"
-#include "util.h"
-#include "vlog.h"
-
-#undef NDEBUG
-#include <assert.h>
+#include "openvswitch/vlog.h"
 
 static void
 check_log_2_floor(uint32_t x, int n)
@@ -42,7 +40,7 @@ check_log_2_floor(uint32_t x, int n)
 }
 
 static void
-test_log_2_floor(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+test_log_2_floor(struct ovs_cmdl_context *ctx OVS_UNUSED)
 {
     int n;
 
@@ -61,33 +59,108 @@ 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();
     }
 }
 
 static void
-test_ctz(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+check_ctz64(uint64_t x, int n)
+{
+    if (ctz64(x) != n) {
+        fprintf(stderr, "ctz64(%"PRIu64") is %d but should be %d\n",
+                x, ctz64(x), n);
+        abort();
+    }
+}
+
+static void
+test_ctz(struct ovs_cmdl_context *ctx OVS_UNUSED)
 {
     int n;
 
     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_ctz32(UINT32_MAX << n, n);
+
+        /* Check a random value in the middle. */
+        check_ctz32((random_uint32() | 1) << n, n);
+    }
+
+
+    for (n = 0; n < 64; n++) {
+        /* Check minimum x such that f(x) == n. */
+        check_ctz64(UINT64_C(1) << n, n);
 
         /* Check maximum x such that f(x) == n. */
-        check_ctz(UINT32_MAX << n, n);
+        check_ctz64(UINT64_MAX << n, n);
 
         /* Check a random value in the middle. */
-        check_ctz((random_uint32() | 1) << n, n);
+        check_ctz64((random_uint64() | UINT64_C(1)) << n, n);
     }
 
     /* 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(struct ovs_cmdl_context *ctx 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. */
@@ -109,7 +182,7 @@ check_rup2(uint32_t x, int n)
 }
 
 static void
-test_round_up_pow2(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+test_round_up_pow2(struct ovs_cmdl_context *ctx OVS_UNUSED)
 {
     int n;
 
@@ -137,7 +210,7 @@ check_rdp2(uint32_t x, int n)
 }
 
 static void
-test_round_down_pow2(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+test_round_down_pow2(struct ovs_cmdl_context *ctx OVS_UNUSED)
 {
     int n;
 
@@ -154,53 +227,53 @@ test_round_down_pow2(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 }
 
 static void
-shuffle(unsigned int *p, size_t n)
+shuffle(uint64_t *p, size_t n)
 {
     for (; n > 1; n--, p++) {
-        unsigned int *q = &p[random_range(n)];
-        unsigned int tmp = *p;
+        uint64_t *q = &p[random_range(n)];
+        uint64_t tmp = *p;
         *p = *q;
         *q = tmp;
     }
 }
 
 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
-test_popcount(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+test_count_1bits(struct ovs_cmdl_context *ctx OVS_UNUSED)
 {
-    unsigned int bits[32];
+    uint64_t bits[64];
     int i;
 
     for (i = 0; i < ARRAY_SIZE(bits); i++) {
-        bits[i] = 1u << i;
+        bits[i] = UINT64_C(1) << i;
     }
 
-    check_popcount(0, 0);
+    check_count_1bits(0, 0);
 
     for (i = 0; i < 1000; i++) {
-        uint32_t x = 0;
+        uint64_t x = 0;
         int j;
 
         shuffle(bits, ARRAY_SIZE(bits));
-        for (j = 0; j < 32; j++) {
+        for (j = 0; j < 64; j++) {
             x |= bits[j];
-            check_popcount(x, j + 1);
+            check_count_1bits(x, j + 1);
         }
-        assert(x == UINT32_MAX);
+        assert(x == UINT64_MAX);
 
         shuffle(bits, ARRAY_SIZE(bits));
-        for (j = 31; j >= 0; j--) {
+        for (j = 63; j >= 0; j--) {
             x &= ~bits[j];
-            check_popcount(x, j);
+            check_count_1bits(x, j);
         }
         assert(x == 0);
     }
@@ -214,7 +287,7 @@ sum_of_squares(int n)
 }
 
 static void
-test_bitwise_copy(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+test_bitwise_copy(struct ovs_cmdl_context *ctx OVS_UNUSED)
 {
     unsigned int n_loops;
     int src_ofs;
@@ -264,7 +337,7 @@ test_bitwise_copy(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 }
 
 static void
-test_bitwise_zero(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+test_bitwise_zero(struct ovs_cmdl_context *ctx OVS_UNUSED)
 {
     unsigned int n_loops;
     int dst_ofs;
@@ -305,7 +378,7 @@ test_bitwise_zero(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 }
 
 static void
-test_bitwise_one(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+test_bitwise_one(struct ovs_cmdl_context *ctx OVS_UNUSED)
 {
     unsigned int n_loops;
     int dst_ofs;
@@ -346,7 +419,7 @@ test_bitwise_one(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 }
 
 static void
-test_bitwise_is_all_zeros(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+test_bitwise_is_all_zeros(struct ovs_cmdl_context *ctx OVS_UNUSED)
 {
     int n_loops;
 
@@ -390,25 +463,25 @@ test_bitwise_is_all_zeros(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 }
 
 static void
-test_follow_symlinks(int argc, char *argv[])
+test_follow_symlinks(struct ovs_cmdl_context *ctx)
 {
     int i;
 
-    for (i = 1; i < argc; i++) {
-        char *target = follow_symlinks(argv[i]);
+    for (i = 1; i < ctx->argc; i++) {
+        char *target = follow_symlinks(ctx->argv[i]);
         puts(target);
         free(target);
     }
 }
 
 static void
-test_assert(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+test_assert(struct ovs_cmdl_context *ctx OVS_UNUSED)
 {
     ovs_assert(false);
 }
 
 static void
-test_ovs_scan(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+test_ovs_scan(struct ovs_cmdl_context *ctx OVS_UNUSED)
 {
     char str[16], str2[16], str3[16];
     long double ld, ld2;
@@ -938,21 +1011,63 @@ 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(struct ovs_cmdl_context *ctx 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);
+}
+
+#ifndef _WIN32
+static void
+test_file_name(struct ovs_cmdl_context *ctx)
+{
+    int i;
+
+    for (i = 1; i < ctx->argc; i++) {
+        char *dir, *base;
+
+        dir = dir_name(ctx->argv[i]);
+        puts(dir);
+        free(dir);
+
+        base = base_name(ctx->argv[i]);
+        puts(base);
+        free(base);
+    }
+}
+#endif /* _WIN32 */
 \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},
-    {"bitwise_zero", 0, 0, test_bitwise_zero},
-    {"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},
-    {"ovs_scan", 0, 0, test_ovs_scan},
-    {NULL, 0, 0, NULL},
+static const struct ovs_cmdl_command commands[] = {
+    {"ctz", NULL, 0, 0, test_ctz},
+    {"clz", NULL, 0, 0, test_clz},
+    {"round_up_pow2", NULL, 0, 0, test_round_up_pow2},
+    {"round_down_pow2", NULL, 0, 0, test_round_down_pow2},
+    {"count_1bits", NULL, 0, 0, test_count_1bits},
+    {"log_2_floor", NULL, 0, 0, test_log_2_floor},
+    {"bitwise_copy", NULL, 0, 0, test_bitwise_copy},
+    {"bitwise_zero", NULL, 0, 0, test_bitwise_zero},
+    {"bitwise_one", NULL, 0, 0, test_bitwise_one},
+    {"bitwise_is_all_zeros", NULL, 0, 0, test_bitwise_is_all_zeros},
+    {"follow-symlinks", NULL, 1, INT_MAX, test_follow_symlinks},
+    {"assert", NULL, 0, 0, test_assert},
+    {"ovs_scan", NULL, 0, 0, test_ovs_scan},
+    {"snprintf", NULL, 0, 0, test_snprintf},
+#ifndef _WIN32
+    {"file_name", NULL, 1, INT_MAX, test_file_name},
+#endif
+    {NULL, NULL, 0, 0, NULL},
 };
 
 static void
@@ -965,7 +1080,7 @@ parse_options(int argc, char *argv[])
         VLOG_LONG_OPTIONS,
         {NULL, 0, NULL, 0},
     };
-    char *short_options = long_options_to_short_options(long_options);
+    char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
 
     for (;;) {
         int c = getopt_long(argc, argv, short_options, long_options, NULL);
@@ -986,11 +1101,20 @@ parse_options(int argc, char *argv[])
     free(short_options);
 }
 
-int
-main(int argc, char *argv[])
+static void
+test_util_main(int argc, char *argv[])
 {
+    struct ovs_cmdl_context ctx = { .argc = 0, };
     set_program_name(argv[0]);
     parse_options(argc, argv);
-    run_command(argc - optind, argv + optind, commands);
-    return 0;
+    /* On Windows, stderr is fully buffered if connected to a pipe.
+     * Make it _IONBF so that an abort does not miss log contents.
+     * POSIX doesn't define the circumstances in which stderr is
+     * fully buffered either. */
+    setvbuf(stderr, NULL, _IONBF, 0);
+    ctx.argc = argc - optind;
+    ctx.argv = argv + optind;
+    ovs_cmdl_run_command(&ctx, commands);
 }
+
+OVSTEST_REGISTER("test-util", test_util_main);