0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2025-01-03 15:41:54 +00:00

Birdtest: add some utils to framework

and updates old tests
This commit is contained in:
Pavel Tvrdík 2015-08-19 14:40:06 +02:00
parent 39059e21b7
commit daaa115943
8 changed files with 83 additions and 49 deletions

View File

@ -7,6 +7,7 @@
*/ */
#include "test/birdtest.h" #include "test/birdtest.h"
#include "test/bt-utils.h"
#include "bitops.h" #include "bitops.h"
#define MAX_NUM 1000 #define MAX_NUM 1000
@ -79,27 +80,12 @@ t_masklen(void)
return BT_SUCCESS; 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 static void
check_log2(u32 n) check_log2(u32 n)
{ {
u32 log = u32_log2(n); u32 log = u32_log2(n);
u32 low = pow2(log); u32 low = naive_pow(2, log);
u32 high = pow2(log+1); 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_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); bt_assert(n >= low && n < high);
@ -117,7 +103,7 @@ t_log2(void)
for (i = 0; i < 31; i++) 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; in_out_data[i].out = i+1;
} }
bt_assert_out_fn_in(u32_log2, in_out_data, "%u", "%u"); bt_assert_out_fn_in(u32_log2, in_out_data, "%u", "%u");
@ -128,7 +114,7 @@ t_log2(void)
check_log2(i); check_log2(i);
for (i = 1; i < MAX_NUM; i++) for (i = 1; i < MAX_NUM; i++)
check_log2(bt_rand_num()); check_log2(bt_rand_num() % 0xffff);
return BT_SUCCESS; return BT_SUCCESS;
} }

View File

@ -14,11 +14,11 @@
#include "lib/buffer.h" #include "lib/buffer.h"
#define MAX_NUM 33 #define MAX_NUM 33
typedef BUFFER(int) buffer_int;
int expected[MAX_NUM]; typedef BUFFER(int) buffer_int;
buffer_int buffer; static int expected[MAX_NUM];
struct pool *buffer_pool; static buffer_int buffer;
static struct pool *buffer_pool;
static void static void
show_buf(buffer_int *b) show_buf(buffer_int *b)

View File

