| 1 |
#include <pj/argparse.h> |
| 2 |
#include <pj/errno.h> |
| 3 |
#include <pj/list.h> |
| 4 |
#include <pj/log.h> |
| 5 |
#include <pj/os.h> |
| 6 |
#include <pj/string.h> |
| 7 |
#include <pj/unittest.h> |
| 8 |
|
| 9 |
#include <stdio.h> |
| 10 |
|
| 11 |
|
| 12 |
#ifndef UT_MAX_TESTS |
| 13 |
# define UT_MAX_TESTS 16 |
| 14 |
#endif |
| 15 |
|
| 16 |
|
| 17 |
#ifndef UT_LOG_BUF_SIZE |
| 18 |
# define UT_LOG_BUF_SIZE 1000 |
| 19 |
#endif |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
#ifndef THIS_FILE |
| 25 |
# define THIS_FILE "test.c" |
| 26 |
# define UNDEF_THIS_FILE 1 |
| 27 |
#endif |
| 28 |
|
| 29 |
|
| 30 |
typedef struct ut_app_t |
| 31 |
{ |
| 32 |
pj_bool_t prm_config; |
| 33 |
pj_test_select_tests prm_logging_policy; |
| 34 |
int prm_nthreads; |
| 35 |
int prm_list_test; |
| 36 |
pj_bool_t prm_stop_on_error; |
| 37 |
pj_bool_t prm_shuffle; |
| 38 |
pj_bool_t prm_ci_mode; |
| 39 |
int prm_seed; |
| 40 |
unsigned flags; |
| 41 |
unsigned verbosity; |
| 42 |
|
| 43 |
pj_pool_t *pool; |
| 44 |
pj_test_suite suite; |
| 45 |
pj_test_runner *runner; |
| 46 |
|
| 47 |
int ntests; |
| 48 |
pj_test_case test_cases[UT_MAX_TESTS]; |
| 49 |
} ut_app_t; |
| 50 |
|
| 51 |
|
| 52 |
PJ_INLINE(void) ut_app_init0(ut_app_t *ut_app) |
| 53 |
{ |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
setvbuf(stdout, NULL, _IONBF, 0); |
| 58 |
setvbuf(stderr, NULL, _IONBF, 0); |
| 59 |
|
| 60 |
pj_bzero(ut_app, sizeof(*ut_app)); |
| 61 |
ut_app->prm_logging_policy = PJ_TEST_FAILED_TESTS; |
| 62 |
ut_app->prm_nthreads = -1; |
| 63 |
ut_app->flags = 0; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
PJ_INLINE(pj_status_t) ut_app_init1(ut_app_t *ut_app, pj_pool_factory *mem_) |
| 68 |
{ |
| 69 |
ut_app->pool = pj_pool_create(mem_, THIS_FILE, 4000, 4000, NULL); |
| 70 |
PJ_TEST_NOT_NULL(ut_app->pool, NULL, return PJ_ENOMEM); |
| 71 |
pj_test_suite_init(&ut_app->suite); |
| 72 |
return PJ_SUCCESS; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
PJ_INLINE(void) ut_app_destroy(ut_app_t *ut_app) |
| 77 |
{ |
| 78 |
pj_pool_release(ut_app->pool); |
| 79 |
ut_app->pool = NULL; |
| 80 |
} |
| 81 |
|
| 82 |
typedef int (*ut_func)(void*); |
| 83 |
|
| 84 |
|
| 85 |
#define UT_ADD_TEST(ut_app, test_func, flags) \ |
| 86 |
ut_add_test(ut_app, (ut_func)test_func, 0, \ |
| 87 |
#test_func, flags | PJ_TEST_FUNC_NO_ARG, argc, argv) |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
#define UT_ADD_TEST1(ut_app, test_func, arg, flags) \ |
| 92 |
ut_add_test(ut_app, (ut_func)test_func, arg, #test_func, flags, argc, argv) |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
PJ_INLINE(pj_bool_t) ut_test_included(const char *name, int argc, char *argv[]) |
| 97 |
{ |
| 98 |
if (argc <= 1) |
| 99 |
return PJ_TRUE; |
| 100 |
|
| 101 |
++argv; |
| 102 |
while (*argv) { |
| 103 |
if (pj_ansi_strcmp(name, *argv)==0) |
| 104 |
return PJ_TRUE; |
| 105 |
++argv; |
| 106 |
} |
| 107 |
return PJ_FALSE; |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
PJ_INLINE(pj_status_t) ut_add_test(ut_app_t *ut_app, int (*test_func)(void*), |
| 112 |
void *arg, const char *test_name, |
| 113 |
unsigned flags, int argc, char *argv[]) |
| 114 |
{ |
| 115 |
char *log_buf; |
| 116 |
pj_test_case *tc; |
| 117 |
|
| 118 |
if (ut_app->ntests >= UT_MAX_TESTS) { |
| 119 |
PJ_LOG(1,(THIS_FILE, "Too many tests for adding %s", test_name)); |
| 120 |
return PJ_ETOOMANY; |
| 121 |
} |
| 122 |
|
| 123 |
if (!ut_test_included(test_name, argc, argv)) { |
| 124 |
return PJ_ENOTFOUND; |
| 125 |
} |
| 126 |
|
| 127 |
log_buf = (char*)pj_pool_alloc(ut_app->pool, UT_LOG_BUF_SIZE); |
| 128 |
tc = &ut_app->test_cases[ut_app->ntests]; |
| 129 |
flags |= ut_app->flags; |
| 130 |
pj_test_case_init(tc, test_name, flags, (int (*)(void*))test_func, arg, |
| 131 |
log_buf, UT_LOG_BUF_SIZE, NULL); |
| 132 |
|
| 133 |
pj_test_suite_add_case( &ut_app->suite, tc); |
| 134 |
++ut_app->ntests; |
| 135 |
|
| 136 |
return PJ_SUCCESS; |
| 137 |
} |
| 138 |
|
| 139 |
PJ_INLINE(void) ut_list_tests(ut_app_t *ut_app, const char *title) |
| 140 |
{ |
| 141 |
unsigned d = pj_log_get_decor(); |
| 142 |
const pj_test_case *tc, *prev=NULL; |
| 143 |
|
| 144 |
pj_log_set_decor(d ^ PJ_LOG_HAS_NEWLINE); |
| 145 |
PJ_LOG(3,(THIS_FILE, "%ld %s:", pj_list_size(&ut_app->suite.tests), |
| 146 |
title)); |
| 147 |
pj_log_set_decor(0); |
| 148 |
for (tc=ut_app->suite.tests.next; tc!=&ut_app->suite.tests; tc=tc->next) { |
| 149 |
if (!prev || pj_ansi_strcmp(tc->obj_name, prev->obj_name)) |
| 150 |
PJ_LOG(3,(THIS_FILE, " %s", tc->obj_name)); |
| 151 |
prev = tc; |
| 152 |
} |
| 153 |
PJ_LOG(3,(THIS_FILE, "\n")); |
| 154 |
pj_log_set_decor(d); |
| 155 |
} |
| 156 |
|
| 157 |
PJ_INLINE(pj_status_t) ut_run_tests(ut_app_t *ut_app, const char *title, |
| 158 |
int argc, char *argv[]) |
| 159 |
{ |
| 160 |
pj_test_runner *runner; |
| 161 |
pj_test_stat stat; |
| 162 |
pj_status_t status; |
| 163 |
pj_test_runner_param runner_prm; |
| 164 |
|
| 165 |
pj_test_runner_param_default(&runner_prm); |
| 166 |
|
| 167 |
if (ut_app->prm_shuffle) { |
| 168 |
PJ_LOG(3,(THIS_FILE, "Shuffling tests, random seed=%d", |
| 169 |
ut_app->prm_seed)); |
| 170 |
pj_test_suite_shuffle(&ut_app->suite, ut_app->prm_seed); |
| 171 |
} |
| 172 |
|
| 173 |
if (ut_app->prm_list_test) { |
| 174 |
ut_list_tests(ut_app, title); |
| 175 |
return PJ_SUCCESS; |
| 176 |
} |
| 177 |
|
| 178 |
if (argc > 1) { |
| 179 |
int i; |
| 180 |
pj_bool_t has_error = PJ_FALSE; |
| 181 |
for (i=1; i<argc; ++i) { |
| 182 |
pj_test_case *tc; |
| 183 |
for (tc=ut_app->suite.tests.next; tc!=&ut_app->suite.tests; |
| 184 |
tc=tc->next) |
| 185 |
{ |
| 186 |
if (pj_ansi_strcmp(argv[i], tc->obj_name)==0) |
| 187 |
break; |
| 188 |
} |
| 189 |
if (tc==&ut_app->suite.tests) { |
| 190 |
PJ_LOG(1,(THIS_FILE, "Test \"%s\" is not found in %s", |
| 191 |
argv[i], title)); |
| 192 |
has_error = PJ_TRUE; |
| 193 |
} |
| 194 |
} |
| 195 |
if (has_error) |
| 196 |
return PJ_ENOTFOUND; |
| 197 |
} |
| 198 |
|
| 199 |
if (ut_app->ntests <= 0) |
| 200 |
return PJ_SUCCESS; |
| 201 |
|
| 202 |
pj_test_runner_param_default(&runner_prm); |
| 203 |
runner_prm.stop_on_error = ut_app->prm_stop_on_error; |
| 204 |
if (ut_app->prm_nthreads >= 0) |
| 205 |
runner_prm.nthreads = ut_app->prm_nthreads; |
| 206 |
runner_prm.verbosity = ut_app->verbosity; |
| 207 |
status = pj_test_create_text_runner(ut_app->pool, &runner_prm, &runner); |
| 208 |
PJ_TEST_SUCCESS(status, "error creating text runner", return status); |
| 209 |
|
| 210 |
PJ_LOG(3,(THIS_FILE, |
| 211 |
"Performing %d %s with %d worker thread%s", |
| 212 |
ut_app->ntests, title, runner_prm.nthreads, |
| 213 |
runner_prm.nthreads>1?"s":"")); |
| 214 |
|
| 215 |
pj_test_run(runner, &ut_app->suite); |
| 216 |
pj_test_runner_destroy(runner); |
| 217 |
pj_test_display_log_messages(&ut_app->suite, ut_app->prm_logging_policy); |
| 218 |
pj_test_get_stat(&ut_app->suite, &stat); |
| 219 |
pj_test_display_stat(&stat, title, THIS_FILE); |
| 220 |
|
| 221 |
return stat.nfailed ? PJ_EBUG : PJ_SUCCESS; |
| 222 |
} |
| 223 |
|
| 224 |
PJ_INLINE(void) ut_usage() |
| 225 |
{ |
| 226 |
puts(" -c, --config Show configuration macros"); |
| 227 |
puts(" -l 0,1,2,3 0: Don't show logging after tests"); |
| 228 |
puts(" 1: Show logs of only failed tests (default)"); |
| 229 |
puts(" 2: Show logs of only successful tests"); |
| 230 |
puts(" 3: Show logs of all tests"); |
| 231 |
puts(" --log-no-cache Do not cache logging"); |
| 232 |
printf(" -w N Set N worker threads (0: disable. Default: %d)\n", |
| 233 |
PJ_HAS_THREADS); |
| 234 |
puts(" -L, --list List the tests and exit"); |
| 235 |
puts(" --stop-err Stop testing on error"); |
| 236 |
puts(" --shuffle Shuffle the test order"); |
| 237 |
puts(" --ci-mode Running in slow CI mode"); |
| 238 |
puts(" --seed N Set shuffle random seed (must be >= 0)"); |
| 239 |
puts(" --stdout-buf N Set stdout buffering mode:"); |
| 240 |
puts(" --stderr-buf N Set stderr buffering mode:"); |
| 241 |
puts(" 0: unbufferred (default for stderr)"); |
| 242 |
puts(" 1: line"); |
| 243 |
puts(" 2: fully bufferred (default for stdout)"); |
| 244 |
puts(" -v, --verbose Show info when starting/stopping tests"); |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
PJ_INLINE(pj_status_t) ut_parse_args(ut_app_t *ut_app, int *argc, char *argv[]) |
| 249 |
{ |
| 250 |
int itmp = -1; |
| 251 |
pj_status_t status; |
| 252 |
|
| 253 |
ut_app->prm_config = pj_argparse_get_bool(argc, argv, "-c") || |
| 254 |
pj_argparse_get_bool(argc, argv, "--config"); |
| 255 |
ut_app->prm_list_test = pj_argparse_get_bool(argc, argv, "-L") || |
| 256 |
pj_argparse_get_bool(argc, argv, "--list"); |
| 257 |
ut_app->prm_stop_on_error = pj_argparse_get_bool(argc, argv, "--stop-err"); |
| 258 |
ut_app->prm_shuffle = pj_argparse_get_bool(argc, argv, "--shuffle"); |
| 259 |
ut_app->prm_ci_mode = pj_argparse_get_bool(argc, argv, "--ci-mode"); |
| 260 |
if (pj_argparse_get_bool(argc, argv, "--log-no-cache")) { |
| 261 |
ut_app->flags |= PJ_TEST_LOG_NO_CACHE; |
| 262 |
} |
| 263 |
|
| 264 |
if (pj_argparse_exists(argv, "-l")) { |
| 265 |
status = pj_argparse_get_int(argc, argv, "-l", &itmp); |
| 266 |
if (status==PJ_SUCCESS && itmp>=0 && itmp<=3) { |
| 267 |
ut_app->prm_logging_policy = (pj_test_select_tests)itmp; |
| 268 |
} else { |
| 269 |
puts("Error: invalid value for -l option"); |
| 270 |
return PJ_EINVAL; |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
if (pj_argparse_exists(argv, "-w")) { |
| 275 |
status = pj_argparse_get_int(argc, argv, "-w", &itmp); |
| 276 |
if (status==PJ_SUCCESS && itmp>=0 && itmp<50) { |
| 277 |
ut_app->prm_nthreads = itmp; |
| 278 |
} else { |
| 279 |
puts("Error: invalid/missing value for -w option"); |
| 280 |
return PJ_EINVAL; |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
if (ut_app->prm_shuffle) { |
| 285 |
pj_time_val tv; |
| 286 |
|
| 287 |
status = pj_gettimeofday(&tv); |
| 288 |
if (status != PJ_SUCCESS) |
| 289 |
return status; |
| 290 |
|
| 291 |
ut_app->prm_seed = (int)(tv.msec); |
| 292 |
status = pj_argparse_get_int(argc, argv, "--seed", &ut_app->prm_seed); |
| 293 |
if (status != PJ_SUCCESS) |
| 294 |
return status; |
| 295 |
} |
| 296 |
|
| 297 |
ut_app->verbosity = pj_argparse_get_bool(argc, argv, "-v") || |
| 298 |
pj_argparse_get_bool(argc, argv, "--verbose"); |
| 299 |
|
| 300 |
itmp = -101; |
| 301 |
if (pj_argparse_get_int(argc, argv, "--stdout-buf", &itmp)==PJ_SUCCESS && |
| 302 |
itmp != -101) |
| 303 |
{ |
| 304 |
switch (itmp) { |
| 305 |
case 0: setvbuf(stdout, NULL, _IONBF, 0); break; |
| 306 |
case 1: setvbuf(stdout, NULL, _IOLBF, 0); break; |
| 307 |
case 2: setvbuf(stdout, NULL, _IOFBF, 0); break; |
| 308 |
default: |
| 309 |
printf("Error: invalid --stdout-buf value %d\n", itmp); |
| 310 |
return PJ_EINVAL; |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
itmp = -101; |
| 315 |
if (pj_argparse_get_int(argc, argv, "--stderr-buf", &itmp)==PJ_SUCCESS && |
| 316 |
itmp != -101) |
| 317 |
{ |
| 318 |
switch (itmp) { |
| 319 |
case 0: setvbuf(stderr, NULL, _IONBF, 0); break; |
| 320 |
case 1: setvbuf(stderr, NULL, _IOLBF, 0); break; |
| 321 |
case 2: setvbuf(stderr, NULL, _IOFBF, 0); break; |
| 322 |
default: |
| 323 |
printf("Error: invalid --stderr-buf value %d\n", itmp); |
| 324 |
return PJ_EINVAL; |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
return PJ_SUCCESS; |
| 329 |
} |
| 330 |
|
| 331 |
#ifdef UNDEF_THIS_FILE |
| 332 |
# undef THIS_FILE |
| 333 |
#endif |