| 1 | /* |
| 2 | * Copyright (C) 2008-2024 Teluu Inc. (http://www.teluu.com) |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | */ |
| 18 | #ifndef __PJ_UNITTEST_H__ |
| 19 | #define __PJ_UNITTEST_H__ |
| 20 | |
| 21 | /** |
| 22 | * @file testing.h |
| 23 | * @brief PJLIB unit testing framework |
| 24 | */ |
| 25 | /** |
| 26 | * @defgroup PJ_UNITTEST Unit testing framework |
| 27 | * @ingroup PJ_MISC |
| 28 | * @{ |
| 29 | */ |
| 30 | #include <pj/fifobuf.h> |
| 31 | #include <pj/list.h> |
| 32 | #include <pj/log.h> |
| 33 | #include <pj/pool.h> |
| 34 | |
| 35 | PJ_BEGIN_DECL |
| 36 | |
| 37 | /* |
| 38 | * These various PJ_TEST_XXX macros can be used in any programs without |
| 39 | * having to use the unit-test framework. |
| 40 | */ |
| 41 | |
| 42 | /** |
| 43 | * Check that an expression is non-zero. If the check fails, informative error |
| 44 | * message will be displayed, and the code in err_action will be executed. |
| 45 | * |
| 46 | * @param expr The expression to check |
| 47 | * @param err_reason NULL or extra text to display when the check fails |
| 48 | * @param err_action Action to perform when the check fails |
| 49 | */ |
| 50 | #define PJ_TEST_NON_ZERO(expr, err_reason, err_action) \ |
| 51 | { \ |
| 52 | if ((expr)==0) { \ |
| 53 | const char *tmp_reason_ = err_reason; \ |
| 54 | const char *sep0_ = (tmp_reason_ ? " (": ""); \ |
| 55 | const char *sep1_ = (tmp_reason_ ? ")": ""); \ |
| 56 | if (!tmp_reason_) tmp_reason_=""; \ |
| 57 | PJ_LOG(1,(THIS_FILE, "Test \"%s\" != 0 fails in " \ |
| 58 | "%s:%d%s%s%s", \ |
| 59 | #expr, THIS_FILE,__LINE__,sep0_, \ |
| 60 | tmp_reason_,sep1_));\ |
| 61 | err_action; \ |
| 62 | } \ |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Generic check for binary operation. If the check fails, informative error |
| 67 | * message will be displayed, and the code in err_action will be executed. |
| 68 | * |
| 69 | * @param expr0 First expression |
| 70 | * @param op The operator |
| 71 | * @param expr1 Second expression |
| 72 | * @param err_reason NULL or extra text to display when the check fails |
| 73 | * @param err_action Action to perform when the check fails |
| 74 | */ |
| 75 | #define PJ_TEST_BINARY_OP(expr0, op, expr1, err_reason, err_action) \ |
| 76 | { \ |
| 77 | intptr_t tmp_value0_ = (intptr_t)(expr0); \ |
| 78 | intptr_t tmp_value1_ = (intptr_t)(expr1); \ |
| 79 | if (!(tmp_value0_ op tmp_value1_)) { \ |
| 80 | const char *tmp_reason_ = err_reason; \ |
| 81 | const char *sep0_ = (tmp_reason_ ? " (": ""); \ |
| 82 | const char *sep1_ = (tmp_reason_ ? ")": ""); \ |
| 83 | if (!tmp_reason_) tmp_reason_=""; \ |
| 84 | PJ_LOG(1,(THIS_FILE, "Test \"%s\" (value=%ld) " #op \ |
| 85 | " \"%s\" (value=%ld) fails in %s:%d%s%s%s", \ |
| 86 | #expr0, (long)tmp_value0_, \ |
| 87 | #expr1, (long)tmp_value1_, \ |
| 88 | THIS_FILE, __LINE__, \ |
| 89 | sep0_, tmp_reason_, sep1_)); \ |
| 90 | err_action; \ |
| 91 | } \ |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Generic check for (PJ) string comparison operation. If the check fails, |
| 96 | * informative error message will be displayed, and the code in err_action |
| 97 | * will be executed. |
| 98 | * |
| 99 | * @param str_op The string operation (e.g. pj_strcmp) |
| 100 | * @param ps0 Pointer to first string |
| 101 | * @param ps1 Pointer to second string |
| 102 | * @param res_op Operator to compare result (e.g. ==) |
| 103 | * @param res Expected return value of str_op(&s0, &s1) |
| 104 | * @param err_reason NULL or extra text to display when the check fails |
| 105 | * @param err_action Action to perform when the check fails |
| 106 | */ |
| 107 | #define PJ_TEST_STR_OP(str_op, ps0, ps1, res_op, res, err_reason, err_action) \ |
| 108 | { \ |
| 109 | int result__ = str_op(ps0, ps1); \ |
| 110 | if (!(result__ res_op res)) { \ |
| 111 | const char *fn_name = #str_op; \ |
| 112 | const char *tmp_reason_ = err_reason; \ |
| 113 | const char *sep0_ = (tmp_reason_ ? " (": ""); \ |
| 114 | const char *sep1_ = (tmp_reason_ ? ")": ""); \ |
| 115 | if (!tmp_reason_) tmp_reason_=""; \ |
| 116 | PJ_LOG(1,(THIS_FILE, "Test %s(\"%.*s\", \"%.*s\")%s%d" \ |
| 117 | " fails (%s result=%d) in %s:%d%s%s%s", \ |
| 118 | fn_name, (int)ps0->slen, ps0->ptr, \ |
| 119 | (int)ps1->slen, ps1->ptr, #res_op, res, \ |
| 120 | fn_name, result__, \ |
| 121 | THIS_FILE, __LINE__, \ |
| 122 | sep0_, tmp_reason_, sep1_)); \ |
| 123 | err_action; \ |
| 124 | } \ |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Check that an expression is PJ_SUCCESS. If the check fails, error message |
| 129 | * explaining the error code will be displayed, and the code in err_action |
| 130 | * will be executed. |
| 131 | * |
| 132 | * @param expr The expression to check |
| 133 | * @param err_reason NULL or extra text to display when the check fails |
| 134 | * @param err_action Action to perform when the check fails |
| 135 | */ |
| 136 | #define PJ_TEST_SUCCESS(expr, err_reason, err_action) \ |
| 137 | { \ |
| 138 | pj_status_t tmp_status_ = (expr); \ |
| 139 | if (tmp_status_ != PJ_SUCCESS) { \ |
| 140 | char errbuf[80]; \ |
| 141 | const char *tmp_reason_ = err_reason; \ |
| 142 | const char *sep0_ = (tmp_reason_ ? " (": ""); \ |
| 143 | const char *sep1_ = (tmp_reason_ ? ")": ""); \ |
| 144 | if (!tmp_reason_) tmp_reason_=""; \ |
| 145 | pj_strerror(tmp_status_, errbuf, sizeof(errbuf)); \ |
| 146 | PJ_LOG(1,(THIS_FILE, "\"%s\" fails in %s:%d, " \ |
| 147 | "status=%d (%s)%s%s%s", \ |
| 148 | #expr, THIS_FILE, __LINE__, tmp_status_,errbuf,\ |
| 149 | sep0_, tmp_reason_, sep1_)); \ |
| 150 | err_action; \ |
| 151 | } \ |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Alias for PJ_TEST_NON_ZERO() |
| 156 | */ |
| 157 | #define PJ_TEST_TRUE(expr, err_reason, err_action) \ |
| 158 | PJ_TEST_NON_ZERO(expr, err_reason, err_action) |
| 159 | |
| 160 | /** |
| 161 | * Alias for PJ_TEST_NON_ZERO() |
| 162 | */ |
| 163 | #define PJ_TEST_NOT_NULL(expr, err_reason, err_action) \ |
| 164 | PJ_TEST_NON_ZERO(expr, err_reason, err_action) |
| 165 | |
| 166 | /** |
| 167 | * Check that expr0 equals expr1. |
| 168 | * If the check fails, informative error message will be displayed and |
| 169 | * the code in err_action will be executed. |
| 170 | * |
| 171 | * @param expr0 First expression |
| 172 | * @param expr1 Second expression |
| 173 | * @param err_reason NULL or extra text to display when the check fails |
| 174 | * @param err_action Action to perform when the check fails |
| 175 | */ |
| 176 | #define PJ_TEST_EQ(expr0, expr1, err_reason, err_action) \ |
| 177 | PJ_TEST_BINARY_OP(expr0, ==, expr1, err_reason, err_action) |
| 178 | |
| 179 | /** |
| 180 | * Check that expr0 does not equal expr1. |
| 181 | * If the check fails, informative error message will be displayed and |
| 182 | * the code in err_action will be executed. |
| 183 | * |
| 184 | * @param expr0 First expression |
| 185 | * @param expr1 Second expression |
| 186 | * @param err_reason NULL or extra text to display when the check fails |
| 187 | * @param err_action Action to perform when the check fails |
| 188 | */ |
| 189 | #define PJ_TEST_NEQ(expr0, expr1, err_reason, err_action) \ |
| 190 | PJ_TEST_BINARY_OP(expr0, !=, expr1, err_reason, err_action) |
| 191 | |
| 192 | /** |
| 193 | * Check that expr0 is less than expr1. |
| 194 | * If the check fails, informative error message will be displayed and |
| 195 | * the code in err_action will be executed. |
| 196 | * |
| 197 | * @param expr0 First expression |
| 198 | * @param expr1 Second expression |
| 199 | * @param err_reason NULL or extra text to display when the check fails |
| 200 | * @param err_action Action to perform when the check fails |
| 201 | */ |
| 202 | #define PJ_TEST_LT(expr0, expr1, err_reason, err_action) \ |
| 203 | PJ_TEST_BINARY_OP(expr0, <, expr1, err_reason, err_action) |
| 204 | |
| 205 | /** |
| 206 | * Check that expr0 is less than or equal to expr1. |
| 207 | * If the check fails, informative error message will be displayed and |
| 208 | * the code in err_action will be executed. |
| 209 | * |
| 210 | * @param expr0 First expression |
| 211 | * @param expr1 Second expression |
| 212 | * @param err_reason NULL or extra text to display when the check fails |
| 213 | * @param err_action Action to perform when the check fails |
| 214 | */ |
| 215 | #define PJ_TEST_LTE(expr0, expr1, err_reason, err_action) \ |
| 216 | PJ_TEST_BINARY_OP(expr0, <=, expr1, err_reason, err_action) |
| 217 | |
| 218 | /** |
| 219 | * Check that expr0 is greater than expr1. |
| 220 | * If the check fails, informative error message will be displayed and |
| 221 | * the code in err_action will be executed. |
| 222 | * |
| 223 | * @param expr0 First expression |
| 224 | * @param expr1 Second expression |
| 225 | * @param err_reason NULL or extra text to display when the check fails |
| 226 | * @param err_action Action to perform when the check fails |
| 227 | */ |
| 228 | #define PJ_TEST_GT(expr0, expr1, err_reason, err_action) \ |
| 229 | PJ_TEST_BINARY_OP(expr0, >, expr1, err_reason, err_action) |
| 230 | |
| 231 | /** |
| 232 | * Check that expr0 is greater than or equal to expr1. |
| 233 | * If the check fails, informative error message will be displayed and |
| 234 | * the code in err_action will be executed. |
| 235 | * |
| 236 | * @param expr0 First expression |
| 237 | * @param expr1 Second expression |
| 238 | * @param err_reason NULL or extra text to display when the check fails |
| 239 | * @param err_action Action to perform when the check fails |
| 240 | */ |
| 241 | #define PJ_TEST_GTE(expr0, expr1, err_reason, err_action) \ |
| 242 | PJ_TEST_BINARY_OP(expr0, >=, expr1, err_reason, err_action) |
| 243 | |
| 244 | |
| 245 | /** |
| 246 | * Check string comparison result. |
| 247 | * If the check fails, informative error message will be displayed and |
| 248 | * the code in err_action will be executed. |
| 249 | * |
| 250 | * @param ps0 Pointer to first string |
| 251 | * @param ps1 Pointer to second string |
| 252 | * @param res_op Operator to compare result (e.g. ==, <, >) |
| 253 | * @param exp_result Expected result (e.g. zero for equal string) |
| 254 | * @param err_reason NULL or extra text to display when the check fails |
| 255 | * @param err_action Action to perform when the check fails |
| 256 | */ |
| 257 | #define PJ_TEST_STRCMP(ps0, ps1, res_op, exp_result, err_reason, err_action) \ |
| 258 | PJ_TEST_STR_OP(pj_strcmp, ps0, ps1, res_op, exp_result, \ |
| 259 | err_reason, err_action) |
| 260 | |
| 261 | /** |
| 262 | * Check case-insensitive string comparison result. |
| 263 | * If the check fails, informative error message will be displayed and |
| 264 | * the code in err_action will be executed. |
| 265 | * |
| 266 | * @param ps0 Pointer to first string |
| 267 | * @param ps1 Pointer to second string |
| 268 | * @param res_op Operator to compare result (e.g. ==, <, >) |
| 269 | * @param exp_result Expected result (e.g. zero for equal) |
| 270 | * @param err_reason NULL or extra text to display when the check fails |
| 271 | * @param err_action Action to perform when the check fails |
| 272 | */ |
| 273 | #define PJ_TEST_STRICMP(ps0, ps1, res_op, exp_result, err_reason, err_action) \ |
| 274 | PJ_TEST_STR_OP(pj_stricmp, ps0, ps1, res_op, exp_result, \ |
| 275 | err_reason, err_action) |
| 276 | |
| 277 | /** |
| 278 | * Check that two strings are equal. |
| 279 | * If the check fails, informative error message will be displayed and |
| 280 | * the code in err_action will be executed. |
| 281 | * |
| 282 | * @param ps0 Pointer to first string |
| 283 | * @param ps1 Pointer to second string |
| 284 | * @param err_reason NULL or extra text to display when the check fails |
| 285 | * @param err_action Action to perform when the check fails |
| 286 | */ |
| 287 | #define PJ_TEST_STREQ(ps0, ps1, err_reason, err_action) \ |
| 288 | PJ_TEST_STRCMP(ps0, ps1, ==, 0, err_reason, err_action) |
| 289 | |
| 290 | /** |
| 291 | * Check that two strings are not equal. |
| 292 | * If the check fails, informative error message will be displayed and |
| 293 | * the code in err_action will be executed. |
| 294 | * |
| 295 | * @param ps0 Pointer to first string |
| 296 | * @param ps1 Pointer to second string |
| 297 | * @param err_reason NULL or extra text to display when the check fails |
| 298 | * @param err_action Action to perform when the check fails |
| 299 | */ |
| 300 | #define PJ_TEST_STRNEQ(ps0, ps1, err_reason, err_action) \ |
| 301 | PJ_TEST_STRCMP(ps0, ps1, !=, 0, err_reason, err_action) |
| 302 | |
| 303 | |
| 304 | /** |
| 305 | * Bitwise constants that can be used in test case flags (see |
| 306 | * pj_test_case_init()). |
| 307 | */ |
| 308 | typedef enum pj_test_case_flag |
| 309 | { |
| 310 | /** |
| 311 | * Do not allow other test cases to run while this test case is running. |
| 312 | * Note this only makes sense for test runners that support worker |
| 313 | * threads. Basic runner will always run test cases serially. |
| 314 | */ |
| 315 | PJ_TEST_EXCLUSIVE = 1, |
| 316 | |
| 317 | /** |
| 318 | * Specify that the test function must be called without argument. |
| 319 | * This is mainly for backward compatibility with existing PJ test |
| 320 | * functions which take no argument. |
| 321 | */ |
| 322 | PJ_TEST_FUNC_NO_ARG = 2, |
| 323 | |
| 324 | /** |
| 325 | * Write the original log at the time it is called instead of pooling |
| 326 | * the logs to be printed after all tests finish. |
| 327 | */ |
| 328 | PJ_TEST_LOG_NO_CACHE = 4, |
| 329 | |
| 330 | /** |
| 331 | * Keep this test case in front of the list when shuffling the test |
| 332 | * cases. |
| 333 | */ |
| 334 | PJ_TEST_KEEP_FIRST = 8, |
| 335 | |
| 336 | /** |
| 337 | * Keep this test case in last in the list when shuffling the test |
| 338 | * cases. |
| 339 | */ |
| 340 | PJ_TEST_KEEP_LAST = 16, |
| 341 | |
| 342 | } pj_test_case_flag; |
| 343 | |
| 344 | |
| 345 | /** |
| 346 | * An internal structure to represent one logging item that is saved |
| 347 | * inside pj_test_case. |
| 348 | */ |
| 349 | typedef struct pj_test_log_item |
| 350 | { |
| 351 | PJ_DECL_LIST_MEMBER(struct pj_test_log_item); |
| 352 | |
| 353 | /** level */ |
| 354 | int level; |
| 355 | |
| 356 | /** len */ |
| 357 | int len; |
| 358 | |
| 359 | /** The log message. The actual buffer is longer. */ |
| 360 | char msg[1]; |
| 361 | |
| 362 | } pj_test_log_item; |
| 363 | |
| 364 | |
| 365 | /** Forward declaration of test runner */ |
| 366 | typedef struct pj_test_runner pj_test_runner; |
| 367 | |
| 368 | |
| 369 | /** |
| 370 | * Additional parameters for creating test case. Use |
| 371 | * pj_test_case_param_default() to initialize this structure. |
| 372 | */ |
| 373 | typedef struct pj_test_case_param |
| 374 | { |
| 375 | /** |
| 376 | * Custom log level for this test case, to filter out logs that are more |
| 377 | * detail than this level. Default is 6, meaning it will accept all log |
| 378 | * levels. |
| 379 | */ |
| 380 | int log_level; |
| 381 | |
| 382 | } pj_test_case_param; |
| 383 | |
| 384 | |
| 385 | /** |
| 386 | * A test case is unit-test object to perform test against a user defined |
| 387 | * function. |
| 388 | */ |
| 389 | typedef struct pj_test_case |
| 390 | { |
| 391 | PJ_DECL_LIST_MEMBER(struct pj_test_case); |
| 392 | |
| 393 | /** Test name */ |
| 394 | char obj_name[PJ_MAX_OBJ_NAME]; |
| 395 | |
| 396 | /** |
| 397 | * The test function to be called to perform the test. By convention, the |
| 398 | * function must return zero for the test to be considered successful, |
| 399 | * non-zero on failure, and MUST NEVER return PJ_EPENDING, otherwise the |
| 400 | * return value will be silently changed to -12345. |
| 401 | */ |
| 402 | int (*test_func)(void*); |
| 403 | |
| 404 | /** Argument to be passed to the test function */ |
| 405 | void *arg; |
| 406 | |
| 407 | /** Flags, combination of pj_test_flag constants */ |
| 408 | unsigned flags; |
| 409 | |
| 410 | /** Circular buffer for logging */ |
| 411 | pj_fifobuf_t fb; |
| 412 | |
| 413 | /** Parameters */ |
| 414 | pj_test_case_param prm; |
| 415 | |
| 416 | /** |
| 417 | * The return value of the test function. Zero indicates success. Initially |
| 418 | * the value is PJ_EPENDING before the test is run. |
| 419 | */ |
| 420 | int result; |
| 421 | |
| 422 | /** List of saved logging messages */ |
| 423 | pj_test_log_item logs; |
| 424 | |
| 425 | /** Pointer to the runner running this test case */ |
| 426 | pj_test_runner *runner; |
| 427 | |
| 428 | /** Start time */ |
| 429 | pj_timestamp start_time; |
| 430 | |
| 431 | /** End time */ |
| 432 | pj_timestamp end_time; |
| 433 | |
| 434 | } pj_test_case; |
| 435 | |
| 436 | |
| 437 | /** |
| 438 | * Test suite is a collection of test cases. |
| 439 | */ |
| 440 | typedef struct pj_test_suite |
| 441 | { |
| 442 | /** List of tests */ |
| 443 | pj_test_case tests; |
| 444 | |
| 445 | /** Start time */ |
| 446 | pj_timestamp start_time; |
| 447 | |
| 448 | /** End time */ |
| 449 | pj_timestamp end_time; |
| 450 | |
| 451 | } pj_test_suite; |
| 452 | |
| 453 | |
| 454 | /** |
| 455 | * Test statistics. Collect the statistics after the test runner finishes |
| 456 | * with pj_test_get_stat(). |
| 457 | */ |
| 458 | typedef struct pj_test_stat |
| 459 | { |
| 460 | /** Total duration */ |
| 461 | pj_time_val duration; |
| 462 | |
| 463 | /** Total number of tests in the test suite */ |
| 464 | unsigned ntests; |
| 465 | |
| 466 | /** Number of tests run */ |
| 467 | unsigned nruns; |
| 468 | |
| 469 | /** Number of failed tests */ |
| 470 | unsigned nfailed; |
| 471 | |
| 472 | /** |
| 473 | * Array of failed test names. Be careful that the number elements are |
| 474 | * fixed, hence it may not be able to store all failed test names (in case |
| 475 | * nfailed is more than the capacity, not all failed test names will be |
| 476 | * stored, hence be careful in the loop). |
| 477 | */ |
| 478 | const char *failed_names[32]; |
| 479 | |
| 480 | } pj_test_stat; |
| 481 | |
| 482 | |
| 483 | /** |
| 484 | * Test runner parameters. Use pj_test_runner_param_default() to initialize |
| 485 | * this structure. |
| 486 | */ |
| 487 | typedef struct pj_test_runner_param |
| 488 | { |
| 489 | /** Stop the test on error (default: false) */ |
| 490 | pj_bool_t stop_on_error; |
| 491 | |
| 492 | /** Number of worker threads. Set to zero to disable parallel testings. |
| 493 | * Only applicable to test text runner. Default is 1 if multithreading |
| 494 | * is available. |
| 495 | */ |
| 496 | unsigned nthreads; |
| 497 | |
| 498 | /** |
| 499 | * 0: only display test name and result after test completion (default) |
| 500 | * 1: display test name test when starting and finishing a test |
| 501 | */ |
| 502 | unsigned verbosity; |
| 503 | |
| 504 | } pj_test_runner_param; |
| 505 | |
| 506 | |
| 507 | /** |
| 508 | * This structure represents a test runner. Currently there are two types |
| 509 | * of test runners, the basic runner and text runner. The basic runner is the |
| 510 | * simplest test runner that can be used without pool and threads, and can be |
| 511 | * created with pj_test_init_basic_runner(). The text runner is more powerful |
| 512 | * since it supports worker threads, and it is mostly suitable for console |
| 513 | * based environments. It is created with pj_test_create_text_runner(). |
| 514 | */ |
| 515 | struct pj_test_runner |
| 516 | { |
| 517 | /** Parameters */ |
| 518 | pj_test_runner_param prm; |
| 519 | |
| 520 | /** The test suite being run */ |
| 521 | pj_test_suite *suite; |
| 522 | |
| 523 | /** Saving the original log writer */ |
| 524 | pj_log_func *orig_log_writer; |
| 525 | |
| 526 | /** Number of tests */ |
| 527 | unsigned ntests; |
| 528 | |
| 529 | /** Number of completed tests */ |
| 530 | unsigned nruns; |
| 531 | |
| 532 | /** Stopping */ |
| 533 | pj_bool_t stopping; |
| 534 | |
| 535 | /** main method */ |
| 536 | void (*main)(pj_test_runner*); |
| 537 | |
| 538 | /** callback when test case completes. Default is to write to log */ |
| 539 | void (*on_test_complete)(pj_test_runner*, pj_test_case*); |
| 540 | |
| 541 | /** destroy method */ |
| 542 | void (*destroy)(pj_test_runner*); |
| 543 | }; |
| 544 | |
| 545 | /** Option to select tests (e.g. in pj_test_dump_log_messages()) */ |
| 546 | typedef enum pj_test_select_tests |
| 547 | { |
| 548 | /** Select no test*/ |
| 549 | PJ_TEST_NO_TEST = 0, |
| 550 | |
| 551 | /** Select only failed tests */ |
| 552 | PJ_TEST_FAILED_TESTS = 1, |
| 553 | |
| 554 | /** Select only successful tests */ |
| 555 | PJ_TEST_SUCCESSFUL_TESTS = 2, |
| 556 | |
| 557 | /** Select all tests*/ |
| 558 | PJ_TEST_ALL_TESTS = 3, |
| 559 | |
| 560 | /** No header/footer separator */ |
| 561 | PJ_TEST_NO_HEADER_FOOTER = 4, |
| 562 | |
| 563 | } pj_test_select_tests; |
| 564 | |
| 565 | |
| 566 | /** |
| 567 | * Initialize test suite. |
| 568 | * |
| 569 | * @param suite The test suite |
| 570 | */ |
| 571 | PJ_DECL(void) pj_test_suite_init(pj_test_suite *suite); |
| 572 | |
| 573 | /** |
| 574 | * Initialize pj_test_case_param with default values. If app only uses |
| 575 | * default values in params, alternatively it doesn't need to use param |
| 576 | * at all and just specify NULL in pj_test_case_init(). |
| 577 | * |
| 578 | * @param prm The parameter. |
| 579 | */ |
| 580 | PJ_DECL(void) pj_test_case_param_default(pj_test_case_param *prm); |
| 581 | |
| 582 | /** |
| 583 | * Initialize test case. |
| 584 | * |
| 585 | * @param tc The test case |
| 586 | * @param obj_name Name that will appear as test name/title |
| 587 | * @param flags Bitwise of pj_test_case_flag to control threading, |
| 588 | * function calling, logging, etc. |
| 589 | * @param test_func The test function to be called to perform the test. |
| 590 | * By convention, the function must return zero for the |
| 591 | * test to be considered successful, non-zero on failure, |
| 592 | * and MUST NEVER return PJ_EPENDING, otherwise the |
| 593 | * return value will be silently changed to -12345. |
| 594 | * @param arg Argument to give to the test function |
| 595 | * @param fifobuf_buf Buffer for saving the logs, if required. |
| 596 | * @param buf_size Size of the buffer for saving the logs. |
| 597 | * @param prm Optional additional settings for the test case or NULL |
| 598 | */ |
| 599 | PJ_DECL(void) pj_test_case_init(pj_test_case *tc, |
| 600 | const char *obj_name, |
| 601 | unsigned flags, |
| 602 | int (*test_func)(void*), |
| 603 | void *arg, |
| 604 | void *fifobuf_buf, |
| 605 | unsigned buf_size, |
| 606 | const pj_test_case_param *prm); |
| 607 | |
| 608 | /** |
| 609 | * Add test case to test suite. A test case can only be added to one suite. |
| 610 | * |
| 611 | * @param suite The test suite |
| 612 | * @param tc The test case |
| 613 | */ |
| 614 | PJ_DECL(void) pj_test_suite_add_case(pj_test_suite *suite, pj_test_case *tc); |
| 615 | |
| 616 | /** |
| 617 | * Shuffle the tests. |
| 618 | * |
| 619 | * @param suite The test suite |
| 620 | * @param seed Optional random seed to use, only if the value is |
| 621 | * greater than or equal to zero. It is recommended |
| 622 | * to set this value to make the test reproducible. |
| 623 | */ |
| 624 | PJ_DECL(void) pj_test_suite_shuffle(pj_test_suite *suite, int seed); |
| 625 | |
| 626 | /** |
| 627 | * Initialize parameters with reasonable default values. This usually means |
| 628 | * using one worker thread if threading is enabled, and zero worker thread |
| 629 | * (i.e. only use the main thread) otherwise. |
| 630 | * |
| 631 | * @param prm Test runner parameter |
| 632 | */ |
| 633 | PJ_DECL(void) pj_test_runner_param_default(pj_test_runner_param *prm); |
| 634 | |
| 635 | /** |
| 636 | * Initialize a basic test runner. A basic runner can be declared in the stack |
| 637 | * and it does not require pool nor multithreading. |
| 638 | * |
| 639 | * @param runner The runner. |
| 640 | * @param prm Runner params, or NULL to accept default values. |
| 641 | */ |
| 642 | PJ_DECL(void) pj_test_init_basic_runner(pj_test_runner *runner, |
| 643 | const pj_test_runner_param *prm); |
| 644 | |
| 645 | /** |
| 646 | * Create console based test runner. |
| 647 | * |
| 648 | * @param pool The pool to use to allocate memory |
| 649 | * @param prm Test runner parameter, or NULL for default values. |
| 650 | * @param p_runner Pointer to receive the text runner |
| 651 | * |
| 652 | * @return PJ_SUCCESS or the appropriate error code. |
| 653 | */ |
| 654 | PJ_DECL(pj_status_t) pj_test_create_text_runner( |
| 655 | pj_pool_t *pool, |
| 656 | const pj_test_runner_param *prm, |
| 657 | pj_test_runner **p_runner); |
| 658 | |
| 659 | /** |
| 660 | * Run test suite with the specified runner. |
| 661 | * |
| 662 | * @param runner The test runner |
| 663 | * @param suite The test suite |
| 664 | */ |
| 665 | PJ_DECL(void) pj_test_run(pj_test_runner *runner, |
| 666 | pj_test_suite *suite); |
| 667 | |
| 668 | /** |
| 669 | * This is a crude test to detect if thread is currently running under |
| 670 | * a test. It is mainly used to prevent nested unit testing. |
| 671 | * |
| 672 | * @return PJ_TRUE if we are currently running in the context of a test case |
| 673 | * being run. |
| 674 | */ |
| 675 | PJ_DECL(pj_bool_t) pj_test_is_under_test(void); |
| 676 | |
| 677 | /** |
| 678 | * Get the test statistics after the run completes. The test suite and |
| 679 | * test cases instances must be kept alive in order to get and access the |
| 680 | * statistics or log messages. |
| 681 | * |
| 682 | * @param suite The test suite |
| 683 | * @param stat The test statistics result. |
| 684 | */ |
| 685 | PJ_DECL(void) pj_test_get_stat(const pj_test_suite *suite, pj_test_stat *stat); |
| 686 | |
| 687 | /** |
| 688 | * Display statistics to the log. |
| 689 | * |
| 690 | * @param stat The test statistics result. |
| 691 | */ |
| 692 | PJ_DECL(void) pj_test_display_stat(const pj_test_stat *stat, |
| 693 | const char *test_name, |
| 694 | const char *log_sender); |
| 695 | |
| 696 | /** |
| 697 | * Display previously saved log messages in the test cases to logging. |
| 698 | * Note that log messages emited during test case's run are only saved |
| 699 | * when fifobuf of the test case is configured with a suitable buffer. |
| 700 | * Also note that the test suite and test cases instances must be kept alive |
| 701 | * in order to get and access the statistics or log messages. |
| 702 | * |
| 703 | * @param suite The test suite |
| 704 | * @param flags Select which test logs to display by choosing |
| 705 | * from pj_test_select_tests. |
| 706 | */ |
| 707 | PJ_DECL(void) pj_test_display_log_messages(const pj_test_suite *suite, |
| 708 | unsigned flags); |
| 709 | |
| 710 | /** |
| 711 | * Destroy the runner. Runner may be destroyed right after it is run, |
| 712 | * but the test suite and test cases instances must be kept alive in order |
| 713 | * to get the statistics or log messages. |
| 714 | * |
| 715 | * @param runner The test runner. |
| 716 | */ |
| 717 | PJ_DECL(void) pj_test_runner_destroy(pj_test_runner *runner); |
| 718 | |
| 719 | |
| 720 | /** |
| 721 | * Macro to control how long worker thread should sleep waiting for next |
| 722 | * ready test. |
| 723 | */ |
| 724 | #ifndef PJ_TEST_THREAD_WAIT_MSEC |
| 725 | # define PJ_TEST_THREAD_WAIT_MSEC 100 |
| 726 | #endif |
| 727 | |
| 728 | PJ_END_DECL |
| 729 | |
| 730 | /** |
| 731 | * @} // PJ_UNITTEST group |
| 732 | */ |
| 733 | |
| 734 | #endif /* __PJ_UNITTEST_H__ */ |
| 735 |