*/
#include "test/birdtest.h"
+#include "test/bt-utils.h"
#include "bitops.h"
#define MAX_NUM 1000
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);
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");
check_log2(i);
for (i = 1; i < MAX_NUM; i++)
- check_log2(bt_rand_num());
+ check_log2(bt_rand_num() % 0xffff);
return BT_SUCCESS;
}
#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)
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();
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
}
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);
}
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)
{
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
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;
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
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__)
* 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;
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");
}
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);
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;
+}
#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_ */
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