command-line: add ovs_cmdl_ prefix
[cascardo/ovs.git] / tests / test-util.c
index 363abb1..01a1a78 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)
@@ -1030,23 +1028,46 @@ test_snprintf(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
 
     ovs_assert(snprintf(NULL, 0, "abcde") == 5);
 }
+
+#ifndef _WIN32
+static void
+test_file_name(int argc, char *argv[])
+{
+    int i;
+
+    for (i = 1; i < argc; i++) {
+        char *dir, *base;
+
+        dir = dir_name(argv[i]);
+        puts(dir);
+        free(dir);
+
+        base = base_name(argv[i]);
+        puts(base);
+        free(base);
+    }
+}
+#endif /* _WIN32 */
 \f
-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},
-    {"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},
-    {"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},
-    {"snprintf", 0, 0, test_snprintf},
-    {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
@@ -1059,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);
@@ -1080,11 +1101,17 @@ 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;
+    /* 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);
+    ovs_cmdl_run_command(argc - optind, argv + optind, commands);
 }
+
+OVSTEST_REGISTER("test-util", test_util_main);