Check
Unit testing framework for C
check.h
Go to the documentation of this file.
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., 51 Franklin St, Fifth Floor, Boston,
19  * MA 02110-1301, USA.
20  */
21 
22 #ifndef CHECK_H
23 #define CHECK_H
24 
25 #include <stddef.h>
26 #include <string.h>
27 #include <math.h>
28 #include <float.h>
29 
30 #include <check_stdint.h>
31 
32 /*
33  Macros and functions starting with _ (underscore) are internal and
34  may change without notice. You have been warned!.
35 */
36 
37 
38 #ifdef __cplusplus
39 #define CK_CPPSTART extern "C" {
40 #define CK_CPPEND }
41 CK_CPPSTART
42 #endif
43 
52 #if defined(__GNUC__) && defined(__GNUC_MINOR__) && defined(__GNUC_PATCHLEVEL__)
53 #define GCC_VERSION_AT_LEAST(major, minor, patch) \
54 ((__GNUC__ > (major)) || \
55  (__GNUC__ == (major) && __GNUC_MINOR__ > (minor)) || \
56  (__GNUC__ == (major) && __GNUC_MINOR__ == (minor) && __GNUC_PATCHLEVEL__ >= (patch)) )
57 #elif defined(__GNUC__) && defined(__GNUC_MINOR__)
58 #define GCC_VERSION_AT_LEAST(major, minor, patch) \
59 ((__GNUC__ > (major)) || \
60  (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
61 #else
62 #define GCC_VERSION_AT_LEAST(major, minor, patch) 0
63 #endif
64 
65 #if GCC_VERSION_AT_LEAST(2,95,3)
66 #define CK_ATTRIBUTE_UNUSED __attribute__ ((unused))
67 #define CK_ATTRIBUTE_FORMAT(a, b, c) __attribute__ ((format (a, b, c)))
68 #else
69 #define CK_ATTRIBUTE_UNUSED
70 #define CK_ATTRIBUTE_FORMAT(a, b, c)
71 #endif /* GCC 2.95 */
72 
73 #if GCC_VERSION_AT_LEAST(2,5,0)
74 #define CK_ATTRIBUTE_NORETURN __attribute__ ((noreturn))
75 #else
76 #define CK_ATTRIBUTE_NORETURN
77 #endif /* GCC 2.5 */
78 
79 #if GCC_VERSION_AT_LEAST(4,7,4) && (__STDC_VERSION__ >= 199901L)
80 /* Operator _Pragma introduced in C99 */
81 #define CK_DIAGNOSTIC_STRINGIFY(x) #x
82 #define CK_DIAGNOSTIC_HELPER1(y) CK_DIAGNOSTIC_STRINGIFY(GCC diagnostic ignored y)
83 #define CK_DIAGNOSTIC_HELPER2(z) CK_DIAGNOSTIC_HELPER1(#z)
84 #define CK_DIAGNOSTIC_PUSH_IGNORE(w) \
85  _Pragma("GCC diagnostic push") \
86  _Pragma(CK_DIAGNOSTIC_HELPER2(w))
87 #define CK_DIAGNOSTIC_POP(w) _Pragma ("GCC diagnostic pop")
88 #else
89 #define CK_DIAGNOSTIC_PUSH_IGNORE(w)
90 #define CK_DIAGNOSTIC_POP(w)
91 #endif /* GCC 4.7.4 */
92 
93 #undef GCC_VERSION_AT_LEAST
94 
95 #include <sys/types.h>
96 
97 #if defined(_MSC_VER)
98 /* define pid_t for Windows, as it is needed later */
99 #define pid_t int
100 #endif /* _MSC_VER */
101 
102 /*
103  * Used to create the linker script for hiding lib-local symbols. Shall
104  * be put directly in front of the exported symbol.
105  */
106 #define CK_EXPORT
107 
108 /*
109  * Used for MSVC to create the export attribute
110  * CK_DLL_EXP is defined during the compilation of the library
111  * on the command line.
112  *
113  * This definition is only used when building or linking to
114  * the shared library, i.e. libcheck.so. When building the library
115  * the value must be "_declspec(dllexport)".
116  * When linking with the library, the value must be "_declspec(dllimport)"
117  *
118  * This is only used with Microsoft Visual C. In other systems
119  * the value is empty. In MSVC the value is empty when linking with
120  * a static library.
121  */
122 #ifndef CK_DLL_EXP
123 #define CK_DLL_EXP
124 #endif
125 
126 /* check version numbers */
127 
128 #define CHECK_MAJOR_VERSION (0)
129 #define CHECK_MINOR_VERSION (15)
130 #define CHECK_MICRO_VERSION (0)
131 
132 CK_DLL_EXP extern int CK_EXPORT check_major_version;
133 CK_DLL_EXP extern int CK_EXPORT check_minor_version;
134 CK_DLL_EXP extern int CK_EXPORT check_micro_version;
135 
136 #ifndef NULL
137 #define NULL ((void*)0)
138 #endif
139 
147 typedef struct TCase TCase;
148 
152 typedef void (*TFun) (int);
153 
157 typedef void (*SFun) (void);
158 
162 typedef struct Suite Suite;
163 
167 typedef struct TTest {
168  const char *name;
170  const char *file;
171  int line;
173 
188 CK_DLL_EXP Suite *CK_EXPORT suite_create(const char *name);
189 
202 CK_DLL_EXP int CK_EXPORT suite_tcase(Suite * s, const char *tcname);
203 
215 CK_DLL_EXP void CK_EXPORT suite_add_tcase(Suite * s, TCase * tc);
216 
230 CK_DLL_EXP TCase *CK_EXPORT tcase_create(const char *name);
231 
243 CK_DLL_EXP void CK_EXPORT tcase_set_tags(TCase * tc,
244  const char *tags);
253 #define tcase_add_test(tc,tf) tcase_add_test_raise_signal(tc,tf,0)
254 
267 #define tcase_add_test_raise_signal(tc,ttest,signal) \
268  _tcase_add_test((tc),(ttest),(signal), 0, 0, 1)
269 
282 #define tcase_add_exit_test(tc, ttest, expected_exit_value) \
283  _tcase_add_test((tc),(ttest),0,(expected_exit_value),0,1)
284 
299 #define tcase_add_loop_test(tc,ttest,s,e) \
300  _tcase_add_test((tc),(ttest),0,0,(s),(e))
301 
320 #define tcase_add_loop_test_raise_signal(tc,ttest,signal,s,e) \
321  _tcase_add_test((tc),(ttest),(signal),0,(s),(e))
322 
341 #define tcase_add_loop_exit_test(tc,ttest,expected_exit_value,s,e) \
342  _tcase_add_test((tc),(ttest),0,(expected_exit_value),(s),(e))
343 
344 /* Add a test function to a test case
345  (function version -- use this when the macro won't work
346 */
347 CK_DLL_EXP void CK_EXPORT _tcase_add_test(TCase * tc, const TTest * ttest,
348  int _signal, int allowed_exit_value,
349  int start, int end);
350 
376 CK_DLL_EXP void CK_EXPORT tcase_add_unchecked_fixture(TCase * tc, SFun setup,
377  SFun teardown);
378 
405 CK_DLL_EXP void CK_EXPORT tcase_add_checked_fixture(TCase * tc, SFun setup,
406  SFun teardown);
407 
428 CK_DLL_EXP void CK_EXPORT tcase_set_timeout(TCase * tc, double timeout);
429 
430 /* Internal function to mark the start of a test function */
431 CK_DLL_EXP void CK_EXPORT tcase_fn_start(const char *fname, const char *file,
432  int line);
433 
442 CK_DLL_EXP const char* CK_EXPORT tcase_name(void);
443 
451 #define START_TEST(__testname)\
452 static void __testname ## _fn (int _i CK_ATTRIBUTE_UNUSED);\
453 static const TTest __testname ## _ttest = {""# __testname, __testname ## _fn, __FILE__, __LINE__};\
454 static const TTest * __testname = & __testname ## _ttest;\
455 static void __testname ## _fn (int _i CK_ATTRIBUTE_UNUSED)
456 
462 #define END_TEST
463 
464 /*
465  * Fail the test case unless expr is false
466  *
467  * This call is deprecated.
468  */
469 #define fail_unless ck_assert_msg
470 
471 /*
472  * Fail the test case if expr is false
473  *
474  * This call is deprecated.
475  *
476  * NOTE: The space before the comma sign before ## is essential to be compatible
477  * with gcc 2.95.3 and earlier.
478  * FIXME: these macros may conflict with C89 if expr is
479  * FIXME: strcmp (str1, str2) due to excessive string length.
480  */
481 #define fail_if(expr, ...)\
482  (expr) ? \
483  _ck_assert_failed(__FILE__, __LINE__, "Failure '"#expr"' occurred" , ## __VA_ARGS__, NULL) \
484  : _mark_point(__FILE__, __LINE__)
485 
486 /*
487  * Fail the test
488  *
489  * This call is deprecated.
490  */
491 #define fail ck_abort_msg
492 
493 /*
494  * This is called whenever an assertion fails.
495  * Note that it only has the noreturn modifier when
496  * using fork. If fork is unavailable, the function
497  * calls longjmp() when a test assertion fails. Marking
498  * the function as noreturn causes gcc to make assumptions
499  * which are not valid, as longjmp() is like a return.
500  */
501 #if 1
502 CK_DLL_EXP void CK_EXPORT _ck_assert_failed(const char *file, int line,
503  const char *expr,
504  ...) CK_ATTRIBUTE_NORETURN CK_ATTRIBUTE_FORMAT(gnu_printf, 3, 4);
505 #else
506 CK_DLL_EXP void CK_EXPORT _ck_assert_failed(const char *file, int line,
507  const char *expr, ...) CK_ATTRIBUTE_FORMAT(gnu_printf, 3, 4);
508 #endif
509 
519 #define ck_assert(expr) ck_assert_msg(expr, NULL)
520 
521 /* The space before the comma sign before ## is essential to be compatible
522  with gcc 2.95.3 and earlier.
523 */
534 #define ck_assert_msg(expr, ...) \
535  (expr) ? \
536  _mark_point(__FILE__, __LINE__) : \
537  _ck_assert_failed(__FILE__, __LINE__, "Assertion '"#expr"' failed" , ## __VA_ARGS__, NULL)
538 
546 #define ck_abort() ck_abort_msg(NULL)
547 
556 #define ck_abort_msg(...) _ck_assert_failed(__FILE__, __LINE__, "Failed" , ## __VA_ARGS__, NULL)
557 
558 /* Signed and unsigned integer comparison macros with improved output compared to ck_assert(). */
559 /* OP may be any comparison operator. */
560 #define _ck_assert_int(X, OP, Y) do { \
561  intmax_t _ck_x = (X); \
562  intmax_t _ck_y = (Y); \
563  ck_assert_msg(_ck_x OP _ck_y, "Assertion '%s' failed: %s == %jd, %s == %jd", #X" "#OP" "#Y, #X, _ck_x, #Y, _ck_y); \
564 } while (0)
565 
578 #define ck_assert_int_eq(X, Y) _ck_assert_int(X, ==, Y)
579 
591 #define ck_assert_int_ne(X, Y) _ck_assert_int(X, !=, Y)
592 
604 #define ck_assert_int_lt(X, Y) _ck_assert_int(X, <, Y)
605 
617 #define ck_assert_int_le(X, Y) _ck_assert_int(X, <=, Y)
618 
630 #define ck_assert_int_gt(X, Y) _ck_assert_int(X, >, Y)
631 
643 #define ck_assert_int_ge(X, Y) _ck_assert_int(X, >=, Y)
644 
645 #define _ck_assert_uint(X, OP, Y) do { \
646  uintmax_t _ck_x = (X); \
647  uintmax_t _ck_y = (Y); \
648  ck_assert_msg(_ck_x OP _ck_y, "Assertion '%s' failed: %s == %ju, %s == %ju", #X" "#OP" "#Y, #X, _ck_x, #Y, _ck_y); \
649 } while (0)
650 
662 #define ck_assert_uint_eq(X, Y) _ck_assert_uint(X, ==, Y)
663 
675 #define ck_assert_uint_ne(X, Y) _ck_assert_uint(X, !=, Y)
676 
688 #define ck_assert_uint_lt(X, Y) _ck_assert_uint(X, <, Y)
689 
701 #define ck_assert_uint_le(X, Y) _ck_assert_uint(X, <=, Y)
702 
714 #define ck_assert_uint_gt(X, Y) _ck_assert_uint(X, >, Y)
715 
727 #define ck_assert_uint_ge(X, Y) _ck_assert_uint(X, >=, Y)
728 
729 /* Number of digits after the decimal point to output via printf */
730 #ifndef CK_FLOATING_DIG
731 # define CK_FLOATING_DIG 6
732 #endif /* CK_FLOATING_DIG */
733 
734 /* Floating point number comparison macros with improved output
735  * compared to ck_assert(). */
736 /* OP may be any comparison operator, TP is type, TM is type modifier. */
737 #define _ck_assert_floating(X, OP, Y, TP, TM) do { \
738  TP _ck_x = (X); \
739  TP _ck_y = (Y); \
740  ck_assert_msg(_ck_x OP _ck_y, \
741  "Assertion '%s' failed: %s == %.*" TM "g, %s == %.*" TM "g", \
742  #X" "#OP" "#Y, \
743  #X, (int)CK_FLOATING_DIG, _ck_x, \
744  #Y, (int)CK_FLOATING_DIG, _ck_y); \
745 } while (0)
746 
747 /* Check floating point number is finise. */
748 /* TP is type, TM is type modifier. */
749 #define _ck_assert_floating_finite(X, TP, TM) \
750 do { \
751  TP _ck_x = (X); \
752  ck_assert_msg(isfinite(_ck_x), \
753  "Assertion '%s' failed: %s == %.*" TM "g", \
754  #X" is finite", \
755  #X, (int)CK_FLOATING_DIG, _ck_x); \
756 } while (0)
757 
758 /* Check floating point number is infinise. */
759 /* TP is type, TM is type modifier. */
760 #define _ck_assert_floating_infinite(X, TP, TM) \
761 do { \
762  TP _ck_x = (X); \
763  ck_assert_msg(isinf(_ck_x), \
764  "Assertion '%s' failed: %s == %.*" TM "g", \
765  #X" is infinite", \
766  #X, (int)CK_FLOATING_DIG, _ck_x); \
767 } while (0)
768 
769 /* Check floating point number is "Not a Number". */
770 /* TP is type, TM is type modifier. */
771 #define _ck_assert_floating_nan(X, TP, TM) \
772 do { \
773  TP _ck_x = (X); \
774  ck_assert_msg(isnan(_ck_x), \
775  "Assertion '%s' failed: %s == %.*" TM "g", \
776  #X" is NaN", \
777  #X, (int)CK_FLOATING_DIG, _ck_x); \
778 } while (0)
779 
780 /* Check floating point number is not "Not a Number". */
781 /* TP is type, TM is type modifier. */
782 #define _ck_assert_floating_nonnan(X, TP, TM) \
783 do { \
784  TP _ck_x = (X); \
785  ck_assert_msg(!isnan(_ck_x), \
786  "Assertion '%s' failed: %s == %.*" TM "g", \
787  #X" is not NaN", \
788  #X, (int)CK_FLOATING_DIG, _ck_x); \
789 } while (0)
790 
791 /* Floating point tolerance comparison macros with improved output
792  * compared to ck_assert(). */
793 /* OP, D can have values: >, -1; <, 1. */
794 #define _ck_assert_floating_op_tol(X, OP, Y, T, D, TP, TM) do { \
795  TP _ck_x = (X); \
796  TP _ck_y = (Y); \
797  TP _ck_t = (T); \
798  ck_assert_msg((_ck_x - _ck_y) OP _ck_t * (D), \
799  "Assertion '%s' failed: %s == %.*" TM "g, %s == %.*" TM "g, %s == %.*" TM "g", \
800  #X" "#OP"= "#Y", error < "#T, \
801  #X, (int)CK_FLOATING_DIG, _ck_x, \
802  #Y, (int)CK_FLOATING_DIG, _ck_y, \
803  #T, (int)CK_FLOATING_DIG, _ck_t); \
804 } while (0)
805 
806 /* Floating point tolerance comparison macros with improved output
807  * compared to ck_assert(). */
808 /* OP can have values: <; >=. */
809 #define _ck_assert_floating_absdiff_op_tol(X, Y, OP, T, TP, TM) \
810 do { \
811  TP _ck_x = (X); \
812  TP _ck_y = (Y); \
813  TP _ck_t = (T); \
814  ck_assert_msg(fabsl(_ck_y - _ck_x) OP _ck_t, \
815  "Assertion '%s' failed: %s == %.*" TM "g, %s == %.*" TM "g, %s == %.*" TM "g", \
816  "fabsl("#Y" - "#X") "#OP" "#T, \
817  #X, (int)CK_FLOATING_DIG, _ck_x, \
818  #Y, (int)CK_FLOATING_DIG, _ck_y, \
819  #T, (int)CK_FLOATING_DIG, _ck_t); \
820 } while (0)
821 
838 #define ck_assert_float_eq(X, Y) _ck_assert_floating(X, ==, Y, float, "")
839 
855 #define ck_assert_float_ne(X, Y) _ck_assert_floating(X, !=, Y, float, "")
856 
868 #define ck_assert_float_lt(X, Y) _ck_assert_floating(X, <, Y, float, "")
869 
881 #define ck_assert_float_le(X, Y) _ck_assert_floating(X, <=, Y, float, "")
882 
894 #define ck_assert_float_gt(X, Y) _ck_assert_floating(X, >, Y, float, "")
895 
907 #define ck_assert_float_ge(X, Y) _ck_assert_floating(X, >=, Y, float, "")
908 
923 #define ck_assert_float_eq_tol(X, Y, T) _ck_assert_floating_absdiff_op_tol(X, Y, <, T, float, "")
924 
939 #define ck_assert_float_ne_tol(X, Y, T) _ck_assert_floating_absdiff_op_tol(X, Y, >=, T, float, "")
940 
955 #define ck_assert_float_ge_tol(X, Y, T) _ck_assert_floating_op_tol(X, >, Y, T, -1, float, "")
956 
971 #define ck_assert_float_le_tol(X, Y, T) _ck_assert_floating_op_tol(X, <, Y, T, 1, float, "")
972 
985 #define ck_assert_float_finite(X) _ck_assert_floating_finite(X, float, "")
986 
999 #define ck_assert_float_infinite(X) _ck_assert_floating_infinite(X, float, "")
1013 #define ck_assert_float_nan(X) _ck_assert_floating_nan(X, float, "")
1027 #define ck_assert_float_nonnan(X) _ck_assert_floating_nonnan(X, float, "")
1045 #define ck_assert_double_eq(X, Y) _ck_assert_floating(X, ==, Y, double, "")
1062 #define ck_assert_double_ne(X, Y) _ck_assert_floating(X, !=, Y, double, "")
1075 #define ck_assert_double_lt(X, Y) _ck_assert_floating(X, <, Y, double, "")
1088 #define ck_assert_double_le(X, Y) _ck_assert_floating(X, <=, Y, double, "")
1101 #define ck_assert_double_gt(X, Y) _ck_assert_floating(X, >, Y, double, "")
1114 #define ck_assert_double_ge(X, Y) _ck_assert_floating(X, >=, Y, double, "")
1130 #define ck_assert_double_eq_tol(X, Y, T) _ck_assert_floating_absdiff_op_tol(X, Y, <, T, double, "")
1146 #define ck_assert_double_ne_tol(X, Y, T) _ck_assert_floating_absdiff_op_tol(X, Y, >=, T, double, "")
1162 #define ck_assert_double_ge_tol(X, Y, T) _ck_assert_floating_op_tol(X, >, Y, T, -1, double, "")
1178 #define ck_assert_double_le_tol(X, Y, T) _ck_assert_floating_op_tol(X, <, Y, T, 1, double, "")
1192 #define ck_assert_double_finite(X) _ck_assert_floating_finite(X, double, "")
1206 #define ck_assert_double_infinite(X) _ck_assert_floating_infinite(X, double, "")
1220 #define ck_assert_double_nan(X) _ck_assert_floating_nan(X, double, "")
1234 #define ck_assert_double_nonnan(X) _ck_assert_floating_nonnan(X, double, "")
1252 #define ck_assert_ldouble_eq(X, Y) _ck_assert_floating(X, ==, Y, long double, "L")
1269 #define ck_assert_ldouble_ne(X, Y) _ck_assert_floating(X, !=, Y, long double, "L")
1282 #define ck_assert_ldouble_lt(X, Y) _ck_assert_floating(X, <, Y, long double, "L")
1295 #define ck_assert_ldouble_le(X, Y) _ck_assert_floating(X, <=, Y, long double, "L")
1308 #define ck_assert_ldouble_gt(X, Y) _ck_assert_floating(X, >, Y, long double, "L")
1321 #define ck_assert_ldouble_ge(X, Y) _ck_assert_floating(X, >=, Y, long double, "L")
1337 #define ck_assert_ldouble_eq_tol(X, Y, T) _ck_assert_floating_absdiff_op_tol(X, Y, <, T, long double, "L")
1353 #define ck_assert_ldouble_ne_tol(X, Y, T) _ck_assert_floating_absdiff_op_tol(X, Y, >=, T, long double, "L")
1369 #define ck_assert_ldouble_ge_tol(X, Y, T) _ck_assert_floating_op_tol(X, >, Y, T, -1, long double, "L")
1385 #define ck_assert_ldouble_le_tol(X, Y, T) _ck_assert_floating_op_tol(X, <, Y, T, 1, long double, "L")
1399 #define ck_assert_ldouble_finite(X) _ck_assert_floating_finite(X, long double, "L")
1413 #define ck_assert_ldouble_infinite(X) _ck_assert_floating_infinite(X, long double, "L")
1427 #define ck_assert_ldouble_nan(X) _ck_assert_floating_nan(X, long double, "L")
1441 #define ck_assert_ldouble_nonnan(X) _ck_assert_floating_nonnan(X, long double, "L")
1443 /* String comparison macros with improved output compared to ck_assert() */
1444 /* OP might be any operator that can be used in '0 OP strcmp(X,Y)' comparison. */
1445 /* String pointer could be compared againts NULL with == (NULLEQ = 1) and != (NULLNE = 1) operators. */
1446 /* The x and y parameter swap in strcmp() is needed to handle >, >=, <, <= operators. */
1447 /* If the x or y parameter is NULL its value will be printed without quotes. */
1448 #define _ck_assert_str(X, OP, Y, NULLEQ, NULLNE) do { \
1449  const char* _ck_x = (X); \
1450  const char* _ck_y = (Y); \
1451  const char* _ck_x_s; \
1452  const char* _ck_y_s; \
1453  const char* _ck_x_q; \
1454  const char* _ck_y_q; \
1455  if (_ck_x != NULL) { \
1456  _ck_x_q = "\""; \
1457  _ck_x_s = _ck_x; \
1458  } else { \
1459  _ck_x_q = ""; \
1460  _ck_x_s = "(null)"; \
1461  } \
1462  if (_ck_y != NULL) { \
1463  _ck_y_q = "\""; \
1464  _ck_y_s = _ck_y; \
1465  } else { \
1466  _ck_y_q = ""; \
1467  _ck_y_s = "(null)"; \
1468  } \
1469  ck_assert_msg( \
1470  (NULLEQ && (_ck_x == NULL) && (_ck_y == NULL)) || \
1471  (NULLNE && ((_ck_x == NULL) || (_ck_y == NULL)) && (_ck_x != _ck_y)) || \
1472  ((_ck_x != NULL) && (_ck_y != NULL) && (0 OP strcmp(_ck_y, _ck_x))), \
1473  "Assertion '%s' failed: %s == %s%s%s, %s == %s%s%s", \
1474  #X" "#OP" "#Y, \
1475  #X, _ck_x_q, _ck_x_s, _ck_x_q, \
1476  #Y, _ck_y_q, _ck_y_s, _ck_y_q); \
1477 } while (0)
1478 
1492 #define ck_assert_str_eq(X, Y) _ck_assert_str(X, ==, Y, 0, 0)
1507 #define ck_assert_str_ne(X, Y) _ck_assert_str(X, !=, Y, 0, 0)
1522 #define ck_assert_str_lt(X, Y) _ck_assert_str(X, <, Y, 0, 0)
1537 #define ck_assert_str_le(X, Y) _ck_assert_str(X, <=, Y, 0, 0)
1552 #define ck_assert_str_gt(X, Y) _ck_assert_str(X, >, Y, 0, 0)
1567 #define ck_assert_str_ge(X, Y) _ck_assert_str(X, >=, Y, 0, 0)
1583 #define ck_assert_pstr_eq(X, Y) _ck_assert_str(X, ==, Y, 1, 0)
1599 #define ck_assert_pstr_ne(X, Y) _ck_assert_str(X, !=, Y, 0, 1)
1601 /* Memory location comparison macros with improved output compared to ck_assert() */
1602 /* OP might be any operator that can be used in '0 OP memcmp(X,Y,L)' comparison */
1603 /* The x and y parameter swap in memcmp() is needed to handle >, >=, <, <= operators */
1604 /* Output is limited to CK_MAX_ASSERT_MEM_PRINT_SIZE bytes */
1605 #ifndef CK_MAX_ASSERT_MEM_PRINT_SIZE
1606 #define CK_MAX_ASSERT_MEM_PRINT_SIZE 64
1607 #endif
1608 
1609 /* Memory location comparison macros with improved output compared to ck_assert() */
1610 /* OP might be any operator that can be used in '0 OP memcmp(X,Y,L)' comparison */
1611 /* The x and y parameter swap in memcmp() is needed to handle >, >=, <, <= operators */
1612 /* Output is limited to CK_MAX_ASSERT_MEM_PRINT_SIZE bytes */
1613 #ifndef CK_MAX_ASSERT_MEM_PRINT_SIZE
1614 #define CK_MAX_ASSERT_MEM_PRINT_SIZE 64
1615 #endif
1616 
1617 #define _ck_assert_mem(X, OP, Y, L) do { \
1618  const uint8_t* _ck_x = (const uint8_t*)(X); \
1619  const uint8_t* _ck_y = (const uint8_t*)(Y); \
1620  size_t _ck_l = (L); \
1621  char _ck_x_str[CK_MAX_ASSERT_MEM_PRINT_SIZE * 2 + 1]; \
1622  char _ck_y_str[CK_MAX_ASSERT_MEM_PRINT_SIZE * 2 + 1]; \
1623  static const char _ck_hexdigits[] = "0123456789abcdef"; \
1624  size_t _ck_i; \
1625  size_t _ck_maxl = (_ck_l > CK_MAX_ASSERT_MEM_PRINT_SIZE) ? CK_MAX_ASSERT_MEM_PRINT_SIZE : _ck_l; \
1626  for (_ck_i = 0; _ck_i < _ck_maxl; _ck_i++) { \
1627  _ck_x_str[_ck_i * 2 ] = _ck_hexdigits[(_ck_x[_ck_i] >> 4) & 0xF]; \
1628  _ck_y_str[_ck_i * 2 ] = _ck_hexdigits[(_ck_y[_ck_i] >> 4) & 0xF]; \
1629  _ck_x_str[_ck_i * 2 + 1] = _ck_hexdigits[_ck_x[_ck_i] & 0xF]; \
1630  _ck_y_str[_ck_i * 2 + 1] = _ck_hexdigits[_ck_y[_ck_i] & 0xF]; \
1631  } \
1632  _ck_x_str[_ck_i * 2] = 0; \
1633  _ck_y_str[_ck_i * 2] = 0; \
1634  if (_ck_maxl != _ck_l) { \
1635  _ck_x_str[_ck_i * 2 - 2] = '.'; \
1636  _ck_y_str[_ck_i * 2 - 2] = '.'; \
1637  _ck_x_str[_ck_i * 2 - 1] = '.'; \
1638  _ck_y_str[_ck_i * 2 - 1] = '.'; \
1639  } \
1640  ck_assert_msg(0 OP memcmp(_ck_y, _ck_x, _ck_l), \
1641  "Assertion '%s' failed: %s == \"%s\", %s == \"%s\"", #X" "#OP" "#Y, #X, _ck_x_str, #Y, _ck_y_str); \
1642 } while (0)
1643 
1655 #define ck_assert_mem_eq(X, Y, L) _ck_assert_mem(X, ==, Y, L)
1668 #define ck_assert_mem_ne(X, Y, L) _ck_assert_mem(X, !=, Y, L)
1681 #define ck_assert_mem_lt(X, Y, L) _ck_assert_mem(X, <, Y, L)
1694 #define ck_assert_mem_le(X, Y, L) _ck_assert_mem(X, <=, Y, L)
1707 #define ck_assert_mem_gt(X, Y, L) _ck_assert_mem(X, >, Y, L)
1720 #define ck_assert_mem_ge(X, Y, L) _ck_assert_mem(X, >=, Y, L)
1722 /* Pointer comparison macros with improved output compared to ck_assert(). */
1723 /* OP may only be == or != */
1724 #define _ck_assert_ptr(X, OP, Y) do { \
1725  const void* _ck_x = (X); \
1726  const void* _ck_y = (Y); \
1727  ck_assert_msg(_ck_x OP _ck_y, "Assertion '%s' failed: %s == %#x, %s == %#x", #X" "#OP" "#Y, #X, _ck_x, #Y, _ck_y); \
1728 } while (0)
1729 
1730 /* Pointer against NULL comparison macros with improved output
1731  * compared to ck_assert(). */
1732 /* OP may only be == or != */
1733 #define _ck_assert_ptr_null(X, OP) do { \
1734  const void* _ck_x = (X); \
1735  ck_assert_msg(_ck_x OP NULL, \
1736  "Assertion '%s' failed: %s == %#x", \
1737  #X" "#OP" NULL", \
1738  #X, _ck_x); \
1739 } while (0)
1740 
1754 #define ck_assert_ptr_eq(X, Y) _ck_assert_ptr(X, ==, Y)
1766 #define ck_assert_ptr_ne(X, Y) _ck_assert_ptr(X, !=, Y)
1779 #define ck_assert_ptr_null(X) _ck_assert_ptr_null(X, ==)
1792 #define ck_assert_ptr_nonnull(X) _ck_assert_ptr_null(X, !=)
1804 #define mark_point() _mark_point(__FILE__,__LINE__)
1806 /* Non macro version of #mark_point */
1807 CK_DLL_EXP void CK_EXPORT _mark_point(const char *file, int line);
1808 
1813 {
1817  CK_ERROR
1819 };
1820 
1825 {
1830  CK_ENV,
1835 #if 0
1836  CK_SUBUNIT,
1837 #endif
1838  CK_LAST
1839 };
1840 
1844 typedef struct SRunner SRunner;
1845 
1849 typedef struct TestResult TestResult;
1850 
1855 {
1860 };
1861 
1874 CK_DLL_EXP int CK_EXPORT tr_rtype(TestResult * tr);
1875 
1888 CK_DLL_EXP enum ck_result_ctx CK_EXPORT tr_ctx(TestResult * tr);
1889 
1897 CK_DLL_EXP const char *CK_EXPORT tr_msg(TestResult * tr);
1898 
1907 CK_DLL_EXP int CK_EXPORT tr_lno(TestResult * tr);
1908 
1918 CK_DLL_EXP const char *CK_EXPORT tr_lfile(TestResult * tr);
1919 
1929 CK_DLL_EXP const char *CK_EXPORT tr_tcname(TestResult * tr);
1930 
1945 CK_DLL_EXP SRunner *CK_EXPORT srunner_create(Suite * s);
1946 
1958 CK_DLL_EXP void CK_EXPORT srunner_add_suite(SRunner * sr, Suite * s);
1959 
1971 CK_DLL_EXP void CK_EXPORT srunner_free(SRunner * sr);
1972 
1989 CK_DLL_EXP void CK_EXPORT srunner_run_all(SRunner * sr,
1990  enum print_output print_mode);
1991 
2020 CK_DLL_EXP void CK_EXPORT srunner_run(SRunner * sr, const char *sname,
2021  const char *tcname,
2022  enum print_output print_mode);
2023 
2024 
2059 CK_DLL_EXP void CK_EXPORT srunner_run_tagged(SRunner * sr, const char *sname,
2060  const char *tcname,
2061  const char *include_tags,
2062  const char *exclude_tags,
2063  enum print_output print_mode);
2064 
2076 CK_DLL_EXP int CK_EXPORT srunner_ntests_failed(SRunner * sr);
2077 
2087 CK_DLL_EXP int CK_EXPORT srunner_ntests_run(SRunner * sr);
2088 
2106 CK_DLL_EXP TestResult **CK_EXPORT srunner_failures(SRunner * sr);
2107 
2126 CK_DLL_EXP TestResult **CK_EXPORT srunner_results(SRunner * sr);
2127 
2137 CK_DLL_EXP void CK_EXPORT srunner_print(SRunner * sr,
2138  enum print_output print_mode);
2139 
2156 CK_DLL_EXP void CK_EXPORT srunner_set_log(SRunner * sr, const char *fname);
2157 
2168 CK_DLL_EXP int CK_EXPORT srunner_has_log(SRunner * sr);
2169 
2178 CK_DLL_EXP const char *CK_EXPORT srunner_log_fname(SRunner * sr);
2179 
2196 CK_DLL_EXP void CK_EXPORT srunner_set_xml(SRunner * sr, const char *fname);
2197 
2208 CK_DLL_EXP int CK_EXPORT srunner_has_xml(SRunner * sr);
2209 
2218 CK_DLL_EXP const char *CK_EXPORT srunner_xml_fname(SRunner * sr);
2219 
2236 CK_DLL_EXP void CK_EXPORT srunner_set_tap(SRunner * sr, const char *fname);
2237 
2248 CK_DLL_EXP int CK_EXPORT srunner_has_tap(SRunner * sr);
2249 
2258 CK_DLL_EXP const char *CK_EXPORT srunner_tap_fname(SRunner * sr);
2259 
2264 {
2267  CK_NOFORK
2268 };
2269 
2277 CK_DLL_EXP enum fork_status CK_EXPORT srunner_fork_status(SRunner * sr);
2278 
2299 CK_DLL_EXP void CK_EXPORT srunner_set_fork_status(SRunner * sr,
2300  enum fork_status fstat);
2301 
2322 CK_DLL_EXP pid_t CK_EXPORT check_fork(void);
2323 
2339 CK_DLL_EXP void CK_EXPORT check_waitpid_and_exit(pid_t pid) CK_ATTRIBUTE_NORETURN;
2340 
2356 CK_DLL_EXP void CK_EXPORT check_set_max_msg_size(size_t max_msg_size);
2357 
2358 #ifdef __cplusplus
2359 CK_CPPEND
2360 #endif
2361 
2362 #endif /* CHECK_H */
fork_status
fork_status
Definition: check.h:2264
CK_LAST
@ CK_LAST
Definition: check.h:1838
check_fork
CK_DLL_EXP pid_t CK_EXPORT check_fork(void)
srunner_tap_fname
CK_DLL_EXP const char *CK_EXPORT srunner_tap_fname(SRunner *sr)
TTest::fn
TFun fn
Definition: check.h:169
tcase_name
CK_DLL_EXP const char *CK_EXPORT tcase_name(void)
srunner_set_fork_status
CK_DLL_EXP void CK_EXPORT srunner_set_fork_status(SRunner *sr, enum fork_status fstat)
print_output
print_output
Definition: check.h:1825
CK_ENV
@ CK_ENV
Definition: check.h:1830
CK_VERBOSE
@ CK_VERBOSE
Definition: check.h:1829
suite_add_tcase
CK_DLL_EXP void CK_EXPORT suite_add_tcase(Suite *s, TCase *tc)
CK_SILENT
@ CK_SILENT
Definition: check.h:1826
tr_msg
CK_DLL_EXP const char *CK_EXPORT tr_msg(TestResult *tr)
CK_PASS
@ CK_PASS
Definition: check.h:1815
check_set_max_msg_size
CK_DLL_EXP void CK_EXPORT check_set_max_msg_size(size_t max_msg_size)
suite_tcase
CK_DLL_EXP int CK_EXPORT suite_tcase(Suite *s, const char *tcname)
tr_rtype
CK_DLL_EXP int CK_EXPORT tr_rtype(TestResult *tr)
tr_lno
CK_DLL_EXP int CK_EXPORT tr_lno(TestResult *tr)
SFun
void(* SFun)(void)
Definition: check.h:157
test_result
test_result
Definition: check.h:1813
srunner_log_fname
CK_DLL_EXP const char *CK_EXPORT srunner_log_fname(SRunner *sr)
srunner_run
CK_DLL_EXP void CK_EXPORT srunner_run(SRunner *sr, const char *sname, const char *tcname, enum print_output print_mode)
srunner_run_tagged
CK_DLL_EXP void CK_EXPORT srunner_run_tagged(SRunner *sr, const char *sname, const char *tcname, const char *include_tags, const char *exclude_tags, enum print_output print_mode)
srunner_set_log
CK_DLL_EXP void CK_EXPORT srunner_set_log(SRunner *sr, const char *fname)
srunner_ntests_failed
CK_DLL_EXP int CK_EXPORT srunner_ntests_failed(SRunner *sr)
suite_create
CK_DLL_EXP Suite *CK_EXPORT suite_create(const char *name)
CK_CTX_TEST
@ CK_CTX_TEST
Definition: check.h:1858
CK_FORK
@ CK_FORK
Definition: check.h:2266
CK_FAILURE
@ CK_FAILURE
Definition: check.h:1816
srunner_ntests_run
CK_DLL_EXP int CK_EXPORT srunner_ntests_run(SRunner *sr)
TTest::file
const char * file
Definition: check.h:170
CK_NORMAL
@ CK_NORMAL
Definition: check.h:1828
tr_lfile
CK_DLL_EXP const char *CK_EXPORT tr_lfile(TestResult *tr)
CK_CTX_SETUP
@ CK_CTX_SETUP
Definition: check.h:1857
srunner_print
CK_DLL_EXP void CK_EXPORT srunner_print(SRunner *sr, enum print_output print_mode)
check_waitpid_and_exit
CK_DLL_EXP void CK_EXPORT check_waitpid_and_exit(pid_t pid) CK_ATTRIBUTE_NORETURN
SRunner
struct SRunner SRunner
Definition: check.h:1844
srunner_results
CK_DLL_EXP TestResult **CK_EXPORT srunner_results(SRunner *sr)
TCase
struct TCase TCase
Definition: check.h:147
Suite
struct Suite Suite
Definition: check.h:162
srunner_xml_fname
CK_DLL_EXP const char *CK_EXPORT srunner_xml_fname(SRunner *sr)
srunner_fork_status
CK_DLL_EXP enum fork_status CK_EXPORT srunner_fork_status(SRunner *sr)
tcase_add_checked_fixture
CK_DLL_EXP void CK_EXPORT tcase_add_checked_fixture(TCase *tc, SFun setup, SFun teardown)
CK_FORK_GETENV
@ CK_FORK_GETENV
Definition: check.h:2265
srunner_free
CK_DLL_EXP void CK_EXPORT srunner_free(SRunner *sr)
CK_TEST_RESULT_INVALID
@ CK_TEST_RESULT_INVALID
Definition: check.h:1814
srunner_add_suite
CK_DLL_EXP void CK_EXPORT srunner_add_suite(SRunner *sr, Suite *s)
tr_tcname
CK_DLL_EXP const char *CK_EXPORT tr_tcname(TestResult *tr)
srunner_set_xml
CK_DLL_EXP void CK_EXPORT srunner_set_xml(SRunner *sr, const char *fname)
TTest
Definition: check.h:167
TTest::line
int line
Definition: check.h:171
srunner_has_tap
CK_DLL_EXP int CK_EXPORT srunner_has_tap(SRunner *sr)
CK_NOFORK
@ CK_NOFORK
Definition: check.h:2267
srunner_set_tap
CK_DLL_EXP void CK_EXPORT srunner_set_tap(SRunner *sr, const char *fname)
TTest
struct TTest TTest
CK_CTX_TEARDOWN
@ CK_CTX_TEARDOWN
Definition: check.h:1859
ck_result_ctx
ck_result_ctx
Definition: check.h:1855
CK_CTX_INVALID
@ CK_CTX_INVALID
Definition: check.h:1856
tcase_fn_start
CK_DLL_EXP void CK_EXPORT tcase_fn_start(const char *fname, const char *file, int line)
tcase_create
CK_DLL_EXP TCase *CK_EXPORT tcase_create(const char *name)
srunner_has_log
CK_DLL_EXP int CK_EXPORT srunner_has_log(SRunner *sr)
tr_ctx
CK_DLL_EXP enum ck_result_ctx CK_EXPORT tr_ctx(TestResult *tr)
srunner_create
CK_DLL_EXP SRunner *CK_EXPORT srunner_create(Suite *s)
tcase_add_unchecked_fixture
CK_DLL_EXP void CK_EXPORT tcase_add_unchecked_fixture(TCase *tc, SFun setup, SFun teardown)
srunner_failures
CK_DLL_EXP TestResult **CK_EXPORT srunner_failures(SRunner *sr)
srunner_has_xml
CK_DLL_EXP int CK_EXPORT srunner_has_xml(SRunner *sr)
TTest::name
const char * name
Definition: check.h:168
CK_ERROR
@ CK_ERROR
Definition: check.h:1817
CK_MINIMAL
@ CK_MINIMAL
Definition: check.h:1827
tcase_set_timeout
CK_DLL_EXP void CK_EXPORT tcase_set_timeout(TCase *tc, double timeout)
srunner_run_all
CK_DLL_EXP void CK_EXPORT srunner_run_all(SRunner *sr, enum print_output print_mode)
TestResult
struct TestResult TestResult
Definition: check.h:1849
tcase_set_tags
CK_DLL_EXP void CK_EXPORT tcase_set_tags(TCase *tc, const char *tags)
TFun
void(* TFun)(int)
Definition: check.h:152