]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Birdtest: add some utils to framework
authorPavel Tvrdík <pawel.tvrdik@gmail.cz>
Wed, 19 Aug 2015 12:40:06 +0000 (14:40 +0200)
committerPavel Tvrdík <pawel.tvrdik@gmail.cz>
Wed, 19 Aug 2015 12:57:34 +0000 (14:57 +0200)
and updates old tests

lib/bitops_test.c
lib/buffer_test.c
lib/hash_test.c
test/birdtest.c
test/birdtest.h
test/bt-utils.c [moved from test/utils.c with 57% similarity]
test/bt-utils.h [moved from test/utils.h with 68% similarity]
tools/Makefile.in

index ac103970b28609452ef27968bce2378c17bf8a48..2699fed098041bbe2141440cb993268d124723a0 100644 (file)
@@ -7,6 +7,7 @@
  */
 
 #include "test/birdtest.h"
+#include "test/bt-utils.h"
 #include "bitops.h"
 
 #define MAX_NUM 1000
@@ -79,27 +80,12 @@ t_masklen(void)
   return BT_SUCCESS;
 }
 
-static u32
-pow2(u32 exp)
-{
-  u32 i, n, r;
-  n = r = 2;
-
-  if (exp == 0)
-    return 1;
-
-  for (i = 2; i <= exp; i++)
-    r *= n;
-
-  return r;
-}
-
 static void
 check_log2(u32 n)
 {
   u32 log  = u32_log2(n);
-  u32 low  = pow2(log);
-  u32 high = pow2(log+1);
+  u32 low  = naive_pow(2, log);
+  u32 high = naive_pow(2, log+1);
 
   bt_debug("Test u32_log2(%u) = %u, %u should be in the range <%u, %u) \n", n, log, n, low, high);
   bt_assert(n >= low && n < high);
@@ -117,7 +103,7 @@ t_log2(void)
 
   for (i = 0; i < 31; i++)
   {
-    in_out_data[i].in  = pow2(i+1);
+    in_out_data[i].in  = naive_pow(2, i+1);
     in_out_data[i].out = i+1;
   }
   bt_assert_out_fn_in(u32_log2, in_out_data, "%u", "%u");
@@ -128,7 +114,7 @@ t_log2(void)
     check_log2(i);
 
   for (i = 1; i < MAX_NUM; i++)
-    check_log2(bt_rand_num());
+    check_log2(bt_rand_num() % 0xffff);
 
   return BT_SUCCESS;
 }
index f9049f3477a70e0f2195dca134a37e24cd16ba21..2c671f16da41311d7dfe0a4461ef61a027440235 100644 (file)
 #include "lib/buffer.h"
 
 #define MAX_NUM 33
-typedef BUFFER(int) buffer_int;
 
-int expected[MAX_NUM];
-buffer_int buffer;
-struct pool *buffer_pool;
+typedef BUFFER(int) buffer_int;
+static int expected[MAX_NUM];
+static buffer_int buffer;
+static struct pool *buffer_pool;
 
 static void
 show_buf(buffer_int *b)
index 86d37174c57651c0a6f5b17acd541f558a64f681..18fdc46640037093c1c0a05c76f491a0e2ece478 100644 (file)
@@ -90,7 +90,7 @@ validate_filled_hash(void)
   for (i = 0; i < MAX_NUM; i++)
   {
     node = HASH_FIND(hash, TEST, nodes[i].key);
-    bt_assert_msg(node == &nodes[i], "Hash should be filled, but we not find (%p) the node[%d] (%p) with .key = %u, .next %p \n", node, i, &nodes[i], nodes[i].key, nodes[i].next);
+    bt_assert_msg(node->key == nodes[i].key, "Hash should be filled, but we not find (%p) the node[%d] (%p) with .key = %u, .next %p \n", node, i, &nodes[i], nodes[i].key, nodes[i].next);
   }
 
   print_rate_of_fulfilment();
index d8b1a42be98001cd593e44791c818cb1c3210ab6..e5974966e2b91fe87d099abc810d92befff412f8 100644 (file)
@@ -36,11 +36,15 @@ const char *bt_test_id;
 uint bt_success;
 uint bt_test_suite_success;
 
-int
+long int
 bt_rand_num(void)
 {
   /* Seeded in bt_init() */
-  return random();
+  long int rand_low, rand_high;
+
+  rand_low = random();
+  rand_high = random();
+  return (rand_low & 0xffff) | ((rand_high & 0xffff) << 16);
 }
 
 void