@ -90,7 +90,7 @@ validate_filled_hash(void)
for (i = 0; i < MAX_NUM; i++) for (i = 0; i < MAX_NUM; i++)
{ {
node = HASH_FIND(hash, TEST, nodes[i].key); 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(); print_rate_of_fulfilment();

View File

@ -36,11 +36,15 @@ const char *bt_test_id;
uint bt_success; uint bt_success;
uint bt_test_suite_success; uint bt_test_suite_success;
int long int
bt_rand_num(void) bt_rand_num(void)
{ {
/* Seeded in bt_init() */ /* 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 void
@ -119,7 +123,7 @@ dump_stack(void)
} }
static 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; int result;
alarm(timeout); alarm(timeout);
@ -133,7 +137,7 @@ int bt_run_test_fn(int (*test_fn)(void *), void *test_fn_argument, int timeout)
} }
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, ...) 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) 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 static uint
get_num_terminal_cols(void) get_num_terminal_cols(void)
{ {
struct winsize w; struct winsize w = {};
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
return w.ws_col; uint cols = w.ws_col;
return (cols > 0 ? cols : 80);
} }
void void
@ -229,7 +234,7 @@ bt_result(const char *to_right_align_msg, const char *to_left_align_msg, ...)
va_list argptr; va_list argptr;
va_start(argptr, to_left_align_msg); 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]; char fmt_buf[BT_BUFFER_SIZE];
uint line_len = strlen(msg_buf) + BT_PROMPT_OK_FAIL_LEN; uint line_len = strlen(msg_buf) + BT_PROMPT_OK_FAIL_LEN;

View File

@ -27,8 +27,8 @@ extern const char *bt_test_id;
void bt_init(int argc, char *argv[]); void bt_init(int argc, char *argv[]);
int bt_end(void); 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, ...); 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, ...);
int bt_rand_num(void); long int bt_rand_num(void);
void bt_result(const char *result, const char *msg, ...); void bt_result(const char *result, const char *msg, ...);
#define BT_SUCCESS 0 #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__) bt_test_suite_extra(fn, BT_DEFAULT_FORKING, BT_DEFAULT_TIMEOUT, dsc, ##__VA_ARGS__)
#define bt_test_suite_extra(fn, f, t, dsc, ...) \ #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, ...) \ #define bt_test_suite_arg(fn, arg, dsc, ...) \
bt_test_suite_arg_extra(fn, arg, BT_DEFAULT_FORKING, BT_DEFAULT_TIMEOUT, dsc, ##__VA_ARGS__) bt_test_suite_arg_extra(fn, arg, BT_DEFAULT_FORKING, BT_DEFAULT_TIMEOUT, dsc, ##__VA_ARGS__)

View File

@ -6,15 +6,18 @@
* Can be freely distributed and used under the terms of the GNU GPL. * Can be freely distributed and used under the terms of the GNU GPL.
*/ */
#include "test/birdtest.h" #include <stdlib.h>
#include "test/utils.h" #include <test/bt-utils.h>
#include "test/birdtest.h"
#include "filter/filter.h" #include "filter/filter.h"
#include "nest/iface.h" #include "nest/iface.h"
#include "nest/locks.h" #include "nest/locks.h"
#include "lib/unix.h" #include "lib/unix.h"
#include "lib/krt.h" #include "lib/krt.h"
#define BETWEEN(a, b, c) (((a) >= (b)) && ((a) <= (c)))
static const byte *bt_config_parse_pos; static const byte *bt_config_parse_pos;
static uint bt_config_parse_remain_len; static uint bt_config_parse_remain_len;
@ -46,26 +49,43 @@ bt_bird_init(void) {
protos_build(); protos_build();
proto_build(&proto_unix_kernel); proto_build(&proto_unix_kernel);
proto_build(&proto_unix_iface); proto_build(&proto_unix_iface);
bt_config_parse(
BT_CONFIG_PARSE_ROUTER_ID
BT_CONFIG_PARSE_KERNEL_DEVICE
);
} }
static void static void
bt_debug_with_line_nums(const char *str) bt_debug_with_line_nums(const char *str)
{ {
const char *c = str; const char *c = str;
uint lineno = 1; uint lino = 0;
while (*c) while (*c)
{ {
bt_debug("%3u ", lineno); lino++;
bt_debug("%3u ", lino);
do do
{ {
bt_debug("%c", *c); bt_debug("%c", *c);
} while (*c && *(c++) != '\n'); } 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"); bt_debug("\n");
} }
@ -73,8 +93,8 @@ bt_debug_with_line_nums(const char *str)
struct config * struct config *
bt_config_parse(const char *str_cfg) bt_config_parse(const char *str_cfg)
{ {
bt_debug("Parsing new configuration:\n"); // bt_debug("Parsing new configuration:\n");
bt_debug_with_line_nums(str_cfg); // bt_debug_with_line_nums(str_cfg);
struct config *cfg = config_alloc(""); struct config *cfg = config_alloc("");
bt_config_parse_pos = str_cfg; bt_config_parse_pos = str_cfg;
bt_config_parse_remain_len = strlen(str_cfg); bt_config_parse_remain_len = strlen(str_cfg);
@ -88,6 +108,25 @@ bt_config_parse(const char *str_cfg)
return 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; 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;
}

View File

@ -11,8 +11,12 @@
#define BT_CONFIG_PARSE_ROUTER_ID "router id 10.0.0.1; \n" #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_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(void);
void bt_bird_init_with_simple_configuration(void);
struct config *bt_config_parse(const char *str_cfg); struct config *bt_config_parse(const char *str_cfg);
#endif /* _BIRDTEST_UTILS_H_ */ uint naive_pow(uint base, uint power);
#endif /* _BIRDTEST_UTILS_H_ */

View 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 $(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 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 $@ $^ $(CC) -nostdlib -r -o $@ $^
test/%.o: $(srcdir)/test/%.c test/%.o: $(srcdir)/test/%.c