Check: check.h Source File

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 #else
68 #define CK_ATTRIBUTE_UNUSED
69 #endif /* GCC 2.95 */
70
71 #if GCC_VERSION_AT_LEAST(2,5,0)
72 #define CK_ATTRIBUTE_NORETURN __attribute__ ((noreturn))
73 #else
74 #define CK_ATTRIBUTE_NORETURN
75 #endif /* GCC 2.5 */
76
77 #undef GCC_VERSION_AT_LEAST
78
79 #include <sys/types.h>
80
81 #if defined(_MSC_VER)
82 /* define pid_t for Windows, as it is needed later */
83 #define pid_t int
84 #endif /* _MSC_VER */
85
86 /*
87 * Used to create the linker script for hiding lib-local symbols. Shall
88 * be put directly in front of the exported symbol.
89 */
90 #define CK_EXPORT
91
92 /*
93 * Used for MSVC to create the export attribute
94 * CK_DLL_EXP is defined during the compilation of the library
95 * on the command line.
96 *
97 * This definition is only used when building or linking to
98 * the shared library, i.e. libcheck.so. When building the library
99 * the value must be "_declspec(dllexport)".
100 * When linking with the library, the value must be "_declspec(dllimport)"
101 *
102 * This is only used with Microsoft Visual C. In other systems
103 * the value is empty. In MSVC the value is empty when linking with
104 * a static library.
105 */
106 #ifndef CK_DLL_EXP
107 #define CK_DLL_EXP
108 #endif
109
110 /* check version numbers */
111
112 #define CHECK_MAJOR_VERSION (0)
113 #define CHECK_MINOR_VERSION (14)
114 #define CHECK_MICRO_VERSION (0)
115
116 CK_DLL_EXP extern int CK_EXPORT check_major_version;
117 CK_DLL_EXP extern int CK_EXPORT check_minor_version;
118 CK_DLL_EXP extern int CK_EXPORT check_micro_version;
119
120 #ifndef NULL
121 #define NULL ((void*)0)
122 #endif
123
131 typedef struct TCase TCase;
132
136 typedef void (*TFun) (int);
137
141 typedef void (*SFun) (void);
142
146 typedef struct Suite Suite;
147
151 typedef struct TTest {
152 const char *name;
153 TFun fn;
154 const char *file;
155 int line;
156 } TTest;
157
172 CK_DLL_EXP Suite *CK_EXPORT suite_create(const char *name);
173
186 CK_DLL_EXP int CK_EXPORT suite_tcase(Suite * s, const char *tcname);
187
199 CK_DLL_EXP void CK_EXPORT suite_add_tcase(Suite * s, TCase * tc);
200
214 CK_DLL_EXP TCase *CK_EXPORT tcase_create(const char *name);
215
227 CK_DLL_EXP void CK_EXPORT tcase_set_tags(TCase * tc,
228 const char *tags);
237 #define tcase_add_test(tc,tf) tcase_add_test_raise_signal(tc,tf,0)
238
251 #define tcase_add_test_raise_signal(tc,ttest,signal) \
252 _tcase_add_test((tc),(ttest),(signal), 0, 0, 1)
253
266 #define tcase_add_exit_test(tc, ttest, expected_exit_value) \
267 _tcase_add_test((tc),(ttest),0,(expected_exit_value),0,1)
268
283 #define tcase_add_loop_test(tc,ttest,s,e) \
284 _tcase_add_test((tc),(ttest),0,0,(s),(e))
285
304 #define tcase_add_loop_test_raise_signal(tc,ttest,signal,s,e) \
305 _tcase_add_test((tc),(ttest),(signal),0,(s),(e))
306
325 #define tcase_add_loop_exit_test(tc,ttest,expected_exit_value,s,e) \
326 _tcase_add_test((tc),(ttest),0,(expected_exit_value),(s),(e))
327
328 /* Add a test function to a test case
329 (function version -- use this when the macro won't work
330 */
331 CK_DLL_EXP void CK_EXPORT _tcase_add_test(TCase * tc, const TTest * ttest,
332 int _signal, int allowed_exit_value,
333 int start, int end);
334
360 CK_DLL_EXP void CK_EXPORT tcase_add_unchecked_fixture(TCase * tc, SFun setup,
361 SFun teardown);
362
389 CK_DLL_EXP void CK_EXPORT tcase_add_checked_fixture(TCase * tc, SFun setup,
390 SFun teardown);
391
412 CK_DLL_EXP void CK_EXPORT tcase_set_timeout(TCase * tc, double timeout);
413
414 /* Internal function to mark the start of a test function */
415 CK_DLL_EXP void CK_EXPORT tcase_fn_start(const char *fname, const char *file,
416 int line);
417
426 CK_DLL_EXP const char* CK_EXPORT tcase_name(void);
427
435 #define START_TEST(__testname)\
436 static void __testname ## _fn (int _i CK_ATTRIBUTE_UNUSED);\
437 static const TTest __testname ## _ttest = {""# __testname, __testname ## _fn, __FILE__, __LINE__};\
438 static const TTest * __testname = & __testname ## _ttest;\
439 static void __testname ## _fn (int _i CK_ATTRIBUTE_UNUSED)
440
446 #define END_TEST
447
448 /*
449 * Fail the test case unless expr is false
450 *
451 * This call is deprecated.
452 */
453 #define fail_unless ck_assert_msg
454
455 /*
456 * Fail the test case if expr is false
457 *
458 * This call is deprecated.
459 *
460 * NOTE: The space before the comma sign before ## is essential to be compatible
461 * with gcc 2.95.3 and earlier.
462 * FIXME: these macros may conflict with C89 if expr is
463 * FIXME: strcmp (str1, str2) due to excessive string length.
464 */
465 #define fail_if(expr, ...)\
466 (expr) ? \
467 _ck_assert_failed(__FILE__, __LINE__, "Failure '"#expr"' occurred" , ## __VA_ARGS__, NULL) \
468 : _mark_point(__FILE__, __LINE__)
469
470 /*
471 * Fail the test
472 *
473 * This call is deprecated.
474 */
475 #define fail ck_abort_msg
476
477 /*
478 * This is called whenever an assertion fails.
479 * Note that it only has the noreturn modifier when
480 * using fork. If fork is unavailable, the function
481 * calls longjmp() when a test assertion fails. Marking
482 * the function as noreturn causes gcc to make assumptions
483 * which are not valid, as longjmp() is like a return.
484 */
485 #if 1
486 CK_DLL_EXP void CK_EXPORT _ck_assert_failed(const char *file, int line,
487 const char *expr,
488 ...) CK_ATTRIBUTE_NORETURN;
489 #else
490 CK_DLL_EXP void CK_EXPORT _ck_assert_failed(const char *file, int line,
491 const char *expr, ...);
492 #endif
493
503 #define ck_assert(expr) ck_assert_msg(expr, NULL)
504
505 /* The space before the comma sign before ## is essential to be compatible
506 with gcc 2.95.3 and earlier.
507 */
518 #define ck_assert_msg(expr, ...) \
519 (expr) ? \
520 _mark_point(__FILE__, __LINE__) : \
521 _ck_assert_failed(__FILE__, __LINE__, "Assertion '"#expr"' failed" , ## __VA_ARGS__, NULL)
522
530 #define ck_abort() ck_abort_msg(NULL)
531
540 #define ck_abort_msg(...) _ck_assert_failed(__FILE__, __LINE__, "Failed" , ## __VA_ARGS__, NULL)
541
542 /* Signed and unsigned integer comparison macros with improved output compared to ck_assert(). */
543 /* OP may be any comparison operator. */
544 #define _ck_assert_int(X, OP, Y) do { \
545 intmax_t _ck_x = (X); \
546 intmax_t _ck_y = (Y); \
547 ck_assert_msg(_ck_x OP _ck_y, "Assertion '%s' failed: %s == %jd, %s == %jd", #X" "#OP" "#Y, #X, _ck_x, #Y, _ck_y); \
548 } while (0)
549
562 #define ck_assert_int_eq(X, Y) _ck_assert_int(X, ==, Y)
563
575 #define ck_assert_int_ne(X, Y) _ck_assert_int(X, !=, Y)
576
588 #define ck_assert_int_lt(X, Y) _ck_assert_int(X, <, Y)
589
601 #define ck_assert_int_le(X, Y) _ck_assert_int(X, <=, Y)
602
614 #define ck_assert_int_gt(X, Y) _ck_assert_int(X, >, Y)
615
627 #define ck_assert_int_ge(X, Y) _ck_assert_int(X, >=, Y)
628
629 #define _ck_assert_uint(X, OP, Y) do { \
630 uintmax_t _ck_x = (X); \
631 uintmax_t _ck_y = (Y); \
632 ck_assert_msg(_ck_x OP _ck_y, "Assertion '%s' failed: %s == %ju, %s == %ju", #X" "#OP" "#Y, #X, _ck_x, #Y, _ck_y); \
633 } while (0)
634
646 #define ck_assert_uint_eq(X, Y) _ck_assert_uint(X, ==, Y)
647
659 #define ck_assert_uint_ne(X, Y) _ck_assert_uint(X, !=, Y)
660
672 #define ck_assert_uint_lt(X, Y) _ck_assert_uint(X, <, Y)
673
685 #define ck_assert_uint_le(X, Y) _ck_assert_uint(X, <=, Y)
686
698 #define ck_assert_uint_gt(X, Y) _ck_assert_uint(X, >, Y)
699
711 #define ck_assert_uint_ge(X, Y) _ck_assert_uint(X, >=, Y)
712
713 /* Number of digits after the decimal point to output via printf */
714 #ifndef CK_FLOATING_DIG
715 # define CK_FLOATING_DIG 6
716 #endif /* CK_FLOATING_DIG */
717
718 /* Floating point number comparison macros with improved output
719 * compared to ck_assert(). */
720 /* OP may be any comparison operator, TP is type, TM is type modifier. */
721 #define _ck_assert_floating(X, OP, Y, TP, TM) do { \
722 TP _ck_x = (X); \
723 TP _ck_y = (Y); \
724 ck_assert_msg(_ck_x OP _ck_y, \
725 "Assertion '%s' failed: %s == %.*" TM "g, %s == %.*" TM "g", \
726 #X" "#OP" "#Y, \
727 #X, (int)CK_FLOATING_DIG, _ck_x, \
728 #Y, (int)CK_FLOATING_DIG, _ck_y); \
729 } while (0)
730
731 /* Check floating point number is finise. */
732 /* TP is type, TM is type modifier. */
733 #define _ck_assert_floating_finite(X, TP, TM) \
734 do { \
735 TP _ck_x = (X); \
736 ck_assert_msg(isfinite(_ck_x), \
737 "Assertion '%s' failed: %s == %.*" TM "g", \
738 #X" is finite", \
739 #X, (int)CK_FLOATING_DIG, _ck_x); \
740 } while (0)
741
742 /* Check floating point number is infinise. */
743 /* TP is type, TM is type modifier. */
744 #define _ck_assert_floating_infinite(X, TP, TM) \
745 do { \
746 TP _ck_x = (X); \
747 ck_assert_msg(isinf(_ck_x), \
748 "Assertion '%s' failed: %s == %.*" TM "g", \
749 #X" is infinite", \
750 #X, (int)CK_FLOATING_DIG, _ck_x); \
751 } while (0)
752
753 /* Check floating point number is "Not a Number". */
754 /* TP is type, TM is type modifier. */
755 #define _ck_assert_floating_nan(X, TP, TM) \
756 do { \
757 TP _ck_x = (X); \
758 ck_assert_msg(isnan(_ck_x), \
759 "Assertion '%s' failed: %s == %.*" TM "g", \
760 #X" is NaN", \
761 #X, (int)CK_FLOATING_DIG, _ck_x); \
762 } while (0)
763
764 /* Check floating point number is not "Not a Number". */
765 /* TP is type, TM is type modifier. */
766 #define _ck_assert_floating_nonnan(X, TP, TM) \
767 do { \
768 TP _ck_x = (X); \
769 ck_assert_msg(!isnan(_ck_x), \
770 "Assertion '%s' failed: %s == %.*" TM "g", \
771 #X" is not NaN", \
772 #X, (int)CK_FLOATING_DIG, _ck_x); \
773 } while (0)
774
775 /* Floating point tolerance comparison macros with improved output
776 * compared to ck_assert(). */
777 /* OP, D can have values: >, -1; <, 1. */
778 #define _ck_assert_floating_op_tol(X, OP, Y, T, D, TP, TM) do { \
779 TP _ck_x = (X); \
780 TP _ck_y = (Y); \
781 TP _ck_t = (T); \
782 ck_assert_msg((_ck_x - _ck_y) OP _ck_t * (D), \
783 "Assertion '%s' failed: %s == %.*" TM "g, %s == %.*" TM "g, %s == %.*" TM "g", \
784 #X" "#OP"= "#Y", error < "#T, \
785 #X, (int)CK_FLOATING_DIG, _ck_x, \
786 #Y, (int)CK_FLOATING_DIG, _ck_y, \
787 #T, (int)CK_FLOATING_DIG, _ck_t); \
788 } while (0)
789
790 /* Floating point tolerance comparison macros with improved output
791 * compared to ck_assert(). */
792 /* OP can have values: <; >=. */
793 #define _ck_assert_floating_absdiff_op_tol(X, Y, OP, T, TP, TM) \
794 do { \
795 TP _ck_x = (X); \
796 TP _ck_y = (Y); \
797 TP _ck_t = (T); \
798 ck_assert_msg(fabsl(_ck_y - _ck_x) OP _ck_t, \
799 "Assertion '%s' failed: %s == %.*" TM "g, %s == %.*" TM "g, %s == %.*" TM "g", \
800 "fabsl("#Y" - "#X") "#OP" "#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
822 #define ck_assert_float_eq(X, Y) _ck_assert_floating(X, ==, Y, float, "")
823
839 #define ck_assert_float_ne(X, Y) _ck_assert_floating(X, !=, Y, float, "")
840
852 #define ck_assert_float_lt(X, Y) _ck_assert_floating(X, <, Y, float, "")
853
865 #define ck_assert_float_le(X, Y) _ck_assert_floating(X, <=, Y, float, "")
866
878 #define ck_assert_float_gt(X, Y) _ck_assert_floating(X, >, Y, float, "")
879
891 #define ck_assert_float_ge(X, Y) _ck_assert_floating(X, >=, Y, float, "")
892
907 #define ck_assert_float_eq_tol(X, Y, T) _ck_assert_floating_absdiff_op_tol(X, Y, <, T, float, "")
908
923 #define ck_assert_float_ne_tol(X, Y, T) _ck_assert_floating_absdiff_op_tol(X, Y, >=, T, float, "")
924
939 #define ck_assert_float_ge_tol(X, Y, T) _ck_assert_floating_op_tol(X, >, Y, T, -1, float, "")
940
955 #define ck_assert_float_le_tol(X, Y, T) _ck_assert_floating_op_tol(X, <, Y, T, 1, float, "")
956
969 #define ck_assert_float_finite(X) _ck_assert_floating_finite(X, float, "")
970
983 #define ck_assert_float_infinite(X) _ck_assert_floating_infinite(X, float, "")
984
997 #define ck_assert_float_nan(X) _ck_assert_floating_nan(X, float, "")
998
1011 #define ck_assert_float_nonnan(X) _ck_assert_floating_nonnan(X, float, "")
1012
1029 #define ck_assert_double_eq(X, Y) _ck_assert_floating(X, ==, Y, double, "")
1030
1046 #define ck_assert_double_ne(X, Y) _ck_assert_floating(X, !=, Y, double, "")
1047
1059 #define ck_assert_double_lt(X, Y) _ck_assert_floating(X, <, Y, double, "")
1060
1072 #define ck_assert_double_le(X, Y) _ck_assert_floating(X, <=, Y, double, "")
1073
1085 #define ck_assert_double_gt(X, Y) _ck_assert_floating(X, >, Y, double, "")
1086
1098 #define ck_assert_double_ge(X, Y) _ck_assert_floating(X, >=, Y, double, "")
1099
1114 #define ck_assert_double_eq_tol(X, Y, T) _ck_assert_floating_absdiff_op_tol(X, Y, <, T, double, "")
1115
1130 #define ck_assert_double_ne_tol(X, Y, T) _ck_assert_floating_absdiff_op_tol(X, Y, >=, T, double, "")
1131
1146 #define ck_assert_double_ge_tol(X, Y, T) _ck_assert_floating_op_tol(X, >, Y, T, -1, double, "")
1147
1162 #define ck_assert_double_le_tol(X, Y, T) _ck_assert_floating_op_tol(X, <, Y, T, 1, double, "")
1163
1176 #define ck_assert_double_finite(X) _ck_assert_floating_finite(X, double, "")
1177
1190 #define ck_assert_double_infinite(X) _ck_assert_floating_infinite(X, double, "")
1191
1204 #define ck_assert_double_nan(X) _ck_assert_floating_nan(X, double, "")
1205
1218 #define ck_assert_double_nonnan(X) _ck_assert_floating_nonnan(X, double, "")
1219
1236 #define ck_assert_ldouble_eq(X, Y) _ck_assert_floating(X, ==, Y, long double, "L")
1237
1253 #define ck_assert_ldouble_ne(X, Y) _ck_assert_floating(X, !=, Y, long double, "L")
1254
1266 #define ck_assert_ldouble_lt(X, Y) _ck_assert_floating(X, <, Y, long double, "L")
1267
1279 #define ck_assert_ldouble_le(X, Y) _ck_assert_floating(X, <=, Y, long double, "L")
1280
1292 #define ck_assert_ldouble_gt(X, Y) _ck_assert_floating(X, >, Y, long double, "L")
1293
1305 #define ck_assert_ldouble_ge(X, Y) _ck_assert_floating(X, >=, Y, long double, "L")
1306
1321 #define ck_assert_ldouble_eq_tol(X, Y, T) _ck_assert_floating_absdiff_op_tol(X, Y, <, T, long double, "L")
1322
1337 #define ck_assert_ldouble_ne_tol(X, Y, T) _ck_assert_floating_absdiff_op_tol(X, Y, >=, T, long double, "L")
1338
1353 #define ck_assert_ldouble_ge_tol(X, Y, T) _ck_assert_floating_op_tol(X, >, Y, T, -1, long double, "L")
1354
1369 #define ck_assert_ldouble_le_tol(X, Y, T) _ck_assert_floating_op_tol(X, <, Y, T, 1, long double, "L")
1370
1383 #define ck_assert_ldouble_finite(X) _ck_assert_floating_finite(X, long double, "L")
1384
1397 #define ck_assert_ldouble_infinite(X) _ck_assert_floating_infinite(X, long double, "L")
1398
1411 #define ck_assert_ldouble_nan(X) _ck_assert_floating_nan(X, long double, "L")
1412
1425 #define ck_assert_ldouble_nonnan(X) _ck_assert_floating_nonnan(X, long double, "L")
1426
1427 /* String comparison macros with improved output compared to ck_assert() */
1428 /* OP might be any operator that can be used in '0 OP strcmp(X,Y)' comparison. */
1429 /* String pointer could be compared againts NULL with == (NULLEQ = 1) and != (NULLNE = 1) operators. */
1430 /* The x and y parameter swap in strcmp() is needed to handle >, >=, <, <= operators. */
1431 /* If the x or y parameter is NULL its value will be printed without quotes. */
1432 #define _ck_assert_str(X, OP, Y, NULLEQ, NULLNE) do { \
1433 const char* _ck_x = (X); \
1434 const char* _ck_y = (Y); \
1435 const char* _ck_x_s; \
1436 const char* _ck_y_s; \
1437 const char* _ck_x_q; \
1438 const char* _ck_y_q; \
1439 if (_ck_x != NULL) { \
1440 _ck_x_q = "\""; \
1441 _ck_x_s = _ck_x; \
1442 } else { \
1443 _ck_x_q = ""; \
1444 _ck_x_s = "(null)"; \
1445 } \
1446 if (_ck_y != NULL) { \
1447 _ck_y_q = "\""; \
1448 _ck_y_s = _ck_y; \
1449 } else { \
1450 _ck_y_q = ""; \
1451 _ck_y_s = "(null)"; \
1452 } \
1453 ck_assert_msg( \
1454 (NULLEQ && (_ck_x == NULL) && (_ck_y == NULL)) || \
1455 (NULLNE && ((_ck_x == NULL) || (_ck_y == NULL)) && (_ck_x != _ck_y)) || \
1456 ((_ck_x != NULL) && (_ck_y != NULL) && (0 OP strcmp(_ck_y, _ck_x))), \
1457 "Assertion '%s' failed: %s == %s%s%s, %s == %s%s%s", \
1458 #X" "#OP" "#Y, \
1459 #X, _ck_x_q, _ck_x_s, _ck_x_q, \
1460 #Y, _ck_y_q, _ck_y_s, _ck_y_q); \
1461 } while (0)
1462
1476 #define ck_assert_str_eq(X, Y) _ck_assert_str(X, ==, Y, 0, 0)
1477
1491 #define ck_assert_str_ne(X, Y) _ck_assert_str(X, !=, Y, 0, 0)
1492
1506 #define ck_assert_str_lt(X, Y) _ck_assert_str(X, <, Y, 0, 0)
1507
1521 #define ck_assert_str_le(X, Y) _ck_assert_str(X, <=, Y, 0, 0)
1522
1536 #define ck_assert_str_gt(X, Y) _ck_assert_str(X, >, Y, 0, 0)
1537
1551 #define ck_assert_str_ge(X, Y) _ck_assert_str(X, >=, Y, 0, 0)
1552
1567 #define ck_assert_pstr_eq(X, Y) _ck_assert_str(X, ==, Y, 1, 0)
1568
1583 #define ck_assert_pstr_ne(X, Y) _ck_assert_str(X, !=, Y, 0, 1)
1584
1585 /* Memory location comparison macros with improved output compared to ck_assert() */
1586 /* OP might be any operator that can be used in '0 OP memcmp(X,Y,L)' comparison */
1587 /* The x and y parameter swap in memcmp() is needed to handle >, >=, <, <= operators */
1588 /* Output is limited to CK_MAX_ASSERT_MEM_PRINT_SIZE bytes */
1589 #ifndef CK_MAX_ASSERT_MEM_PRINT_SIZE
1590 #define CK_MAX_ASSERT_MEM_PRINT_SIZE 64
1591 #endif
1592
1593 /* Memory location comparison macros with improved output compared to ck_assert() */
1594 /* OP might be any operator that can be used in '0 OP memcmp(X,Y,L)' comparison */
1595 /* The x and y parameter swap in memcmp() is needed to handle >, >=, <, <= operators */
1596 /* Output is limited to CK_MAX_ASSERT_MEM_PRINT_SIZE bytes */
1597 #ifndef CK_MAX_ASSERT_MEM_PRINT_SIZE
1598 #define CK_MAX_ASSERT_MEM_PRINT_SIZE 64
1599 #endif
1600
1601 #define _ck_assert_mem(X, OP, Y, L) do { \
1602 const uint8_t* _ck_x = (const uint8_t*)(X); \
1603 const uint8_t* _ck_y = (const uint8_t*)(Y); \
1604 size_t _ck_l = (L); \
1605 char _ck_x_str[CK_MAX_ASSERT_MEM_PRINT_SIZE * 2 + 1]; \
1606 char _ck_y_str[CK_MAX_ASSERT_MEM_PRINT_SIZE * 2 + 1]; \
1607 static const char _ck_hexdigits[] = "0123456789abcdef"; \
1608 size_t _ck_i; \
1609 size_t _ck_maxl = (_ck_l > CK_MAX_ASSERT_MEM_PRINT_SIZE) ? CK_MAX_ASSERT_MEM_PRINT_SIZE : _ck_l; \
1610 for (_ck_i = 0; _ck_i < _ck_maxl; _ck_i++) { \
1611 _ck_x_str[_ck_i * 2 ] = _ck_hexdigits[(_ck_x[_ck_i] >> 4) & 0xF]; \
1612 _ck_y_str[_ck_i * 2 ] = _ck_hexdigits[(_ck_y[_ck_i] >> 4) & 0xF]; \
1613 _ck_x_str[_ck_i * 2 + 1] = _ck_hexdigits[_ck_x[_ck_i] & 0xF]; \
1614 _ck_y_str[_ck_i * 2 + 1] = _ck_hexdigits[_ck_y[_ck_i] & 0xF]; \
1615 } \
1616 _ck_x_str[_ck_i * 2] = 0; \
1617 _ck_y_str[_ck_i * 2] = 0; \
1618 if (_ck_maxl != _ck_l) { \
1619 _ck_x_str[_ck_i * 2 - 2] = '.'; \
1620 _ck_y_str[_ck_i * 2 - 2] = '.'; \
1621 _ck_x_str[_ck_i * 2 - 1] = '.'; \
1622 _ck_y_str[_ck_i * 2 - 1] = '.'; \
1623 } \
1624 ck_assert_msg(0 OP memcmp(_ck_y, _ck_x, _ck_l), \
1625 "Assertion '%s' failed: %s == \"%s\", %s == \"%s\"", #X" "#OP" "#Y, #X, _ck_x_str, #Y, _ck_y_str); \
1626 } while (0)
1627
1639 #define ck_assert_mem_eq(X, Y, L) _ck_assert_mem(X, ==, Y, L)
1640
1652 #define ck_assert_mem_ne(X, Y, L) _ck_assert_mem(X, !=, Y, L)
1653
1665 #define ck_assert_mem_lt(X, Y, L) _ck_assert_mem(X, <, Y, L)
1666
1678 #define ck_assert_mem_le(X, Y, L) _ck_assert_mem(X, <=, Y, L)
1679
1691 #define ck_assert_mem_gt(X, Y, L) _ck_assert_mem(X, >, Y, L)
1692
1704 #define ck_assert_mem_ge(X, Y, L) _ck_assert_mem(X, >=, Y, L)
1705
1706 /* Pointer comparison macros with improved output compared to ck_assert(). */
1707 /* OP may only be == or != */
1708 #define _ck_assert_ptr(X, OP, Y) do { \
1709 const void* _ck_x = (X); \
1710 const void* _ck_y = (Y); \
1711 ck_assert_msg(_ck_x OP _ck_y, "Assertion '%s' failed: %s == %#x, %s == %#x", #X" "#OP" "#Y, #X, _ck_x, #Y, _ck_y); \
1712 } while (0)
1713
1714 /* Pointer against NULL comparison macros with improved output
1715 * compared to ck_assert(). */
1716 /* OP may only be == or != */
1717 #define _ck_assert_ptr_null(X, OP) do { \
1718 const void* _ck_x = (X); \
1719 ck_assert_msg(_ck_x OP NULL, \
1720 "Assertion '%s' failed: %s == %#x", \
1721 #X" "#OP" NULL", \
1722 #X, _ck_x); \
1723 } while (0)
1724
1738 #define ck_assert_ptr_eq(X, Y) _ck_assert_ptr(X, ==, Y)
1739
1750 #define ck_assert_ptr_ne(X, Y) _ck_assert_ptr(X, !=, Y)
1751
1763 #define ck_assert_ptr_null(X) _ck_assert_ptr_null(X, ==)
1764
1776 #define ck_assert_ptr_nonnull(X) _ck_assert_ptr_null(X, !=)
1777
1788 #define mark_point() _mark_point(__FILE__,__LINE__)
1789
1790 /* Non macro version of #mark_point */
1791 CK_DLL_EXP void CK_EXPORT _mark_point(const char *file, int line);
1792
1796 enum test_result
1797 {
1798 CK_TEST_RESULT_INVALID,
1799 CK_PASS,
1800 CK_FAILURE,
1801 CK_ERROR
1803 };
1804
1808 enum print_output
1809 {
1810 CK_SILENT,
1811 CK_MINIMAL,
1812 CK_NORMAL,
1813 CK_VERBOSE,
1814 CK_ENV,
1819 #if 0
1820 CK_SUBUNIT,
1821 #endif
1822 CK_LAST
1823 };
1824
1828 typedef struct SRunner SRunner;
1829
1833 typedef struct TestResult TestResult;
1834
1838 enum ck_result_ctx
1839 {
1840 CK_CTX_INVALID,
1841 CK_CTX_SETUP,
1842 CK_CTX_TEST,
1843 CK_CTX_TEARDOWN
1844 };
1845
1858 CK_DLL_EXP int CK_EXPORT tr_rtype(TestResult * tr);
1859
1872 CK_DLL_EXP enum ck_result_ctx CK_EXPORT tr_ctx(TestResult * tr);
1873
1881 CK_DLL_EXP const char *CK_EXPORT tr_msg(TestResult * tr);
1882
1891 CK_DLL_EXP int CK_EXPORT tr_lno(TestResult * tr);
1892
1902 CK_DLL_EXP const char *CK_EXPORT tr_lfile(TestResult * tr);
1903
1913 CK_DLL_EXP const char *CK_EXPORT tr_tcname(TestResult * tr);
1914
1929 CK_DLL_EXP SRunner *CK_EXPORT srunner_create(Suite * s);
1930
1942 CK_DLL_EXP void CK_EXPORT srunner_add_suite(SRunner * sr, Suite * s);
1943
1955 CK_DLL_EXP void CK_EXPORT srunner_free(SRunner * sr);
1956
1973 CK_DLL_EXP void CK_EXPORT srunner_run_all(SRunner * sr,
1974 enum print_output print_mode);
1975
2004 CK_DLL_EXP void CK_EXPORT srunner_run(SRunner * sr, const char *sname,
2005 const char *tcname,
2006 enum print_output print_mode);
2007
2008
2043 CK_DLL_EXP void CK_EXPORT srunner_run_tagged(SRunner * sr, const char *sname,
2044 const char *tcname,
2045 const char *include_tags,
2046 const char *exclude_tags,
2047 enum print_output print_mode);
2048
2060 CK_DLL_EXP int CK_EXPORT srunner_ntests_failed(SRunner * sr);
2061
2071 CK_DLL_EXP int CK_EXPORT srunner_ntests_run(SRunner * sr);
2072
2090 CK_DLL_EXP TestResult **CK_EXPORT srunner_failures(SRunner * sr);
2091
2110 CK_DLL_EXP TestResult **CK_EXPORT srunner_results(SRunner * sr);
2111
2121 CK_DLL_EXP void CK_EXPORT srunner_print(SRunner * sr,
2122 enum print_output print_mode);
2123
2140 CK_DLL_EXP void CK_EXPORT srunner_set_log(SRunner * sr, const char *fname);
2141
2152 CK_DLL_EXP int CK_EXPORT srunner_has_log(SRunner * sr);
2153
2162 CK_DLL_EXP const char *CK_EXPORT srunner_log_fname(SRunner * sr);
2163
2180 CK_DLL_EXP void CK_EXPORT srunner_set_xml(SRunner * sr, const char *fname);
2181
2192 CK_DLL_EXP int CK_EXPORT srunner_has_xml(SRunner * sr);
2193
2202 CK_DLL_EXP const char *CK_EXPORT srunner_xml_fname(SRunner * sr);
2203
2220 CK_DLL_EXP void CK_EXPORT srunner_set_tap(SRunner * sr, const char *fname);
2221
2232 CK_DLL_EXP int CK_EXPORT srunner_has_tap(SRunner * sr);
2233
2242 CK_DLL_EXP const char *CK_EXPORT srunner_tap_fname(SRunner * sr);
2243
2247 enum fork_status
2248 {
2249 CK_FORK_GETENV,
2250 CK_FORK,
2251 CK_NOFORK
2252 };
2253
2261 CK_DLL_EXP enum fork_status CK_EXPORT srunner_fork_status(SRunner * sr);
2262
2283 CK_DLL_EXP void CK_EXPORT srunner_set_fork_status(SRunner * sr,
2284 enum fork_status fstat);
2285
2306 CK_DLL_EXP pid_t CK_EXPORT check_fork(void);
2307
2323 CK_DLL_EXP void CK_EXPORT check_waitpid_and_exit(pid_t pid) CK_ATTRIBUTE_NORETURN;
2324
2340 CK_DLL_EXP void CK_EXPORT check_set_max_msg_size(size_t max_msg_size);
2341
2342 #ifdef __cplusplus
2343 CK_CPPEND
2344 #endif
2345
2346 #endif /* CHECK_H */
fork_status
fork_status
Definition: check.h:2247
CK_LAST
@ CK_LAST
Definition: check.h:1822
check_fork
CK_DLL_EXP pid_t CK_EXPORT check_fork(void)
TTest::fn
TFun fn
Definition: check.h:153
srunner_xml_fname
const CK_DLL_EXP char *CK_EXPORT srunner_xml_fname(SRunner *sr)
srunner_set_fork_status
CK_DLL_EXP void CK_EXPORT srunner_set_fork_status(SRunner *sr, enum fork_status fstat)
tr_lfile
const CK_DLL_EXP char *CK_EXPORT tr_lfile(TestResult *tr)
print_output
print_output
Definition: check.h:1808
CK_ENV
@ CK_ENV
Definition: check.h:1814
CK_VERBOSE
@ CK_VERBOSE
Definition: check.h:1813
suite_add_tcase
CK_DLL_EXP void CK_EXPORT suite_add_tcase(Suite *s, TCase *tc)
CK_SILENT
@ CK_SILENT
Definition: check.h:1810
CK_PASS
@ CK_PASS
Definition: check.h:1799
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:141
test_result
test_result
Definition: check.h:1796
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:1842
CK_FORK
@ CK_FORK
Definition: check.h:2250
CK_FAILURE
@ CK_FAILURE
Definition: check.h:1800
srunner_ntests_run
CK_DLL_EXP int CK_EXPORT srunner_ntests_run(SRunner *sr)
TTest::file
const char * file
Definition: check.h:154
CK_NORMAL
@ CK_NORMAL
Definition: check.h:1812
CK_CTX_SETUP
@ CK_CTX_SETUP
Definition: check.h:1841
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:1828
srunner_results
CK_DLL_EXP TestResult **CK_EXPORT srunner_results(SRunner *sr)
TCase
struct TCase TCase
Definition: check.h:131
Suite
struct Suite Suite
Definition: check.h:146
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:2249
srunner_free
CK_DLL_EXP void CK_EXPORT srunner_free(SRunner *sr)
CK_TEST_RESULT_INVALID
@ CK_TEST_RESULT_INVALID
Definition: check.h:1798
srunner_add_suite
CK_DLL_EXP void CK_EXPORT srunner_add_suite(SRunner *sr, Suite *s)
srunner_set_xml
CK_DLL_EXP void CK_EXPORT srunner_set_xml(SRunner *sr, const char *fname)
TTest
Definition: check.h:151
TTest::line
int line
Definition: check.h:155
srunner_has_tap
CK_DLL_EXP int CK_EXPORT srunner_has_tap(SRunner *sr)
CK_NOFORK
@ CK_NOFORK
Definition: check.h:2251
tr_tcname
const CK_DLL_EXP char *CK_EXPORT tr_tcname(TestResult *tr)
tr_msg
const CK_DLL_EXP char *CK_EXPORT tr_msg(TestResult *tr)
srunner_set_tap
CK_DLL_EXP void CK_EXPORT srunner_set_tap(SRunner *sr, const char *fname)
TTest
struct TTest TTest
srunner_tap_fname
const CK_DLL_EXP char *CK_EXPORT srunner_tap_fname(SRunner *sr)
CK_CTX_TEARDOWN
@ CK_CTX_TEARDOWN
Definition: check.h:1843
ck_result_ctx
ck_result_ctx
Definition: check.h:1838
CK_CTX_INVALID
@ CK_CTX_INVALID
Definition: check.h:1840
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:152
CK_ERROR
@ CK_ERROR
Definition: check.h:1801
srunner_log_fname
const CK_DLL_EXP char *CK_EXPORT srunner_log_fname(SRunner *sr)
CK_MINIMAL
@ CK_MINIMAL
Definition: check.h:1811
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:1833
tcase_set_tags
CK_DLL_EXP void CK_EXPORT tcase_set_tags(TCase *tc, const char *tags)
TFun
void(* TFun)(int)
Definition: check.h:136
tcase_name
const CK_DLL_EXP char *CK_EXPORT tcase_name(void)