OpenVPN
cmocka.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifndef CMOCKA_H_
17 #define CMOCKA_H_
18 
19 #ifdef _WIN32
20 # ifdef _MSC_VER
21 
22 #define __func__ __FUNCTION__
23 
24 # ifndef inline
25 #define inline __inline
26 # endif /* inline */
27 
28 # if _MSC_VER < 1500
29 # ifdef __cplusplus
30 extern "C" {
31 # endif /* __cplusplus */
32 int __stdcall IsDebuggerPresent();
33 # ifdef __cplusplus
34 } /* extern "C" */
35 # endif /* __cplusplus */
36 # endif /* _MSC_VER < 1500 */
37 # endif /* _MSC_VER */
38 #endif /* _WIN32 */
39 
57 /* If __WORDSIZE is not set, try to figure it out and default to 32 bit. */
58 #ifndef __WORDSIZE
59 # if defined(__x86_64__) && !defined(__ILP32__)
60 # define __WORDSIZE 64
61 # else
62 # define __WORDSIZE 32
63 # endif
64 #endif
65 
66 #ifdef DOXYGEN
67 
71 typedef uintmax_t LargestIntegralType;
72 #else /* DOXGEN */
73 #ifndef LargestIntegralType
74 # if __WORDSIZE == 64
75 # define LargestIntegralType unsigned long int
76 # else
77 # define LargestIntegralType unsigned long long int
78 # endif
79 #endif /* LargestIntegralType */
80 #endif /* DOXYGEN */
81 
82 /* Printf format used to display LargestIntegralType. */
83 #ifndef LargestIntegralTypePrintfFormat
84 # ifdef _WIN32
85 # define LargestIntegralTypePrintfFormat "0x%I64x"
86 # else
87 # if __WORDSIZE == 64
88 # define LargestIntegralTypePrintfFormat "%#lx"
89 # else
90 # define LargestIntegralTypePrintfFormat "%#llx"
91 # endif
92 # endif /* _WIN32 */
93 #endif /* LargestIntegralTypePrintfFormat */
94 
95 /* Perform an unsigned cast to LargestIntegralType. */
96 #define cast_to_largest_integral_type(value) \
97  ((LargestIntegralType)(value))
98 
99 /* Smallest integral type capable of holding a pointer. */
100 #if !defined(_UINTPTR_T) && !defined(_UINTPTR_T_DEFINED)
101 # if defined(_WIN32)
102  /* WIN32 is an ILP32 platform */
103  typedef unsigned int uintptr_t;
104 # elif defined(_WIN64)
105  typedef unsigned long int uintptr_t
106 # else /* _WIN32 */
107 
108 /* ILP32 and LP64 platforms */
109 # ifdef __WORDSIZE /* glibc */
110 # if __WORDSIZE == 64
111  typedef unsigned long int uintptr_t;
112 # else
113  typedef unsigned int uintptr_t;
114 # endif /* __WORDSIZE == 64 */
115 # else /* __WORDSIZE */
116 # if defined(_LP64) || defined(_I32LPx)
117  typedef unsigned long int uintptr_t;
118 # else
119  typedef unsigned int uintptr_t;
120 # endif
121 # endif /* __WORDSIZE */
122 # endif /* _WIN32 */
123 
124 # define _UINTPTR_T
125 # define _UINTPTR_T_DEFINED
126 #endif /* !defined(_UINTPTR_T) || !defined(_UINTPTR_T_DEFINED) */
127 
128 /* Perform an unsigned cast to uintptr_t. */
129 #define cast_to_pointer_integral_type(value) \
130  ((uintptr_t)((size_t)(value)))
131 
132 /* Perform a cast of a pointer to LargestIntegralType */
133 #define cast_ptr_to_largest_integral_type(value) \
134 cast_to_largest_integral_type(cast_to_pointer_integral_type(value))
135 
136 /* GCC have printf type attribute check. */
137 #ifdef __GNUC__
138 #define CMOCKA_PRINTF_ATTRIBUTE(a,b) \
139  __attribute__ ((__format__ (__printf__, a, b)))
140 #else
141 #define CMOCKA_PRINTF_ATTRIBUTE(a,b)
142 #endif /* __GNUC__ */
143 
144 #if defined(__GNUC__)
145 #define CMOCKA_DEPRECATED __attribute__ ((deprecated))
146 #elif defined(_MSC_VER)
147 #define CMOCKA_DEPRECATED __declspec(deprecated)
148 #else
149 #define CMOCKA_DEPRECATED
150 #endif
151 
200 #ifdef DOXYGEN
201 
209 #else
210 #define mock() _mock(__func__, __FILE__, __LINE__)
211 #endif
212 
213 #ifdef DOXYGEN
214 
234 #type mock_type(#type);
235 #else
236 #define mock_type(type) ((type) mock())
237 #endif
238 
239 #ifdef DOXYGEN
240 
261 type mock_ptr_type(#type);
262 #else
263 #define mock_ptr_type(type) ((type) (uintptr_t) mock())
264 #endif
265 
266 
267 #ifdef DOXYGEN
268 
292 void will_return(#function, LargestIntegralType value);
293 #else
294 #define will_return(function, value) \
295  _will_return(#function, __FILE__, __LINE__, \
296  cast_to_largest_integral_type(value), 1)
297 #endif
298 
299 #ifdef DOXYGEN
300 
315 void will_return_count(#function, LargestIntegralType value, int count);
316 #else
317 #define will_return_count(function, value, count) \
318  _will_return(#function, __FILE__, __LINE__, \
319  cast_to_largest_integral_type(value), count)
320 #endif
321 
322 #ifdef DOXYGEN
323 
338 void will_return_always(#function, LargestIntegralType value);
339 #else
340 #define will_return_always(function, value) \
341  will_return_count(function, (value), -1)
342 #endif
343 
344 #ifdef DOXYGEN
345 
366 void will_return_maybe(#function, LargestIntegralType value);
367 #else
368 #define will_return_maybe(function, value) \
369  will_return_count(function, (value), -2)
370 #endif
371 
417 /*
418  * Add a custom parameter checking function. If the event parameter is NULL
419  * the event structure is allocated internally by this function. If event
420  * parameter is provided it must be allocated on the heap and doesn't need to
421  * be deallocated by the caller.
422  */
423 #ifdef DOXYGEN
424 
440 void expect_check(#function, #parameter, #check_function, const void *check_data);
441 #else
442 #define expect_check(function, parameter, check_function, check_data) \
443  _expect_check(#function, #parameter, __FILE__, __LINE__, check_function, \
444  cast_to_largest_integral_type(check_data), NULL, 1)
445 #endif
446 
447 #ifdef DOXYGEN
448 
462 void expect_in_set(#function, #parameter, LargestIntegralType value_array[]);
463 #else
464 #define expect_in_set(function, parameter, value_array) \
465  expect_in_set_count(function, parameter, value_array, 1)
466 #endif
467 
468 #ifdef DOXYGEN
469 
487 void expect_in_set_count(#function, #parameter, LargestIntegralType value_array[], size_t count);
488 #else
489 #define expect_in_set_count(function, parameter, value_array, count) \
490  _expect_in_set(#function, #parameter, __FILE__, __LINE__, value_array, \
491  sizeof(value_array) / sizeof((value_array)[0]), count)
492 #endif
493 
494 #ifdef DOXYGEN
495 
509 void expect_not_in_set(#function, #parameter, LargestIntegralType value_array[]);
510 #else
511 #define expect_not_in_set(function, parameter, value_array) \
512  expect_not_in_set_count(function, parameter, value_array, 1)
513 #endif
514 
515 #ifdef DOXYGEN
516 
534 void expect_not_in_set_count(#function, #parameter, LargestIntegralType value_array[], size_t count);
535 #else
536 #define expect_not_in_set_count(function, parameter, value_array, count) \
537  _expect_not_in_set( \
538  #function, #parameter, __FILE__, __LINE__, value_array, \
539  sizeof(value_array) / sizeof((value_array)[0]), count)
540 #endif
541 
542 
543 #ifdef DOXYGEN
544 
560 void expect_in_range(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum);
561 #else
562 #define expect_in_range(function, parameter, minimum, maximum) \
563  expect_in_range_count(function, parameter, minimum, maximum, 1)
564 #endif
565 
566 #ifdef DOXYGEN
567 
587 void expect_in_range_count(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum, size_t count);
588 #else
589 #define expect_in_range_count(function, parameter, minimum, maximum, count) \
590  _expect_in_range(#function, #parameter, __FILE__, __LINE__, minimum, \
591  maximum, count)
592 #endif
593 
594 #ifdef DOXYGEN
595 
611 void expect_not_in_range(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum);
612 #else
613 #define expect_not_in_range(function, parameter, minimum, maximum) \
614  expect_not_in_range_count(function, parameter, minimum, maximum, 1)
615 #endif
616 
617 #ifdef DOXYGEN
618 
638 void expect_not_in_range_count(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum, size_t count);
639 #else
640 #define expect_not_in_range_count(function, parameter, minimum, maximum, \
641  count) \
642  _expect_not_in_range(#function, #parameter, __FILE__, __LINE__, \
643  minimum, maximum, count)
644 #endif
645 
646 #ifdef DOXYGEN
647 
660 void expect_value(#function, #parameter, LargestIntegralType value);
661 #else
662 #define expect_value(function, parameter, value) \
663  expect_value_count(function, parameter, value, 1)
664 #endif
665 
666 #ifdef DOXYGEN
667 
684 void expect_value_count(#function, #parameter, LargestIntegralType value, size_t count);
685 #else
686 #define expect_value_count(function, parameter, value, count) \
687  _expect_value(#function, #parameter, __FILE__, __LINE__, \
688  cast_to_largest_integral_type(value), count)
689 #endif
690 
691 #ifdef DOXYGEN
692 
705 void expect_not_value(#function, #parameter, LargestIntegralType value);
706 #else
707 #define expect_not_value(function, parameter, value) \
708  expect_not_value_count(function, parameter, value, 1)
709 #endif
710 
711 #ifdef DOXYGEN
712 
729 void expect_not_value_count(#function, #parameter, LargestIntegralType value, size_t count);
730 #else
731 #define expect_not_value_count(function, parameter, value, count) \
732  _expect_not_value(#function, #parameter, __FILE__, __LINE__, \
733  cast_to_largest_integral_type(value), count)
734 #endif
735 
736 #ifdef DOXYGEN
737 
751 void expect_string(#function, #parameter, const char *string);
752 #else
753 #define expect_string(function, parameter, string) \
754  expect_string_count(function, parameter, string, 1)
755 #endif
756 
757 #ifdef DOXYGEN
758 
776 void expect_string_count(#function, #parameter, const char *string, size_t count);
777 #else
778 #define expect_string_count(function, parameter, string, count) \
779  _expect_string(#function, #parameter, __FILE__, __LINE__, \
780  (const char*)(string), count)
781 #endif
782 
783 #ifdef DOXYGEN
784 
798 void expect_not_string(#function, #parameter, const char *string);
799 #else
800 #define expect_not_string(function, parameter, string) \
801  expect_not_string_count(function, parameter, string, 1)
802 #endif
803 
804 #ifdef DOXYGEN
805 
823 void expect_not_string_count(#function, #parameter, const char *string, size_t count);
824 #else
825 #define expect_not_string_count(function, parameter, string, count) \
826  _expect_not_string(#function, #parameter, __FILE__, __LINE__, \
827  (const char*)(string), count)
828 #endif
829 
830 #ifdef DOXYGEN
831 
846 void expect_memory(#function, #parameter, void *memory, size_t size);
847 #else
848 #define expect_memory(function, parameter, memory, size) \
849  expect_memory_count(function, parameter, memory, size, 1)
850 #endif
851 
852 #ifdef DOXYGEN
853 
873 void expect_memory_count(#function, #parameter, void *memory, size_t size, size_t count);
874 #else
875 #define expect_memory_count(function, parameter, memory, size, count) \
876  _expect_memory(#function, #parameter, __FILE__, __LINE__, \
877  (const void*)(memory), size, count)
878 #endif
879 
880 #ifdef DOXYGEN
881 
897 void expect_not_memory(#function, #parameter, void *memory, size_t size);
898 #else
899 #define expect_not_memory(function, parameter, memory, size) \
900  expect_not_memory_count(function, parameter, memory, size, 1)
901 #endif
902 
903 #ifdef DOXYGEN
904 
924 void expect_not_memory_count(#function, #parameter, void *memory, size_t size, size_t count);
925 #else
926 #define expect_not_memory_count(function, parameter, memory, size, count) \
927  _expect_not_memory(#function, #parameter, __FILE__, __LINE__, \
928  (const void*)(memory), size, count)
929 #endif
930 
931 
932 #ifdef DOXYGEN
933 
944 void expect_any(#function, #parameter);
945 #else
946 #define expect_any(function, parameter) \
947  expect_any_count(function, parameter, 1)
948 #endif
949 
950 #ifdef DOXYGEN
951 
967 void expect_any_count(#function, #parameter, size_t count);
968 #else
969 #define expect_any_count(function, parameter, count) \
970  _expect_any(#function, #parameter, __FILE__, __LINE__, count)
971 #endif
972 
973 #ifdef DOXYGEN
974 
984 void check_expected(#parameter);
985 #else
986 #define check_expected(parameter) \
987  _check_expected(__func__, #parameter, __FILE__, __LINE__, \
988  cast_to_largest_integral_type(parameter))
989 #endif
990 
991 #ifdef DOXYGEN
992 
1002 void check_expected_ptr(#parameter);
1003 #else
1004 #define check_expected_ptr(parameter) \
1005  _check_expected(__func__, #parameter, __FILE__, __LINE__, \
1006  cast_ptr_to_largest_integral_type(parameter))
1007 #endif
1008 
1030 #ifdef DOXYGEN
1031 
1043 void assert_true(scalar expression);
1044 #else
1045 #define assert_true(c) _assert_true(cast_to_largest_integral_type(c), #c, \
1046  __FILE__, __LINE__)
1047 #endif
1048 
1049 #ifdef DOXYGEN
1050 
1061 void assert_false(scalar expression);
1062 #else
1063 #define assert_false(c) _assert_true(!(cast_to_largest_integral_type(c)), #c, \
1064  __FILE__, __LINE__)
1065 #endif
1066 
1067 #ifdef DOXYGEN
1068 
1080 void assert_return_code(int rc, int error);
1081 #else
1082 #define assert_return_code(rc, error) \
1083  _assert_return_code(cast_to_largest_integral_type(rc), \
1084  sizeof(rc), \
1085  cast_to_largest_integral_type(error), \
1086  #rc, __FILE__, __LINE__)
1087 #endif
1088 
1089 #ifdef DOXYGEN
1090 
1100 void assert_non_null(void *pointer);
1101 #else
1102 #define assert_non_null(c) _assert_true(cast_ptr_to_largest_integral_type(c), #c, \
1103  __FILE__, __LINE__)
1104 #endif
1105 
1106 #ifdef DOXYGEN
1107 
1117 void assert_null(void *pointer);
1118 #else
1119 #define assert_null(c) _assert_true(!(cast_ptr_to_largest_integral_type(c)), #c, \
1120 __FILE__, __LINE__)
1121 #endif
1122 
1123 #ifdef DOXYGEN
1124 
1134 void assert_ptr_equal(void *a, void *b);
1135 #else
1136 #define assert_ptr_equal(a, b) \
1137  _assert_int_equal(cast_ptr_to_largest_integral_type(a), \
1138  cast_ptr_to_largest_integral_type(b), \
1139  __FILE__, __LINE__)
1140 #endif
1141 
1142 #ifdef DOXYGEN
1143 
1153 void assert_ptr_not_equal(void *a, void *b);
1154 #else
1155 #define assert_ptr_not_equal(a, b) \
1156  _assert_int_not_equal(cast_ptr_to_largest_integral_type(a), \
1157  cast_ptr_to_largest_integral_type(b), \
1158  __FILE__, __LINE__)
1159 #endif
1160 
1161 #ifdef DOXYGEN
1162 
1172 void assert_int_equal(int a, int b);
1173 #else
1174 #define assert_int_equal(a, b) \
1175  _assert_int_equal(cast_to_largest_integral_type(a), \
1176  cast_to_largest_integral_type(b), \
1177  __FILE__, __LINE__)
1178 #endif
1179 
1180 #ifdef DOXYGEN
1181 
1193 void assert_int_not_equal(int a, int b);
1194 #else
1195 #define assert_int_not_equal(a, b) \
1196  _assert_int_not_equal(cast_to_largest_integral_type(a), \
1197  cast_to_largest_integral_type(b), \
1198  __FILE__, __LINE__)
1199 #endif
1200 
1201 #ifdef DOXYGEN
1202 
1212 void assert_string_equal(const char *a, const char *b);
1213 #else
1214 #define assert_string_equal(a, b) \
1215  _assert_string_equal((const char*)(a), (const char*)(b), __FILE__, \
1216  __LINE__)
1217 #endif
1218 
1219 #ifdef DOXYGEN
1220 
1230 void assert_string_not_equal(const char *a, const char *b);
1231 #else
1232 #define assert_string_not_equal(a, b) \
1233  _assert_string_not_equal((const char*)(a), (const char*)(b), __FILE__, \
1234  __LINE__)
1235 #endif
1236 
1237 #ifdef DOXYGEN
1238 
1252 void assert_memory_equal(const void *a, const void *b, size_t size);
1253 #else
1254 #define assert_memory_equal(a, b, size) \
1255  _assert_memory_equal((const void*)(a), (const void*)(b), size, __FILE__, \
1256  __LINE__)
1257 #endif
1258 
1259 #ifdef DOXYGEN
1260 
1274 void assert_memory_not_equal(const void *a, const void *b, size_t size);
1275 #else
1276 #define assert_memory_not_equal(a, b, size) \
1277  _assert_memory_not_equal((const void*)(a), (const void*)(b), size, \
1278  __FILE__, __LINE__)
1279 #endif
1280 
1281 #ifdef DOXYGEN
1282 
1296 #else
1297 #define assert_in_range(value, minimum, maximum) \
1298  _assert_in_range( \
1299  cast_to_largest_integral_type(value), \
1300  cast_to_largest_integral_type(minimum), \
1301  cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
1302 #endif
1303 
1304 #ifdef DOXYGEN
1305 
1319 #else
1320 #define assert_not_in_range(value, minimum, maximum) \
1321  _assert_not_in_range( \
1322  cast_to_largest_integral_type(value), \
1323  cast_to_largest_integral_type(minimum), \
1324  cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
1325 #endif
1326 
1327 #ifdef DOXYGEN
1328 
1340 void assert_in_set(LargestIntegralType value, LargestIntegralType values[], size_t count);
1341 #else
1342 #define assert_in_set(value, values, number_of_values) \
1343  _assert_in_set(value, values, number_of_values, __FILE__, __LINE__)
1344 #endif
1345 
1346 #ifdef DOXYGEN
1347 
1359 void assert_not_in_set(LargestIntegralType value, LargestIntegralType values[], size_t count);
1360 #else
1361 #define assert_not_in_set(value, values, number_of_values) \
1362  _assert_not_in_set(value, values, number_of_values, __FILE__, __LINE__)
1363 #endif
1364 
1425 #ifdef DOXYGEN
1426 
1432 void function_called(void);
1433 #else
1434 #define function_called() _function_called(__func__, __FILE__, __LINE__)
1435 #endif
1436 
1437 #ifdef DOXYGEN
1438 
1448 void expect_function_calls(#function, const int times);
1449 #else
1450 #define expect_function_calls(function, times) \
1451  _expect_function_call(#function, __FILE__, __LINE__, times)
1452 #endif
1453 
1454 #ifdef DOXYGEN
1455 
1463 void expect_function_call(#function);
1464 #else
1465 #define expect_function_call(function) \
1466  _expect_function_call(#function, __FILE__, __LINE__, 1)
1467 #endif
1468 
1469 #ifdef DOXYGEN
1470 
1477 void expect_function_call_any(#function);
1478 #else
1479 #define expect_function_call_any(function) \
1480  _expect_function_call(#function, __FILE__, __LINE__, -1)
1481 #endif
1482 
1483 #ifdef DOXYGEN
1484 
1491 void ignore_function_calls(#function);
1492 #else
1493 #define ignore_function_calls(function) \
1494  _expect_function_call(#function, __FILE__, __LINE__, -2)
1495 #endif
1496 
1525 #ifdef DOXYGEN
1526 
1529 void fail(void);
1530 #else
1531 #define fail() _fail(__FILE__, __LINE__)
1532 #endif
1533 
1534 #ifdef DOXYGEN
1535 
1538 void skip(void);
1539 #else
1540 #define skip() _skip(__FILE__, __LINE__)
1541 #endif
1542 
1543 #ifdef DOXYGEN
1544 
1558 void fail_msg(const char *msg, ...);
1559 #else
1560 #define fail_msg(msg, ...) do { \
1561  print_error("ERROR: " msg "\n", ##__VA_ARGS__); \
1562  fail(); \
1563 } while (0)
1564 #endif
1565 
1566 #ifdef DOXYGEN
1567 
1586 int run_test(#function);
1587 #else
1588 #define run_test(f) _run_test(#f, f, NULL, UNIT_TEST_FUNCTION_TYPE_TEST, NULL)
1589 #endif
1590 
1591 static inline void _unit_test_dummy(void **state) {
1592  (void)state;
1593 }
1594 
1599 #define unit_test(f) { #f, f, UNIT_TEST_FUNCTION_TYPE_TEST }
1600 
1601 #define _unit_test_setup(test, setup) \
1602  { #test "_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_SETUP }
1603 
1608 #define unit_test_setup(test, setup) \
1609  _unit_test_setup(test, setup), \
1610  unit_test(test), \
1611  _unit_test_teardown(test, _unit_test_dummy)
1612 
1613 #define _unit_test_teardown(test, teardown) \
1614  { #test "_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_TEARDOWN }
1615 
1620 #define unit_test_teardown(test, teardown) \
1621  _unit_test_setup(test, _unit_test_dummy), \
1622  unit_test(test), \
1623  _unit_test_teardown(test, teardown)
1624 
1629 #define group_test_setup(setup) \
1630  { "group_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_GROUP_SETUP }
1631 
1636 #define group_test_teardown(teardown) \
1637  { "group_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_GROUP_TEARDOWN }
1638 
1646 #define unit_test_setup_teardown(test, setup, teardown) \
1647  _unit_test_setup(test, setup), \
1648  unit_test(test), \
1649  _unit_test_teardown(test, teardown)
1650 
1651 
1653 #define cmocka_unit_test(f) { #f, f, NULL, NULL, NULL }
1654 
1656 #define cmocka_unit_test_setup(f, setup) { #f, f, setup, NULL, NULL }
1657 
1659 #define cmocka_unit_test_teardown(f, teardown) { #f, f, NULL, teardown, NULL }
1660 
1665 #define cmocka_unit_test_setup_teardown(f, setup, teardown) { #f, f, setup, teardown, NULL }
1666 
1674 #define cmocka_unit_test_prestate(f, state) { #f, f, NULL, NULL, state }
1675 
1683 #define cmocka_unit_test_prestate_setup_teardown(f, setup, teardown, state) { #f, f, setup, teardown, state }
1684 
1685 #define run_tests(tests) _run_tests(tests, sizeof(tests) / sizeof(tests)[0])
1686 #define run_group_tests(tests) _run_group_tests(tests, sizeof(tests) / sizeof(tests)[0])
1687 
1688 #ifdef DOXYGEN
1689 
1745 int cmocka_run_group_tests(const struct CMUnitTest group_tests[],
1748 #else
1749 # define cmocka_run_group_tests(group_tests, group_setup, group_teardown) \
1750  _cmocka_run_group_tests(#group_tests, group_tests, sizeof(group_tests) / sizeof(group_tests)[0], group_setup, group_teardown)
1751 #endif
1752 
1753 #ifdef DOXYGEN
1754 
1813 int cmocka_run_group_tests_name(const char *group_name,
1814  const struct CMUnitTest group_tests[],
1817 #else
1818 # define cmocka_run_group_tests_name(group_name, group_tests, group_setup, group_teardown) \
1819  _cmocka_run_group_tests(group_name, group_tests, sizeof(group_tests) / sizeof(group_tests)[0], group_setup, group_teardown)
1820 #endif
1821 
1847 #ifdef DOXYGEN
1848 
1870 void *test_malloc(size_t size);
1871 #else
1872 #define test_malloc(size) _test_malloc(size, __FILE__, __LINE__)
1873 #endif
1874 
1875 #ifdef DOXYGEN
1876 
1889 void *test_calloc(size_t nmemb, size_t size);
1890 #else
1891 #define test_calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__)
1892 #endif
1893 
1894 #ifdef DOXYGEN
1895 
1905 void *test_realloc(void *ptr, size_t size);
1906 #else
1907 #define test_realloc(ptr, size) _test_realloc(ptr, size, __FILE__, __LINE__)
1908 #endif
1909 
1910 #ifdef DOXYGEN
1911 
1918 void test_free(void *ptr);
1919 #else
1920 #define test_free(ptr) _test_free(ptr, __FILE__, __LINE__)
1921 #endif
1922 
1923 /* Redirect malloc, calloc and free to the unit test allocators. */
1924 #ifdef UNIT_TESTING
1925 #define malloc test_malloc
1926 #define realloc test_realloc
1927 #define calloc test_calloc
1928 #define free test_free
1929 #endif /* UNIT_TESTING */
1930 
1984 void mock_assert(const int result, const char* const expression,
1985  const char * const file, const int line);
1986 
1987 #ifdef DOXYGEN
1988 
2010 void expect_assert_failure(function fn_call);
2011 #else
2012 #define expect_assert_failure(function_call) \
2013  { \
2014  const int result = setjmp(global_expect_assert_env); \
2015  global_expecting_assert = 1; \
2016  if (result) { \
2017  print_message("Expected assertion %s occurred\n", \
2018  global_last_failed_assert); \
2019  global_expecting_assert = 0; \
2020  } else { \
2021  function_call ; \
2022  global_expecting_assert = 0; \
2023  print_error("Expected assert in %s\n", #function_call); \
2024  _fail(__FILE__, __LINE__); \
2025  } \
2026  }
2027 #endif
2028 
2031 /* Function prototype for setup, test and teardown functions. */
2032 typedef void (*UnitTestFunction)(void **state);
2033 
2034 /* Function that determines whether a function parameter value is correct. */
2035 typedef int (*CheckParameterValue)(const LargestIntegralType value,
2036  const LargestIntegralType check_value_data);
2037 
2038 /* Type of the unit test function. */
2039 typedef enum UnitTestFunctionType {
2046 
2047 /*
2048  * Stores a unit test function with its name and type.
2049  * NOTE: Every setup function must be paired with a teardown function. It's
2050  * possible to specify NULL function pointers.
2051  */
2052 typedef struct UnitTest {
2053  const char* name;
2056 } UnitTest;
2057 
2058 typedef struct GroupTest {
2061  const UnitTest *tests;
2062  const size_t number_of_tests;
2063 } GroupTest;
2064 
2065 /* Function prototype for test functions. */
2066 typedef void (*CMUnitTestFunction)(void **state);
2067 
2068 /* Function prototype for setup and teardown functions. */
2069 typedef int (*CMFixtureFunction)(void **state);
2070 
2071 struct CMUnitTest {
2072  const char *name;
2077 };
2078 
2079 /* Location within some source code. */
2080 typedef struct SourceLocation {
2081  const char* file;
2082  int line;
2083 } SourceLocation;
2084 
2085 /* Event that's called to check a parameter value. */
2086 typedef struct CheckParameterEvent {
2088  const char *parameter_name;
2092 
2093 /* Used by expect_assert_failure() and mock_assert(). */
2094 extern int global_expecting_assert;
2095 extern jmp_buf global_expect_assert_env;
2096 extern const char * global_last_failed_assert;
2097 
2098 /* Retrieves a value for the given function, as set by "will_return". */
2099 LargestIntegralType _mock(const char * const function, const char* const file,
2100  const int line);
2101 
2103  const char * const function_name,
2104  const char * const file,
2105  const int line,
2106  const int count);
2107 
2108 void _function_called(const char * const function, const char* const file,
2109  const int line);
2110 
2111 void _expect_check(
2112  const char* const function, const char* const parameter,
2113  const char* const file, const int line,
2114  const CheckParameterValue check_function,
2115  const LargestIntegralType check_data, CheckParameterEvent * const event,
2116  const int count);
2117 
2118 void _expect_in_set(
2119  const char* const function, const char* const parameter,
2120  const char* const file, const int line, const LargestIntegralType values[],
2121  const size_t number_of_values, const int count);
2122 void _expect_not_in_set(
2123  const char* const function, const char* const parameter,
2124  const char* const file, const int line, const LargestIntegralType values[],
2125  const size_t number_of_values, const int count);
2126 
2127 void _expect_in_range(
2128  const char* const function, const char* const parameter,
2129  const char* const file, const int line,
2130  const LargestIntegralType minimum,
2131  const LargestIntegralType maximum, const int count);
2133  const char* const function, const char* const parameter,
2134  const char* const file, const int line,
2135  const LargestIntegralType minimum,
2136  const LargestIntegralType maximum, const int count);
2137 
2138 void _expect_value(
2139  const char* const function, const char* const parameter,
2140  const char* const file, const int line, const LargestIntegralType value,
2141  const int count);
2142 void _expect_not_value(
2143  const char* const function, const char* const parameter,
2144  const char* const file, const int line, const LargestIntegralType value,
2145  const int count);
2146 
2147 void _expect_string(
2148  const char* const function, const char* const parameter,
2149  const char* const file, const int line, const char* string,
2150  const int count);
2151 void _expect_not_string(
2152  const char* const function, const char* const parameter,
2153  const char* const file, const int line, const char* string,
2154  const int count);
2155 
2156 void _expect_memory(
2157  const char* const function, const char* const parameter,
2158  const char* const file, const int line, const void* const memory,
2159  const size_t size, const int count);
2160 void _expect_not_memory(
2161  const char* const function, const char* const parameter,
2162  const char* const file, const int line, const void* const memory,
2163  const size_t size, const int count);
2164 
2165 void _expect_any(
2166  const char* const function, const char* const parameter,
2167  const char* const file, const int line, const int count);
2168 
2169 void _check_expected(
2170  const char * const function_name, const char * const parameter_name,
2171  const char* file, const int line, const LargestIntegralType value);
2172 
2173 void _will_return(const char * const function_name, const char * const file,
2174  const int line, const LargestIntegralType value,
2175  const int count);
2176 void _assert_true(const LargestIntegralType result,
2177  const char* const expression,
2178  const char * const file, const int line);
2179 void _assert_return_code(const LargestIntegralType result,
2180  size_t rlen,
2181  const LargestIntegralType error,
2182  const char * const expression,
2183  const char * const file,
2184  const int line);
2185 void _assert_int_equal(
2186  const LargestIntegralType a, const LargestIntegralType b,
2187  const char * const file, const int line);
2189  const LargestIntegralType a, const LargestIntegralType b,
2190  const char * const file, const int line);
2191 void _assert_string_equal(const char * const a, const char * const b,
2192  const char * const file, const int line);
2193 void _assert_string_not_equal(const char * const a, const char * const b,
2194  const char *file, const int line);
2195 void _assert_memory_equal(const void * const a, const void * const b,
2196  const size_t size, const char* const file,
2197  const int line);
2198 void _assert_memory_not_equal(const void * const a, const void * const b,
2199  const size_t size, const char* const file,
2200  const int line);
2201 void _assert_in_range(
2202  const LargestIntegralType value, const LargestIntegralType minimum,
2203  const LargestIntegralType maximum, const char* const file, const int line);
2205  const LargestIntegralType value, const LargestIntegralType minimum,
2206  const LargestIntegralType maximum, const char* const file, const int line);
2207 void _assert_in_set(
2208  const LargestIntegralType value, const LargestIntegralType values[],
2209  const size_t number_of_values, const char* const file, const int line);
2210 void _assert_not_in_set(
2211  const LargestIntegralType value, const LargestIntegralType values[],
2212  const size_t number_of_values, const char* const file, const int line);
2213 
2214 void* _test_malloc(const size_t size, const char* file, const int line);
2215 void* _test_realloc(void *ptr, const size_t size, const char* file, const int line);
2216 void* _test_calloc(const size_t number_of_elements, const size_t size,
2217  const char* file, const int line);
2218 void _test_free(void* const ptr, const char* file, const int line);
2219 
2220 void _fail(const char * const file, const int line);
2221 
2222 void _skip(const char * const file, const int line);
2223 
2224 int _run_test(
2225  const char * const function_name, const UnitTestFunction Function,
2226  void ** const volatile state, const UnitTestFunctionType function_type,
2227  const void* const heap_check_point);
2228 CMOCKA_DEPRECATED int _run_tests(const UnitTest * const tests,
2229  const size_t number_of_tests);
2230 CMOCKA_DEPRECATED int _run_group_tests(const UnitTest * const tests,
2231  const size_t number_of_tests);
2232 
2233 /* Test runner */
2234 int _cmocka_run_group_tests(const char *group_name,
2235  const struct CMUnitTest * const tests,
2236  const size_t num_tests,
2239 
2240 /* Standard output and error print methods. */
2241 void print_message(const char* const format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2);
2242 void print_error(const char* const format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2);
2243 void vprint_message(const char* const format, va_list args) CMOCKA_PRINTF_ATTRIBUTE(1, 0);
2244 void vprint_error(const char* const format, va_list args) CMOCKA_PRINTF_ATTRIBUTE(1, 0);
2245 
2251 };
2252 
2265 
2268 #endif /* CMOCKA_H_ */
struct CheckParameterEvent CheckParameterEvent
struct UnitTest UnitTest
#define assert_true(c)
Definition: cmocka.h:1045
#define expect_not_in_set_count(function, parameter, value_array, count)
Definition: cmocka.h:536
void _assert_memory_equal(const void *const a, const void *const b, const size_t size, const char *const file, const int line)
#define assert_non_null(c)
Definition: cmocka.h:1102
#define expect_in_range(function, parameter, minimum, maximum)
Definition: cmocka.h:562
#define expect_memory_count(function, parameter, memory, size, count)
Definition: cmocka.h:875
CheckParameterValue check_value
Definition: cmocka.h:2089
#define assert_return_code(rc, error)
Definition: cmocka.h:1082
CMFixtureFunction setup_func
Definition: cmocka.h:2074
#define expect_not_in_set(function, parameter, value_array)
Definition: cmocka.h:511
void _expect_not_memory(const char *const function, const char *const parameter, const char *const file, const int line, const void *const memory, const size_t size, const int count)
Definition: cmocka.c:1448
#define test_calloc(num, size)
Definition: cmocka.h:1891
void(* CMUnitTestFunction)(void **state)
Definition: cmocka.h:2066
const char * file
Definition: cmocka.h:2081
struct GroupTest GroupTest
SourceLocation location
Definition: cmocka.h:2087
#define assert_not_in_set(value, values, number_of_values)
Definition: cmocka.h:1361
#define expect_not_memory(function, parameter, memory, size)
Definition: cmocka.h:899
#define expect_not_in_range(function, parameter, minimum, maximum)
Definition: cmocka.h:613
void _assert_int_equal(const LargestIntegralType a, const LargestIntegralType b, const char *const file, const int line)
int(* CheckParameterValue)(const LargestIntegralType value, const LargestIntegralType check_value_data)
Definition: cmocka.h:2035
#define expect_in_set(function, parameter, value_array)
Definition: cmocka.h:464
#define will_return_maybe(function, value)
Definition: cmocka.h:368
#define expect_not_string(function, parameter, string)
Definition: cmocka.h:800
#define assert_ptr_equal(a, b)
Definition: cmocka.h:1136
UnitTestFunction teardown
Definition: cmocka.h:2060
int _run_test(const char *const function_name, const UnitTestFunction Function, void **const volatile state, const UnitTestFunctionType function_type, const void *const heap_check_point)
Definition: cmocka.c:2850
void _expect_not_string(const char *const function, const char *const parameter, const char *const file, const int line, const char *string, const int count)
Definition: cmocka.c:1379
#define fail_msg(msg,...)
Definition: cmocka.h:1560
void _assert_memory_not_equal(const void *const a, const void *const b, const size_t size, const char *const file, const int line)
void mock_assert(const int result, const char *const expression, const char *const file, const int line)
Function to replace assert(3) in tested code.
Definition: cmocka.c:1520
const UnitTest * tests
Definition: cmocka.h:2061
#define test_free(ptr)
Definition: cmocka.h:1920
void void void void vprint_error(const char *const format, va_list args) CMOCKA_PRINTF_ATTRIBUTE(1
#define test_realloc(ptr, size)
Definition: cmocka.h:1907
#define expect_not_string_count(function, parameter, string, count)
Definition: cmocka.h:825
#define fail()
Definition: cmocka.h:1531
#define expect_value(function, parameter, value)
Definition: cmocka.h:662
void void print_error(const char *const format,...) CMOCKA_PRINTF_ATTRIBUTE(1
LargestIntegralType check_value_data
Definition: cmocka.h:2090
void _expect_any(const char *const function, const char *const parameter, const char *const file, const int line, const int count)
Definition: cmocka.c:1467
CMUnitTestFunction test_func
Definition: cmocka.h:2073
#define expect_function_calls(function, times)
Definition: cmocka.h:1450
#define CMOCKA_PRINTF_ATTRIBUTE(a, b)
Definition: cmocka.h:141
static void _unit_test_dummy(void **state)
Definition: cmocka.h:1591
void _expect_string(const char *const function, const char *const parameter, const char *const file, const int line, const char *string, const int count)
Definition: cmocka.c:1357
#define expect_in_set_count(function, parameter, value_array, count)
Definition: cmocka.h:489
#define expect_not_value(function, parameter, value)
Definition: cmocka.h:707
void _will_return(const char *const function_name, const char *const file, const int line, const LargestIntegralType value, const int count)
Definition: cmocka.c:943
static int group_teardown(void **state)
#define cmocka_run_group_tests_name(group_name, group_tests, group_setup, group_teardown)
Definition: cmocka.h:1818
#define assert_int_not_equal(a, b)
Definition: cmocka.h:1195
CMOCKA_DEPRECATED int _run_group_tests(const UnitTest *const tests, const size_t number_of_tests)
Definition: cmocka.c:3082
#define assert_string_equal(a, b)
Definition: cmocka.h:1214
void _expect_memory(const char *const function, const char *const parameter, const char *const file, const int line, const void *const memory, const size_t size, const int count)
Definition: cmocka.c:1424
void _expect_not_in_range(const char *const function, const char *const parameter, const char *const file, const int line, const LargestIntegralType minimum, const LargestIntegralType maximum, const int count)
Definition: cmocka.c:1301
void * initial_state
Definition: cmocka.h:2076
void _test_free(void *const ptr, const char *file, const int line)
Definition: cmocka.c:1810
#define assert_not_in_range(value, minimum, maximum)
Definition: cmocka.h:1320
#define assert_memory_equal(a, b, size)
Definition: cmocka.h:1254
#define will_return_count(function, value, count)
Definition: cmocka.h:317
#define expect_check(function, parameter, check_function, check_data)
Definition: cmocka.h:442
void _fail(const char *const file, const int line)
Definition: cmocka.c:1956
CMOCKA_DEPRECATED int _run_tests(const UnitTest *const tests, const size_t number_of_tests)
Definition: cmocka.c:2927
#define expect_function_call_any(function)
Definition: cmocka.h:1479
#define expect_any(function, parameter)
Definition: cmocka.h:946
#define expect_memory(function, parameter, memory, size)
Definition: cmocka.h:848
void * _test_malloc(const size_t size, const char *file, const int line)
Definition: cmocka.c:1767
int global_expecting_assert
Definition: cmocka.c:278
#define expect_string_count(function, parameter, string, count)
Definition: cmocka.h:778
void _assert_in_range(const LargestIntegralType value, const LargestIntegralType minimum, const LargestIntegralType maximum, const char *const file, const int line)
#define run_test(f)
Definition: cmocka.h:1588
#define function_called()
Definition: cmocka.h:1434
jmp_buf global_expect_assert_env
Definition: cmocka.c:277
WINBASEAPI BOOL WINAPI IsDebuggerPresent(VOID)
UnitTestFunction setup
Definition: cmocka.h:2059
#define LargestIntegralType
Definition: cmocka.h:77
void _assert_return_code(const LargestIntegralType result, size_t rlen, const LargestIntegralType error, const char *const expression, const char *const file, const int line)
void _assert_not_in_set(const LargestIntegralType value, const LargestIntegralType values[], const size_t number_of_values, const char *const file, const int line)
#define will_return_always(function, value)
Definition: cmocka.h:340
void _function_called(const char *const function, const char *const file, const int line)
Definition: cmocka.c:868
const size_t number_of_tests
Definition: cmocka.h:2062
void _expect_check(const char *const function, const char *const parameter, const char *const file, const int line, const CheckParameterValue check_function, const LargestIntegralType check_data, CheckParameterEvent *const event, const int count)
Definition: cmocka.c:962
void _expect_in_range(const char *const function, const char *const parameter, const char *const file, const int line, const LargestIntegralType minimum, const LargestIntegralType maximum, const int count)
Definition: cmocka.c:1290
struct SourceLocation SourceLocation
#define expect_value_count(function, parameter, value, count)
Definition: cmocka.h:686
void _assert_string_equal(const char *const a, const char *const b, const char *const file, const int line)
#define will_return(function, value)
Definition: cmocka.h:294
#define expect_function_call(function)
Definition: cmocka.h:1465
int(* CMFixtureFunction)(void **state)
Definition: cmocka.h:2069
#define skip()
Definition: cmocka.h:1540
#define expect_any_count(function, parameter, count)
Definition: cmocka.h:969
#define msg(flags,...)
Definition: error.h:153
void print_message(const char *const format,...) CMOCKA_PRINTF_ATTRIBUTE(1
cm_message_output
Definition: cmocka.h:2246
unsigned int uintptr_t
Definition: cmocka.h:103
#define CMOCKA_DEPRECATED
Definition: cmocka.h:149
#define expect_not_in_range_count(function, parameter, minimum, maximum, count)
Definition: cmocka.h:640
UnitTestFunctionType
Definition: cmocka.h:2039
void _expect_in_set(const char *const function, const char *const parameter, const char *const file, const int line, const LargestIntegralType values[], const size_t number_of_values, const int count)
Definition: cmocka.c:1227
const char * global_last_failed_assert
Definition: cmocka.c:279
#define check_expected(parameter)
Definition: cmocka.h:986
#define mock()
Definition: cmocka.h:210
LargestIntegralType _mock(const char *const function, const char *const file, const int line)
Definition: cmocka.c:837
void _expect_function_call(const char *const function_name, const char *const file, const int line, const int count)
Definition: cmocka.c:985
void _assert_not_in_range(const LargestIntegralType value, const LargestIntegralType minimum, const LargestIntegralType maximum, const char *const file, const int line)
#define cmocka_run_group_tests(group_tests, group_setup, group_teardown)
Definition: cmocka.h:1749
CMFixtureFunction teardown_func
Definition: cmocka.h:2075
#define assert_ptr_not_equal(a, b)
Definition: cmocka.h:1155
void _skip(const char *const file, const int line)
Definition: cmocka.c:392
void void void vprint_message(const char *const format, va_list args) CMOCKA_PRINTF_ATTRIBUTE(1
void cmocka_set_message_output(enum cm_message_output output)
Function to set the output format for a test.
Definition: cmocka.c:2431
const char * parameter_name
Definition: cmocka.h:2088
void * _test_calloc(const size_t number_of_elements, const size_t size, const char *file, const int line)
Definition: cmocka.c:1798
const char * name
Definition: cmocka.h:2053
#define test_malloc(size)
Definition: cmocka.h:1872
#define assert_memory_not_equal(a, b, size)
Definition: cmocka.h:1276
#define expect_not_memory_count(function, parameter, memory, size, count)
Definition: cmocka.h:926
const char * name
Definition: cmocka.h:2072
#define assert_null(c)
Definition: cmocka.h:1119
#define assert_string_not_equal(a, b)
Definition: cmocka.h:1232
void _expect_not_in_set(const char *const function, const char *const parameter, const char *const file, const int line, const LargestIntegralType values[], const size_t number_of_values, const int count)
Definition: cmocka.c:1238
#define expect_not_value_count(function, parameter, value, count)
Definition: cmocka.h:731
void _check_expected(const char *const function_name, const char *const parameter_name, const char *file, const int line, const LargestIntegralType value)
Definition: cmocka.c:1475
void _expect_not_value(const char *const function, const char *const parameter, const char *const file, const int line, const LargestIntegralType value, const int count)
Definition: cmocka.c:1338
void _assert_string_not_equal(const char *const a, const char *const b, const char *file, const int line)
#define mock_ptr_type(type)
Definition: cmocka.h:263
void _assert_true(const LargestIntegralType result, const char *const expression, const char *const file, const int line)
#define assert_in_range(value, minimum, maximum)
Definition: cmocka.h:1297
void _expect_value(const char *const function, const char *const parameter, const char *const file, const int line, const LargestIntegralType value, const int count)
Definition: cmocka.c:1320
#define expect_in_range_count(function, parameter, minimum, maximum, count)
Definition: cmocka.h:589
#define check_expected_ptr(parameter)
Definition: cmocka.h:1004
UnitTestFunctionType function_type
Definition: cmocka.h:2055
#define assert_false(c)
Definition: cmocka.h:1063
void(* UnitTestFunction)(void **state)
Definition: cmocka.h:2032
void * _test_realloc(void *ptr, const size_t size, const char *file, const int line)
Definition: cmocka.c:1853
static int group_setup(void **state)
#define assert_in_set(value, values, number_of_values)
Definition: cmocka.h:1342
void _assert_in_set(const LargestIntegralType value, const LargestIntegralType values[], const size_t number_of_values, const char *const file, const int line)
#define expect_assert_failure(function_call)
Definition: cmocka.h:2012
void _assert_int_not_equal(const LargestIntegralType a, const LargestIntegralType b, const char *const file, const int line)
#define expect_string(function, parameter, string)
Definition: cmocka.h:753
#define assert_int_equal(a, b)
Definition: cmocka.h:1174
#define ignore_function_calls(function)
Definition: cmocka.h:1493
int _cmocka_run_group_tests(const char *group_name, const struct CMUnitTest *const tests, const size_t num_tests, CMFixtureFunction group_setup, CMFixtureFunction group_teardown)
Definition: cmocka.c:2688