0
0
mirror of https://gitlab.nic.cz/labs/bird.git synced 2024-12-23 02:01:55 +00:00

Birdtest: Improve reliable

Fixing some bugs
Add option to call birdtest with an argument (void *)
This commit is contained in:
Pavel Tvrdík 2015-08-14 16:08:04 +02:00
parent 0f38795b33
commit aa96f486e0
3 changed files with 74 additions and 38 deletions

View File

@ -29,12 +29,12 @@ static int do_core;
static int no_fork;
static int no_timeout;
int bt_verbose;
uint bt_verbose;
const char *bt_filename;
const char *bt_test_id;
int bt_success;
int bt_test_suite_success;
uint bt_success;
uint bt_test_suite_success;
int
bt_rand_num(void)
@ -48,7 +48,7 @@ bt_init(int argc, char *argv[])
{
int c;
bt_success = 1;
bt_success = BT_SUCCESS;
srandom(BT_RANDOM_SEED);
bt_verbose = 0;
@ -118,12 +118,31 @@ dump_stack(void)
#endif
}
static
int bt_run_test_fn(int (*test_fn)(void *), void *test_fn_argument, int timeout)
{
int result;
alarm(timeout);
if (test_fn_argument)
result = test_fn(test_fn_argument);
else
result = ((int (*)(void))test_fn)();
if (bt_test_suite_success == BT_FAILURE)
result = BT_FAILURE;
return result;
}
void
bt_test_suite5(int (*test_fn)(void), const char *test_id, const char *dsc, int forked, int timeout)
bt_test_suite_base(int (*test_fn)(void *), const char *test_id, void *test_fn_argument, int forked, int timeout, const char *dsc, ...)
{
if (list_tests)
{
printf("%28s : %s\n", test_id, dsc);
printf("%28s : ", test_id);
va_list args;
va_start(args, dsc);
vprintf(dsc, args);
va_end(args);
printf("\n");
return;
}
@ -136,8 +155,8 @@ bt_test_suite5(int (*test_fn)(void), const char *test_id, const char *dsc, int f
if (request && strcmp(test_id, request))
return;
int result = 0;
bt_test_suite_success = 1;
int result;
bt_test_suite_success = BT_SUCCESS;
bt_test_id = test_id;
@ -146,9 +165,7 @@ bt_test_suite5(int (*test_fn)(void), const char *test_id, const char *dsc, int f
if (!forked)
{
alarm(timeout);
result = test_fn();
result &= bt_test_suite_success;
result = bt_run_test_fn(test_fn, test_fn_argument, timeout);
}
else
{
@ -157,9 +174,7 @@ bt_test_suite5(int (*test_fn)(void), const char *test_id, const char *dsc, int f
if (pid == 0)
{
alarm(timeout);
result = test_fn();
result &= bt_test_suite_success;
result = bt_run_test_fn(test_fn, test_fn_argument, timeout);
_exit(result);
}
@ -188,7 +203,9 @@ bt_test_suite5(int (*test_fn)(void), const char *test_id, const char *dsc, int f
bt_log("Core dumped");
}
bt_success &= (result == BT_SUCCESS ? 1 : 0);
if (result == BT_FAILURE)
bt_success = BT_FAILURE;
bt_result((result == BT_SUCCESS ? BT_PROMPT_OK : BT_PROMPT_FAIL), "%s", bt_test_id);
bt_test_id = NULL;
}
@ -226,7 +243,7 @@ bt_result(const char *to_right_align_msg, const char *to_left_align_msg, ...)
int
bt_end(void)
{
return !bt_success;
return (bt_success == BT_SUCCESS ? 0 : 1);
}
void

View File

@ -13,10 +13,10 @@
#include <string.h>
#include <errno.h>
extern int bt_success;
extern int bt_test_suite_success;
extern uint bt_success;
extern uint bt_test_suite_success;
extern int bt_verbose;
extern uint bt_verbose;
#define BT_VERBOSE_NOTHING 0
#define BT_VERBOSE_TEST_SUITE 1
#define BT_VERBOSE_TEST_CASE 2
@ -27,12 +27,12 @@ extern const char *bt_test_id;
void bt_init(int argc, char *argv[]);
int bt_end(void);
void bt_test_suite5(int (*fn)(void), const char *id, const char *dsc, int forked, 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, ...);
int bt_rand_num(void);
void bt_result(const char *result, const char *msg, ...);
#define BT_SUCCESS 1
#define BT_FAILURE 0
#define BT_SUCCESS 0
#define BT_FAILURE 1
#define BT_DEFAULT_TIMEOUT 5
#define BT_DEFAULT_FORKING 1
@ -50,11 +50,17 @@ void bt_result(const char *result, const char *msg, ...);
#define BT_PROMPT_FN_GIVES(in_fmt) "%s(" in_fmt ") gives "
#define BT_PROMPT_EXPECTING ", but expecting is "
#define bt_test_suite(fn,dsc) \
bt_test_suite4(fn, dsc, BT_DEFAULT_FORKING, BT_DEFAULT_TIMEOUT)
#define bt_test_suite(fn, dsc, ...) \
bt_test_suite_extra(fn, BT_DEFAULT_FORKING, BT_DEFAULT_TIMEOUT, dsc, ##__VA_ARGS__)
#define bt_test_suite4(fn,dsc,f,t) \
bt_test_suite5(fn, #fn, dsc, f, t)
#define bt_test_suite_extra(fn, f, t, dsc, ...) \
bt_test_suite_base((int (*)(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__)
#define bt_test_suite_arg_extra(fn, arg, f, t, dsc, ...) \
bt_test_suite_base(fn, #fn, arg, f, t, dsc, ##__VA_ARGS__)
#define bt_log(format, ...) \
do { \
@ -98,8 +104,10 @@ void bt_result(const char *result, const char *msg, ...);
if (!(test)) \
{ \
if (bt_verbose) \
{ \
bt_log(format, ##__VA_ARGS__); \
bt_success = bt_test_suite_success = 0; \
} \
bt_test_suite_success = BT_FAILURE; \
} \
} while (0)
@ -119,7 +127,9 @@ void bt_strncat_(char *buf, size_t buf_size, const char *str, ...);
u32 *pc = (u32*) data; \
bt_strncat(buf, "{"); \
for (k = 0; k < (sizeof(*data) / sizeof(typeof(*pc))); k++) \
{ \
bt_strncat(buf, "%s0x%08X", (k ? ", " : ""), pc[k]); \
} \
bt_strncat(buf, "}"); \
} while (0)
@ -127,9 +137,13 @@ void bt_strncat_(char *buf, size_t buf_size, const char *str, ...);
do \
{ \
if (fmt == NULL) \
{ \
bt_dump_struct(buf, &data); \
} \
else \
{ \
bt_strncat_(buf, sizeof(buf), fmt, data); \
} \
} while (0)
#define bt_print_result_line(fn, in, out, fn_out, in_fmt, out_fmt, result) \
@ -168,7 +182,10 @@ void bt_strncat_(char *buf, size_t buf_size, const char *str, ...);
{ \
typeof(in_out[i].out) fn_out = fn(in_out[i].in); \
int single_test_case_success = (fn(in_out[i].in) == in_out[i].out); \
bt_test_suite_success &= single_test_case_success; \
if (!single_test_case_success) \
{ \
bt_test_suite_success = BT_FAILURE; \
} \
bt_print_result_line(fn, in_out[i].in, in_out[i].out, fn_out, in_fmt, out_fmt, single_test_case_success); \
} \
} while (0)
@ -194,8 +211,10 @@ void bt_strncat_(char *buf, size_t buf_size, const char *str, ...);
memset(&fn_out, '\0', sizeof(fn_out)); \
fn(in_out[i].in, &fn_out); \
int single_test_case_success = !memcmp(&fn_out, &in_out[i].out, sizeof(in_out[i].out)); \
bt_test_suite_success &= single_test_case_success; \
\
if (!single_test_case_success) \
{ \
bt_test_suite_success = BT_FAILURE; \
} \
bt_print_result_line(fn, in_out[i].in, in_out[i].out, fn_out, in_fmt, out_fmt, single_test_case_success); \
} \
} while (0)

View File

@ -34,7 +34,7 @@ num_succ_tests=0
num_fail_tests=0
echo -e " == Start all $num_all_tests unit tests ==\n"
for test in $all_tests ; do
./$test ; exit_code=$?
./$test > /dev/null 2>&1 ; exit_code=$?
cols=$(tput cols)
offset=$((cols-17))
fmt=" [%2d/%-2d] %-${offset}s"