From e7bb2daf1f69289edfc2fa3968315d585bd50d6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Tvrd=C3=ADk?= Date: Fri, 3 Apr 2015 12:46:04 +0200 Subject: [PATCH] Birdtest: improve bt_check macro 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 | 16 +++++++++------- test/birdtest.h | 21 +++++++++------------ 2 files changed, 18 insertions(+), 19 deletions(-) diff --git a/lib/bitops_test.c b/lib/bitops_test.c index 4084d229..3b58ae1d 100644 --- a/lib/bitops_test.c +++ b/lib/bitops_test.c @@ -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; } diff --git a/test/birdtest.h b/test/birdtest.h index d4a54407..8f9d8c14 100644 --- a/test/birdtest.h +++ b/test/birdtest.h @@ -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)