cmocka  1.1.3
Unit testing library with mock support
cmocka.h
1 /*
2  * Copyright 2008 Google Inc.
3  * Copyright 2014-2018 Andreas Schneider <asn@cryptomilk.org>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #ifndef CMOCKA_H_
18 #define CMOCKA_H_
19 
20 #ifdef _WIN32
21 # ifdef _MSC_VER
22 
23 #define __func__ __FUNCTION__
24 
25 # ifndef inline
26 #define inline __inline
27 # endif /* inline */
28 
29 # if _MSC_VER < 1500
30 # ifdef __cplusplus
31 extern "C" {
32 # endif /* __cplusplus */
33 int __stdcall IsDebuggerPresent();
34 # ifdef __cplusplus
35 } /* extern "C" */
36 # endif /* __cplusplus */
37 # endif /* _MSC_VER < 1500 */
38 # endif /* _MSC_VER */
39 #endif /* _WIN32 */
40 
58 /* If __WORDSIZE is not set, try to figure it out and default to 32 bit. */
59 #ifndef __WORDSIZE
60 # if (defined(__x86_64__) && !defined(__ILP32__)) || defined(__sparc_v9__) || defined(__sparcv9)
61 # define __WORDSIZE 64
62 # else
63 # define __WORDSIZE 32
64 # endif
65 #endif
66 
67 #ifdef DOXYGEN
68 
72 typedef uintmax_t LargestIntegralType;
73 #else /* DOXGEN */
74 #ifndef LargestIntegralType
75 # if __WORDSIZE == 64 && !defined(_WIN64)
76 # define LargestIntegralType unsigned long int
77 # else
78 # define LargestIntegralType unsigned long long int
79 # endif
80 #endif /* LargestIntegralType */
81 #endif /* DOXYGEN */
82 
83 /* Printf format used to display LargestIntegralType as a hexidecimal. */
84 #ifndef LargestIntegralTypePrintfFormat
85 # ifdef _WIN32
86 # define LargestIntegralTypePrintfFormat "0x%I64x"
87 # else
88 # if __WORDSIZE == 64
89 # define LargestIntegralTypePrintfFormat "%#lx"
90 # else
91 # define LargestIntegralTypePrintfFormat "%#llx"
92 # endif
93 # endif /* _WIN32 */
94 #endif /* LargestIntegralTypePrintfFormat */
95 
96 /* Printf format used to display LargestIntegralType as a decimal. */
97 #ifndef LargestIntegralTypePrintfFormatDecimal
98 # ifdef _WIN32
99 # define LargestIntegralTypePrintfFormatDecimal "%I64u"
100 # else
101 # if __WORDSIZE == 64
102 # define LargestIntegralTypePrintfFormatDecimal "%lu"
103 # else
104 # define LargestIntegralTypePrintfFormatDecimal "%llu"
105 # endif
106 # endif /* _WIN32 */
107 #endif /* LargestIntegralTypePrintfFormat */
108 
109 /* Perform an unsigned cast to LargestIntegralType. */
110 #define cast_to_largest_integral_type(value) \
111  ((LargestIntegralType)(value))
112 
113 /* Smallest integral type capable of holding a pointer. */
114 #if !defined(_UINTPTR_T) && !defined(_UINTPTR_T_DEFINED)
115 # if defined(_WIN32)
116  /* WIN32 is an ILP32 platform */
117  typedef unsigned int uintptr_t;
118 # elif defined(_WIN64)
119  typedef unsigned long int uintptr_t
120 # else /* _WIN32 */
121 
122 /* ILP32 and LP64 platforms */
123 # ifdef __WORDSIZE /* glibc */
124 # if __WORDSIZE == 64
125  typedef unsigned long int uintptr_t;
126 # else
127  typedef unsigned int uintptr_t;
128 # endif /* __WORDSIZE == 64 */
129 # else /* __WORDSIZE */
130 # if defined(_LP64) || defined(_I32LPx)
131  typedef unsigned long int uintptr_t;
132 # else
133  typedef unsigned int uintptr_t;
134 # endif
135 # endif /* __WORDSIZE */
136 # endif /* _WIN32 */
137 
138 # define _UINTPTR_T
139 # define _UINTPTR_T_DEFINED
140 #endif /* !defined(_UINTPTR_T) || !defined(_UINTPTR_T_DEFINED) */
141 
142 /* Perform an unsigned cast to uintptr_t. */
143 #define cast_to_pointer_integral_type(value) \
144  ((uintptr_t)((size_t)(value)))
145 
146 /* Perform a cast of a pointer to LargestIntegralType */
147 #define cast_ptr_to_largest_integral_type(value) \
148 cast_to_largest_integral_type(cast_to_pointer_integral_type(value))
149 
150 /* GCC have printf type attribute check. */
151 #ifdef __GNUC__
152 #define CMOCKA_PRINTF_ATTRIBUTE(a,b) \
153  __attribute__ ((__format__ (__printf__, a, b)))
154 #else
155 #define CMOCKA_PRINTF_ATTRIBUTE(a,b)
156 #endif /* __GNUC__ */
157 
158 #if defined(__GNUC__)
159 #define CMOCKA_DEPRECATED __attribute__ ((deprecated))
160 #elif defined(_MSC_VER)
161 #define CMOCKA_DEPRECATED __declspec(deprecated)
162 #else
163 #define CMOCKA_DEPRECATED
164 #endif
165 
166 #define WILL_RETURN_ALWAYS -1
167 #define WILL_RETURN_ONCE -2
168 
217 #ifdef DOXYGEN
218 
226 #else
227 #define mock() _mock(__func__, __FILE__, __LINE__)
228 #endif
229 
230 #ifdef DOXYGEN
231 
251 #type mock_type(#type);
252 #else
253 #define mock_type(type) ((type) mock())
254 #endif
255 
256 #ifdef DOXYGEN
257 
278 type mock_ptr_type(#type);
279 #else
280 #define mock_ptr_type(type) ((type) (uintptr_t) mock())
281 #endif
282 
283 
284 #ifdef DOXYGEN
285 
309 void will_return(#function, LargestIntegralType value);
310 #else
311 #define will_return(function, value) \
312  _will_return(#function, __FILE__, __LINE__, \
313  cast_to_largest_integral_type(value), 1)
314 #endif
315 
316 #ifdef DOXYGEN
317 
332 void will_return_count(#function, LargestIntegralType value, int count);
333 #else
334 #define will_return_count(function, value, count) \
335  _will_return(#function, __FILE__, __LINE__, \
336  cast_to_largest_integral_type(value), count)
337 #endif
338 
339 #ifdef DOXYGEN
340 
355 void will_return_always(#function, LargestIntegralType value);
356 #else
357 #define will_return_always(function, value) \
358  will_return_count(function, (value), WILL_RETURN_ALWAYS)
359 #endif
360 
361 #ifdef DOXYGEN
362 
383 void will_return_maybe(#function, LargestIntegralType value);
384 #else
385 #define will_return_maybe(function, value) \
386  will_return_count(function, (value), WILL_RETURN_ONCE)
387 #endif
388 
434 /*
435  * Add a custom parameter checking function. If the event parameter is NULL
436  * the event structure is allocated internally by this function. If event
437  * parameter is provided it must be allocated on the heap and doesn't need to
438  * be deallocated by the caller.
439  */
440 #ifdef DOXYGEN
441 
457 void expect_check(#function, #parameter, #check_function, const void *check_data);
458 #else
459 #define expect_check(function, parameter, check_function, check_data) \
460  _expect_check(#function, #parameter, __FILE__, __LINE__, check_function, \
461  cast_to_largest_integral_type(check_data), NULL, 1)
462 #endif
463 
464 #ifdef DOXYGEN
465 
479 void expect_in_set(#function, #parameter, LargestIntegralType value_array[]);
480 #else
481 #define expect_in_set(function, parameter, value_array) \
482  expect_in_set_count(function, parameter, value_array, 1)
483 #endif
484 
485 #ifdef DOXYGEN
486 
504 void expect_in_set_count(#function, #parameter, LargestIntegralType value_array[], size_t count);
505 #else
506 #define expect_in_set_count(function, parameter, value_array, count) \
507  _expect_in_set(#function, #parameter, __FILE__, __LINE__, value_array, \
508  sizeof(value_array) / sizeof((value_array)[0]), count)
509 #endif
510 
511 #ifdef DOXYGEN
512 
526 void expect_not_in_set(#function, #parameter, LargestIntegralType value_array[]);
527 #else
528 #define expect_not_in_set(function, parameter, value_array) \
529  expect_not_in_set_count(function, parameter, value_array, 1)
530 #endif
531 
532 #ifdef DOXYGEN
533 
551 void expect_not_in_set_count(#function, #parameter, LargestIntegralType value_array[], size_t count);
552 #else
553 #define expect_not_in_set_count(function, parameter, value_array, count) \
554  _expect_not_in_set( \
555  #function, #parameter, __FILE__, __LINE__, value_array, \
556  sizeof(value_array) / sizeof((value_array)[0]), count)
557 #endif
558 
559 
560 #ifdef DOXYGEN
561 
577 void expect_in_range(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum);
578 #else
579 #define expect_in_range(function, parameter, minimum, maximum) \
580  expect_in_range_count(function, parameter, minimum, maximum, 1)
581 #endif
582 
583 #ifdef DOXYGEN
584 
604 void expect_in_range_count(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum, size_t count);
605 #else
606 #define expect_in_range_count(function, parameter, minimum, maximum, count) \
607  _expect_in_range(#function, #parameter, __FILE__, __LINE__, minimum, \
608  maximum, count)
609 #endif
610 
611 #ifdef DOXYGEN
612 
628 void expect_not_in_range(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum);
629 #else
630 #define expect_not_in_range(function, parameter, minimum, maximum) \
631  expect_not_in_range_count(function, parameter, minimum, maximum, 1)
632 #endif
633 
634 #ifdef DOXYGEN
635 
655 void expect_not_in_range_count(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum, size_t count);
656 #else
657 #define expect_not_in_range_count(function, parameter, minimum, maximum, \
658  count) \
659  _expect_not_in_range(#function, #parameter, __FILE__, __LINE__, \
660  minimum, maximum, count)
661 #endif
662 
663 #ifdef DOXYGEN
664 
677 void expect_value(#function, #parameter, LargestIntegralType value);
678 #else
679 #define expect_value(function, parameter, value) \
680  expect_value_count(function, parameter, value, 1)
681 #endif
682 
683 #ifdef DOXYGEN
684 
701 void expect_value_count(#function, #parameter, LargestIntegralType value, size_t count);
702 #else
703 #define expect_value_count(function, parameter, value, count) \
704  _expect_value(#function, #parameter, __FILE__, __LINE__, \
705  cast_to_largest_integral_type(value), count)
706 #endif
707 
708 #ifdef DOXYGEN
709 
722 void expect_not_value(#function, #parameter, LargestIntegralType value);
723 #else
724 #define expect_not_value(function, parameter, value) \
725  expect_not_value_count(function, parameter, value, 1)
726 #endif
727 
728 #ifdef DOXYGEN
729 
746 void expect_not_value_count(#function, #parameter, LargestIntegralType value, size_t count);
747 #else
748 #define expect_not_value_count(function, parameter, value, count) \
749  _expect_not_value(#function, #parameter, __FILE__, __LINE__, \
750  cast_to_largest_integral_type(value), count)
751 #endif
752 
753 #ifdef DOXYGEN
754 
768 void expect_string(#function, #parameter, const char *string);
769 #else
770 #define expect_string(function, parameter, string) \
771  expect_string_count(function, parameter, string, 1)
772 #endif
773 
774 #ifdef DOXYGEN
775 
793 void expect_string_count(#function, #parameter, const char *string, size_t count);
794 #else
795 #define expect_string_count(function, parameter, string, count) \
796  _expect_string(#function, #parameter, __FILE__, __LINE__, \
797  (const char*)(string), count)
798 #endif
799 
800 #ifdef DOXYGEN
801 
815 void expect_not_string(#function, #parameter, const char *string);
816 #else
817 #define expect_not_string(function, parameter, string) \
818  expect_not_string_count(function, parameter, string, 1)
819 #endif
820 
821 #ifdef DOXYGEN
822 
840 void expect_not_string_count(#function, #parameter, const char *string, size_t count);
841 #else
842 #define expect_not_string_count(function, parameter, string, count) \
843  _expect_not_string(#function, #parameter, __FILE__, __LINE__, \
844  (const char*)(string), count)
845 #endif
846 
847 #ifdef DOXYGEN
848 
863 void expect_memory(#function, #parameter, void *memory, size_t size);
864 #else
865 #define expect_memory(function, parameter, memory, size) \
866  expect_memory_count(function, parameter, memory, size, 1)
867 #endif
868 
869 #ifdef DOXYGEN
870 
890 void expect_memory_count(#function, #parameter, void *memory, size_t size, size_t count);
891 #else
892 #define expect_memory_count(function, parameter, memory, size, count) \
893  _expect_memory(#function, #parameter, __FILE__, __LINE__, \
894  (const void*)(memory), size, count)
895 #endif
896 
897 #ifdef DOXYGEN
898 
914 void expect_not_memory(#function, #parameter, void *memory, size_t size);
915 #else
916 #define expect_not_memory(function, parameter, memory, size) \
917  expect_not_memory_count(function, parameter, memory, size, 1)
918 #endif
919 
920 #ifdef DOXYGEN
921 
941 void expect_not_memory_count(#function, #parameter, void *memory, size_t size, size_t count);
942 #else
943 #define expect_not_memory_count(function, parameter, memory, size, count) \
944  _expect_not_memory(#function, #parameter, __FILE__, __LINE__, \
945  (const void*)(memory), size, count)
946 #endif
947 
948 
949 #ifdef DOXYGEN
950 
961 void expect_any(#function, #parameter);
962 #else
963 #define expect_any(function, parameter) \
964  expect_any_count(function, parameter, 1)
965 #endif
966 
967 #ifdef DOXYGEN
968 
984 void expect_any_count(#function, #parameter, size_t count);
985 #else
986 #define expect_any_count(function, parameter, count) \
987  _expect_any(#function, #parameter, __FILE__, __LINE__, count)
988 #endif
989 
990 #ifdef DOXYGEN
991 
1001 void check_expected(#parameter);
1002 #else
1003 #define check_expected(parameter) \
1004  _check_expected(__func__, #parameter, __FILE__, __LINE__, \
1005  cast_to_largest_integral_type(parameter))
1006 #endif
1007 
1008 #ifdef DOXYGEN
1009 
1019 void check_expected_ptr(#parameter);
1020 #else
1021 #define check_expected_ptr(parameter) \
1022  _check_expected(__func__, #parameter, __FILE__, __LINE__, \
1023  cast_ptr_to_largest_integral_type(parameter))
1024 #endif
1025 
1047 #ifdef DOXYGEN
1048 
1060 void assert_true(scalar expression);
1061 #else
1062 #define assert_true(c) _assert_true(cast_to_largest_integral_type(c), #c, \
1063  __FILE__, __LINE__)
1064 #endif
1065 
1066 #ifdef DOXYGEN
1067 
1078 void assert_false(scalar expression);
1079 #else
1080 #define assert_false(c) _assert_true(!(cast_to_largest_integral_type(c)), #c, \
1081  __FILE__, __LINE__)
1082 #endif
1083 
1084 #ifdef DOXYGEN
1085 
1097 void assert_return_code(int rc, int error);
1098 #else
1099 #define assert_return_code(rc, error) \
1100  _assert_return_code(cast_to_largest_integral_type(rc), \
1101  sizeof(rc), \
1102  cast_to_largest_integral_type(error), \
1103  #rc, __FILE__, __LINE__)
1104 #endif
1105 
1106 #ifdef DOXYGEN
1107 
1117 void assert_non_null(void *pointer);
1118 #else
1119 #define assert_non_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_null(void *pointer);
1135 #else
1136 #define assert_null(c) _assert_true(!(cast_ptr_to_largest_integral_type(c)), #c, \
1137 __FILE__, __LINE__)
1138 #endif
1139 
1140 #ifdef DOXYGEN
1141 
1151 void assert_ptr_equal(void *a, void *b);
1152 #else
1153 #define assert_ptr_equal(a, b) \
1154  _assert_int_equal(cast_ptr_to_largest_integral_type(a), \
1155  cast_ptr_to_largest_integral_type(b), \
1156  __FILE__, __LINE__)
1157 #endif
1158 
1159 #ifdef DOXYGEN
1160 
1170 void assert_ptr_not_equal(void *a, void *b);
1171 #else
1172 #define assert_ptr_not_equal(a, b) \
1173  _assert_int_not_equal(cast_ptr_to_largest_integral_type(a), \
1174  cast_ptr_to_largest_integral_type(b), \
1175  __FILE__, __LINE__)
1176 #endif
1177 
1178 #ifdef DOXYGEN
1179 
1189 void assert_int_equal(int a, int b);
1190 #else
1191 #define assert_int_equal(a, b) \
1192  _assert_int_equal(cast_to_largest_integral_type(a), \
1193  cast_to_largest_integral_type(b), \
1194  __FILE__, __LINE__)
1195 #endif
1196 
1197 #ifdef DOXYGEN
1198 
1210 void assert_int_not_equal(int a, int b);
1211 #else
1212 #define assert_int_not_equal(a, b) \
1213  _assert_int_not_equal(cast_to_largest_integral_type(a), \
1214  cast_to_largest_integral_type(b), \
1215  __FILE__, __LINE__)
1216 #endif
1217 
1218 #ifdef DOXYGEN
1219 
1229 void assert_string_equal(const char *a, const char *b);
1230 #else
1231 #define assert_string_equal(a, b) \
1232  _assert_string_equal((const char*)(a), (const char*)(b), __FILE__, \
1233  __LINE__)
1234 #endif
1235 
1236 #ifdef DOXYGEN
1237 
1247 void assert_string_not_equal(const char *a, const char *b);
1248 #else
1249 #define assert_string_not_equal(a, b) \
1250  _assert_string_not_equal((const char*)(a), (const char*)(b), __FILE__, \
1251  __LINE__)
1252 #endif
1253 
1254 #ifdef DOXYGEN
1255 
1269 void assert_memory_equal(const void *a, const void *b, size_t size);
1270 #else
1271 #define assert_memory_equal(a, b, size) \
1272  _assert_memory_equal((const void*)(a), (const void*)(b), size, __FILE__, \
1273  __LINE__)
1274 #endif
1275 
1276 #ifdef DOXYGEN
1277 
1291 void assert_memory_not_equal(const void *a, const void *b, size_t size);
1292 #else
1293 #define assert_memory_not_equal(a, b, size) \
1294  _assert_memory_not_equal((const void*)(a), (const void*)(b), size, \
1295  __FILE__, __LINE__)
1296 #endif
1297 
1298 #ifdef DOXYGEN
1299 
1313 #else
1314 #define assert_in_range(value, minimum, maximum) \
1315  _assert_in_range( \
1316  cast_to_largest_integral_type(value), \
1317  cast_to_largest_integral_type(minimum), \
1318  cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
1319 #endif
1320 
1321 #ifdef DOXYGEN
1322 
1336 #else
1337 #define assert_not_in_range(value, minimum, maximum) \
1338  _assert_not_in_range( \
1339  cast_to_largest_integral_type(value), \
1340  cast_to_largest_integral_type(minimum), \
1341  cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
1342 #endif
1343 
1344 #ifdef DOXYGEN
1345 
1357 void assert_in_set(LargestIntegralType value, LargestIntegralType values[], size_t count);
1358 #else
1359 #define assert_in_set(value, values, number_of_values) \
1360  _assert_in_set(value, values, number_of_values, __FILE__, __LINE__)
1361 #endif
1362 
1363 #ifdef DOXYGEN
1364 
1376 void assert_not_in_set(LargestIntegralType value, LargestIntegralType values[], size_t count);
1377 #else
1378 #define assert_not_in_set(value, values, number_of_values) \
1379  _assert_not_in_set(value, values, number_of_values, __FILE__, __LINE__)
1380 #endif
1381 
1442 #ifdef DOXYGEN
1443 
1449 void function_called(void);
1450 #else
1451 #define function_called() _function_called(__func__, __FILE__, __LINE__)
1452 #endif
1453 
1454 #ifdef DOXYGEN
1455 
1465 void expect_function_calls(#function, const int times);
1466 #else
1467 #define expect_function_calls(function, times) \
1468  _expect_function_call(#function, __FILE__, __LINE__, times)
1469 #endif
1470 
1471 #ifdef DOXYGEN
1472 
1480 void expect_function_call(#function);
1481 #else
1482 #define expect_function_call(function) \
1483  _expect_function_call(#function, __FILE__, __LINE__, 1)
1484 #endif
1485 
1486 #ifdef DOXYGEN
1487 
1494 void expect_function_call_any(#function);
1495 #else
1496 #define expect_function_call_any(function) \
1497  _expect_function_call(#function, __FILE__, __LINE__, -1)
1498 #endif
1499 
1500 #ifdef DOXYGEN
1501 
1508 void ignore_function_calls(#function);
1509 #else
1510 #define ignore_function_calls(function) \
1511  _expect_function_call(#function, __FILE__, __LINE__, -2)
1512 #endif
1513 
1542 #ifdef DOXYGEN
1543 
1546 void fail(void);
1547 #else
1548 #define fail() _fail(__FILE__, __LINE__)
1549 #endif
1550 
1551 #ifdef DOXYGEN
1552 
1555 void skip(void);
1556 #else
1557 #define skip() _skip(__FILE__, __LINE__)
1558 #endif
1559 
1560 #ifdef DOXYGEN
1561 
1575 void fail_msg(const char *msg, ...);
1576 #else
1577 #define fail_msg(msg, ...) do { \
1578  print_error("ERROR: " msg "\n", ##__VA_ARGS__); \
1579  fail(); \
1580 } while (0)
1581 #endif
1582 
1583 #ifdef DOXYGEN
1584 
1603 int run_test(#function);
1604 #else
1605 #define run_test(f) _run_test(#f, f, NULL, UNIT_TEST_FUNCTION_TYPE_TEST, NULL)
1606 #endif
1607 
1608 static inline void _unit_test_dummy(void **state) {
1609  (void)state;
1610 }
1611 
1616 #define unit_test(f) { #f, f, UNIT_TEST_FUNCTION_TYPE_TEST }
1617 
1618 #define _unit_test_setup(test, setup) \
1619  { #test "_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_SETUP }
1620 
1625 #define unit_test_setup(test, setup) \
1626  _unit_test_setup(test, setup), \
1627  unit_test(test), \
1628  _unit_test_teardown(test, _unit_test_dummy)
1629 
1630 #define _unit_test_teardown(test, teardown) \
1631  { #test "_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_TEARDOWN }
1632 
1637 #define unit_test_teardown(test, teardown) \
1638  _unit_test_setup(test, _unit_test_dummy), \
1639  unit_test(test), \
1640  _unit_test_teardown(test, teardown)
1641 
1646 #define group_test_setup(setup) \
1647  { "group_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_GROUP_SETUP }
1648 
1653 #define group_test_teardown(teardown) \
1654  { "group_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_GROUP_TEARDOWN }
1655 
1663 #define unit_test_setup_teardown(test, setup, teardown) \
1664  _unit_test_setup(test, setup), \
1665  unit_test(test), \
1666  _unit_test_teardown(test, teardown)
1667 
1668 
1670 #define cmocka_unit_test(f) { #f, f, NULL, NULL, NULL }
1671 
1673 #define cmocka_unit_test_setup(f, setup) { #f, f, setup, NULL, NULL }
1674 
1676 #define cmocka_unit_test_teardown(f, teardown) { #f, f, NULL, teardown, NULL }
1677 
1682 #define cmocka_unit_test_setup_teardown(f, setup, teardown) { #f, f, setup, teardown, NULL }
1683 
1691 #define cmocka_unit_test_prestate(f, state) { #f, f, NULL, NULL, state }
1692 
1700 #define cmocka_unit_test_prestate_setup_teardown(f, setup, teardown, state) { #f, f, setup, teardown, state }
1701 
1702 #define run_tests(tests) _run_tests(tests, sizeof(tests) / sizeof((tests)[0]))
1703 #define run_group_tests(tests) _run_group_tests(tests, sizeof(tests) / sizeof((tests)[0]))
1704 
1705 #ifdef DOXYGEN
1706 
1762 int cmocka_run_group_tests(const struct CMUnitTest group_tests[],
1763  CMFixtureFunction group_setup,
1764  CMFixtureFunction group_teardown);
1765 #else
1766 # define cmocka_run_group_tests(group_tests, group_setup, group_teardown) \
1767  _cmocka_run_group_tests(#group_tests, group_tests, sizeof(group_tests) / sizeof((group_tests)[0]), group_setup, group_teardown)
1768 #endif
1769 
1770 #ifdef DOXYGEN
1771 
1830 int cmocka_run_group_tests_name(const char *group_name,
1831  const struct CMUnitTest group_tests[],
1832  CMFixtureFunction group_setup,
1833  CMFixtureFunction group_teardown);
1834 #else
1835 # define cmocka_run_group_tests_name(group_name, group_tests, group_setup, group_teardown) \
1836  _cmocka_run_group_tests(group_name, group_tests, sizeof(group_tests) / sizeof((group_tests)[0]), group_setup, group_teardown)
1837 #endif
1838 
1864 #ifdef DOXYGEN
1865 
1887 void *test_malloc(size_t size);
1888 #else
1889 #define test_malloc(size) _test_malloc(size, __FILE__, __LINE__)
1890 #endif
1891 
1892 #ifdef DOXYGEN
1893 
1906 void *test_calloc(size_t nmemb, size_t size);
1907 #else
1908 #define test_calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__)
1909 #endif
1910 
1911 #ifdef DOXYGEN
1912 
1922 void *test_realloc(void *ptr, size_t size);
1923 #else
1924 #define test_realloc(ptr, size) _test_realloc(ptr, size, __FILE__, __LINE__)
1925 #endif
1926 
1927 #ifdef DOXYGEN
1928 
1935 void test_free(void *ptr);
1936 #else
1937 #define test_free(ptr) _test_free(ptr, __FILE__, __LINE__)
1938 #endif
1939 
1940 /* Redirect malloc, calloc and free to the unit test allocators. */
1941 #ifdef UNIT_TESTING
1942 #define malloc test_malloc
1943 #define realloc test_realloc
1944 #define calloc test_calloc
1945 #define free test_free
1946 #endif /* UNIT_TESTING */
1947 
2001 void mock_assert(const int result, const char* const expression,
2002  const char * const file, const int line);
2003 
2004 #ifdef DOXYGEN
2005 
2027 void expect_assert_failure(function fn_call);
2028 #else
2029 #define expect_assert_failure(function_call) \
2030  { \
2031  const int result = setjmp(global_expect_assert_env); \
2032  global_expecting_assert = 1; \
2033  if (result) { \
2034  print_message("Expected assertion %s occurred\n", \
2035  global_last_failed_assert); \
2036  global_expecting_assert = 0; \
2037  } else { \
2038  function_call ; \
2039  global_expecting_assert = 0; \
2040  print_error("Expected assert in %s\n", #function_call); \
2041  _fail(__FILE__, __LINE__); \
2042  } \
2043  }
2044 #endif
2045 
2048 /* Function prototype for setup, test and teardown functions. */
2049 typedef void (*UnitTestFunction)(void **state);
2050 
2051 /* Function that determines whether a function parameter value is correct. */
2052 typedef int (*CheckParameterValue)(const LargestIntegralType value,
2053  const LargestIntegralType check_value_data);
2054 
2055 /* Type of the unit test function. */
2056 typedef enum UnitTestFunctionType {
2057  UNIT_TEST_FUNCTION_TYPE_TEST = 0,
2058  UNIT_TEST_FUNCTION_TYPE_SETUP,
2059  UNIT_TEST_FUNCTION_TYPE_TEARDOWN,
2060  UNIT_TEST_FUNCTION_TYPE_GROUP_SETUP,
2061  UNIT_TEST_FUNCTION_TYPE_GROUP_TEARDOWN,
2062 } UnitTestFunctionType;
2063 
2064 /*
2065  * Stores a unit test function with its name and type.
2066  * NOTE: Every setup function must be paired with a teardown function. It's
2067  * possible to specify NULL function pointers.
2068  */
2069 typedef struct UnitTest {
2070  const char* name;
2071  UnitTestFunction function;
2072  UnitTestFunctionType function_type;
2073 } UnitTest;
2074 
2075 typedef struct GroupTest {
2076  UnitTestFunction setup;
2077  UnitTestFunction teardown;
2078  const UnitTest *tests;
2079  const size_t number_of_tests;
2080 } GroupTest;
2081 
2082 /* Function prototype for test functions. */
2083 typedef void (*CMUnitTestFunction)(void **state);
2084 
2085 /* Function prototype for setup and teardown functions. */
2086 typedef int (*CMFixtureFunction)(void **state);
2087 
2088 struct CMUnitTest {
2089  const char *name;
2090  CMUnitTestFunction test_func;
2091  CMFixtureFunction setup_func;
2092  CMFixtureFunction teardown_func;
2093  void *initial_state;
2094 };
2095 
2096 /* Location within some source code. */
2097 typedef struct SourceLocation {
2098  const char* file;
2099  int line;
2100 } SourceLocation;
2101 
2102 /* Event that's called to check a parameter value. */
2103 typedef struct CheckParameterEvent {
2104  SourceLocation location;
2105  const char *parameter_name;
2106  CheckParameterValue check_value;
2107  LargestIntegralType check_value_data;
2109 
2110 /* Used by expect_assert_failure() and mock_assert(). */
2111 extern int global_expecting_assert;
2112 extern jmp_buf global_expect_assert_env;
2113 extern const char * global_last_failed_assert;
2114 
2115 /* Retrieves a value for the given function, as set by "will_return". */
2116 LargestIntegralType _mock(const char * const function, const char* const file,
2117  const int line);
2118 
2119 void _expect_function_call(
2120  const char * const function_name,
2121  const char * const file,
2122  const int line,
2123  const int count);
2124 
2125 void _function_called(const char * const function, const char* const file,
2126  const int line);
2127 
2128 void _expect_check(
2129  const char* const function, const char* const parameter,
2130  const char* const file, const int line,
2131  const CheckParameterValue check_function,
2132  const LargestIntegralType check_data, CheckParameterEvent * const event,
2133  const int count);
2134 
2135 void _expect_in_set(
2136  const char* const function, const char* const parameter,
2137  const char* const file, const int line, const LargestIntegralType values[],
2138  const size_t number_of_values, const int count);
2139 void _expect_not_in_set(
2140  const char* const function, const char* const parameter,
2141  const char* const file, const int line, const LargestIntegralType values[],
2142  const size_t number_of_values, const int count);
2143 
2144 void _expect_in_range(
2145  const char* const function, const char* const parameter,
2146  const char* const file, const int line,
2147  const LargestIntegralType minimum,
2148  const LargestIntegralType maximum, const int count);
2149 void _expect_not_in_range(
2150  const char* const function, const char* const parameter,
2151  const char* const file, const int line,
2152  const LargestIntegralType minimum,
2153  const LargestIntegralType maximum, const int count);
2154 
2155 void _expect_value(
2156  const char* const function, const char* const parameter,
2157  const char* const file, const int line, const LargestIntegralType value,
2158  const int count);
2159 void _expect_not_value(
2160  const char* const function, const char* const parameter,
2161  const char* const file, const int line, const LargestIntegralType value,
2162  const int count);
2163 
2164 void _expect_string(
2165  const char* const function, const char* const parameter,
2166  const char* const file, const int line, const char* string,
2167  const int count);
2168 void _expect_not_string(
2169  const char* const function, const char* const parameter,
2170  const char* const file, const int line, const char* string,
2171  const int count);
2172 
2173 void _expect_memory(
2174  const char* const function, const char* const parameter,
2175  const char* const file, const int line, const void* const memory,
2176  const size_t size, const int count);
2177 void _expect_not_memory(
2178  const char* const function, const char* const parameter,
2179  const char* const file, const int line, const void* const memory,
2180  const size_t size, const int count);
2181 
2182 void _expect_any(
2183  const char* const function, const char* const parameter,
2184  const char* const file, const int line, const int count);
2185 
2186 void _check_expected(
2187  const char * const function_name, const char * const parameter_name,
2188  const char* file, const int line, const LargestIntegralType value);
2189 
2190 void _will_return(const char * const function_name, const char * const file,
2191  const int line, const LargestIntegralType value,
2192  const int count);
2193 void _assert_true(const LargestIntegralType result,
2194  const char* const expression,
2195  const char * const file, const int line);
2196 void _assert_return_code(const LargestIntegralType result,
2197  size_t rlen,
2198  const LargestIntegralType error,
2199  const char * const expression,
2200  const char * const file,
2201  const int line);
2202 void _assert_int_equal(
2203  const LargestIntegralType a, const LargestIntegralType b,
2204  const char * const file, const int line);
2205 void _assert_int_not_equal(
2206  const LargestIntegralType a, const LargestIntegralType b,
2207  const char * const file, const int line);
2208 void _assert_string_equal(const char * const a, const char * const b,
2209  const char * const file, const int line);
2210 void _assert_string_not_equal(const char * const a, const char * const b,
2211  const char *file, const int line);
2212 void _assert_memory_equal(const void * const a, const void * const b,
2213  const size_t size, const char* const file,
2214  const int line);
2215 void _assert_memory_not_equal(const void * const a, const void * const b,
2216  const size_t size, const char* const file,
2217  const int line);
2218 void _assert_in_range(
2219  const LargestIntegralType value, const LargestIntegralType minimum,
2220  const LargestIntegralType maximum, const char* const file, const int line);
2221 void _assert_not_in_range(
2222  const LargestIntegralType value, const LargestIntegralType minimum,
2223  const LargestIntegralType maximum, const char* const file, const int line);
2224 void _assert_in_set(
2225  const LargestIntegralType value, const LargestIntegralType values[],
2226  const size_t number_of_values, const char* const file, const int line);
2227 void _assert_not_in_set(
2228  const LargestIntegralType value, const LargestIntegralType values[],
2229  const size_t number_of_values, const char* const file, const int line);
2230 
2231 void* _test_malloc(const size_t size, const char* file, const int line);
2232 void* _test_realloc(void *ptr, const size_t size, const char* file, const int line);
2233 void* _test_calloc(const size_t number_of_elements, const size_t size,
2234  const char* file, const int line);
2235 void _test_free(void* const ptr, const char* file, const int line);
2236 
2237 void _fail(const char * const file, const int line);
2238 
2239 void _skip(const char * const file, const int line);
2240 
2241 int _run_test(
2242  const char * const function_name, const UnitTestFunction Function,
2243  void ** const volatile state, const UnitTestFunctionType function_type,
2244  const void* const heap_check_point);
2245 CMOCKA_DEPRECATED int _run_tests(const UnitTest * const tests,
2246  const size_t number_of_tests);
2247 CMOCKA_DEPRECATED int _run_group_tests(const UnitTest * const tests,
2248  const size_t number_of_tests);
2249 
2250 /* Test runner */
2251 int _cmocka_run_group_tests(const char *group_name,
2252  const struct CMUnitTest * const tests,
2253  const size_t num_tests,
2254  CMFixtureFunction group_setup,
2255  CMFixtureFunction group_teardown);
2256 
2257 /* Standard output and error print methods. */
2258 void print_message(const char* const format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2);
2259 void print_error(const char* const format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2);
2260 void vprint_message(const char* const format, va_list args) CMOCKA_PRINTF_ATTRIBUTE(1, 0);
2261 void vprint_error(const char* const format, va_list args) CMOCKA_PRINTF_ATTRIBUTE(1, 0);
2262 
2263 enum cm_message_output {
2264  CM_OUTPUT_STDOUT,
2265  CM_OUTPUT_SUBUNIT,
2266  CM_OUTPUT_TAP,
2267  CM_OUTPUT_XML,
2268 };
2269 
2281 void cmocka_set_message_output(enum cm_message_output output);
2282 
2283 
2294 void cmocka_set_test_filter(const char *pattern);
2295 
2298 #endif /* CMOCKA_H_ */
void assert_null(void *pointer)
Assert that the given pointer is NULL.
void fail(void)
Forces the test to fail immediately and quit.
void expect_any(#function, #parameter)
Add an event to check if a parameter (of any value) has been passed.
void expect_not_memory_count(#function, #parameter, void *memory, size_t size, size_t count)
Add an event to repeatedly check if the parameter doesn&#39;t match an area of memory.
void expect_not_string_count(#function, #parameter, const char *string, size_t count)
Add an event to check if the parameter value isn&#39;t equal to the provided string.
void will_return_always(#function, LargestIntegralType value)
Store a value that will be always returned by mock().
void expect_any_count(#function, #parameter, size_t count)
Add an event to repeatedly check if a parameter (of any value) has been passed.
void expect_check(#function, #parameter, #check_function, const void *check_data)
Add a custom parameter checking function.
void assert_memory_not_equal(const void *a, const void *b, size_t size)
Assert that the two given areas of memory are not equal.
void expect_in_set(#function, #parameter, LargestIntegralType value_array[])
Add an event to check if the parameter value is part of the provided array.
void expect_not_in_set_count(#function, #parameter, LargestIntegralType value_array[], size_t count)
Add an event to check if the parameter value is not part of the provided array.
void expect_memory(#function, #parameter, void *memory, size_t size)
Add an event to check if the parameter does match an area of memory.
LargestIntegralType mock(void)
Retrieve a return value of the current function.
void fail_msg(const char *msg,...)
Forces the test to fail immediately and quit, printing the reason.
void assert_in_range(LargestIntegralType value, LargestIntegralType minimum, LargestIntegralType maximum)
Assert that the specified value is not smaller than the minimum and and not greater than the maximum...
void expect_in_range(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum)
Add an event to check a parameter is inside a numerical range. The check would succeed if minimum <= ...
void will_return(#function, LargestIntegralType value)
Store a value to be returned by mock() later.
void assert_ptr_equal(void *a, void *b)
Assert that the two given pointers are equal.
void will_return_count(#function, LargestIntegralType value, int count)
Store a value to be returned by mock() later.
void expect_function_call(#function)
Store expected single call to a mock to be checked by function_called() later.
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:1652
int cmocka_run_group_tests(const struct CMUnitTest group_tests[], CMFixtureFunction group_setup, CMFixtureFunction group_teardown)
Run tests specified by an array of CMUnitTest structures.
void expect_in_range_count(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum, size_t count)
Add an event to repeatedly check a parameter is inside a numerical range. The check would succeed if ...
void will_return_maybe(#function, LargestIntegralType value)
Store a value that may be always returned by mock().
void expect_string_count(#function, #parameter, const char *string, size_t count)
Add an event to check if the parameter value is equal to the provided string.
void expect_function_call_any(#function)
Expects function_called() from given mock at least once.
void expect_not_string(#function, #parameter, const char *string)
Add an event to check if the parameter value isn&#39;t equal to the provided string.
void expect_not_in_range(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum)
Add an event to check a parameter is outside a numerical range. The check would succeed if minimum > ...
void assert_string_equal(const char *a, const char *b)
Assert that the two given strings are equal.
void * test_malloc(size_t size)
Test function overriding malloc.
void assert_return_code(int rc, int error)
Assert that the return_code is greater than or equal to 0.
void assert_non_null(void *pointer)
Assert that the given pointer is non-NULL.
void assert_not_in_set(LargestIntegralType value, LargestIntegralType values[], size_t count)
Assert that the specified value is not within a set.
uintmax_t LargestIntegralType
Definition: cmocka.h:72
void expect_in_set_count(#function, #parameter, LargestIntegralType value_array[], size_t count)
Add an event to check if the parameter value is part of the provided array.
void assert_true(scalar expression)
Assert that the given expression is true.
void assert_string_not_equal(const char *a, const char *b)
Assert that the two given strings are not equal.
void assert_false(scalar expression)
Assert that the given expression is false.
void check_expected_ptr(#parameter)
Determine whether a function parameter is correct.
void expect_function_calls(#function, const int times)
Store expected call(s) to a mock to be checked by function_called() later.
type mock_ptr_type(#type)
Retrieve a typed return value of the current function.
Definition: cmocka.h:2075
void expect_not_in_range_count(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum, size_t count)
Add an event to repeatedly check a parameter is outside a numerical range. The check would succeed if...
int cmocka_run_group_tests_name(const char *group_name, const struct CMUnitTest group_tests[], CMFixtureFunction group_setup, CMFixtureFunction group_teardown)
Run tests specified by an array of CMUnitTest structures and specify a name.
void assert_ptr_not_equal(void *a, void *b)
Assert that the two given pointers are not equal.
void assert_int_not_equal(int a, int b)
Assert that the two given integers are not equal.
void expect_memory_count(#function, #parameter, void *memory, size_t size, size_t count)
Add an event to repeatedly check if the parameter does match an area of memory.
int run_test(#function)
Generic method to run a single test.
void function_called(void)
Check that current mocked function is being called in the expected order.
void skip(void)
Forces the test to not be executed, but marked as skipped.
Definition: cmocka.h:2097
void cmocka_set_message_output(enum cm_message_output output)
Function to set the output format for a test.
Definition: cmocka.c:2608
void * test_calloc(size_t nmemb, size_t size)
Test function overriding calloc.
void assert_not_in_range(LargestIntegralType value, LargestIntegralType minimum, LargestIntegralType maximum)
Assert that the specified value is smaller than the minimum or greater than the maximum.
void ignore_function_calls(#function)
Ignores function_called() invocations from given mock function.
Definition: cmocka.h:2088
void expect_not_in_set(#function, #parameter, LargestIntegralType value_array[])
Add an event to check if the parameter value is not part of the provided array.
void assert_int_equal(int a, int b)
Assert that the two given integers are equal.
void assert_memory_equal(const void *a, const void *b, size_t size)
Assert that the two given areas of memory are equal, otherwise fail.
void check_expected(#parameter)
Determine whether a function parameter is correct.
void expect_not_value(#function, #parameter, LargestIntegralType value)
Add an event to check if a parameter isn&#39;t the given value.
void * test_realloc(void *ptr, size_t size)
Test function overriding realloc which detects buffer overruns and memoery leaks. ...
void expect_not_value_count(#function, #parameter, LargestIntegralType value, size_t count)
Add an event to repeatedly check if a parameter isn&#39;t the given value.
void expect_not_memory(#function, #parameter, void *memory, size_t size)
Add an event to check if the parameter doesn&#39;t match an area of memory.
void expect_string(#function, #parameter, const char *string)
Add an event to check if the parameter value is equal to the provided string.
void expect_value(#function, #parameter, LargestIntegralType value)
Add an event to check if a parameter is the given value.
void test_free(void *ptr)
Test function overriding free(3).
void expect_assert_failure(function fn_call)
Ensure that mock_assert() is called.
Definition: cmocka.h:2069
void cmocka_set_test_filter(const char *pattern)
Set a pattern to only run the test matching the pattern.
Definition: cmocka.c:2613
void expect_value_count(#function, #parameter, LargestIntegralType value, size_t count)
Add an event to repeatedly check if a parameter is the given value.
Definition: cmocka.h:2103
void assert_in_set(LargestIntegralType value, LargestIntegralType values[], size_t count)
Assert that the specified value is within a set.