unittest.h 24 KB

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