check.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  1. /*-*- mode:C; -*- */
  2. /*
  3. * Check: a unit test framework for C
  4. * Copyright (C) 2001, 2002, Arien Malec
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the
  18. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. */
  21. #ifndef CHECK_H
  22. #define CHECK_H
  23. #include <stddef.h>
  24. #include <string.h>
  25. #include <check_stdint.h>
  26. /*
  27. Macros and functions starting with _ (underscore) are internal and
  28. may change without notice. You have been warned!.
  29. */
  30. #ifdef __cplusplus
  31. #define CK_CPPSTART extern "C" {
  32. #define CK_CPPEND }
  33. CK_CPPSTART
  34. #endif
  35. #if defined(__GNUC__) && defined(__GNUC_MINOR__)
  36. #define GCC_VERSION_AT_LEAST(major, minor) \
  37. ((__GNUC__ > (major)) || \
  38. (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
  39. #else
  40. #define GCC_VERSION_AT_LEAST(major, minor) 0
  41. #endif
  42. #if GCC_VERSION_AT_LEAST(2,95)
  43. #define CK_ATTRIBUTE_UNUSED __attribute__ ((unused))
  44. #else
  45. #define CK_ATTRIBUTE_UNUSED
  46. #endif /* GCC 2.95 */
  47. #if GCC_VERSION_AT_LEAST(2,5)
  48. #define CK_ATTRIBUTE_NORETURN __attribute__ ((noreturn))
  49. #else
  50. #define CK_ATTRIBUTE_NORETURN
  51. #endif /* GCC 2.5 */
  52. #include <sys/types.h>
  53. /*
  54. * Used to create the linker script for hiding lib-local symbols. Shall
  55. * be put directly in front of the exported symbol.
  56. */
  57. #define CK_EXPORT
  58. /*
  59. * Used for MSVC to create the export attribute
  60. * CK_DLL_EXP is defined during the compilation of the library
  61. * on the command line.
  62. */
  63. #ifndef CK_DLL_EXP
  64. #define CK_DLL_EXP
  65. #endif
  66. /* check version numbers */
  67. #define CHECK_MAJOR_VERSION (0)
  68. #define CHECK_MINOR_VERSION (10)
  69. #define CHECK_MICRO_VERSION (0)
  70. CK_DLL_EXP extern int CK_EXPORT check_major_version;
  71. CK_DLL_EXP extern int CK_EXPORT check_minor_version;
  72. CK_DLL_EXP extern int CK_EXPORT check_micro_version;
  73. #ifndef NULL
  74. #define NULL ((void*)0)
  75. #endif
  76. /**
  77. * Type for a test case
  78. *
  79. * A TCase represents a test case. Create with tcase_create, free
  80. * with tcase_free. For the moment, test cases can only be run
  81. * through a suite
  82. */
  83. typedef struct TCase TCase;
  84. /**
  85. * Type for a test function
  86. */
  87. typedef void (*TFun) (int);
  88. /**
  89. * Type for a setup/teardown function
  90. */
  91. typedef void (*SFun) (void);
  92. /**
  93. * Type for a test suite
  94. */
  95. typedef struct Suite Suite;
  96. /**
  97. * Creates a test suite with the given name.
  98. *
  99. * Create a suite, which will contain test cases. Once
  100. * created, use suite_add_tcase() to add test cases.
  101. * When finished, create a suite runner from the
  102. * suite using srunner_create()
  103. *
  104. * @param name name of the suite
  105. *
  106. * @return suite
  107. *
  108. * @since 0.6.0
  109. */
  110. CK_DLL_EXP Suite *CK_EXPORT suite_create(const char *name);
  111. /**
  112. * Determines whether a given test suite contains a case named after a
  113. * given string.
  114. *
  115. * @param s suite to check
  116. * @param tcname test case to look for
  117. *
  118. * @return 1 iff the given test case is within the given suite;
  119. * 0 otherwise
  120. *
  121. * @since 0.9.9
  122. */
  123. CK_DLL_EXP int CK_EXPORT suite_tcase(Suite * s, const char *tcname);
  124. /**
  125. * Add a test case to a suite
  126. *
  127. * @param s suite to add test case to
  128. * @param tc test case to add to suite
  129. *
  130. * @since 0.6.0
  131. */
  132. CK_DLL_EXP void CK_EXPORT suite_add_tcase(Suite * s, TCase * tc);
  133. /**
  134. * Create a test case.
  135. *
  136. * Once created, tests can be added with the tcase_add_test()
  137. * function, and the test case assigned to a suite with the
  138. * suite_add_tcase() function.
  139. *
  140. * @param name name of the test case
  141. *
  142. * @return test case containing no tests
  143. *
  144. * @since 0.6.0
  145. * */
  146. CK_DLL_EXP TCase *CK_EXPORT tcase_create(const char *name);
  147. /**
  148. * Add a test function to a test case
  149. *
  150. * @param tc test case to add test to
  151. * @param tf test function to add to test case
  152. *
  153. * @since 0.6.0
  154. * */
  155. #define tcase_add_test(tc,tf) tcase_add_test_raise_signal(tc,tf,0)
  156. /**
  157. * Add a test function with signal handling to a test case
  158. *
  159. * The added test is expected to terminate by throwing the given signal
  160. *
  161. * @param tc test case to add test to
  162. * @param tf test function to add to test case
  163. * @param signal expected signal for test function to throw in order for
  164. * the test to be considered passing
  165. *
  166. * @since 0.9.2
  167. * */
  168. #define tcase_add_test_raise_signal(tc,tf,signal) \
  169. _tcase_add_test((tc),(tf),"" # tf "",(signal), 0, 0, 1)
  170. /**
  171. * Add a test function with an expected exit value to a test case
  172. *
  173. * The added test is expected to terminate by exiting with the given value
  174. *
  175. * @param tc test case to add test to
  176. * @param tf test function to add to test case
  177. * @param expected_exit_value exit value for test function to return in
  178. * order for the test to be considered passing
  179. *
  180. * @since 0.9.7
  181. */
  182. #define tcase_add_exit_test(tc, tf, expected_exit_value) \
  183. _tcase_add_test((tc),(tf),"" # tf "",0,(expected_exit_value),0,1)
  184. /**
  185. * Add a looping test function to a test case
  186. *
  187. * The test will be called in a for(i = s; i < e; i++) loop with each
  188. * iteration being executed in a new context. The loop variable 'i' is
  189. * available in the test.
  190. *
  191. * @param tc test case to add test to
  192. * @param tf function to add to test case
  193. * @param s starting index for value "i" in test
  194. * @param e ending index for value "i" in test
  195. *
  196. * @since 0.9.4
  197. */
  198. #define tcase_add_loop_test(tc,tf,s,e) \
  199. _tcase_add_test((tc),(tf),"" # tf "",0,0,(s),(e))
  200. /**
  201. * Add a looping test function with signal handling to a test case
  202. *
  203. * The test will be called in a for(i = s; i < e; i++) loop with each
  204. * iteration being executed in a new context. The loop variable 'i' is
  205. * available in the test.
  206. *
  207. * The added test is expected to terminate by throwing the given signal
  208. *
  209. * @param tc test case to add test to
  210. * @param tf function to add to test case
  211. * @param signal expected signal for test function to throw in order for
  212. * the test to be considered passing
  213. * @param s starting index for value "i" in test
  214. * @param e ending index for value "i" in test
  215. *
  216. * @since 0.9.5
  217. */
  218. #define tcase_add_loop_test_raise_signal(tc,tf,signal,s,e) \
  219. _tcase_add_test((tc),(tf),"" # tf "",(signal),0,(s),(e))
  220. /**
  221. * Add a looping test function with an expected exit value to a test case
  222. *
  223. * The test will be called in a for(i = s; i < e; i++) loop with each
  224. * iteration being executed in a new context. The loop variable 'i' is
  225. * available in the test.
  226. *
  227. * The added test is expected to terminate by exiting with the given value
  228. *
  229. * @param tc test case to add test to
  230. * @param tf function to add to test case
  231. * @param expected_exit_value exit value for test function to return in
  232. * order for the test to be considered passing
  233. * @param s starting index for value "i" in test
  234. * @param e ending index for value "i" in test
  235. *
  236. * @since 0.9.7
  237. */
  238. #define tcase_add_loop_exit_test(tc,tf,expected_exit_value,s,e) \
  239. _tcase_add_test((tc),(tf),"" # tf "",0,(expected_exit_value),(s),(e))
  240. /* Add a test function to a test case
  241. (function version -- use this when the macro won't work
  242. */
  243. CK_DLL_EXP void CK_EXPORT _tcase_add_test(TCase * tc, TFun tf,
  244. const char *fname, int _signal,
  245. int allowed_exit_value, int start,
  246. int end);
  247. /**
  248. * Add unchecked fixture setup/teardown functions to a test case
  249. *
  250. * Unchecked fixture functions are run at the start and end of the
  251. * test case, and not before and after unit tests. Further,
  252. * unchecked fixture functions are not run in a separate address space,
  253. * like test functions, and so must not exit or signal (e.g.,
  254. * segfault).
  255. *
  256. * Also, when run in CK_NOFORK mode, unchecked fixture functions may
  257. * lead to different unit test behavior if unit tests change data
  258. * setup by the fixture functions.
  259. *
  260. * Note that if a setup function fails, the remaining setup functions
  261. * will be omitted, as will the test case and the teardown functions.
  262. * If a teardown function fails the remaining teardown functins will be
  263. * omitted.
  264. *
  265. * @param tc test case to add unchecked fixture setup/teardown to
  266. * @param setup function to add to be executed before the test case;
  267. * if NULL no setup function is added
  268. * @param teardown function to add to be executed after the test case;
  269. * if NULL no teardown function is added
  270. * @since 0.8.0
  271. */
  272. CK_DLL_EXP void CK_EXPORT tcase_add_unchecked_fixture(TCase * tc, SFun setup,
  273. SFun teardown);
  274. /**
  275. * Add checked fixture setup/teardown functions to a test case
  276. *
  277. * Checked fixture functions are run before and after each unit test inside
  278. * of the address space of the test. Thus, if using CK_FORK
  279. * mode the separate process running the unit test will survive signals
  280. * or unexpected exits in the fixture function. Also, if the setup
  281. * function is idempotent, unit test behavior will be the same in
  282. * CK_FORK and CK_NOFORK modes.
  283. *
  284. * However, since fixture functions are run before and after each unit
  285. * test, they should not be expensive code.
  286. *
  287. * Note that if a setup function fails, the remaining setup functions
  288. * will be omitted, as will the test and the teardown functions. If a
  289. * teardown function fails the remaining teardown functins will be
  290. * omitted.
  291. *
  292. * @param tc test case to add checked fixture setup/teardown to
  293. * @param setup function to add to be executed before each unit test in
  294. * the test case; if NULL no setup function is added
  295. * @param teardown function to add to be executed after each unit test in
  296. * the test case; if NULL no teardown function is added
  297. *
  298. * @since 0.8.0
  299. */
  300. CK_DLL_EXP void CK_EXPORT tcase_add_checked_fixture(TCase * tc, SFun setup,
  301. SFun teardown);
  302. /**
  303. * Set the timeout for all tests in a test case.
  304. *
  305. * A test that lasts longer than the timeout (in seconds) will be killed
  306. * and thus fail with an error.
  307. *
  308. * If not set, the default timeout is one assigned at compile time. If
  309. * the environment variable CK_DEFAULT_TIMEOUT is defined and no timeout
  310. * is set, the value in the environment variable is used.
  311. *
  312. * If Check is compile without fork() support this call is ignored,
  313. * as timeouts are not possible.
  314. *
  315. * @param tc test case to assign timeout to
  316. * @param timeout to use, in seconds. If the value contains a decimal
  317. * portion, but no high resolution timer is available,
  318. * the value is rounded up to the nearest second.
  319. *
  320. * @since 0.9.2
  321. */
  322. CK_DLL_EXP void CK_EXPORT tcase_set_timeout(TCase * tc, double timeout);
  323. /* Internal function to mark the start of a test function */
  324. CK_DLL_EXP void CK_EXPORT tcase_fn_start(const char *fname, const char *file,
  325. int line);
  326. /**
  327. * Start a unit test with START_TEST(unit_name), end with END_TEST.
  328. *
  329. * One must use braces within a START_/END_ pair to declare new variables
  330. *
  331. * @since 0.6.0
  332. */
  333. #define START_TEST(__testname)\
  334. static void __testname (int _i CK_ATTRIBUTE_UNUSED)\
  335. {\
  336. tcase_fn_start (""# __testname, __FILE__, __LINE__);
  337. /**
  338. * End a unit test
  339. *
  340. * @since 0.6.0
  341. */
  342. #define END_TEST }
  343. /*
  344. * Fail the test case unless expr is false
  345. *
  346. * This call is deprecated.
  347. */
  348. #define fail_unless ck_assert_msg
  349. /*
  350. * Fail the test case if expr is false
  351. *
  352. * This call is deprecated.
  353. *
  354. * NOTE: The space before the comma sign before ## is essential to be compatible
  355. * with gcc 2.95.3 and earlier.
  356. * FIXME: these macros may conflict with C89 if expr is
  357. * FIXME: strcmp (str1, str2) due to excessive string length.
  358. */
  359. #define fail_if(expr, ...)\
  360. (expr) ? \
  361. _ck_assert_failed(__FILE__, __LINE__, "Failure '"#expr"' occurred" , ## __VA_ARGS__, NULL) \
  362. : _mark_point(__FILE__, __LINE__)
  363. /*
  364. * Fail the test
  365. *
  366. * This call is deprecated.
  367. */
  368. #define fail ck_abort_msg
  369. /*
  370. * This is called whenever an assertion fails.
  371. * Note that it only has the noreturn modifier when
  372. * using fork. If fork is unavailable, the function
  373. * calls longjmp() when a test assertion fails. Marking
  374. * the function as noreturn causes gcc to make assumptions
  375. * which are not valid, as longjmp() is like a return.
  376. */
  377. #if 1
  378. CK_DLL_EXP void CK_EXPORT _ck_assert_failed(const char *file, int line,
  379. const char *expr,
  380. ...) CK_ATTRIBUTE_NORETURN;
  381. #else
  382. CK_DLL_EXP void CK_EXPORT _ck_assert_failed(const char *file, int line,
  383. const char *expr, ...);
  384. #endif
  385. /**
  386. * Fail the test if expression is false
  387. *
  388. * @param expr expression to evaluate
  389. *
  390. * @note If the check fails, the remaining of the test is aborted
  391. *
  392. * @since 0.9.6
  393. */
  394. #define ck_assert(expr) ck_assert_msg(expr, NULL)
  395. /* The space before the comma sign before ## is essential to be compatible
  396. with gcc 2.95.3 and earlier.
  397. */
  398. /**
  399. * Fail the test if the expression is false; print message on failure
  400. *
  401. * @param expr expression to evaluate
  402. * @param ... message to print (in printf format) if expression is false
  403. *
  404. * @note If the check fails, the remaining of the test is aborted
  405. *
  406. * @since 0.9.6
  407. */
  408. #define ck_assert_msg(expr, ...) \
  409. (expr) ? \
  410. _mark_point(__FILE__, __LINE__) : \
  411. _ck_assert_failed(__FILE__, __LINE__, "Assertion '"#expr"' failed" , ## __VA_ARGS__, NULL)
  412. /**
  413. * Unconditionally fail the test
  414. *
  415. * @note Once called, the remaining of the test is aborted
  416. *
  417. * @since 0.9.6
  418. */
  419. #define ck_abort() ck_abort_msg(NULL)
  420. /**
  421. * Unconditionally fail the test; print a message
  422. *
  423. * @param ... message to print (in printf format)
  424. *
  425. * @note Once called, the remaining of the test is aborted
  426. *
  427. * @since 0.9.6
  428. */
  429. #define ck_abort_msg(...) _ck_assert_failed(__FILE__, __LINE__, "Failed" , ## __VA_ARGS__, NULL)
  430. /* Signed and unsigned integer comparison macros with improved output compared to ck_assert(). */
  431. /* OP may be any comparison operator. */
  432. #define _ck_assert_int(X, OP, Y) do { \
  433. intmax_t _ck_x = (X); \
  434. intmax_t _ck_y = (Y); \
  435. ck_assert_msg(_ck_x OP _ck_y, "Assertion '%s' failed: %s == %jd, %s == %jd", #X" "#OP" "#Y, #X, _ck_x, #Y, _ck_y); \
  436. } while (0)
  437. /**
  438. * Check two signed integers to determine if X==Y
  439. *
  440. * If not X==Y, the test fails.
  441. *
  442. * @param X signed integer
  443. * @param Y signed integer to compare against X
  444. *
  445. * @note If the check fails, the remaining of the test is aborted
  446. *
  447. * @since 0.9.6
  448. */
  449. #define ck_assert_int_eq(X, Y) _ck_assert_int(X, ==, Y)
  450. /**
  451. * Check two signed integers to determine if X!=Y
  452. *
  453. * If not X!=Y, the test fails.
  454. *
  455. * @param X signed integer
  456. * @param Y signed integer to compare against X
  457. *
  458. * @note If the check fails, the remaining of the test is aborted
  459. *
  460. * @since 0.9.6
  461. */
  462. #define ck_assert_int_ne(X, Y) _ck_assert_int(X, !=, Y)
  463. /**
  464. * Check two signed integers to determine if X<Y
  465. *
  466. * If not X<Y, the test fails.
  467. *
  468. * @param X signed integer
  469. * @param Y signed integer to compare against X
  470. *
  471. * @note If the check fails, the remaining of the test is aborted
  472. *
  473. * @since 0.9.10
  474. */
  475. #define ck_assert_int_lt(X, Y) _ck_assert_int(X, <, Y)
  476. /**
  477. * Check two signed integers to determine if X<=Y
  478. *
  479. * If not X<=Y, the test fails.
  480. *
  481. * @param X signed integer
  482. * @param Y signed integer to compare against X
  483. *
  484. * @note If the check fails, the remaining of the test is aborted
  485. *
  486. * @since 0.9.10
  487. */
  488. #define ck_assert_int_le(X, Y) _ck_assert_int(X, <=, Y)
  489. /**
  490. * Check two signed integers to determine if X>Y
  491. *
  492. * If not X>Y, the test fails.
  493. *
  494. * @param X signed integer
  495. * @param Y signed integer to compare against X
  496. *
  497. * @note If the check fails, the remaining of the test is aborted
  498. *
  499. * @since 0.9.10
  500. */
  501. #define ck_assert_int_gt(X, Y) _ck_assert_int(X, >, Y)
  502. /**
  503. * Check two signed integers to determine if X>=Y
  504. *
  505. * If not X>=Y, the test fails.
  506. *
  507. * @param X signed integer
  508. * @param Y signed integer to compare against X
  509. *
  510. * @note If the check fails, the remaining of the test is aborted
  511. *
  512. * @since 0.9.10
  513. */
  514. #define ck_assert_int_ge(X, Y) _ck_assert_int(X, >=, Y)
  515. #define _ck_assert_uint(X, OP, Y) do { \
  516. uintmax_t _ck_x = (X); \
  517. uintmax_t _ck_y = (Y); \
  518. ck_assert_msg(_ck_x OP _ck_y, "Assertion '%s' failed: %s == %ju, %s == %ju", #X" "#OP" "#Y, #X, _ck_x, #Y, _ck_y); \
  519. } while (0)
  520. /**
  521. * Check two unsigned integers to determine if X==Y
  522. *
  523. * If not X==Y, the test fails.
  524. *
  525. * @param X signed integer
  526. * @param Y signed integer to compare against X
  527. *
  528. * @note If the check fails, the remaining of the test is aborted
  529. *
  530. * @since 0.9.10
  531. */
  532. #define ck_assert_uint_eq(X, Y) _ck_assert_uint(X, ==, Y)
  533. /**
  534. * Check two unsigned integers to determine if X!=Y
  535. *
  536. * If not X!=Y, the test fails.
  537. *
  538. * @param X signed integer
  539. * @param Y signed integer to compare against X
  540. *
  541. * @note If the check fails, the remaining of the test is aborted
  542. *
  543. * @since 0.9.10
  544. */
  545. #define ck_assert_uint_ne(X, Y) _ck_assert_uint(X, !=, Y)
  546. /**
  547. * Check two unsigned integers to determine if X<Y
  548. *
  549. * If not X<Y, the test fails.
  550. *
  551. * @param X signed integer
  552. * @param Y signed integer to compare against X
  553. *
  554. * @note If the check fails, the remaining of the test is aborted
  555. *
  556. * @since 0.9.10
  557. */
  558. #define ck_assert_uint_lt(X, Y) _ck_assert_uint(X, <, Y)
  559. /**
  560. * Check two unsigned integers to determine if X<=Y
  561. *
  562. * If not X<=Y, the test fails.
  563. *
  564. * @param X signed integer
  565. * @param Y signed integer to compare against X
  566. *
  567. * @note If the check fails, the remaining of the test is aborted
  568. *
  569. * @since 0.9.10
  570. */
  571. #define ck_assert_uint_le(X, Y) _ck_assert_uint(X, <=, Y)
  572. /**
  573. * Check two unsigned integers to determine if X>Y
  574. *
  575. * If not X>Y, the test fails.
  576. *
  577. * @param X signed integer
  578. * @param Y signed integer to compare against X
  579. *
  580. * @note If the check fails, the remaining of the test is aborted
  581. *
  582. * @since 0.9.10
  583. */
  584. #define ck_assert_uint_gt(X, Y) _ck_assert_uint(X, >, Y)
  585. /**
  586. * Check two unsigned integers to determine if X>=Y
  587. *
  588. * If not X>=Y, the test fails.
  589. *
  590. * @param X signed integer
  591. * @param Y signed integer to compare against X
  592. *
  593. * @note If the check fails, the remaining of the test is aborted
  594. *
  595. * @since 0.9.10
  596. */
  597. #define ck_assert_uint_ge(X, Y) _ck_assert_uint(X, >=, Y)
  598. /* String comparison macros with improved output compared to ck_assert() */
  599. /* OP might be any operator that can be used in '0 OP strcmp(X,Y)' comparison */
  600. /* The x and y parameter swap in strcmp() is needed to handle >, >=, <, <= operators */
  601. #define _ck_assert_str(X, OP, Y) do { \
  602. const char* _ck_x = (X); \
  603. const char* _ck_y = (Y); \
  604. ck_assert_msg(0 OP strcmp(_ck_y, _ck_x), \
  605. "Assertion '%s' failed: %s == \"%s\", %s == \"%s\"", #X" "#OP" "#Y, #X, _ck_x, #Y, _ck_y); \
  606. } while (0)
  607. /**
  608. * Check two strings to determine if 0==strcmp(X,Y)
  609. *
  610. * If not 0==strcmp(X,Y), the test fails.
  611. *
  612. * @param X string
  613. * @param Y string to compare against X
  614. *
  615. * @note If the check fails, the remaining of the test is aborted
  616. *
  617. * @since 0.9.6
  618. */
  619. #define ck_assert_str_eq(X, Y) _ck_assert_str(X, ==, Y)
  620. /**
  621. * Check two strings to determine if 0!=strcmp(X,Y)
  622. *
  623. * If not 0!=strcmp(X,Y), the test fails.
  624. *
  625. * @param X string
  626. * @param Y string to compare against X
  627. *
  628. * @note If the check fails, the remaining of the test is aborted
  629. *
  630. * @since 0.9.6
  631. */
  632. #define ck_assert_str_ne(X, Y) _ck_assert_str(X, !=, Y)
  633. /**
  634. * Check two strings to determine if 0<strcmp(X,Y), (e.g. strcmp(X,Y)>0)
  635. *
  636. * If not 0<strcmp(X,Y), the test fails.
  637. *
  638. * @param X string
  639. * @param Y string to compare against X
  640. *
  641. * @note If the check fails, the remaining of the test is aborted
  642. *
  643. * @since 0.9.10
  644. */
  645. #define ck_assert_str_lt(X, Y) _ck_assert_str(X, <, Y)
  646. /**
  647. * Check two strings to determine if 0<=strcmp(X,Y) (e.g. strcmp(X,Y)>=0)
  648. *
  649. * If not 0<=strcmp(X,Y), the test fails.
  650. *
  651. * @param X string
  652. * @param Y string to compare against X
  653. *
  654. * @note If the check fails, the remaining of the test is aborted
  655. *
  656. * @since 0.9.10
  657. */
  658. #define ck_assert_str_le(X, Y) _ck_assert_str(X, <=, Y)
  659. /**
  660. * Check two strings to determine if 0<strcmp(X,Y) (e.g. strcmp(X,Y)>0)
  661. *
  662. * If not 0<strcmp(X,Y), the test fails.
  663. *
  664. * @param X string
  665. * @param Y string to compare against X
  666. *
  667. * @note If the check fails, the remaining of the test is aborted
  668. *
  669. * @since 0.9.10
  670. */
  671. #define ck_assert_str_gt(X, Y) _ck_assert_str(X, >, Y)
  672. /**
  673. * Check two strings to determine if 0>=strcmp(X,Y) (e.g. strcmp(X,Y)<=0)
  674. *
  675. * If not 0>=strcmp(X,Y), the test fails.
  676. *
  677. * @param X string
  678. * @param Y string to compare against X
  679. *
  680. * @note If the check fails, the remaining of the test is aborted
  681. *
  682. * @since 0.9.10
  683. */
  684. #define ck_assert_str_ge(X, Y) _ck_assert_str(X, >=, Y)
  685. /* Pointer comparison macros with improved output compared to ck_assert(). */
  686. /* OP may only be == or != */
  687. #define _ck_assert_ptr(X, OP, Y) do { \
  688. const void* _ck_x = (X); \
  689. const void* _ck_y = (Y); \
  690. ck_assert_msg(_ck_x OP _ck_y, "Assertion '%s' failed: %s == %#x, %s == %#x", #X" "#OP" "#Y, #X, _ck_x, #Y, _ck_y); \
  691. } while (0)
  692. /**
  693. * Check if two pointers are equal.
  694. *
  695. * If the two passed pointers are not equal, the test
  696. * fails.
  697. *
  698. * @param X pointer
  699. * @param Y pointer to compare against X
  700. *
  701. * @note If the check fails, the remaining of the test is aborted
  702. *
  703. * @since 0.9.10
  704. */
  705. #define ck_assert_ptr_eq(X, Y) _ck_assert_ptr(X, ==, Y)
  706. /**
  707. * Check if two pointers are not.
  708. *
  709. * If the two passed pointers are equal, the test fails.
  710. *
  711. * @param X pointer
  712. * @param Y pointer to compare against X
  713. *
  714. * @since 0.9.10
  715. */
  716. #define ck_assert_ptr_ne(X, Y) _ck_assert_ptr(X, !=, Y)
  717. /**
  718. * Mark the last point reached in a unit test.
  719. *
  720. * If the test throws a signal or exits, the location noted with the
  721. * failure is the last location of a ck_assert*() or ck_abort() call.
  722. * Use mark_point() to record intermediate locations (useful for tracking down
  723. * crashes or exits).
  724. *
  725. * @since 0.6.0
  726. */
  727. #define mark_point() _mark_point(__FILE__,__LINE__)
  728. /* Non macro version of #mark_point */
  729. CK_DLL_EXP void CK_EXPORT _mark_point(const char *file, int line);
  730. /**
  731. * Enum describing the possible results of a test
  732. */
  733. enum test_result
  734. {
  735. CK_TEST_RESULT_INVALID, /**< Default value; should not encounter this */
  736. CK_PASS, /**< Test passed */
  737. CK_FAILURE, /**< Test completed but failed */
  738. CK_ERROR /**< Test failed to complete
  739. (unexpected signal or non-zero early exit) */
  740. };
  741. /**
  742. * Enum specifying the verbosity of output a SRunner should produce
  743. */
  744. enum print_output
  745. {
  746. CK_SILENT, /**< No output */
  747. CK_MINIMAL, /**< Only summary output */
  748. CK_NORMAL, /**< All failed tests */
  749. CK_VERBOSE, /**< All tests */
  750. CK_ENV, /**< Look at environment var CK_VERBOSITY
  751. for what verbosity to use, which can be
  752. either "silent", "minimal", "normal",
  753. or "verbose". If the environment variable
  754. is not set, then CK_NORMAL will be used.*/
  755. #if 0
  756. CK_SUBUNIT, /**< Run as a subunit child process */
  757. #endif
  758. CK_LAST /**< Not a valid option */
  759. };
  760. /**
  761. * Holds state for a running of a test suite
  762. */
  763. typedef struct SRunner SRunner;
  764. /**
  765. * Opaque type for a test failure
  766. */
  767. typedef struct TestResult TestResult;
  768. /**
  769. * Enum representing the types of contexts for a test
  770. */
  771. enum ck_result_ctx
  772. {
  773. CK_CTX_INVALID, /**< Default value; should not encounter this */
  774. CK_CTX_SETUP, /**< Setup before a test */
  775. CK_CTX_TEST, /**< Body of test itself */
  776. CK_CTX_TEARDOWN /**< Teardown after a test */
  777. };
  778. /**
  779. * Retrieve type of result that the given test result represents.
  780. *
  781. * This is a member of test_result, and can represent a
  782. * pass, failure, or error.
  783. *
  784. * @param tr test result to retrieve result from
  785. *
  786. * @return result of given test
  787. *
  788. * @since 0.6.0
  789. */
  790. CK_DLL_EXP int CK_EXPORT tr_rtype(TestResult * tr);
  791. /**
  792. * Retrieve context in which the result occurred for the given test result.
  793. *
  794. * The types of contents include the test setup, teardown, or the
  795. * body of the test itself.
  796. *
  797. * @param tr test result to retrieve context from
  798. *
  799. * @return context to which the given test result applies
  800. *
  801. * @since 0.8.0
  802. */
  803. CK_DLL_EXP enum ck_result_ctx CK_EXPORT tr_ctx(TestResult * tr);
  804. /**
  805. * Retrieve failure message from test result, if applicable.
  806. *
  807. * @return pointer to a message, if one exists. NULL otherwise.
  808. *
  809. * @since 0.6.0
  810. */
  811. CK_DLL_EXP const char *CK_EXPORT tr_msg(TestResult * tr);
  812. /**
  813. * Retrieve line number at which a failure occurred, if applicable.
  814. *
  815. * @return If the test resulted in a failure, returns the line number
  816. * that the failure occurred on; otherwise returns -1.
  817. *
  818. * @since 0.6.0
  819. */
  820. CK_DLL_EXP int CK_EXPORT tr_lno(TestResult * tr);
  821. /**
  822. * Retrieve file name at which a failure occurred, if applicable.
  823. *
  824. * @return If the test resulted in a failure, returns a string
  825. * containing the name of the file where the failure
  826. * occurred; otherwise returns NULL.
  827. *
  828. * @since 0.6.0
  829. */
  830. CK_DLL_EXP const char *CK_EXPORT tr_lfile(TestResult * tr);
  831. /**
  832. * Retrieve test case name in which a failure occurred, if applicable.
  833. *
  834. * @return If the test resulted in a failure, returns a string
  835. * containing the name of the test suite where the failure
  836. * occurred; otherwise returns NULL.
  837. *
  838. * @since 0.6.0
  839. */
  840. CK_DLL_EXP const char *CK_EXPORT tr_tcname(TestResult * tr);
  841. /**
  842. * Creates a suite runner for the given suite.
  843. *
  844. * Once created, additional suites can be added to the
  845. * suite runner using srunner_add_suite(), and the suite runner can be
  846. * run with srunner_run_all(). Once finished, the suite runner
  847. * must be freed with srunner_free().
  848. *
  849. * @param s suite to generate a suite runner for
  850. *
  851. * @return suite runner for the given suite
  852. *
  853. * @since 0.6.0
  854. */
  855. CK_DLL_EXP SRunner *CK_EXPORT srunner_create(Suite * s);
  856. /**
  857. * Add an additional suite to a suite runner.
  858. *
  859. * The first suite in a suite runner is always added in srunner_create().
  860. * This call adds additional suites to a suite runner.
  861. *
  862. * @param sr suite runner to add the given suite
  863. * @param s suite to add to the given suite runner
  864. *
  865. * @since 0.7.0
  866. */
  867. CK_DLL_EXP void CK_EXPORT srunner_add_suite(SRunner * sr, Suite * s);
  868. /**
  869. * Frees a suite runner, including all contained suite and test cases.
  870. *
  871. * This call is responsible for freeing all resources related to a
  872. * suite runner and all contained suites and test cases. Suite and
  873. * test cases need not be freed individually, as this call handles that.
  874. *
  875. * @param sr suite runner to free
  876. *
  877. * @since 0.6.0
  878. */
  879. CK_DLL_EXP void CK_EXPORT srunner_free(SRunner * sr);
  880. /**
  881. * Runs a suite runner and all contained suite, printing results to
  882. * stdout as specified by the print_mode.
  883. *
  884. * In addition to running all suites, if the suite runner has been
  885. * configured to output to a log, that is also performed.
  886. *
  887. * Note that if the CK_RUN_CASE and/or CK_RUN_SUITE environment variables
  888. * are defined, then only the named suite and/or test case is run.
  889. *
  890. * @param sr suite runner to run all suites from
  891. * @param print_mode the verbosity in which to report results to stdout
  892. *
  893. * @since 0.6.0
  894. */
  895. CK_DLL_EXP void CK_EXPORT srunner_run_all(SRunner * sr,
  896. enum print_output print_mode);
  897. /**
  898. * Run a specific suite or test case from a suite runner, printing results
  899. * to stdout as specified by the print_mode.
  900. *
  901. * In addition to running any applicable suites or test cases, if the
  902. * suite runner has been configured to output to a log, that is also
  903. * performed.
  904. *
  905. * @param sr suite runner where the given suite or test case must be
  906. * @param sname suite name to run. A NULL means "any suite".
  907. * @param tcname test case name to run. A NULL means "any test case"
  908. * @param print_mode the verbosity in which to report results to stdout
  909. *
  910. * @since 0.9.9
  911. */
  912. CK_DLL_EXP void CK_EXPORT srunner_run(SRunner * sr, const char *sname,
  913. const char *tcname,
  914. enum print_output print_mode);
  915. /**
  916. * Retrieve the number of failed tests executed by a suite runner.
  917. *
  918. * This value represents both test failures and errors.
  919. *
  920. * @param sr suite runner to query for all failed tests
  921. *
  922. * @return number of test failures and errors found by the suite runner
  923. *
  924. * @since 0.6.1
  925. */
  926. CK_DLL_EXP int CK_EXPORT srunner_ntests_failed(SRunner * sr);
  927. /**
  928. * Retrieve the total number of tests run by a suite runner.
  929. *
  930. * @param sr suite runner to query for all tests run
  931. *
  932. * @return number of all tests run by the suite runner
  933. *
  934. * @since 0.6.1
  935. */
  936. CK_DLL_EXP int CK_EXPORT srunner_ntests_run(SRunner * sr);
  937. /**
  938. * Return an array of results for all failures found by a suite runner.
  939. *
  940. * Number of results is equal to srunner_nfailed_tests().
  941. *
  942. * Information about individual results can be queried using:
  943. * tr_rtype(), tr_ctx(), tr_msg(), tr_lno(), tr_lfile(), and tr_tcname().
  944. *
  945. * Memory is malloc'ed and must be freed; however free the entire structure
  946. * instead of individual test cases.
  947. *
  948. * @param sr suite runner to retrieve results from
  949. *
  950. * @return array of TestResult objects
  951. *
  952. * @since 0.6.0
  953. */
  954. CK_DLL_EXP TestResult **CK_EXPORT srunner_failures(SRunner * sr);
  955. /**
  956. * Return an array of results for all tests run by a suite runner.
  957. *
  958. * Number of results is equal to srunner_ntests_run(), and excludes
  959. * failures due to setup function failure.
  960. *
  961. * Information about individual results can be queried using:
  962. * tr_rtype(), tr_ctx(), tr_msg(), tr_lno(), tr_lfile(), and tr_tcname().
  963. *
  964. * Memory is malloc'ed and must be freed; however free the entire structure
  965. * instead of individual test cases.
  966. *
  967. * @param sr suite runner to retrieve results from
  968. *
  969. * @return array of TestResult objects
  970. *
  971. * @since 0.6.1
  972. */
  973. CK_DLL_EXP TestResult **CK_EXPORT srunner_results(SRunner * sr);
  974. /**
  975. * Print the results contained in an SRunner to stdout.
  976. *
  977. * @param sr suite runner to print results for to stdout
  978. * @param print_mode the print_output (verbosity) to use to report
  979. * the result
  980. *
  981. * @since 0.7.0
  982. */
  983. CK_DLL_EXP void CK_EXPORT srunner_print(SRunner * sr,
  984. enum print_output print_mode);
  985. /**
  986. * Set the suite runner to output the result in log format to the
  987. * given file.
  988. *
  989. * Note: log file setting is an initialize only operation -- it should
  990. * be done immediately after SRunner creation, and the log file can't be
  991. * changed after being set.
  992. *
  993. * This setting does not conflict with the other log output types;
  994. * all logging types can occur concurrently if configured.
  995. *
  996. * @param sr suite runner to log results of in log format
  997. * @param fname file name to output log results to
  998. *
  999. * @since 0.7.1
  1000. */
  1001. CK_DLL_EXP void CK_EXPORT srunner_set_log(SRunner * sr, const char *fname);
  1002. /**
  1003. * Checks if the suite runner is assigned a file for log output.
  1004. *
  1005. * @param sr suite runner to check
  1006. *
  1007. * @return 1 iff the suite runner currently is configured to output
  1008. * in log format; 0 otherwise
  1009. *
  1010. * @since 0.7.1
  1011. */
  1012. CK_DLL_EXP int CK_EXPORT srunner_has_log(SRunner * sr);
  1013. /**
  1014. * Retrieves the name of the currently assigned file
  1015. * for log output, if any exists.
  1016. *
  1017. * @return the name of the log file, or NULL if none is configured
  1018. *
  1019. * @since 0.7.1
  1020. */
  1021. CK_DLL_EXP const char *CK_EXPORT srunner_log_fname(SRunner * sr);
  1022. /**
  1023. * Set the suite runner to output the result in XML format to the
  1024. * given file.
  1025. *
  1026. * Note: XML file setting is an initialize only operation -- it should
  1027. * be done immediately after SRunner creation, and the XML file can't be
  1028. * changed after being set.
  1029. *
  1030. * This setting does not conflict with the other log output types;
  1031. * all logging types can occur concurrently if configured.
  1032. *
  1033. * @param sr suite runner to log results of in XML format
  1034. * @param fname file name to output XML results to
  1035. *
  1036. * @since 0.9.1
  1037. */
  1038. CK_DLL_EXP void CK_EXPORT srunner_set_xml(SRunner * sr, const char *fname);
  1039. /**
  1040. * Checks if the suite runner is assigned a file for XML output.
  1041. *
  1042. * @param sr suite runner to check
  1043. *
  1044. * @return 1 iff the suite runner currently is configured to output
  1045. * in XML format; 0 otherwise
  1046. *
  1047. * @since 0.9.1
  1048. */
  1049. CK_DLL_EXP int CK_EXPORT srunner_has_xml(SRunner * sr);
  1050. /**
  1051. * Retrieves the name of the currently assigned file
  1052. * for XML output, if any exists.
  1053. *
  1054. * @return the name of the XML file, or NULL if none is configured
  1055. *
  1056. * @since 0.9.1
  1057. */
  1058. CK_DLL_EXP const char *CK_EXPORT srunner_xml_fname(SRunner * sr);
  1059. /**
  1060. * Set the suite runner to output the result in TAP format to the
  1061. * given file.
  1062. *
  1063. * Note: TAP file setting is an initialize only operation -- it should
  1064. * be done immediately after SRunner creation, and the TAP file can't be
  1065. * changed after being set.
  1066. *
  1067. * This setting does not conflict with the other log output types;
  1068. * all logging types can occur concurrently if configured.
  1069. *
  1070. * @param sr suite runner to log results of in TAP format
  1071. * @param fname file name to output TAP results to
  1072. *
  1073. * @since 0.9.12
  1074. */
  1075. CK_DLL_EXP void CK_EXPORT srunner_set_tap(SRunner * sr, const char *fname);
  1076. /**
  1077. * Checks if the suite runner is assigned a file for TAP output.
  1078. *
  1079. * @param sr suite runner to check
  1080. *
  1081. * @return 1 iff the suite runner currently is configured to output
  1082. * in TAP format; 0 otherwise
  1083. *
  1084. * @since 0.9.12
  1085. */
  1086. CK_DLL_EXP int CK_EXPORT srunner_has_tap(SRunner * sr);
  1087. /**
  1088. * Retrieves the name of the currently assigned file
  1089. * for TAP output, if any exists.
  1090. *
  1091. * @return the name of the TAP file, or NULL if none is configured
  1092. *
  1093. * @since 0.9.12
  1094. */
  1095. CK_DLL_EXP const char *CK_EXPORT srunner_tap_fname(SRunner * sr);
  1096. /**
  1097. * Enum describing the current fork usage.
  1098. */
  1099. enum fork_status
  1100. {
  1101. CK_FORK_GETENV, /**< look in the environment for CK_FORK */
  1102. CK_FORK, /**< call fork to run tests */
  1103. CK_NOFORK /**< don't call fork */
  1104. };
  1105. /**
  1106. * Retrieve the current fork status for the given suite runner
  1107. *
  1108. * @param sr suite runner to check fork status of
  1109. *
  1110. * @since 0.8.0
  1111. */
  1112. CK_DLL_EXP enum fork_status CK_EXPORT srunner_fork_status(SRunner * sr);
  1113. /**
  1114. * Set the fork status for a given suite runner.
  1115. *
  1116. * The default fork status is CK_FORK_GETENV, which will look
  1117. * for the CK_FORK environment variable, which can be set to
  1118. * "yes" or "no". If the environment variable is not present,
  1119. * CK_FORK will be used if fork() is available on the system,
  1120. * otherwise CK_NOFORK is used.
  1121. *
  1122. * If set to CK_FORK or CK_NOFORK, the environment variable
  1123. * if defined is ignored.
  1124. *
  1125. * If Check is compiled without support for fork(), attempting
  1126. * to set the status to CK_FORK is ignored.
  1127. *
  1128. * @param sr suite runner to assign the fork status to
  1129. * @param fstat fork status to assign
  1130. *
  1131. * @since 0.8.0
  1132. */
  1133. CK_DLL_EXP void CK_EXPORT srunner_set_fork_status(SRunner * sr,
  1134. enum fork_status fstat);
  1135. /**
  1136. * Invoke fork() during a test and assign the child to the same
  1137. * process group that the rest of the test case uses.
  1138. *
  1139. * One can invoke fork() directly during a test; however doing so
  1140. * may not guarantee that any children processes are destroyed once
  1141. * the test finishes. Once a test has completed, all processes in
  1142. * the process group will be killed; using this wrapper will prevent
  1143. * orphan processes.
  1144. *
  1145. * If Check is compiled without fork() support this call simply
  1146. * return -1 and does nothing.
  1147. *
  1148. * @return On success, the PID of the child process is returned in
  1149. * the parent, and 0 is returned in the child. On failure,
  1150. * a value of -1 is returned to the parent process and no
  1151. * child process is created.
  1152. *
  1153. * @since 0.9.3
  1154. */
  1155. CK_DLL_EXP pid_t CK_EXPORT check_fork(void);
  1156. /**
  1157. * Wait for the pid and exit.
  1158. *
  1159. * This is to be used in conjunction with check_fork(). When called,
  1160. * will wait for the given process to terminate. If the process
  1161. * exited without error, exit(EXIT_SUCCESS) is invoked; otherwise
  1162. * exit(EXIT_FAILURE) is invoked.
  1163. *
  1164. * If Check is compiled without support for fork(), this invokes
  1165. * exit(EXIT_FAILURE).
  1166. *
  1167. * @param pid process to wait for, created by check_fork()
  1168. *
  1169. * @since 0.9.3
  1170. */
  1171. CK_DLL_EXP void CK_EXPORT check_waitpid_and_exit(pid_t pid) CK_ATTRIBUTE_NORETURN;
  1172. #ifdef __cplusplus
  1173. CK_CPPEND
  1174. #endif
  1175. #endif /* CHECK_H */