2015-03-03 12:51:35 +00:00
|
|
|
/*
|
2015-03-19 17:38:38 +00:00
|
|
|
* BIRD -- Unit Test Framework (BIRD Test)
|
2015-03-03 12:51:35 +00:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
2015-03-17 10:27:40 +00:00
|
|
|
#ifndef _BIRDTEST_H_
|
|
|
|
#define _BIRDTEST_H_
|
|
|
|
|
2015-03-03 12:51:35 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
2015-08-27 07:25:28 +00:00
|
|
|
#include <sys/types.h>
|
2015-03-03 12:51:35 +00:00
|
|
|
|
2015-08-14 14:08:04 +00:00
|
|
|
extern uint bt_success;
|
|
|
|
extern uint bt_test_suite_success;
|
2015-04-13 08:52:21 +00:00
|
|
|
|
2015-08-14 14:08:04 +00:00
|
|
|
extern uint bt_verbose;
|
2015-04-13 08:52:21 +00:00
|
|
|
#define BT_VERBOSE_NOTHING 0
|
2015-08-04 12:40:40 +00:00
|
|
|
#define BT_VERBOSE_TEST_SUITE 1
|
|
|
|
#define BT_VERBOSE_TEST_CASE 2
|
|
|
|
#define BT_VERBOSE_DEBUG 3
|
2015-04-13 08:52:21 +00:00
|
|
|
|
2015-03-03 12:51:35 +00:00
|
|
|
extern const char *bt_filename;
|
|
|
|
extern const char *bt_test_id;
|
|
|
|
|
2015-03-17 10:27:40 +00:00
|
|
|
void bt_init(int argc, char *argv[]);
|
2015-04-13 08:52:21 +00:00
|
|
|
int bt_end(void);
|
2015-08-19 12:40:06 +00:00
|
|
|
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, ...);
|
2015-08-27 07:25:28 +00:00
|
|
|
long int bt_random(void);
|
2015-04-13 08:52:21 +00:00
|
|
|
void bt_result(const char *result, const char *msg, ...);
|
|
|
|
|
2015-08-14 14:08:04 +00:00
|
|
|
#define BT_SUCCESS 0
|
|
|
|
#define BT_FAILURE 1
|
2015-03-03 12:51:35 +00:00
|
|
|
|
2015-04-13 08:52:21 +00:00
|
|
|
#define BT_DEFAULT_TIMEOUT 5
|
|
|
|
#define BT_DEFAULT_FORKING 1
|
2015-03-13 17:30:03 +00:00
|
|
|
|
2015-04-13 08:52:21 +00:00
|
|
|
#define BT_RANDOM_SEED 982451653
|
2015-03-17 10:27:40 +00:00
|
|
|
|
2015-04-25 08:49:43 +00:00
|
|
|
#define BT_BUFFER_SIZE 10000
|
2015-03-27 13:03:47 +00:00
|
|
|
|
2015-04-13 08:52:21 +00:00
|
|
|
#define BT_PROMPT_GREEN "\e[1;32m"
|
|
|
|
#define BT_PROMPT_RED "\e[1;31m"
|
|
|
|
#define BT_PROMPT_NORMAL "\e[0m"
|
|
|
|
#define BT_PROMPT_OK " [" BT_PROMPT_GREEN " OK " BT_PROMPT_NORMAL "] "
|
|
|
|
#define BT_PROMPT_FAIL " [" BT_PROMPT_RED "FAIL" BT_PROMPT_NORMAL "] "
|
|
|
|
#define BT_PROMPT_OK_FAIL_LEN 8
|
|
|
|
#define BT_PROMPT_FN_GIVES(in_fmt) "%s(" in_fmt ") gives "
|
|
|
|
#define BT_PROMPT_EXPECTING ", but expecting is "
|
2015-03-17 10:27:40 +00:00
|
|
|
|
2015-08-14 14:08:04 +00:00
|
|
|
#define bt_test_suite(fn, dsc, ...) \
|
|
|
|
bt_test_suite_extra(fn, BT_DEFAULT_FORKING, BT_DEFAULT_TIMEOUT, dsc, ##__VA_ARGS__)
|
2015-04-13 08:42:10 +00:00
|
|
|
|
2015-08-14 14:08:04 +00:00
|
|
|
#define bt_test_suite_extra(fn, f, t, dsc, ...) \
|
2015-08-19 12:40:06 +00:00
|
|
|
bt_test_suite_base((int (*)(const void *))fn, #fn, NULL, f, t, dsc, ##__VA_ARGS__)
|
2015-08-14 14:08:04 +00:00
|
|
|
|
|
|
|
#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__)
|
2015-03-03 12:51:35 +00:00
|
|
|
|
|
|
|
#define bt_log(format, ...) \
|
2015-08-14 14:08:04 +00:00
|
|
|
do { \
|
|
|
|
if (bt_test_id == NULL) \
|
2015-04-13 08:52:21 +00:00
|
|
|
fprintf(stderr, "%s: " format "\n", bt_filename, ##__VA_ARGS__); \
|
2015-08-14 14:08:04 +00:00
|
|
|
else \
|
2015-04-13 08:52:21 +00:00
|
|
|
fprintf(stderr, "%s: %s: " format "\n", bt_filename, bt_test_id, ##__VA_ARGS__); \
|
2015-08-14 14:08:04 +00:00
|
|
|
} while(0)
|
2015-03-03 12:51:35 +00:00
|
|
|
|
2015-03-13 17:27:33 +00:00
|
|
|
#define bt_debug(format, ...) \
|
2015-04-13 08:52:21 +00:00
|
|
|
do { if (bt_verbose >= BT_VERBOSE_DEBUG) printf(format, ##__VA_ARGS__); } while (0)
|
|
|
|
|
|
|
|
#define bt_result_(result, format, ...) bt_result(result, "%s: " format, bt_test_id, ##__VA_ARGS__)
|
|
|
|
#define bt_result_ok(format, ...) bt_result_(BT_PROMPT_OK, format, ##__VA_ARGS__)
|
|
|
|
#define bt_result_fail(format, ...) bt_result_(BT_PROMPT_FAIL, format, ##__VA_ARGS__)
|
|
|
|
|
|
|
|
#define bt_result_check(result, format, ...) \
|
|
|
|
do { if (bt_verbose >= BT_VERBOSE_TEST_CASE) bt_result_(result, format, ##__VA_ARGS__); } while (0)
|
|
|
|
|
|
|
|
#define bt_result_check_ok(format, ...) \
|
2015-08-14 14:08:04 +00:00
|
|
|
do { if (bt_verbose >= BT_VERBOSE_TEST_CASE) bt_result_ok(format, ##__VA_ARGS__); } while (0)
|
2015-04-13 08:52:21 +00:00
|
|
|
|
|
|
|
#define bt_result_check_fail(format, ...) \
|
2015-08-14 14:08:04 +00:00
|
|
|
do { if (bt_verbose >= BT_VERBOSE_TEST_CASE) bt_result_fail(format, ##__VA_ARGS__); } while (0)
|
2015-03-13 17:27:33 +00:00
|
|
|
|
2015-03-03 12:51:35 +00:00
|
|
|
#define bt_abort() \
|
|
|
|
bt_abort_msg("Aborted at %s:%d", __FILE__, __LINE__)
|
|
|
|
|
|
|
|
#define bt_abort_msg(format, ...) \
|
|
|
|
do { bt_log(format, ##__VA_ARGS__); abort(); } while (0)
|
|
|
|
|
|
|
|
#define bt_assert(test) \
|
|
|
|
bt_assert_msg(test, "Assertion (%s) failed at %s:%d", #test, __FILE__, __LINE__)
|
|
|
|
|
2015-04-15 09:51:24 +00:00
|
|
|
#define bt_equal(a, b) \
|
|
|
|
bt_assert_msg((a) == (b), "Assertion (%s == %s) failed at %s:%d.", #a, #b, __FILE__, __LINE__)
|
|
|
|
|
|
|
|
#define bt_assert_msg(test, format, ...) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
if (!(test)) \
|
|
|
|
{ \
|
|
|
|
if (bt_verbose) \
|
2015-08-14 14:08:04 +00:00
|
|
|
{ \
|
2015-04-15 09:51:24 +00:00
|
|
|
bt_log(format, ##__VA_ARGS__); \
|
2015-08-14 14:08:04 +00:00
|
|
|
} \
|
|
|
|
bt_test_suite_success = BT_FAILURE; \
|
2015-04-15 09:51:24 +00:00
|
|
|
} \
|
|
|
|
} while (0)
|
2015-03-03 12:51:35 +00:00
|
|
|
|
2015-04-13 08:52:21 +00:00
|
|
|
#define bt_syscall(test,format, ...) \
|
2015-03-03 12:51:35 +00:00
|
|
|
do { if (test) { bt_log(format ": %s", ##__VA_ARGS__, strerror(errno)); exit(3); } } while (0)
|
2015-03-17 10:27:40 +00:00
|
|
|
|
2015-04-15 09:51:24 +00:00
|
|
|
|
|
|
|
#define bt_strncat(buf, str, ...) \
|
2015-08-14 14:08:04 +00:00
|
|
|
snprintf(buf + strlen(buf), sizeof(buf), str, ##__VA_ARGS__)
|
2015-04-15 09:51:24 +00:00
|
|
|
|
|
|
|
void bt_strncat_(char *buf, size_t buf_size, const char *str, ...);
|
|
|
|
|
|
|
|
#define bt_dump_struct(buf, data) \
|
2015-04-13 08:52:21 +00:00
|
|
|
do \
|
|
|
|
{ \
|
2015-05-19 06:53:34 +00:00
|
|
|
uint k; \
|
2015-04-15 09:51:24 +00:00
|
|
|
u32 *pc = (u32*) data; \
|
|
|
|
bt_strncat(buf, "{"); \
|
2015-04-15 13:42:43 +00:00
|
|
|
for (k = 0; k < (sizeof(*data) / sizeof(typeof(*pc))); k++) \
|
2015-08-14 14:08:04 +00:00
|
|
|
{ \
|
2015-04-15 13:42:43 +00:00
|
|
|
bt_strncat(buf, "%s0x%08X", (k ? ", " : ""), pc[k]); \
|
2015-08-14 14:08:04 +00:00
|
|
|
} \
|
2015-04-15 09:51:24 +00:00
|
|
|
bt_strncat(buf, "}"); \
|
2015-04-13 08:52:21 +00:00
|
|
|
} while (0)
|
|
|
|
|
2015-04-15 09:51:24 +00:00
|
|
|
#define bt_dump(buf, data, fmt) \
|
2015-04-13 08:52:21 +00:00
|
|
|
do \
|
|
|
|
{ \
|
2015-04-15 09:51:24 +00:00
|
|
|
if (fmt == NULL) \
|
2015-08-14 14:08:04 +00:00
|
|
|
{ \
|
2015-04-15 09:51:24 +00:00
|
|
|
bt_dump_struct(buf, &data); \
|
2015-08-14 14:08:04 +00:00
|
|
|
} \
|
2015-04-15 09:51:24 +00:00
|
|
|
else \
|
2015-08-14 14:08:04 +00:00
|
|
|
{ \
|
2015-04-15 09:51:24 +00:00
|
|
|
bt_strncat_(buf, sizeof(buf), fmt, data); \
|
2015-08-14 14:08:04 +00:00
|
|
|
} \
|
2015-04-13 08:52:21 +00:00
|
|
|
} while (0)
|
|
|
|
|
2015-04-15 09:51:24 +00:00
|
|
|
#define bt_print_result_line(fn, in, out, fn_out, in_fmt, out_fmt, result) \
|
|
|
|
do \
|
|
|
|
{ \
|
2016-03-29 08:27:50 +00:00
|
|
|
char buf[BT_BUFFER_SIZE]; \
|
2015-04-15 09:51:24 +00:00
|
|
|
snprintf(buf, sizeof(buf), "%s(", #fn); \
|
|
|
|
bt_dump(buf, in, in_fmt); \
|
|
|
|
bt_strncat(buf, ") gives "); \
|
|
|
|
bt_dump(buf, fn_out, out_fmt); \
|
|
|
|
if (!result) \
|
|
|
|
{ \
|
|
|
|
bt_strncat(buf, BT_PROMPT_EXPECTING); \
|
|
|
|
bt_dump(buf, out, out_fmt); \
|
|
|
|
} \
|
|
|
|
bt_result_check((single_test_case_success ? BT_PROMPT_OK : BT_PROMPT_FAIL), "%s", buf); \
|
|
|
|
} while (0)
|
2015-04-13 08:52:21 +00:00
|
|
|
|
2015-04-15 09:51:24 +00:00
|
|
|
/**
|
|
|
|
* Usage:
|
|
|
|
* u32 my_function(const char *input_data) { ... }
|
|
|
|
*
|
|
|
|
* struct in_out {
|
|
|
|
* char *in;
|
|
|
|
* u32 out;
|
|
|
|
* } in_out[] = { ... };
|
|
|
|
*
|
|
|
|
* bt_assert_out_fn_in(my_function, in_out, "%s", "%u");
|
|
|
|
*/
|
|
|
|
#define bt_assert_out_fn_in(fn, in_out, in_fmt, out_fmt) \
|
2015-04-13 08:52:21 +00:00
|
|
|
do \
|
|
|
|
{ \
|
2015-05-19 06:53:34 +00:00
|
|
|
uint i; \
|
2015-04-15 09:51:24 +00:00
|
|
|
for (i = 0; i < (sizeof(in_out)/sizeof(in_out[0])); i++) \
|
|
|
|
{ \
|
|
|
|
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); \
|
2015-08-14 14:08:04 +00:00
|
|
|
if (!single_test_case_success) \
|
|
|
|
{ \
|
|
|
|
bt_test_suite_success = BT_FAILURE; \
|
|
|
|
} \
|
2015-04-15 09:51:24 +00:00
|
|
|
bt_print_result_line(fn, in_out[i].in, in_out[i].out, fn_out, in_fmt, out_fmt, single_test_case_success); \
|
|
|
|
} \
|
2015-04-13 08:52:21 +00:00
|
|
|
} while (0)
|
|
|
|
|
2015-04-15 09:51:24 +00:00
|
|
|
/**
|
|
|
|
* Usage:
|
|
|
|
* void my_function(const char *input_data, u32 *output_data) { ... }
|
|
|
|
*
|
|
|
|
* struct in_out {
|
|
|
|
* char *in;
|
|
|
|
* u32 out;
|
|
|
|
* } in_out[] = { ... };
|
|
|
|
*
|
|
|
|
* bt_assert_fn_in_out(my_function, in_out, "%s", "%u");
|
|
|
|
*/
|
|
|
|
#define bt_assert_fn_in_out(fn, in_out, in_fmt, out_fmt) \
|
2015-04-13 08:52:21 +00:00
|
|
|
do \
|
|
|
|
{ \
|
2015-05-19 06:53:34 +00:00
|
|
|
uint i; \
|
2015-04-13 08:52:21 +00:00
|
|
|
for (i = 0; i < (sizeof(in_out)/sizeof(in_out[0])); i++) \
|
|
|
|
{ \
|
2015-04-15 09:51:24 +00:00
|
|
|
typeof(in_out[i].out) fn_out; \
|
2015-11-02 12:28:15 +00:00
|
|
|
bzero(&fn_out, sizeof(fn_out)); \
|
2015-04-15 09:51:24 +00:00
|
|
|
fn(in_out[i].in, &fn_out); \
|
|
|
|
int single_test_case_success = !memcmp(&fn_out, &in_out[i].out, sizeof(in_out[i].out)); \
|
2015-08-14 14:08:04 +00:00
|
|
|
if (!single_test_case_success) \
|
|
|
|
{ \
|
|
|
|
bt_test_suite_success = BT_FAILURE; \
|
|
|
|
} \
|
2015-04-15 09:51:24 +00:00
|
|
|
bt_print_result_line(fn, in_out[i].in, in_out[i].out, fn_out, in_fmt, out_fmt, single_test_case_success); \
|
2015-04-13 08:52:21 +00:00
|
|
|
} \
|
|
|
|
} while (0)
|
2015-03-27 13:04:37 +00:00
|
|
|
|
2015-03-17 10:27:40 +00:00
|
|
|
#endif /* _BIRDTEST_H_ */
|