cmocka  1.1.7
Unit testing library with mock support
cmocka.h
1 /*
2  * Copyright 2008 Google Inc.
3  * Copyright 2014-2022 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 
60 /* If __WORDSIZE is not set, try to figure it out and default to 32 bit. */
61 #ifndef __WORDSIZE
62 # if (defined(__x86_64__) && !defined(__ILP32__)) || defined(__sparc_v9__) || defined(__sparcv9)
63 # define __WORDSIZE 64
64 # else
65 # define __WORDSIZE 32
66 # endif
67 #endif
68 
69 #ifdef DOXYGEN
74 typedef uintmax_t LargestIntegralType;
75 #else /* DOXGEN */
76 #ifndef LargestIntegralType
77 # if __WORDSIZE == 64 && !defined(_WIN64)
78 # define LargestIntegralType unsigned long int
79 # else
80 # define LargestIntegralType unsigned long long int
81 # endif
82 #endif /* LargestIntegralType */
83 #endif /* DOXYGEN */
84 
85 /* Printf format used to display LargestIntegralType as a hexidecimal. */
86 #ifndef LargestIntegralTypePrintfFormat
87 # ifdef _WIN32
88 # define LargestIntegralTypePrintfFormat "0x%I64x"
89 # else
90 # if __WORDSIZE == 64
91 # define LargestIntegralTypePrintfFormat "%#lx"
92 # else
93 # define LargestIntegralTypePrintfFormat "%#llx"
94 # endif
95 # endif /* _WIN32 */
96 #endif /* LargestIntegralTypePrintfFormat */
97 
98 /* Printf format used to display LargestIntegralType as a decimal. */
99 #ifndef LargestIntegralTypePrintfFormatDecimal
100 # ifdef _WIN32
101 # define LargestIntegralTypePrintfFormatDecimal "%I64u"
102 # else
103 # if __WORDSIZE == 64
104 # define LargestIntegralTypePrintfFormatDecimal "%lu"
105 # else
106 # define LargestIntegralTypePrintfFormatDecimal "%llu"
107 # endif
108 # endif /* _WIN32 */
109 #endif /* LargestIntegralTypePrintfFormat */
110 
111 #ifndef FloatPrintfFormat
112 # define FloatPrintfFormat "%f"
113 #endif /* FloatPrintfFormat */
114 
115 #ifndef DoublePrintfFormat
116 # define DoublePrintfFormat "%f"
117 #endif /* DoublePrintfFormat */
118 
119 /* Perform an unsigned cast to LargestIntegralType. */
120 #define cast_to_largest_integral_type(value) \
121  ((LargestIntegralType)(value))
122 
123 /* Smallest integral type capable of holding a pointer. */
124 #if !defined(_UINTPTR_T) && !defined(_UINTPTR_T_DEFINED) && !defined(HAVE_UINTPTR_T)
125 # if defined(_WIN32)
126  /* WIN32 is an ILP32 platform */
127  typedef unsigned int uintptr_t;
128 # elif defined(_WIN64)
129  typedef unsigned long int uintptr_t;
130 # else /* _WIN32 */
131 
132 /* ILP32 and LP64 platforms */
133 # ifdef __WORDSIZE /* glibc */
134 # if __WORDSIZE == 64
135  typedef unsigned long int uintptr_t;
136 # else
137  typedef unsigned int uintptr_t;
138 # endif /* __WORDSIZE == 64 */
139 # else /* __WORDSIZE */
140 # if defined(_LP64) || defined(_I32LPx)
141  typedef unsigned long int uintptr_t;
142 # else
143  typedef unsigned int uintptr_t;
144 # endif
145 # endif /* __WORDSIZE */
146 # endif /* _WIN32 */
147 
148 # define _UINTPTR_T
149 # define _UINTPTR_T_DEFINED
150 #endif /* !defined(_UINTPTR_T) || !defined(_UINTPTR_T_DEFINED) */
151 
152 /* Perform an unsigned cast to uintptr_t. */
153 #define cast_to_pointer_integral_type(value) \
154  ((uintptr_t)((size_t)(value)))
155 
156 /* Perform a cast of a pointer to LargestIntegralType */
157 #define cast_ptr_to_largest_integral_type(value) \
158 cast_to_largest_integral_type(cast_to_pointer_integral_type(value))
159 
160 /* GCC have printf type attribute check. */
161 #ifdef __GNUC__
162 #define CMOCKA_PRINTF_ATTRIBUTE(a,b) \
163  __attribute__ ((__format__ (__printf__, a, b)))
164 #else
165 #define CMOCKA_PRINTF_ATTRIBUTE(a,b)
166 #endif /* __GNUC__ */
167 
168 #if defined(__GNUC__)
169 #define CMOCKA_DEPRECATED __attribute__ ((deprecated))
170 #elif defined(_MSC_VER)
171 #define CMOCKA_DEPRECATED __declspec(deprecated)
172 #else
173 #define CMOCKA_DEPRECATED
174 #endif
175 
176 #if defined(__GNUC__)
177 #define CMOCKA_NORETURN __attribute__ ((noreturn))
178 #elif defined(_MSC_VER)
179 #define CMOCKA_NORETURN __declspec(noreturn)
180 #else
181 #define CMOCKA_NORETURN
182 #endif
183 
184 #define WILL_RETURN_ALWAYS -1
185 #define WILL_RETURN_ONCE -2
186 
235 #ifdef DOXYGEN
244 #else
245 #define mock() _mock(__func__, __FILE__, __LINE__)
246 #endif
247 
248 #ifdef DOXYGEN
269 #type mock_type(#type);
270 #else
271 #define mock_type(type) ((type) mock())
272 #endif
273 
274 #ifdef DOXYGEN
296 type mock_ptr_type(#type);
297 #else
298 #define mock_ptr_type(type) ((type) (uintptr_t) mock())
299 #endif
300 
301 
302 #ifdef DOXYGEN
327 void will_return(#function, LargestIntegralType value);
328 #else
329 #define will_return(function, value) \
330  _will_return(#function, __FILE__, __LINE__, \
331  cast_to_largest_integral_type(value), 1)
332 #endif
333 
334 #ifdef DOXYGEN
350 void will_return_count(#function, LargestIntegralType value, int count);
351 #else
352 #define will_return_count(function, value, count) \
353  _will_return(#function, __FILE__, __LINE__, \
354  cast_to_largest_integral_type(value), count)
355 #endif
356 
357 #ifdef DOXYGEN
374 #else
375 #define will_return_always(function, value) \
376  will_return_count(function, (value), WILL_RETURN_ALWAYS)
377 #endif
378 
379 #ifdef DOXYGEN
401 void will_return_maybe(#function, LargestIntegralType value);
402 #else
403 #define will_return_maybe(function, value) \
404  will_return_count(function, (value), WILL_RETURN_ONCE)
405 #endif
452 /*
453  * Add a custom parameter checking function. If the event parameter is NULL
454  * the event structure is allocated internally by this function. If event
455  * parameter is provided it must be allocated on the heap and doesn't need to
456  * be deallocated by the caller.
457  */
458 #ifdef DOXYGEN
475 void expect_check(#function, #parameter, #check_function, const void *check_data);
476 #else
477 #define expect_check(function, parameter, check_function, check_data) \
478  _expect_check(#function, #parameter, __FILE__, __LINE__, check_function, \
479  cast_to_largest_integral_type(check_data), NULL, 1)
480 #endif
481 
482 #ifdef DOXYGEN
497 void expect_in_set(#function, #parameter, LargestIntegralType value_array[]);
498 #else
499 #define expect_in_set(function, parameter, value_array) \
500  expect_in_set_count(function, parameter, value_array, 1)
501 #endif
502 
503 #ifdef DOXYGEN
522 void expect_in_set_count(#function, #parameter, LargestIntegralType value_array[], size_t count);
523 #else
524 #define expect_in_set_count(function, parameter, value_array, count) \
525  _expect_in_set(#function, #parameter, __FILE__, __LINE__, value_array, \
526  sizeof(value_array) / sizeof((value_array)[0]), count)
527 #endif
528 
529 #ifdef DOXYGEN
544 void expect_not_in_set(#function, #parameter, LargestIntegralType value_array[]);
545 #else
546 #define expect_not_in_set(function, parameter, value_array) \
547  expect_not_in_set_count(function, parameter, value_array, 1)
548 #endif
549 
550 #ifdef DOXYGEN
569 void expect_not_in_set_count(#function, #parameter, LargestIntegralType value_array[], size_t count);
570 #else
571 #define expect_not_in_set_count(function, parameter, value_array, count) \
572  _expect_not_in_set( \
573  #function, #parameter, __FILE__, __LINE__, value_array, \
574  sizeof(value_array) / sizeof((value_array)[0]), count)
575 #endif
576 
577 
578 #ifdef DOXYGEN
595 void expect_in_range(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum);
596 #else
597 #define expect_in_range(function, parameter, minimum, maximum) \
598  expect_in_range_count(function, parameter, minimum, maximum, 1)
599 #endif
600 
601 #ifdef DOXYGEN
622 void expect_in_range_count(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum, size_t count);
623 #else
624 #define expect_in_range_count(function, parameter, minimum, maximum, count) \
625  _expect_in_range(#function, #parameter, __FILE__, __LINE__, minimum, \
626  maximum, count)
627 #endif
628 
629 #ifdef DOXYGEN
646 void expect_not_in_range(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum);
647 #else
648 #define expect_not_in_range(function, parameter, minimum, maximum) \
649  expect_not_in_range_count(function, parameter, minimum, maximum, 1)
650 #endif
651 
652 #ifdef DOXYGEN
673 void expect_not_in_range_count(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum, size_t count);
674 #else
675 #define expect_not_in_range_count(function, parameter, minimum, maximum, \
676  count) \
677  _expect_not_in_range(#function, #parameter, __FILE__, __LINE__, \
678  minimum, maximum, count)
679 #endif
680 
681 #ifdef DOXYGEN
698 void expect_value(#function, #parameter, LargestIntegralType value);
699 #else
700 #define expect_value(function, parameter, value) \
701  expect_value_count(function, parameter, value, 1)
702 #endif
703 
704 #ifdef DOXYGEN
725 void expect_value_count(#function, #parameter, LargestIntegralType value, size_t count);
726 #else
727 #define expect_value_count(function, parameter, value, count) \
728  _expect_value(#function, #parameter, __FILE__, __LINE__, \
729  cast_to_largest_integral_type(value), count)
730 #endif
731 
732 #ifdef DOXYGEN
746 void expect_not_value(#function, #parameter, LargestIntegralType value);
747 #else
748 #define expect_not_value(function, parameter, value) \
749  expect_not_value_count(function, parameter, value, 1)
750 #endif
751 
752 #ifdef DOXYGEN
770 void expect_not_value_count(#function, #parameter, LargestIntegralType value, size_t count);
771 #else
772 #define expect_not_value_count(function, parameter, value, count) \
773  _expect_not_value(#function, #parameter, __FILE__, __LINE__, \
774  cast_to_largest_integral_type(value), count)
775 #endif
776 
777 #ifdef DOXYGEN
792 void expect_string(#function, #parameter, const char *string);
793 #else
794 #define expect_string(function, parameter, string) \
795  expect_string_count(function, parameter, string, 1)
796 #endif
797 
798 #ifdef DOXYGEN
817 void expect_string_count(#function, #parameter, const char *string, size_t count);
818 #else
819 #define expect_string_count(function, parameter, string, count) \
820  _expect_string(#function, #parameter, __FILE__, __LINE__, \
821  (const char*)(string), count)
822 #endif
823 
824 #ifdef DOXYGEN
839 void expect_not_string(#function, #parameter, const char *string);
840 #else
841 #define expect_not_string(function, parameter, string) \
842  expect_not_string_count(function, parameter, string, 1)
843 #endif
844 
845 #ifdef DOXYGEN
864 void expect_not_string_count(#function, #parameter, const char *string, size_t count);
865 #else
866 #define expect_not_string_count(function, parameter, string, count) \
867  _expect_not_string(#function, #parameter, __FILE__, __LINE__, \
868  (const char*)(string), count)
869 #endif
870 
871 #ifdef DOXYGEN
887 void expect_memory(#function, #parameter, void *memory, size_t size);
888 #else
889 #define expect_memory(function, parameter, memory, size) \
890  expect_memory_count(function, parameter, memory, size, 1)
891 #endif
892 
893 #ifdef DOXYGEN
914 void expect_memory_count(#function, #parameter, void *memory, size_t size, size_t count);
915 #else
916 #define expect_memory_count(function, parameter, memory, size, count) \
917  _expect_memory(#function, #parameter, __FILE__, __LINE__, \
918  (const void*)(memory), size, count)
919 #endif
920 
921 #ifdef DOXYGEN
938 void expect_not_memory(#function, #parameter, void *memory, size_t size);
939 #else
940 #define expect_not_memory(function, parameter, memory, size) \
941  expect_not_memory_count(function, parameter, memory, size, 1)
942 #endif
943 
944 #ifdef DOXYGEN
965 void expect_not_memory_count(#function, #parameter, void *memory, size_t size, size_t count);
966 #else
967 #define expect_not_memory_count(function, parameter, memory, size, count) \
968  _expect_not_memory(#function, #parameter, __FILE__, __LINE__, \
969  (const void*)(memory), size, count)
970 #endif
971 
972 
973 #ifdef DOXYGEN
985 void expect_any(#function, #parameter);
986 #else
987 #define expect_any(function, parameter) \
988  expect_any_count(function, parameter, 1)
989 #endif
990 
991 #ifdef DOXYGEN
1003 void expect_any_always(#function, #parameter);
1004 #else
1005 #define expect_any_always(function, parameter) \
1006  expect_any_count(function, parameter, WILL_RETURN_ALWAYS)
1007 #endif
1008 
1009 #ifdef DOXYGEN
1026 void expect_any_count(#function, #parameter, size_t count);
1027 #else
1028 #define expect_any_count(function, parameter, count) \
1029  _expect_any(#function, #parameter, __FILE__, __LINE__, count)
1030 #endif
1031 
1032 #ifdef DOXYGEN
1043 void check_expected(#parameter);
1044 #else
1045 #define check_expected(parameter) \
1046  _check_expected(__func__, #parameter, __FILE__, __LINE__, \
1047  cast_to_largest_integral_type(parameter))
1048 #endif
1049 
1050 #ifdef DOXYGEN
1061 void check_expected_ptr(#parameter);
1062 #else
1063 #define check_expected_ptr(parameter) \
1064  _check_expected(__func__, #parameter, __FILE__, __LINE__, \
1065  cast_ptr_to_largest_integral_type(parameter))
1066 #endif
1067 
1089 #ifdef DOXYGEN
1102 void assert_true(scalar expression);
1103 #else
1104 #define assert_true(c) _assert_true(cast_to_largest_integral_type(c), #c, \
1105  __FILE__, __LINE__)
1106 #endif
1107 
1108 #ifdef DOXYGEN
1120 void assert_false(scalar expression);
1121 #else
1122 #define assert_false(c) _assert_true(!(cast_to_largest_integral_type(c)), #c, \
1123  __FILE__, __LINE__)
1124 #endif
1125 
1126 #ifdef DOXYGEN
1139 void assert_return_code(int rc, int error);
1140 #else
1141 #define assert_return_code(rc, error) \
1142  _assert_return_code(cast_to_largest_integral_type(rc), \
1143  sizeof(rc), \
1144  cast_to_largest_integral_type(error), \
1145  #rc, __FILE__, __LINE__)
1146 #endif
1147 
1148 #ifdef DOXYGEN
1159 void assert_non_null(void *pointer);
1160 #else
1161 #define assert_non_null(c) _assert_true(cast_ptr_to_largest_integral_type(c), #c, \
1162  __FILE__, __LINE__)
1163 #endif
1164 
1165 #ifdef DOXYGEN
1176 void assert_null(void *pointer);
1177 #else
1178 #define assert_null(c) _assert_true(!(cast_ptr_to_largest_integral_type(c)), #c, \
1179 __FILE__, __LINE__)
1180 #endif
1181 
1182 #ifdef DOXYGEN
1193 void assert_ptr_equal(void *a, void *b);
1194 #else
1195 #define assert_ptr_equal(a, b) \
1196  _assert_int_equal(cast_ptr_to_largest_integral_type(a), \
1197  cast_ptr_to_largest_integral_type(b), \
1198  __FILE__, __LINE__)
1199 #endif
1200 
1201 #ifdef DOXYGEN
1212 void assert_ptr_not_equal(void *a, void *b);
1213 #else
1214 #define assert_ptr_not_equal(a, b) \
1215  _assert_int_not_equal(cast_ptr_to_largest_integral_type(a), \
1216  cast_ptr_to_largest_integral_type(b), \
1217  __FILE__, __LINE__)
1218 #endif
1219 
1220 #ifdef DOXYGEN
1231 void assert_int_equal(int a, int b);
1232 #else
1233 #define assert_int_equal(a, b) \
1234  _assert_int_equal(cast_to_largest_integral_type(a), \
1235  cast_to_largest_integral_type(b), \
1236  __FILE__, __LINE__)
1237 #endif
1238 
1239 #ifdef DOXYGEN
1252 void assert_int_not_equal(int a, int b);
1253 #else
1254 #define assert_int_not_equal(a, b) \
1255  _assert_int_not_equal(cast_to_largest_integral_type(a), \
1256  cast_to_largest_integral_type(b), \
1257  __FILE__, __LINE__)
1258 #endif
1259 
1260 #ifdef DOXYGEN
1273 void assert_float_equal(float a, float b, float epsilon);
1274 #else
1275 #define assert_float_equal(a, b, epsilon) \
1276  _assert_float_equal((float)a, \
1277  (float)b, \
1278  (float)epsilon, \
1279  __FILE__, __LINE__)
1280 #endif
1281 
1282 #ifdef DOXYGEN
1295 void assert_float_not_equal(float a, float b, float epsilon);
1296 #else
1297 #define assert_float_not_equal(a, b, epsilon) \
1298  _assert_float_not_equal((float)a, \
1299  (float)b, \
1300  (float)epsilon, \
1301  __FILE__, __LINE__)
1302 #endif
1303 
1304 #ifdef DOXYGEN
1317 void assert_double_equal(double a, double b, double epsilon);
1318 #else
1319 #define assert_double_equal(a, b, epsilon) \
1320  _assert_double_equal((double)a, \
1321  (double)b, \
1322  (double)epsilon, \
1323  __FILE__, __LINE__)
1324 #endif
1325 
1326 #ifdef DOXYGEN
1339 void assert_double_not_equal(double a, double b, double epsilon);
1340 #else
1341 #define assert_double_not_equal(a, b, epsilon) \
1342  _assert_double_not_equal((float)a, \
1343  (double)b, \
1344  (double)epsilon, \
1345  __FILE__, __LINE__)
1346 #endif
1347 
1348 
1349 #ifdef DOXYGEN
1360 void assert_string_equal(const char *a, const char *b);
1361 #else
1362 #define assert_string_equal(a, b) \
1363  _assert_string_equal((a), (b), __FILE__, __LINE__)
1364 #endif
1365 
1366 #ifdef DOXYGEN
1377 void assert_string_not_equal(const char *a, const char *b);
1378 #else
1379 #define assert_string_not_equal(a, b) \
1380  _assert_string_not_equal((a), (b), __FILE__, __LINE__)
1381 #endif
1382 
1383 #ifdef DOXYGEN
1398 void assert_memory_equal(const void *a, const void *b, size_t size);
1399 #else
1400 #define assert_memory_equal(a, b, size) \
1401  _assert_memory_equal((const void*)(a), (const void*)(b), size, __FILE__, \
1402  __LINE__)
1403 #endif
1404 
1405 #ifdef DOXYGEN
1420 void assert_memory_not_equal(const void *a, const void *b, size_t size);
1421 #else
1422 #define assert_memory_not_equal(a, b, size) \
1423  _assert_memory_not_equal((const void*)(a), (const void*)(b), size, \
1424  __FILE__, __LINE__)
1425 #endif
1426 
1427 #ifdef DOXYGEN
1442 #else
1443 #define assert_in_range(value, minimum, maximum) \
1444  _assert_in_range( \
1445  cast_to_largest_integral_type(value), \
1446  cast_to_largest_integral_type(minimum), \
1447  cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
1448 #endif
1449 
1450 #ifdef DOXYGEN
1465 #else
1466 #define assert_not_in_range(value, minimum, maximum) \
1467  _assert_not_in_range( \
1468  cast_to_largest_integral_type(value), \
1469  cast_to_largest_integral_type(minimum), \
1470  cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
1471 #endif
1472 
1473 #ifdef DOXYGEN
1486 void assert_in_set(LargestIntegralType value, LargestIntegralType values[], size_t count);
1487 #else
1488 #define assert_in_set(value, values, number_of_values) \
1489  _assert_in_set(value, values, number_of_values, __FILE__, __LINE__)
1490 #endif
1491 
1492 #ifdef DOXYGEN
1505 void assert_not_in_set(LargestIntegralType value, LargestIntegralType values[], size_t count);
1506 #else
1507 #define assert_not_in_set(value, values, number_of_values) \
1508  _assert_not_in_set(value, values, number_of_values, __FILE__, __LINE__)
1509 #endif
1510 
1574 #ifdef DOXYGEN
1581 void function_called(void);
1582 #else
1583 #define function_called() _function_called(__func__, __FILE__, __LINE__)
1584 #endif
1585 
1586 #ifdef DOXYGEN
1597 void expect_function_calls(#function, const int times);
1598 #else
1599 #define expect_function_calls(function, times) \
1600  _expect_function_call(#function, __FILE__, __LINE__, times)
1601 #endif
1602 
1603 #ifdef DOXYGEN
1612 void expect_function_call(#function);
1613 #else
1614 #define expect_function_call(function) \
1615  _expect_function_call(#function, __FILE__, __LINE__, 1)
1616 #endif
1617 
1618 #ifdef DOXYGEN
1627 #else
1628 #define expect_function_call_any(function) \
1629  _expect_function_call(#function, __FILE__, __LINE__, -1)
1630 #endif
1631 
1632 #ifdef DOXYGEN
1640 void ignore_function_calls(#function);
1641 #else
1642 #define ignore_function_calls(function) \
1643  _expect_function_call(#function, __FILE__, __LINE__, -2)
1644 #endif
1645 
1674 #ifdef DOXYGEN
1678 void fail(void);
1679 #else
1680 #define fail() _fail(__FILE__, __LINE__)
1681 #endif
1682 
1683 #ifdef DOXYGEN
1687 void skip(void);
1688 #else
1689 #define skip() _skip(__FILE__, __LINE__)
1690 #endif
1691 
1692 #ifdef DOXYGEN
1707 void fail_msg(const char *msg, ...);
1708 #else
1709 #define fail_msg(msg, ...) do { \
1710  cm_print_error("ERROR: " msg "\n", ##__VA_ARGS__); \
1711  fail(); \
1712 } while (0)
1713 #endif
1714 
1715 #ifdef DOXYGEN
1735 int run_test(#function);
1736 #else
1737 #define run_test(f) _run_test(#f, f, NULL, UNIT_TEST_FUNCTION_TYPE_TEST, NULL)
1738 #endif
1739 
1740 static inline void _unit_test_dummy(void **state) {
1741  (void)state;
1742 }
1743 
1748 #define unit_test(f) { #f, f, UNIT_TEST_FUNCTION_TYPE_TEST }
1749 
1750 #define _unit_test_setup(test, setup) \
1751  { #test "_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_SETUP }
1752 
1757 #define unit_test_setup(test, setup) \
1758  _unit_test_setup(test, setup), \
1759  unit_test(test), \
1760  _unit_test_teardown(test, _unit_test_dummy)
1761 
1762 #define _unit_test_teardown(test, teardown) \
1763  { #test "_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_TEARDOWN }
1764 
1769 #define unit_test_teardown(test, teardown) \
1770  _unit_test_setup(test, _unit_test_dummy), \
1771  unit_test(test), \
1772  _unit_test_teardown(test, teardown)
1773 
1778 #define group_test_setup(setup) \
1779  { "group_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_GROUP_SETUP }
1780 
1785 #define group_test_teardown(teardown) \
1786  { "group_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_GROUP_TEARDOWN }
1787 
1795 #define unit_test_setup_teardown(test, setup, teardown) \
1796  _unit_test_setup(test, setup), \
1797  unit_test(test), \
1798  _unit_test_teardown(test, teardown)
1799 
1800 
1802 #define cmocka_unit_test(f) { #f, f, NULL, NULL, NULL }
1803 
1805 #define cmocka_unit_test_setup(f, setup) { #f, f, setup, NULL, NULL }
1806 
1808 #define cmocka_unit_test_teardown(f, teardown) { #f, f, NULL, teardown, NULL }
1809 
1814 #define cmocka_unit_test_setup_teardown(f, setup, teardown) { #f, f, setup, teardown, NULL }
1815 
1823 #define cmocka_unit_test_prestate(f, state) { #f, f, NULL, NULL, state }
1824 
1832 #define cmocka_unit_test_prestate_setup_teardown(f, setup, teardown, state) { #f, f, setup, teardown, state }
1833 
1834 #define run_tests(tests) _run_tests(tests, sizeof(tests) / sizeof((tests)[0]))
1835 #define run_group_tests(tests) _run_group_tests(tests, sizeof(tests) / sizeof((tests)[0]))
1836 
1837 #ifdef DOXYGEN
1894 int cmocka_run_group_tests(const struct CMUnitTest group_tests[],
1895  CMFixtureFunction group_setup,
1896  CMFixtureFunction group_teardown);
1897 #else
1898 # define cmocka_run_group_tests(group_tests, group_setup, group_teardown) \
1899  _cmocka_run_group_tests(#group_tests, group_tests, sizeof(group_tests) / sizeof((group_tests)[0]), group_setup, group_teardown)
1900 #endif
1901 
1902 #ifdef DOXYGEN
1962 int cmocka_run_group_tests_name(const char *group_name,
1963  const struct CMUnitTest group_tests[],
1964  CMFixtureFunction group_setup,
1965  CMFixtureFunction group_teardown);
1966 #else
1967 # define cmocka_run_group_tests_name(group_name, group_tests, group_setup, group_teardown) \
1968  _cmocka_run_group_tests(group_name, group_tests, sizeof(group_tests) / sizeof((group_tests)[0]), group_setup, group_teardown)
1969 #endif
1970 
1996 #ifdef DOXYGEN
2019 void *test_malloc(size_t size);
2020 #else
2021 #define test_malloc(size) _test_malloc(size, __FILE__, __LINE__)
2022 #endif
2023 
2024 #ifdef DOXYGEN
2038 void *test_calloc(size_t nmemb, size_t size);
2039 #else
2040 #define test_calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__)
2041 #endif
2042 
2043 #ifdef DOXYGEN
2054 void *test_realloc(void *ptr, size_t size);
2055 #else
2056 #define test_realloc(ptr, size) _test_realloc(ptr, size, __FILE__, __LINE__)
2057 #endif
2058 
2059 #ifdef DOXYGEN
2067 void test_free(void *ptr);
2068 #else
2069 #define test_free(ptr) _test_free(ptr, __FILE__, __LINE__)
2070 #endif
2071 
2072 /* Redirect malloc, calloc and free to the unit test allocators. */
2073 #ifdef UNIT_TESTING
2074 #define malloc test_malloc
2075 #define realloc test_realloc
2076 #define calloc test_calloc
2077 #define free test_free
2078 #endif /* UNIT_TESTING */
2079 
2133 void mock_assert(const int result, const char* const expression,
2134  const char * const file, const int line);
2135 
2136 #ifdef DOXYGEN
2159 void expect_assert_failure(function fn_call);
2160 #else
2161 #define expect_assert_failure(function_call) \
2162  { \
2163  const int result = setjmp(global_expect_assert_env); \
2164  global_expecting_assert = 1; \
2165  if (result) { \
2166  print_message("Expected assertion %s occurred\n", \
2167  global_last_failed_assert); \
2168  global_expecting_assert = 0; \
2169  } else { \
2170  function_call ; \
2171  global_expecting_assert = 0; \
2172  print_error("Expected assert in %s\n", #function_call); \
2173  _fail(__FILE__, __LINE__); \
2174  } \
2175  }
2176 #endif
2177 
2180 /* Function prototype for setup, test and teardown functions. */
2181 typedef void (*UnitTestFunction)(void **state);
2182 
2183 /* Function that determines whether a function parameter value is correct. */
2184 typedef int (*CheckParameterValue)(const LargestIntegralType value,
2185  const LargestIntegralType check_value_data);
2186 
2187 /* Type of the unit test function. */
2188 typedef enum UnitTestFunctionType {
2189  UNIT_TEST_FUNCTION_TYPE_TEST = 0,
2190  UNIT_TEST_FUNCTION_TYPE_SETUP,
2191  UNIT_TEST_FUNCTION_TYPE_TEARDOWN,
2192  UNIT_TEST_FUNCTION_TYPE_GROUP_SETUP,
2193  UNIT_TEST_FUNCTION_TYPE_GROUP_TEARDOWN,
2194 } UnitTestFunctionType;
2195 
2196 /*
2197  * Stores a unit test function with its name and type.
2198  * NOTE: Every setup function must be paired with a teardown function. It's
2199  * possible to specify NULL function pointers.
2200  */
2201 typedef struct UnitTest {
2202  const char* name;
2203  UnitTestFunction function;
2204  UnitTestFunctionType function_type;
2205 } UnitTest;
2206 
2207 typedef struct GroupTest {
2208  UnitTestFunction setup;
2209  UnitTestFunction teardown;
2210  const UnitTest *tests;
2211  const size_t number_of_tests;
2212 } GroupTest;
2213 
2214 /* Function prototype for test functions. */
2215 typedef void (*CMUnitTestFunction)(void **state);
2216 
2217 /* Function prototype for setup and teardown functions. */
2218 typedef int (*CMFixtureFunction)(void **state);
2219 
2220 struct CMUnitTest {
2221  const char *name;
2222  CMUnitTestFunction test_func;
2223  CMFixtureFunction setup_func;
2224  CMFixtureFunction teardown_func;
2225  void *initial_state;
2226 };
2227 
2228 /* Location within some source code. */
2229 typedef struct SourceLocation {
2230  const char* file;
2231  int line;
2232 } SourceLocation;
2233 
2234 /* Event that's called to check a parameter value. */
2235 typedef struct CheckParameterEvent {
2236  SourceLocation location;
2237  const char *parameter_name;
2238  CheckParameterValue check_value;
2239  LargestIntegralType check_value_data;
2241 
2242 /* Used by expect_assert_failure() and mock_assert(). */
2243 extern int global_expecting_assert;
2244 extern jmp_buf global_expect_assert_env;
2245 extern const char * global_last_failed_assert;
2246 
2247 /* Retrieves a value for the given function, as set by "will_return". */
2248 LargestIntegralType _mock(const char * const function, const char* const file,
2249  const int line);
2250 
2251 void _expect_function_call(
2252  const char * const function_name,
2253  const char * const file,
2254  const int line,
2255  const int count);
2256 
2257 void _function_called(const char * const function, const char* const file,
2258  const int line);
2259 
2260 void _expect_check(
2261  const char* const function, const char* const parameter,
2262  const char* const file, const int line,
2263  const CheckParameterValue check_function,
2264  const LargestIntegralType check_data, CheckParameterEvent * const event,
2265  const int count);
2266 
2267 void _expect_in_set(
2268  const char* const function, const char* const parameter,
2269  const char* const file, const int line, const LargestIntegralType values[],
2270  const size_t number_of_values, const int count);
2271 void _expect_not_in_set(
2272  const char* const function, const char* const parameter,
2273  const char* const file, const int line, const LargestIntegralType values[],
2274  const size_t number_of_values, const int count);
2275 
2276 void _expect_in_range(
2277  const char* const function, const char* const parameter,
2278  const char* const file, const int line,
2279  const LargestIntegralType minimum,
2280  const LargestIntegralType maximum, const int count);
2281 void _expect_not_in_range(
2282  const char* const function, const char* const parameter,
2283  const char* const file, const int line,
2284  const LargestIntegralType minimum,
2285  const LargestIntegralType maximum, const int count);
2286 
2287 void _expect_value(
2288  const char* const function, const char* const parameter,
2289  const char* const file, const int line, const LargestIntegralType value,
2290  const int count);
2291 void _expect_not_value(
2292  const char* const function, const char* const parameter,
2293  const char* const file, const int line, const LargestIntegralType value,
2294  const int count);
2295 
2296 void _expect_string(
2297  const char* const function, const char* const parameter,
2298  const char* const file, const int line, const char* string,
2299  const int count);
2300 void _expect_not_string(
2301  const char* const function, const char* const parameter,
2302  const char* const file, const int line, const char* string,
2303  const int count);
2304 
2305 void _expect_memory(
2306  const char* const function, const char* const parameter,
2307  const char* const file, const int line, const void* const memory,
2308  const size_t size, const int count);
2309 void _expect_not_memory(
2310  const char* const function, const char* const parameter,
2311  const char* const file, const int line, const void* const memory,
2312  const size_t size, const int count);
2313 
2314 void _expect_any(
2315  const char* const function, const char* const parameter,
2316  const char* const file, const int line, const int count);
2317 
2318 void _check_expected(
2319  const char * const function_name, const char * const parameter_name,
2320  const char* file, const int line, const LargestIntegralType value);
2321 
2322 void _will_return(const char * const function_name, const char * const file,
2323  const int line, const LargestIntegralType value,
2324  const int count);
2325 void _assert_true(const LargestIntegralType result,
2326  const char* const expression,
2327  const char * const file, const int line);
2328 void _assert_return_code(const LargestIntegralType result,
2329  size_t rlen,
2330  const LargestIntegralType error,
2331  const char * const expression,
2332  const char * const file,
2333  const int line);
2334 void _assert_float_equal(const float a, const float n,
2335  const float epsilon, const char* const file,
2336  const int line);
2337 void _assert_float_not_equal(const float a, const float n,
2338  const float epsilon, const char* const file,
2339  const int line);
2340 void _assert_double_equal(const double a, const double n,
2341  const double epsilon, const char* const file,
2342  const int line);
2343 void _assert_double_not_equal(const double a, const double n,
2344  const double epsilon, const char* const file,
2345  const int line);
2346 void _assert_int_equal(
2347  const LargestIntegralType a, const LargestIntegralType b,
2348  const char * const file, const int line);
2349 void _assert_int_not_equal(
2350  const LargestIntegralType a, const LargestIntegralType b,
2351  const char * const file, const int line);
2352 void _assert_string_equal(const char * const a, const char * const b,
2353  const char * const file, const int line);
2354 void _assert_string_not_equal(const char * const a, const char * const b,
2355  const char *file, const int line);
2356 void _assert_memory_equal(const void * const a, const void * const b,
2357  const size_t size, const char* const file,
2358  const int line);
2359 void _assert_memory_not_equal(const void * const a, const void * const b,
2360  const size_t size, const char* const file,
2361  const int line);
2362 void _assert_in_range(
2363  const LargestIntegralType value, const LargestIntegralType minimum,
2364  const LargestIntegralType maximum, const char* const file, const int line);
2365 void _assert_not_in_range(
2366  const LargestIntegralType value, const LargestIntegralType minimum,
2367  const LargestIntegralType maximum, const char* const file, const int line);
2368 void _assert_in_set(
2369  const LargestIntegralType value, const LargestIntegralType values[],
2370  const size_t number_of_values, const char* const file, const int line);
2371 void _assert_not_in_set(
2372  const LargestIntegralType value, const LargestIntegralType values[],
2373  const size_t number_of_values, const char* const file, const int line);
2374 
2375 void* _test_malloc(const size_t size, const char* file, const int line);
2376 void* _test_realloc(void *ptr, const size_t size, const char* file, const int line);
2377 void* _test_calloc(const size_t number_of_elements, const size_t size,
2378  const char* file, const int line);
2379 void _test_free(void* const ptr, const char* file, const int line);
2380 
2381 CMOCKA_NORETURN void _fail(const char * const file, const int line);
2382 
2383 CMOCKA_NORETURN void _skip(const char * const file, const int line);
2384 
2385 int _run_test(
2386  const char * const function_name, const UnitTestFunction Function,
2387  void ** const volatile state, const UnitTestFunctionType function_type,
2388  const void* const heap_check_point);
2389 CMOCKA_DEPRECATED int _run_tests(const UnitTest * const tests,
2390  const size_t number_of_tests);
2391 CMOCKA_DEPRECATED int _run_group_tests(const UnitTest * const tests,
2392  const size_t number_of_tests);
2393 
2394 /* Test runner */
2395 int _cmocka_run_group_tests(const char *group_name,
2396  const struct CMUnitTest * const tests,
2397  const size_t num_tests,
2398  CMFixtureFunction group_setup,
2399  CMFixtureFunction group_teardown);
2400 
2401 /* Standard output and error print methods. */
2402 void print_message(const char* const format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2);
2403 void print_error(const char* const format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2);
2404 void vprint_message(const char* const format, va_list args) CMOCKA_PRINTF_ATTRIBUTE(1, 0);
2405 void vprint_error(const char* const format, va_list args) CMOCKA_PRINTF_ATTRIBUTE(1, 0);
2406 void cm_print_error(const char* const format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2);
2407 
2408 enum cm_message_output {
2409  CM_OUTPUT_STDOUT,
2410  CM_OUTPUT_SUBUNIT,
2411  CM_OUTPUT_TAP,
2412  CM_OUTPUT_XML,
2413 };
2414 
2426 void cmocka_set_message_output(enum cm_message_output output);
2427 
2428 
2439 void cmocka_set_test_filter(const char *pattern);
2440 
2451 void cmocka_set_skip_filter(const char *pattern);
2452 
2455 #endif /* CMOCKA_H_ */
void * test_malloc(size_t size)
Test function overriding malloc.
void * test_realloc(void *ptr, size_t size)
Test function overriding realloc which detects buffer overruns and memoery leaks.
void * test_calloc(size_t nmemb, size_t size)
Test function overriding calloc.
void test_free(void *ptr)
Test function overriding free(3).
void assert_ptr_equal(void *a, void *b)
Assert that the two given pointers are equal.
void assert_int_equal(int a, int b)
Assert that the two given integers are equal.
void assert_string_equal(const char *a, const char *b)
Assert that the two given strings are equal.
void assert_null(void *pointer)
Assert that the given pointer is NULL.
void assert_int_not_equal(int a, int b)
Assert that the two given integers are not equal.
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 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 assert_ptr_not_equal(void *a, void *b)
Assert that the two given pointers are not equal.
void assert_double_equal(double a, double b, double epsilon)
Assert that the two given double are equal given an epsilon.
void assert_not_in_set(LargestIntegralType value, LargestIntegralType values[], size_t count)
Assert that the specified value is not within a set.
void assert_non_null(void *pointer)
Assert that the given pointer is non-NULL.
void assert_return_code(int rc, int error)
Assert that the return_code is greater than or equal to 0.
void assert_true(scalar expression)
Assert that the given expression is true.
void assert_in_set(LargestIntegralType value, LargestIntegralType values[], size_t count)
Assert that the specified value is within a set.
void assert_false(scalar expression)
Assert that the given expression is false.
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 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 assert_float_equal(float a, float b, float epsilon)
Assert that the two given float are equal given an epsilon.
void assert_double_not_equal(double a, double b, double epsilon)
Assert that the two given double are not equal given an epsilon.
void assert_string_not_equal(const char *a, const char *b)
Assert that the two given strings are not equal.
void assert_float_not_equal(float a, float b, float epsilon)
Assert that the two given float are not equal given an epsilon.
void ignore_function_calls(#function)
Ignores function_called() invocations from given mock function.
void expect_function_call_any(#function)
Expects function_called() from given mock at least once.
void expect_function_call(#function)
Store expected single call to a mock to be checked by function_called() later.
void function_called(void)
Check that current mocked function is being called in the expected order.
void expect_function_calls(#function, const int times)
Store expected call(s) to a mock to be checked by function_called() later.
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 fail(void)
Forces the test to fail immediately and quit.
void fail_msg(const char *msg,...)
Forces the test to fail immediately and quit, printing the reason.
int run_test(#function)
Generic method to run a single test.
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 skip(void)
Forces the test to not be executed, but marked as skipped.
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:1769
void expect_assert_failure(function fn_call)
Ensure that mock_assert() is called.
LargestIntegralType mock(void)
Retrieve a return value of the current function.
void will_return_always(#function, LargestIntegralType value)
Store a value that will be always returned by mock().
void will_return(#function, LargestIntegralType value)
Store a value to be returned by mock() later.
void will_return_maybe(#function, LargestIntegralType value)
Store a value that may be always returned by mock().
void will_return_count(#function, LargestIntegralType value, int count)
Store a value to be returned by mock() later.
type mock_ptr_type(#type)
Retrieve a typed return value of the current function.
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 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_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 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_not_string(#function, #parameter, const char *string)
Add an event to check if the parameter value isn't equal to the provided string.
void check_expected_ptr(#parameter)
Determine whether a function parameter is correct.
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 check_expected(#parameter)
Determine whether a function parameter is correct.
void expect_check(#function, #parameter, #check_function, const void *check_data)
Add a custom parameter checking function.
void expect_not_string_count(#function, #parameter, const char *string, size_t count)
Add an event to check if the parameter value isn't equal to the provided string.
void expect_not_value_count(#function, #parameter, LargestIntegralType value, size_t count)
Add an event to repeatedly check if a parameter isn't the given value.
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't match an area of memory.
void expect_value_count(#function, #parameter, LargestIntegralType value, size_t count)
Add an event to repeatedly check if a parameter is the given integer based value.
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_any_always(#function, #parameter)
Add an event to always check if a parameter (of any value) has been passed.
void expect_not_memory(#function, #parameter, void *memory, size_t size)
Add an event to check if the parameter doesn't match an area of memory.
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.
void expect_value(#function, #parameter, LargestIntegralType value)
Add an event to check if a parameter is the given integer based value.
void expect_memory(#function, #parameter, void *memory, size_t size)
Add an event to check if the parameter does match an area of memory.
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 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...
void expect_any(#function, #parameter)
Add an event to check if a parameter (of any value) has been passed.
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_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_value(#function, #parameter, LargestIntegralType value)
Add an event to check if a parameter isn't the given value.
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 cmocka_set_skip_filter(const char *pattern)
Set a pattern to skip tests matching the pattern.
Definition: cmocka.c:2806
void cmocka_set_test_filter(const char *pattern)
Set a pattern to only run the test matching the pattern.
Definition: cmocka.c:2801
uintmax_t LargestIntegralType
Definition: cmocka.h:74
void cmocka_set_message_output(enum cm_message_output output)
Function to set the output format for a test.
Definition: cmocka.c:2796
Definition: cmocka.h:2220
Definition: cmocka.h:2235
Definition: cmocka.h:2207
Definition: cmocka.h:2229
Definition: cmocka.h:2201