3 * Check: a unit test framework for C
4 * Copyright (C) 2001, 2002 Arien Malec
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.
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.
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., 51 Franklin St, Fifth Floor, Boston,
29 Macros and functions starting with _ (underscore) are internal and
30 may change without notice. You have been warned!.
35 #define CK_CPPSTART extern "C" {
39 #if defined(__GNUC__) && defined(__GNUC_MINOR__)
40 #define GCC_VERSION_AT_LEAST(major, minor) \
41 ((__GNUC__ > (major)) || \
42 (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
44 #define GCC_VERSION_AT_LEAST(major, minor) 0
46 #if GCC_VERSION_AT_LEAST(2,95)
47 #define CK_ATTRIBUTE_UNUSED __attribute__ ((unused))
49 #define CK_ATTRIBUTE_UNUSED
51 #if GCC_VERSION_AT_LEAST(2,5)
52 #define CK_ATTRIBUTE_NORETURN __attribute__ ((noreturn))
54 #define CK_ATTRIBUTE_NORETURN
56 #include <sys/types.h>
59 * Used to create the linker script for hiding lib-local symbols. Shall
60 * be put directly in front of the exported symbol.
65 * Used for MSVC to create the export attribute
66 * CK_DLL_EXP is defined during the compilation of the library
67 * on the command line.
70 # if defined(_MSC_VER)
71 # define CK_DLL_EXP __declspec(dllimport)
73 # define CK_DLL_EXP extern
77 /* check version numbers */
78 #define CHECK_MAJOR_VERSION (@CHECK_MAJOR_VERSION@)
79 #define CHECK_MINOR_VERSION (@CHECK_MINOR_VERSION@)
80 #define CHECK_MICRO_VERSION (@CHECK_MICRO_VERSION@)
81 CK_DLL_EXP /*extern*/ int CK_EXPORT check_major_version;
82 CK_DLL_EXP /*extern*/ int CK_EXPORT check_minor_version;
83 CK_DLL_EXP /*extern*/ int CK_EXPORT check_micro_version;
86 #define NULL ((void*)0)
94 * Type for a test case
96 * A TCase represents a test case. Create with tcase_create, free
97 * with tcase_free. For the moment, test cases can only be run
100 typedef struct TCase TCase;
103 * Type for a test function
105 typedef void (*TFun) (int);
108 * Type for a setup/teardown function
110 typedef void (*SFun) (void);
113 * Type for a test suite
115 typedef struct Suite Suite;
118 * Creates a test suite with the given name.
120 * Create a suite, which will contain test cases. Once
121 * created, use suite_add_tcase() to add test cases.
122 * When finished, create a suite runner from the
123 * suite using srunner_create()
125 * @param name name of the suite
131 CK_DLL_EXP Suite *CK_EXPORT suite_create (const char *name);
134 * Determines whether a given test suite contains a case named after a
137 * @param s suite to check
138 * @param tcname test case to look for
140 * @return 1 iff the given test case is within the given suite;
145 CK_DLL_EXP int CK_EXPORT suite_tcase (Suite * s, const char *tcname);
148 * Add a test case to a suite.
150 * Note that if the TCase has already been added attempting
151 * to add it again will be ignored.
153 * @param s suite to add test case to
154 * @param tc test case to add to suite
158 CK_DLL_EXP void CK_EXPORT suite_add_tcase (Suite * s, TCase * tc);
161 * Create a test case.
163 * Once created, tests can be added with the tcase_add_test()
164 * function, and the test case assigned to a suite with the
165 * suite_add_tcase() function.
167 * @param name name of the test case
169 * @return test case containing no tests
173 CK_DLL_EXP TCase *CK_EXPORT tcase_create (const char *name);
176 * Associate a test case with certain tags.
177 * Replaces any existing tags with the new set.
179 * @param tc the test case
181 * @param tags string containing arbitrary tags separated by spaces.
182 * This will be copied. Passing NULL clears all tags.
186 CK_DLL_EXP void CK_EXPORT tcase_set_tags (TCase * tc, const char *tags);
188 * Add a test function to a test case
190 * @param tc test case to add test to
191 * @param tf test function to add to test case
195 #define tcase_add_test(tc,tf) tcase_add_test_raise_signal(tc,tf,0)
198 * Add a test function with signal handling to a test case
200 * The added test is expected to terminate by throwing the given signal
202 * @param tc test case to add test to
203 * @param tf test function to add to test case
204 * @param signal expected signal for test function to throw in order for
205 * the test to be considered passing
209 #define tcase_add_test_raise_signal(tc,tf,signal) \
210 _tcase_add_test((tc),(tf),"" # tf "",(signal), 0, 0, 1)
213 * Add a test function with an expected exit value to a test case
215 * The added test is expected to terminate by exiting with the given value
217 * @param tc test case to add test to
218 * @param tf test function to add to test case
219 * @param expected_exit_value exit value for test function to return in
220 * order for the test to be considered passing
224 #define tcase_add_exit_test(tc, tf, expected_exit_value) \
225 _tcase_add_test((tc),(tf),"" # tf "",0,(expected_exit_value),0,1)
228 * Add a looping test function to a test case
230 * The test will be called in a for(i = s; i < e; i++) loop with each
231 * iteration being executed in a new context. The loop variable 'i' is
232 * available in the test.
234 * @param tc test case to add test to
235 * @param tf function to add to test case
236 * @param s starting index for value "i" in test
237 * @param e ending index for value "i" in test
241 #define tcase_add_loop_test(tc,tf,s,e) \
242 _tcase_add_test((tc),(tf),"" # tf "",0,0,(s),(e))
245 * Add a looping test function with signal handling to a test case
247 * The test will be called in a for(i = s; i < e; i++) loop with each
248 * iteration being executed in a new context. The loop variable 'i' is
249 * available in the test.
251 * The added test is expected to terminate by throwing the given signal
253 * @param tc test case to add test to
254 * @param tf function to add to test case
255 * @param signal expected signal for test function to throw in order for
256 * the test to be considered passing
257 * @param s starting index for value "i" in test
258 * @param e ending index for value "i" in test
262 #define tcase_add_loop_test_raise_signal(tc,tf,signal,s,e) \
263 _tcase_add_test((tc),(tf),"" # tf "",(signal),0,(s),(e))
266 * Add a looping test function with an expected exit value to a test case
268 * The test will be called in a for(i = s; i < e; i++) loop with each
269 * iteration being executed in a new context. The loop variable 'i' is
270 * available in the test.
272 * The added test is expected to terminate by exiting with the given value
274 * @param tc test case to add test to
275 * @param tf function to add to test case
276 * @param expected_exit_value exit value for test function to return in
277 * order for the test to be considered passing
278 * @param s starting index for value "i" in test
279 * @param e ending index for value "i" in test
283 #define tcase_add_loop_exit_test(tc,tf,expected_exit_value,s,e) \
284 _tcase_add_test((tc),(tf),"" # tf "",0,(expected_exit_value),(s),(e))
286 /* Add a test function to a test case
287 (function version -- use this when the macro won't work
289 CK_DLL_EXP void CK_EXPORT _tcase_add_test (TCase * tc, TFun tf,
290 const char *fname, int _signal, int allowed_exit_value, int start, int end);
293 * Add unchecked fixture setup/teardown functions to a test case
295 * Unchecked fixture functions are run at the start and end of the
296 * test case, and not before and after unit tests. Further,
297 * unchecked fixture functions are not run in a separate address space,
298 * like test functions, and so must not exit or signal (e.g.,
301 * Also, when run in CK_NOFORK mode, unchecked fixture functions may
302 * lead to different unit test behavior if unit tests change data
303 * setup by the fixture functions.
305 * Note that if a setup function fails, the remaining setup functions
306 * will be omitted, as will the test case and the teardown functions.
307 * If a teardown function fails the remaining teardown functins will be
310 * @param tc test case to add unchecked fixture setup/teardown to
311 * @param setup function to add to be executed before the test case;
312 * if NULL no setup function is added
313 * @param teardown function to add to be executed after the test case;
314 * if NULL no teardown function is added
317 CK_DLL_EXP void CK_EXPORT tcase_add_unchecked_fixture (TCase * tc, SFun setup,
321 * Add checked fixture setup/teardown functions to a test case
323 * Checked fixture functions are run before and after each unit test inside
324 * of the address space of the test. Thus, if using CK_FORK
325 * mode the separate process running the unit test will survive signals
326 * or unexpected exits in the fixture function. Also, if the setup
327 * function is idempotent, unit test behavior will be the same in
328 * CK_FORK and CK_NOFORK modes.
330 * However, since fixture functions are run before and after each unit
331 * test, they should not be expensive code.
333 * Note that if a setup function fails, the remaining setup functions
334 * will be omitted, as will the test and the teardown functions. If a
335 * teardown function fails the remaining teardown functins will be
338 * @param tc test case to add checked fixture setup/teardown to
339 * @param setup function to add to be executed before each unit test in
340 * the test case; if NULL no setup function is added
341 * @param teardown function to add to be executed after each unit test in
342 * the test case; if NULL no teardown function is added
346 CK_DLL_EXP void CK_EXPORT tcase_add_checked_fixture (TCase * tc, SFun setup,
350 * Set the timeout for all tests in a test case.
352 * A test that lasts longer than the timeout (in seconds) will be killed
353 * and thus fail with an error.
355 * If not set, the default timeout is one assigned at compile time. If
356 * the environment variable CK_DEFAULT_TIMEOUT is defined and no timeout
357 * is set, the value in the environment variable is used.
359 * If Check is compile without fork() support this call is ignored,
360 * as timeouts are not possible.
362 * @param tc test case to assign timeout to
363 * @param timeout to use, in seconds. If the value contains a decimal
364 * portion, but no high resolution timer is available,
365 * the value is rounded up to the nearest second.
369 CK_DLL_EXP void CK_EXPORT tcase_set_timeout (TCase * tc, double timeout);
371 /* Internal function to mark the start of a test function */
372 CK_DLL_EXP void CK_EXPORT tcase_fn_start (const char *fname, const char *file,
376 * Start a unit test with START_TEST(unit_name), end with END_TEST.
378 * One must use braces within a START_/END_ pair to declare new variables
382 #define START_TEST(__testname)\
383 static void __testname (int _i CK_ATTRIBUTE_UNUSED)\
385 tcase_fn_start (""# __testname, __FILE__, __LINE__);
395 * Fail the test case unless expr is false
397 * This call is deprecated.
399 #define fail_unless ck_assert_msg
402 * Fail the test case if expr is false
404 * This call is deprecated.
406 * NOTE: The space before the comma sign before ## is essential to be compatible
407 * with gcc 2.95.3 and earlier.
408 * FIXME: these macros may conflict with C89 if expr is
409 * FIXME: strcmp (str1, str2) due to excessive string length.
411 #define fail_if(expr, ...)\
413 _ck_assert_failed(__FILE__, __LINE__, "Failure '"#expr"' occurred" , ## __VA_ARGS__, NULL) \
414 : _mark_point(__FILE__, __LINE__)
419 * This call is deprecated.
421 #define fail ck_abort_msg
424 * This is called whenever an assertion fails.
425 * Note that it only has the noreturn modifier when
426 * using fork. If fork is unavailable, the function
427 * calls longjmp() when a test assertion fails. Marking
428 * the function as noreturn causes gcc to make assumptions
429 * which are not valid, as longjmp() is like a return.
432 CK_DLL_EXP void CK_EXPORT
433 _ck_assert_failed (const char *file, int line, const char *expr, ...)
434 CK_ATTRIBUTE_NORETURN;
436 CK_DLL_EXP void CK_EXPORT _ck_assert_failed (const char *file, int line,
437 const char *expr, ...);
441 * Fail the test if expression is false
443 * @param expr expression to evaluate
445 * @note If the check fails, the remaining of the test is aborted
449 #define ck_assert(expr) ck_assert_msg(expr, NULL)
451 /* The space before the comma sign before ## is essential to be compatible
452 with gcc 2.95.3 and earlier.
455 * Fail the test if the expression is false; print message on failure
457 * @param expr expression to evaluate
458 * @param ... message to print (in printf format) if expression is false
460 * @note If the check fails, the remaining of the test is aborted
464 #define ck_assert_msg(expr, ...) \
466 _mark_point(__FILE__, __LINE__) : \
467 _ck_assert_failed(__FILE__, __LINE__, "Assertion '"#expr"' failed" , ## __VA_ARGS__, NULL)
470 * Unconditionally fail the test
472 * @note Once called, the remaining of the test is aborted
476 #define ck_abort() ck_abort_msg(NULL)
478 * Unconditionally fail the test; print a message
480 * @param ... message to print (in printf format)
482 * @note Once called, the remaining of the test is aborted
486 #define ck_abort_msg(...) _ck_assert_failed(__FILE__, __LINE__, "Failed" , ## __VA_ARGS__, NULL)
488 /* Signed and unsigned integer comparison macros with improved output compared to ck_assert(). */
489 /* OP may be any comparison operator. */
490 #define _ck_assert_int(X, OP, Y) do { \
491 gint64 _ck_x = (X); \
492 gint64 _ck_y = (Y); \
493 ck_assert_msg(_ck_x OP _ck_y, "Assertion '%s' failed: " \
494 "%s==%" G_GINT64_FORMAT ", %s==%" G_GINT64_FORMAT, #X#OP#Y, #X, _ck_x, #Y, _ck_y); \
498 * Check two signed integers to determine if X==Y
500 * If not X==Y, the test fails.
502 * @param X signed integer
503 * @param Y signed integer to compare against X
505 * @note If the check fails, the remaining of the test is aborted
509 #define ck_assert_int_eq(X, Y) _ck_assert_int(X, ==, Y)
511 * Check two signed integers to determine if X!=Y
513 * If not X!=Y, the test fails.
515 * @param X signed integer
516 * @param Y signed integer to compare against X
518 * @note If the check fails, the remaining of the test is aborted
522 #define ck_assert_int_ne(X, Y) _ck_assert_int(X, !=, Y)
524 * Check two signed integers to determine if X<Y
526 * If not X<Y, the test fails.
528 * @param X signed integer
529 * @param Y signed integer to compare against X
531 * @note If the check fails, the remaining of the test is aborted
535 #define ck_assert_int_lt(X, Y) _ck_assert_int(X, <, Y)
537 * Check two signed integers to determine if X<=Y
539 * If not X<=Y, the test fails.
541 * @param X signed integer
542 * @param Y signed integer to compare against X
544 * @note If the check fails, the remaining of the test is aborted
548 #define ck_assert_int_le(X, Y) _ck_assert_int(X, <=, Y)
550 * Check two signed integers to determine if X>Y
552 * If not X>Y, the test fails.
554 * @param X signed integer
555 * @param Y signed integer to compare against X
557 * @note If the check fails, the remaining of the test is aborted
561 #define ck_assert_int_gt(X, Y) _ck_assert_int(X, >, Y)
563 * Check two signed integers to determine if X>=Y
565 * If not X>=Y, the test fails.
567 * @param X signed integer
568 * @param Y signed integer to compare against X
570 * @note If the check fails, the remaining of the test is aborted
574 #define ck_assert_int_ge(X, Y) _ck_assert_int(X, >=, Y)
576 #define _ck_assert_uint(X, OP, Y) do { \
577 guint64 _ck_x = (X); \
578 guint64 _ck_y = (Y); \
579 ck_assert_msg(_ck_x OP _ck_y, "Assertion '%s' failed: " \
580 "%s==%" G_GUINT64_FORMAT ", %s==%" G_GUINT64_FORMAT, #X#OP#Y, #X, _ck_x, #Y, _ck_y); \
583 * Check two unsigned integers to determine if X==Y
585 * If not X==Y, the test fails.
587 * @param X signed integer
588 * @param Y signed integer to compare against X
590 * @note If the check fails, the remaining of the test is aborted
594 #define ck_assert_uint_eq(X, Y) _ck_assert_uint(X, ==, Y)
596 * Check two unsigned integers to determine if X!=Y
598 * If not X!=Y, the test fails.
600 * @param X signed integer
601 * @param Y signed integer to compare against X
603 * @note If the check fails, the remaining of the test is aborted
607 #define ck_assert_uint_ne(X, Y) _ck_assert_uint(X, !=, Y)
609 * Check two unsigned integers to determine if X<Y
611 * If not X<Y, the test fails.
613 * @param X signed integer
614 * @param Y signed integer to compare against X
616 * @note If the check fails, the remaining of the test is aborted
620 #define ck_assert_uint_lt(X, Y) _ck_assert_uint(X, <, Y)
622 * Check two unsigned integers to determine if X<=Y
624 * If not X<=Y, the test fails.
626 * @param X signed integer
627 * @param Y signed integer to compare against X
629 * @note If the check fails, the remaining of the test is aborted
633 #define ck_assert_uint_le(X, Y) _ck_assert_uint(X, <=, Y)
635 * Check two unsigned integers to determine if X>Y
637 * If not X>Y, the test fails.
639 * @param X signed integer
640 * @param Y signed integer to compare against X
642 * @note If the check fails, the remaining of the test is aborted
646 #define ck_assert_uint_gt(X, Y) _ck_assert_uint(X, >, Y)
648 * Check two unsigned integers to determine if X>=Y
650 * If not X>=Y, the test fails.
652 * @param X signed integer
653 * @param Y signed integer to compare against X
655 * @note If the check fails, the remaining of the test is aborted
659 #define ck_assert_uint_ge(X, Y) _ck_assert_uint(X, >=, Y)
661 /* String comparison macros with improved output compared to ck_assert() */
662 /* OP might be any operator that can be used in '0 OP strcmp(X,Y)' comparison */
663 /* The x and y parameter swap in strcmp() is needed to handle >, >=, <, <= operators */
664 #define _ck_assert_str(X, OP, Y) do { \
665 const char* _ck_x = (X); \
666 const char* _ck_y = (Y); \
667 ck_assert_msg(0 OP strcmp(_ck_y, _ck_x), \
668 "Assertion '%s' failed: %s==\"%s\", %s==\"%s\"", #X#OP#Y, #X, _ck_x, #Y, _ck_y); \
671 * Check two strings to determine if 0==strcmp(X,Y)
673 * If not 0==strcmp(X,Y), the test fails.
676 * @param Y string to compare against X
678 * @note If the check fails, the remaining of the test is aborted
682 #define ck_assert_str_eq(X, Y) _ck_assert_str(X, ==, Y)
684 * Check two strings to determine if 0!=strcmp(X,Y)
686 * If not 0!=strcmp(X,Y), the test fails.
689 * @param Y string to compare against X
691 * @note If the check fails, the remaining of the test is aborted
695 #define ck_assert_str_ne(X, Y) _ck_assert_str(X, !=, Y)
697 * Check two strings to determine if 0<strcmp(X,Y), (e.g. strcmp(X,Y)>0)
699 * If not 0<strcmp(X,Y), the test fails.
702 * @param Y string to compare against X
704 * @note If the check fails, the remaining of the test is aborted
708 #define ck_assert_str_lt(X, Y) _ck_assert_str(X, <, Y)
710 * Check two strings to determine if 0<=strcmp(X,Y) (e.g. strcmp(X,Y)>=0)
712 * If not 0<=strcmp(X,Y), the test fails.
715 * @param Y string to compare against X
717 * @note If the check fails, the remaining of the test is aborted
721 #define ck_assert_str_le(X, Y) _ck_assert_str(X, <=, Y)
723 * Check two strings to determine if 0<strcmp(X,Y) (e.g. strcmp(X,Y)>0)
725 * If not 0<strcmp(X,Y), the test fails.
728 * @param Y string to compare against X
730 * @note If the check fails, the remaining of the test is aborted
734 #define ck_assert_str_gt(X, Y) _ck_assert_str(X, >, Y)
736 * Check two strings to determine if 0>=strcmp(X,Y) (e.g. strcmp(X,Y)<=0)
738 * If not 0>=strcmp(X,Y), the test fails.
741 * @param Y string to compare against X
743 * @note If the check fails, the remaining of the test is aborted
747 #define ck_assert_str_ge(X, Y) _ck_assert_str(X, >=, Y)
749 /* Pointer comparison macros with improved output compared to ck_assert(). */
750 /* OP may only be == or != */
751 #define _ck_assert_ptr(X, OP, Y) do { \
752 const void* _ck_x = (X); \
753 const void* _ck_y = (Y); \
754 ck_assert_msg(_ck_x OP _ck_y, "Assertion '%s' failed: %s==%#x, %s==%#x", #X#OP#Y, #X, _ck_x, #Y, _ck_y); \
758 * Check if two pointers are equal.
760 * If the two passed pointers are not equal, the test
764 * @param Y pointer to compare against X
766 * @note If the check fails, the remaining of the test is aborted
770 #define ck_assert_ptr_eq(X, Y) _ck_assert_ptr(X, ==, Y)
773 * Check if two pointers are not.
775 * If the two passed pointers are equal, the test fails.
778 * @param Y pointer to compare against X
782 #define ck_assert_ptr_ne(X, Y) _ck_assert_ptr(X, !=, Y)
785 * Mark the last point reached in a unit test.
787 * If the test throws a signal or exits, the location noted with the
788 * failure is the last location of a ck_assert*() or ck_abort() call.
789 * Use mark_point() to record intermediate locations (useful for tracking down
794 #define mark_point() _mark_point(__FILE__,__LINE__)
796 /* Non macro version of #mark_point */
797 CK_DLL_EXP void CK_EXPORT _mark_point (const char *file, int line);
800 * Enum describing the possible results of a test
804 CK_TEST_RESULT_INVALID, /**< Default value; should not encounter this */
805 CK_PASS, /**< Test passed */
806 CK_FAILURE, /**< Test completed but failed */
807 CK_ERROR /**< Test failed to complete
808 (unexpected signal or non-zero early exit) */
812 * Enum specifying the verbosity of output a SRunner should produce
816 CK_SILENT, /**< No output */
817 CK_MINIMAL, /**< Only summary output */
818 CK_NORMAL, /**< All failed tests */
819 CK_VERBOSE, /**< All tests */
820 CK_ENV, /**< Look at environment var CK_VERBOSITY
821 for what verbosity to use, which can be
822 either "silent", "minimal", "normal",
823 or "verbose". If the environment variable
824 is not set, then CK_NORMAL will be used.*/
826 CK_SUBUNIT, /**< Run as a subunit child process */
828 CK_LAST /**< Not a valid option */
832 * Holds state for a running of a test suite
834 typedef struct SRunner SRunner;
837 * Opaque type for a test failure
839 typedef struct TestResult TestResult;
842 * Enum representing the types of contexts for a test
846 CK_CTX_INVALID, /**< Default value; should not encounter this */
847 CK_CTX_SETUP, /**< Setup before a test */
848 CK_CTX_TEST, /**< Body of test itself */
849 CK_CTX_TEARDOWN /**< Teardown after a test */
853 * Retrieve type of result that the given test result represents.
855 * This is a member of test_result, and can represent a
856 * pass, failure, or error.
858 * @param tr test result to retrieve result from
860 * @return result of given test
864 CK_DLL_EXP int CK_EXPORT tr_rtype (TestResult * tr);
867 * Retrieve context in which the result occurred for the given test result.
869 * The types of contents include the test setup, teardown, or the
870 * body of the test itself.
872 * @param tr test result to retrieve context from
874 * @return context to which the given test result applies
878 CK_DLL_EXP enum ck_result_ctx CK_EXPORT tr_ctx (TestResult * tr);
881 * Retrieve failure message from test result, if applicable.
883 * @return pointer to a message, if one exists. NULL otherwise.
887 CK_DLL_EXP const char *CK_EXPORT tr_msg (TestResult * tr);
890 * Retrieve line number at which a failure occurred, if applicable.
892 * @return If the test resulted in a failure, returns the line number
893 * that the failure occurred on; otherwise returns -1.
897 CK_DLL_EXP int CK_EXPORT tr_lno (TestResult * tr);
900 * Retrieve file name at which a failure occurred, if applicable.
902 * @return If the test resulted in a failure, returns a string
903 * containing the name of the file where the failure
904 * occurred; otherwise returns NULL.
908 CK_DLL_EXP const char *CK_EXPORT tr_lfile (TestResult * tr);
911 * Retrieve test case name in which a failure occurred, if applicable.
913 * @return If the test resulted in a failure, returns a string
914 * containing the name of the test suite where the failure
915 * occurred; otherwise returns NULL.
919 CK_DLL_EXP const char *CK_EXPORT tr_tcname (TestResult * tr);
922 * Creates a suite runner for the given suite.
924 * Once created, additional suites can be added to the
925 * suite runner using srunner_add_suite(), and the suite runner can be
926 * run with srunner_run_all(). Once finished, the suite runner
927 * must be freed with srunner_free().
929 * @param s suite to generate a suite runner for
931 * @return suite runner for the given suite
935 CK_DLL_EXP SRunner *CK_EXPORT srunner_create (Suite * s);
938 * Add an additional suite to a suite runner.
940 * The first suite in a suite runner is always added in srunner_create().
941 * This call adds additional suites to a suite runner.
943 * @param sr suite runner to add the given suite
944 * @param s suite to add to the given suite runner
948 CK_DLL_EXP void CK_EXPORT srunner_add_suite (SRunner * sr, Suite * s);
951 * Frees a suite runner, including all contained suite and test cases.
953 * This call is responsible for freeing all resources related to a
954 * suite runner and all contained suites and test cases. Suite and
955 * test cases need not be freed individually, as this call handles that.
957 * @param sr suite runner to free
961 CK_DLL_EXP void CK_EXPORT srunner_free (SRunner * sr);
964 * Runs a suite runner and all contained suite, printing results to
965 * stdout as specified by the print_mode.
967 * In addition to running all suites, if the suite runner has been
968 * configured to output to a log, that is also performed.
970 * Note that if the CK_RUN_CASE, CK_RUN_SUITE, CK_INCLUDE_TAGS and/or
971 * CK_EXCLUDE_TAGS environment variables are defined, then only the
972 * named suites or test cases will run.
974 * @param sr suite runner to run all suites from
975 * @param print_mode the verbosity in which to report results to stdout
979 CK_DLL_EXP void CK_EXPORT srunner_run_all (SRunner * sr,
980 enum print_output print_mode);
983 * Run a specific suite or test case from a suite runner, printing results
984 * to stdout as specified by the print_mode.
986 * In addition to running any applicable suites or test cases, if the
987 * suite runner has been configured to output to a log, that is also
990 * Note that if the sname and tcname parameters are passed as null
991 * then the function will fallback to using the environment variables
992 * CK_RUN_SUITE and CK_RUN_CASE respectively in order to select the
995 * Similarly if the CK_INCLUDE_TAGS and/or CK_EXCLUDE_TAGS environment
996 * variables are defined then these will further filter the test cases
997 * (see srunner_run_tagged, below).
999 * @param sr suite runner where the given suite or test case must be
1000 * @param sname suite name to run. A NULL means use the value of the
1001 * environment variable CK_RUN_SUITE if set, otherwise run "any/every
1003 * @param tcname test case name to run. A NULL means use the value of
1004 * the environment variable CK_RUN_CASE if set, otherwise run
1006 * @param print_mode the verbosity in which to report results to stdout
1010 CK_DLL_EXP void CK_EXPORT srunner_run (SRunner * sr, const char *sname,
1011 const char *tcname, enum print_output print_mode);
1015 * Run a specific suite or test case or testcases with specific tags
1016 * from a suite runner, printing results to stdout as specified by the
1019 * In addition to running any applicable suites or test cases, if the
1020 * suite runner has been configured to output to a log, that is also
1023 * Note that if sname, tcname, include_tags, exclude_tags parameters
1024 * are passed as NULL then if the environment variables CK_RUN_SUITE,
1025 * CK_RUN_CASE, CK_INCLUDE_TAGS, CK_EXCLUDE_TAGS are defined then these
1026 * values will be used instead.
1028 * @param sr suite runner where the given suite or test case must be
1029 * @param sname suite name to run. A NULL means use the value of the
1030 * environment variable CK_RUN_SUITE if set, otherwise run "any/every
1032 * @param tcname test case name to run. A NULL means use the value of
1033 * the environment variable CK_RUN_CASE if set, otherwise run
1035 * @param include_tags space separate list of tags. Only run test
1036 * cases that share one of these tags. A NULL means use the value of
1037 * the environment variable CK_INCLUDE_TAGS if set, otherwise run
1038 * "any/every test case".
1039 * @param exclude_tags space separate list of tags. Only run test
1040 * cases that do not share one of these tags even if they are selected
1041 * by an included tag. A NULL means use the value of the environment
1042 * variable CK_EXCLUDE_TAGS if set, otherwise run "any/every test
1044 * @param print_mode the verbosity in which to report results to stdout
1048 CK_DLL_EXP void CK_EXPORT srunner_run_tagged (SRunner * sr, const char *sname,
1050 const char *include_tags,
1051 const char *exclude_tags, enum print_output print_mode);
1054 * Retrieve the number of failed tests executed by a suite runner.
1056 * This value represents both test failures and errors.
1058 * @param sr suite runner to query for all failed tests
1060 * @return number of test failures and errors found by the suite runner
1064 CK_DLL_EXP int CK_EXPORT srunner_ntests_failed (SRunner * sr);
1067 * Retrieve the total number of tests run by a suite runner.
1069 * @param sr suite runner to query for all tests run
1071 * @return number of all tests run by the suite runner
1075 CK_DLL_EXP int CK_EXPORT srunner_ntests_run (SRunner * sr);
1078 * Return an array of results for all failures found by a suite runner.
1080 * Number of results is equal to srunner_nfailed_tests().
1082 * Information about individual results can be queried using:
1083 * tr_rtype(), tr_ctx(), tr_msg(), tr_lno(), tr_lfile(), and tr_tcname().
1085 * Memory is malloc'ed and must be freed; however free the entire structure
1086 * instead of individual test cases.
1088 * @param sr suite runner to retrieve results from
1090 * @return array of TestResult objects
1094 CK_DLL_EXP TestResult **CK_EXPORT srunner_failures (SRunner * sr);
1097 * Return an array of results for all tests run by a suite runner.
1099 * Number of results is equal to srunner_ntests_run(), and excludes
1100 * failures due to setup function failure.
1102 * Information about individual results can be queried using:
1103 * tr_rtype(), tr_ctx(), tr_msg(), tr_lno(), tr_lfile(), and tr_tcname().
1105 * Memory is malloc'ed and must be freed; however free the entire structure
1106 * instead of individual test cases.
1108 * @param sr suite runner to retrieve results from
1110 * @return array of TestResult objects
1114 CK_DLL_EXP TestResult **CK_EXPORT srunner_results (SRunner * sr);
1117 * Print the results contained in an SRunner to stdout.
1119 * @param sr suite runner to print results for to stdout
1120 * @param print_mode the print_output (verbosity) to use to report
1125 CK_DLL_EXP void CK_EXPORT srunner_print (SRunner * sr,
1126 enum print_output print_mode);
1129 * Set the suite runner to output the result in log format to the
1132 * Note: log file setting is an initialize only operation -- it should
1133 * be done immediately after SRunner creation, and the log file can't be
1134 * changed after being set.
1136 * This setting does not conflict with the other log output types;
1137 * all logging types can occur concurrently if configured.
1139 * @param sr suite runner to log results of in log format
1140 * @param fname file name to output log results to
1144 CK_DLL_EXP void CK_EXPORT srunner_set_log (SRunner * sr, const char *fname);
1147 * Checks if the suite runner is assigned a file for log output.
1149 * @param sr suite runner to check
1151 * @return 1 iff the suite runner currently is configured to output
1152 * in log format; 0 otherwise
1156 CK_DLL_EXP int CK_EXPORT srunner_has_log (SRunner * sr);
1159 * Retrieves the name of the currently assigned file
1160 * for log output, if any exists.
1162 * @return the name of the log file, or NULL if none is configured
1166 CK_DLL_EXP const char *CK_EXPORT srunner_log_fname (SRunner * sr);
1169 * Set the suite runner to output the result in XML format to the
1172 * Note: XML file setting is an initialize only operation -- it should
1173 * be done immediately after SRunner creation, and the XML file can't be
1174 * changed after being set.
1176 * This setting does not conflict with the other log output types;
1177 * all logging types can occur concurrently if configured.
1179 * @param sr suite runner to log results of in XML format
1180 * @param fname file name to output XML results to
1184 CK_DLL_EXP void CK_EXPORT srunner_set_xml (SRunner * sr, const char *fname);
1187 * Checks if the suite runner is assigned a file for XML output.
1189 * @param sr suite runner to check
1191 * @return 1 iff the suite runner currently is configured to output
1192 * in XML format; 0 otherwise
1196 CK_DLL_EXP int CK_EXPORT srunner_has_xml (SRunner * sr);
1199 * Retrieves the name of the currently assigned file
1200 * for XML output, if any exists.
1202 * @return the name of the XML file, or NULL if none is configured
1206 CK_DLL_EXP const char *CK_EXPORT srunner_xml_fname (SRunner * sr);
1209 * Set the suite runner to output the result in TAP format to the
1212 * Note: TAP file setting is an initialize only operation -- it should
1213 * be done immediately after SRunner creation, and the TAP file can't be
1214 * changed after being set.
1216 * This setting does not conflict with the other log output types;
1217 * all logging types can occur concurrently if configured.
1219 * @param sr suite runner to log results of in TAP format
1220 * @param fname file name to output TAP results to
1224 CK_DLL_EXP void CK_EXPORT srunner_set_tap (SRunner * sr, const char *fname);
1227 * Checks if the suite runner is assigned a file for TAP output.
1229 * @param sr suite runner to check
1231 * @return 1 iff the suite runner currently is configured to output
1232 * in TAP format; 0 otherwise
1236 CK_DLL_EXP int CK_EXPORT srunner_has_tap (SRunner * sr);
1239 * Retrieves the name of the currently assigned file
1240 * for TAP output, if any exists.
1242 * @return the name of the TAP file, or NULL if none is configured
1246 CK_DLL_EXP const char *CK_EXPORT srunner_tap_fname (SRunner * sr);
1249 * Enum describing the current fork usage.
1253 CK_FORK_GETENV, /**< look in the environment for CK_FORK */
1254 CK_FORK, /**< call fork to run tests */
1255 CK_NOFORK /**< don't call fork */
1259 * Retrieve the current fork status for the given suite runner
1261 * @param sr suite runner to check fork status of
1265 CK_DLL_EXP enum fork_status CK_EXPORT srunner_fork_status (SRunner * sr);
1268 * Set the fork status for a given suite runner.
1270 * The default fork status is CK_FORK_GETENV, which will look
1271 * for the CK_FORK environment variable, which can be set to
1272 * "yes" or "no". If the environment variable is not present,
1273 * CK_FORK will be used if fork() is available on the system,
1274 * otherwise CK_NOFORK is used.
1276 * If set to CK_FORK or CK_NOFORK, the environment variable
1277 * if defined is ignored.
1279 * If Check is compiled without support for fork(), attempting
1280 * to set the status to CK_FORK is ignored.
1282 * @param sr suite runner to assign the fork status to
1283 * @param fstat fork status to assign
1287 CK_DLL_EXP void CK_EXPORT srunner_set_fork_status (SRunner * sr,
1288 enum fork_status fstat);
1291 * Invoke fork() during a test and assign the child to the same
1292 * process group that the rest of the test case uses.
1294 * One can invoke fork() directly during a test; however doing so
1295 * may not guarantee that any children processes are destroyed once
1296 * the test finishes. Once a test has completed, all processes in
1297 * the process group will be killed; using this wrapper will prevent
1300 * If Check is compiled without fork() support this call simply
1301 * return -1 and does nothing.
1303 * @return On success, the PID of the child process is returned in
1304 * the parent, and 0 is returned in the child. On failure,
1305 * a value of -1 is returned to the parent process and no
1306 * child process is created.
1310 #if !defined(_MSC_VER)
1311 CK_DLL_EXP pid_t CK_EXPORT check_fork (void);
1315 * Wait for the pid and exit.
1317 * This is to be used in conjunction with check_fork(). When called,
1318 * will wait for the given process to terminate. If the process
1319 * exited without error, exit(EXIT_SUCCESS) is invoked; otherwise
1320 * exit(EXIT_FAILURE) is invoked.
1322 * If Check is compiled without support for fork(), this invokes
1323 * exit(EXIT_FAILURE).
1325 * @param pid process to wait for, created by check_fork()
1329 #if !defined(_MSC_VER)
1330 CK_DLL_EXP void CK_EXPORT
1331 check_waitpid_and_exit (pid_t pid)
1332 CK_ATTRIBUTE_NORETURN;
1338 #endif /* CHECK_H */