]> git.ipfire.org Git - thirdparty/bird.git/commitdiff
Birdtest: improve bt_check macro
authorPavel Tvrdík <pawel.tvrdik@gmail.cz>
Fri, 3 Apr 2015 10:46:04 +0000 (12:46 +0200)
committerPavel Tvrdík <pawel.tvrdik@gmail.cz>
Fri, 3 Apr 2015 10:46:04 +0000 (12:46 +0200)
Now dat for testing can be defined somelike this:

  struct in_out_data_ {
    char *in;
    u32 out;
  } in_out_data[] = {
      {
  .in  = "192.168.1.128",
  .out = build_ip4(192, 168, 1, 128),
      },
      {
  .in  = "255.255.255.255",
  .out = build_ip4(255, 255, 255, 255),
      },
      ...
  };

  bt_check(ip4_pton_, in_out_data, "%s", "0x%08X");

lib/bitops_test.c
test/birdtest.h

index 4084d229cd607fa0d0eaaf5233377456cf66abd3..3b58ae1d48f545a6fdaa5495c4ab929ac6eee526 100644 (file)
@@ -101,7 +101,7 @@ check_log2(u32 n)
   u32 low  = pow2(log);
   u32 high = pow2(log+1);
 
-  bt_debug("Test u32_log2(%u) = %u,  %u should be in <%u, %u) \n", n, log, n, low, high);
+  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);
 }
 
@@ -110,14 +110,17 @@ t_log2(void)
 {
   u32 i;
 
-  u32 in[31];
-  u32 expected[31];
+  struct in_out_data_ {
+    u32 in;
+    u32 out;
+  } in_out_data[31];
+
   for (i = 0; i < 31; i++)
   {
-    in[i] = pow2(i+1);
-    expected[i] = i+1;
+    in_out_data[i].in  = pow2(i+1);
+    in_out_data[i].out = i+1;
   }
-  bt_check(u32_log2, in, expected, 31);
+  bt_check(u32_log2, in_out_data, "%u", "%u");
 
   u32_log2(0);
 
@@ -139,6 +142,5 @@ main(int argc, char *argv[])
   bt_test_case(t_masklen, "u32_masklen()");
   bt_test_case(t_log2, "u32_log2()");
 
-
   return 0;
 }
index d4a54407fcee298beeccc2809403d973b7fc8667..8f9d8c14f52f6377b137cf396e74fdee0d8d18de 100644 (file)
@@ -59,19 +59,16 @@ int  bt_rand_num(void);
 #define bt_syscall(test,format, ...)                   \
     do { if (test) { bt_log(format ": %s", ##__VA_ARGS__, strerror(errno)); exit(3); } } while (0)
 
-#define bt_check(fn, in_arr, expected_arr, len)                        \
-  do {                                                                 \
-    unsigned int bt_i_;                                                        \
-    for (bt_i_ = 0; bt_i_ < len; bt_i_++)                              \
+#define bt_check(fn, in_out, in_fmt, out_fmt)                          \
+  do                                                                   \
+  {                                                                    \
+    unsigned int bt_i;                                                 \
+    for (bt_i = 0; bt_i < (sizeof(in_out)/sizeof(in_out[0])); bt_i++)  \
     {                                                                  \
-      bt_debug("%s(%u) = %u", #fn, in_arr[bt_i_], fn(in_arr[bt_i_]));  \
-      if(fn(in_arr[bt_i_]) != expected_arr[bt_i_])                     \
-      {                                                                        \
-       bt_debug(", expected %u  FAIL! \n", expected_arr[bt_i_]);       \
-       bt_abort_msg("%s(%u) = %u, but expected %u",  #fn, in_arr[bt_i_], fn(in_arr[bt_i_]), expected_arr[bt_i_]); \
-      }                                                                        \
-      else                                                             \
-       bt_debug("  OK \n");                                            \
+      if (fn(in_out[bt_i].in) == in_out[bt_i].out)                     \
+       bt_debug    ("[ OK ] %s(" in_fmt ") got " out_fmt " \n", #fn, in_out[bt_i].in, fn(in_out[bt_i].in)); \
+      else                                                             \
+       bt_abort_msg("[FAIL] %s(" in_fmt ") got " out_fmt ", but was expected " out_fmt " \n", #fn, in_out[bt_i].in, fn(in_out[bt_i].in), in_out[bt_i].out); \
     }                                                                  \
   } while(0)