@@ -119,7 +123,7 @@ dump_stack(void)
 }
 
 static
-int bt_run_test_fn(int (*test_fn)(void *), void *test_fn_argument, int timeout)
+int bt_run_test_fn(int (*test_fn)(const void *), const void *test_fn_argument, int timeout)
 {
   int result;
   alarm(timeout);
@@ -133,7 +137,7 @@ int bt_run_test_fn(int (*test_fn)(void *), void *test_fn_argument, int timeout)
 }
 
 void
-bt_test_suite_base(int (*test_fn)(void *), const char *test_id, void *test_fn_argument, int forked, int timeout, const char *dsc, ...)
+bt_test_suite_base(int (*test_fn)(const void *), const char *test_id, const void *test_fn_argument, int forked, int timeout, const char *dsc, ...)
 {
   if (list_tests)
   {
@@ -213,9 +217,10 @@ bt_test_suite_base(int (*test_fn)(void *), const char *test_id, void *test_fn_ar
 static uint
 get_num_terminal_cols(void)
 {
-  struct winsize w;
+  struct winsize w = {};
   ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
-  return w.ws_col;
+  uint cols = w.ws_col;
+  return (cols > 0 ? cols : 80);
 }
 
 void
@@ -229,7 +234,7 @@ bt_result(const char *to_right_align_msg, const char *to_left_align_msg, ...)
 
     va_list argptr;
     va_start(argptr, to_left_align_msg);
-    vsnprintf(msg_buf + strlen(msg_buf), sizeof(msg_buf), to_left_align_msg, argptr);
+    vsnprintf(msg_buf + strlen((char *)msg_buf), sizeof(msg_buf), to_left_align_msg, argptr);
 
     char fmt_buf[BT_BUFFER_SIZE];
     uint line_len = strlen(msg_buf) + BT_PROMPT_OK_FAIL_LEN;
index 9025f35d4c77c6b392a783bc489660bd28cfbcc7..f7e4b31202f80eca0084d38d48791b5c8da9f374 100644 (file)
@@ -27,8 +27,8 @@ extern const char *bt_test_id;
 
 void bt_init(int argc, char *argv[]);
 int  bt_end(void);
-void bt_test_suite_base(int (*test_fn)(void *), const char *test_id, void *test_fn_argument, int forked, int timeout, const char *dsc, ...);
-int  bt_rand_num(void);
+void bt_test_suite_base(int (*test_fn)(const void *), const char *test_id, const void *test_fn_argument, int forked, int timeout, const char *dsc, ...);
+long int bt_rand_num(void);
 void bt_result(const char *result, const char *msg, ...);
 
 #define BT_SUCCESS                     0
@@ -54,7 +54,7 @@ void bt_result(const char *result, const char *msg, ...);
     bt_test_suite_extra(fn, BT_DEFAULT_FORKING, BT_DEFAULT_TIMEOUT, dsc, ##__VA_ARGS__)
 
 #define bt_test_suite_extra(fn, f, t, dsc, ...) \
-    bt_test_suite_base((int (*)(void *))fn, #fn, NULL, f, t, dsc, ##__VA_ARGS__)
+    bt_test_suite_base((int (*)(const void *))fn, #fn, NULL, f, t, dsc, ##__VA_ARGS__)
 
 #define bt_test_suite_arg(fn, arg, dsc, ...) \
     bt_test_suite_arg_extra(fn, arg, BT_DEFAULT_FORKING, BT_DEFAULT_TIMEOUT, dsc, ##__VA_ARGS__)
similarity index 57%
rename from test/utils.c
rename to test/bt-utils.c
index 47ce9c1545d661eaa7ee661f68bd6592d919ec90..881f4fc54edbc4c9c0702f047ffd6ec23fed1a7f 100644 (file)
@@ -6,15 +6,18 @@
  *     Can be freely distributed and used under the terms of the GNU GPL.
  */
 
-#include "test/birdtest.h"
-#include "test/utils.h"
+#include <stdlib.h>
+#include <test/bt-utils.h>
 
+#include "test/birdtest.h"
 #include "filter/filter.h"
 #include "nest/iface.h"
 #include "nest/locks.h"
 #include "lib/unix.h"
 #include "lib/krt.h"
 
+#define BETWEEN(a, b, c)  (((a) >= (b)) && ((a) <= (c)))
+
 static const byte *bt_config_parse_pos;
 static uint bt_config_parse_remain_len;
 
@@ -46,26 +49,43 @@ bt_bird_init(void) {
   protos_build();
   proto_build(&proto_unix_kernel);
   proto_build(&proto_unix_iface);
-
-  bt_config_parse(
-    BT_CONFIG_PARSE_ROUTER_ID
-    BT_CONFIG_PARSE_KERNEL_DEVICE
-  );
 }
 
 static void
 bt_debug_with_line_nums(const char *str)
 {
   const char *c = str;
-  uint lineno = 1;
+  uint lino = 0;
   while (*c)
   {
-    bt_debug("%3u ", lineno);
+    lino++;
+    bt_debug("%3u   ", lino);
     do
     {
       bt_debug("%c", *c);
     } while (*c && *(c++) != '\n');
-    lineno++;
+  }
+  bt_debug("\n");
+}
+
+static void
+bt_show_cfg_error(const char *str, const struct config *cfg)
+{
+  const char *c = str;
+  int lino = 0;
+  int lino_delta = 5;
+  int lino_err = cfg->err_lino;
+
+  while (*c)
+  {
+    lino++;
+    if (BETWEEN(lino, lino_err - lino_delta, lino_err + lino_delta))
+      bt_debug("%3u%s", lino, (lino_err == lino ? " > " : "   "));
+    do
+    {
+      if (BETWEEN(lino, lino_err - lino_delta, lino_err + lino_delta))
+       bt_debug("%c", *c);
+    } while (*c && *(c++) != '\n');
   }
   bt_debug("\n");
 }
@@ -73,8 +93,8 @@ bt_debug_with_line_nums(const char *str)
 struct config *
 bt_config_parse(const char *str_cfg)
 {
-  bt_debug("Parsing new configuration:\n");
-  bt_debug_with_line_nums(str_cfg);
+//  bt_debug("Parsing new configuration:\n");
+//  bt_debug_with_line_nums(str_cfg);
   struct config *cfg = config_alloc("");
   bt_config_parse_pos = str_cfg;
   bt_config_parse_remain_len = strlen(str_cfg);
@@ -88,6 +108,25 @@ bt_config_parse(const char *str_cfg)
     return cfg;
   }
 
-  bt_assert_msg(0, "At line %d is error: %s \n", new_config->err_lino, new_config->err_msg);
+  bt_assert_msg(0, "At line %d is error: %s", new_config->err_lino, new_config->err_msg);
+  bt_show_cfg_error(str_cfg, new_config);
+
   return NULL;
 }
+
+void
+bt_bird_init_with_simple_configuration(void)
+{
+  bt_bird_init();
+  bt_config_parse(BT_CONFIG_SIMPLE);
+}
+
+uint
+naive_pow(uint base, uint power)
+{
+  uint result = 1;
+  uint i;
+  for (i = 0; i < power; i++)
+    result *= base;
+  return result;
+}
similarity index 68%
rename from test/utils.h
rename to test/bt-utils.h
index fdb24fbe5934d4770b27faa12571bacec437c10f..da6e6a3a0494b6af1d83048f88a96398253594e0 100644 (file)
 
 #define BT_CONFIG_PARSE_ROUTER_ID       "router id 10.0.0.1; \n"
 #define BT_CONFIG_PARSE_KERNEL_DEVICE   "protocol device {} \n"
+#define BT_CONFIG_SIMPLE               BT_CONFIG_PARSE_ROUTER_ID BT_CONFIG_PARSE_KERNEL_DEVICE
 
 void bt_bird_init(void);
+void bt_bird_init_with_simple_configuration(void);
 struct config *bt_config_parse(const char *str_cfg);
 
-#endif /* _BIRDTEST_UTILS_H_ */
\ No newline at end of file
+uint naive_pow(uint base, uint power);
+
+#endif /* _BIRDTEST_UTILS_H_ */
index 1eb1ecce0deeef256bc359fb11034f6189c3ef67..3c4573b3ccea03009cb034ec46c4898bde2c2925 100644 (file)
@@ -13,7 +13,7 @@ tests: test/all.o
        set -e ; for a in $(dynamic-dirs) ; do $(MAKE) -C $$a $@ ; done
        set -e ; for a in $(static-dirs) $(client-dirs) ; do $(MAKE) -C $$a -f $(srcdir_abs)/$$a/Makefile $@ ; done
 
-test/all.o: test/birdtest.o test/utils.o
+test/all.o: test/birdtest.o test/bt-utils.o
        $(CC) -nostdlib -r -o $@ $^
 
 test/%.o: $(srcdir)/test/%.c