From daaa115943fb3535a8805a443b581193a5291108 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20Tvrd=C3=ADk?= Date: Wed, 19 Aug 2015 14:40:06 +0200 Subject: [PATCH] Birdtest: add some utils to framework and updates old tests --- lib/bitops_test.c | 24 +++---------- lib/buffer_test.c | 8 ++--- lib/hash_test.c | 2 +- test/birdtest.c | 19 +++++++---- test/birdtest.h | 6 ++-- test/{utils.c => bt-utils.c} | 65 ++++++++++++++++++++++++++++-------- test/{utils.h => bt-utils.h} | 6 +++- tools/Makefile.in | 2 +- 8 files changed, 83 insertions(+), 49 deletions(-) rename test/{utils.c => bt-utils.c} (57%) rename test/{utils.h => bt-utils.h} (68%) diff --git a/lib/bitops_test.c b/lib/bitops_test.c index ac103970..2699fed0 100644 --- a/lib/bitops_test.c +++ b/lib/bitops_test.c @@ -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; } diff --git a/lib/buffer_test.c b/lib/buffer_test.c index f9049f34..2c671f16 100644 --- a/lib/buffer_test.c +++ b/lib/buffer_test.c @@ -14,11 +14,11 @@ #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) diff --git a/lib/hash_test.c b/lib/hash_test.c index 86d37174..18fdc466 100644 --- a/lib/hash_test.c +++ b/lib/hash_test.c @@ -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(); diff --git a/test/birdtest.c b/test/birdtest.c index d8b1a42b..e5974966 100644 --- a/test/birdtest.c +++ b/test/birdtest.c @@ -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; diff --git a/test/birdtest.h b/test/birdtest.h index 9025f35d..f7e4b312 100644 --- a/test/birdtest.h +++ b/test/birdtest.h @@ -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__) diff --git a/test/utils.c b/test/bt-utils.c similarity index 57% rename from test/utils.c rename to test/bt-utils.c index 47ce9c15..881f4fc5 100644 --- a/test/utils.c +++ b/test/bt-utils.c @@ -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 +#include +#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; +} diff --git a/test/utils.h b/test/bt-utils.h similarity index 68% rename from test/utils.h rename to test/bt-utils.h index fdb24fbe..da6e6a3a 100644 --- a/test/utils.h +++ b/test/bt-utils.h @@ -11,8 +11,12 @@ #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_ */ diff --git a/tools/Makefile.in b/tools/Makefile.in index 1eb1ecce..3c4573b3 100644 --- a/tools/Makefile.in +++ b/tools/Makefile.in @@ -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