cmocka  1.1.1
cmocka.h
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 #include <sys/int_types.h>
20 
21 #ifdef _WIN32
22 # ifdef _MSC_VER
23 
24 #define __func__ __FUNCTION__
25 
26 # ifndef inline
27 #define inline __inline
28 # endif /* inline */
29 
30 # if _MSC_VER < 1500
31 # ifdef __cplusplus
32 extern "C" {
33 # endif /* __cplusplus */
34 int __stdcall IsDebuggerPresent();
35 # ifdef __cplusplus
36 } /* extern "C" */
37 # endif /* __cplusplus */
38 # endif /* _MSC_VER < 1500 */
39 # endif /* _MSC_VER */
40 #endif /* _WIN32 */
41 
59 /* If __WORDSIZE is not set, try to figure it out and default to 32 bit. */
60 #ifndef __WORDSIZE
61 # if defined(_LP64)
62 # define __WORDSIZE 64
63 # else
64 # define __WORDSIZE 32
65 # endif
66 #endif
67 
68 #ifdef DOXYGEN
69 
73 typedef uintmax_t LargestIntegralType;
74 #else /* DOXGEN */
75 #ifndef LargestIntegralType
76 # if __WORDSIZE == 64 && !defined(_WIN64)
77 # define LargestIntegralType unsigned long int
78 # else
79 # define LargestIntegralType unsigned long long int
80 # endif
81 #endif /* LargestIntegralType */
82 #endif /* DOXYGEN */
83 
84 /* Printf format used to display LargestIntegralType as a hexidecimal. */
85 #ifndef LargestIntegralTypePrintfFormat
86 # ifdef _WIN32
87 # define LargestIntegralTypePrintfFormat "0x%I64x"
88 # else
89 # if __WORDSIZE == 64
90 # define LargestIntegralTypePrintfFormat "%#lx"
91 # else
92 # define LargestIntegralTypePrintfFormat "%#llx"
93 # endif
94 # endif /* _WIN32 */
95 #endif /* LargestIntegralTypePrintfFormat */
96 
97 /* Printf format used to display LargestIntegralType as a decimal. */
98 #ifndef LargestIntegralTypePrintfFormatDecimal
99 # ifdef _WIN32
100 # define LargestIntegralTypePrintfFormatDecimal "%I64u"
101 # else
102 # if __WORDSIZE == 64
103 # define LargestIntegralTypePrintfFormatDecimal "%lu"
104 # else
105 # define LargestIntegralTypePrintfFormatDecimal "%llu"
106 # endif
107 # endif /* _WIN32 */
108 #endif /* LargestIntegralTypePrintfFormat */
109 
110 /* Perform an unsigned cast to LargestIntegralType. */
111 #define cast_to_largest_integral_type(value) \
112  ((LargestIntegralType)(value))
113 
114 /* Smallest integral type capable of holding a pointer. */
115 #if !defined(_UINTPTR_T) && !defined(_UINTPTR_T_DEFINED)
116 # if defined(_WIN32)
117  /* WIN32 is an ILP32 platform */
118  typedef unsigned int uintptr_t;
119 # elif defined(_WIN64)
120  typedef unsigned long int uintptr_t
121 # else /* _WIN32 */
122 
123 /* ILP32 and LP64 platforms */
124 # ifdef __WORDSIZE /* glibc */
125 # if __WORDSIZE == 64
126  typedef unsigned long int uintptr_t;
127 # else
128  typedef unsigned int uintptr_t;
129 # endif /* __WORDSIZE == 64 */
130 # else /* __WORDSIZE */
131 # if defined(_LP64) || defined(_I32LPx)
132  typedef unsigned long int uintptr_t;
133 # else
134  typedef unsigned int uintptr_t;
135 # endif
136 # endif /* __WORDSIZE */
137 # endif /* _WIN32 */
138 
139 # define _UINTPTR_T
140 # define _UINTPTR_T_DEFINED
141 #endif /* !defined(_UINTPTR_T) || !defined(_UINTPTR_T_DEFINED) */
142 
143 /* Perform an unsigned cast to uintptr_t. */
144 #define cast_to_pointer_integral_type(value) \
145  ((uintptr_t)((size_t)(value)))
146 
147 /* Perform a cast of a pointer to LargestIntegralType */
148 #define cast_ptr_to_largest_integral_type(value) \
149 cast_to_largest_integral_type(cast_to_pointer_integral_type(value))
150 
151 /* GCC have printf type attribute check. */
152 #ifdef __GNUC__
153 #define CMOCKA_PRINTF_ATTRIBUTE(a,b) \
154  __attribute__ ((__format__ (__printf__, a, b)))
155 #else
156 #define CMOCKA_PRINTF_ATTRIBUTE(a,b)
157 #endif /* __GNUC__ */
158 
159 #if defined(__GNUC__)
160 #define CMOCKA_DEPRECATED __attribute__ ((deprecated))
161 #elif defined(_MSC_VER)
162 #define CMOCKA_DEPRECATED __declspec(deprecated)
163 #else
164 #define CMOCKA_DEPRECATED
165 #endif
166 
167 #define WILL_RETURN_ALWAYS -1
168 #define WILL_RETURN_ONCE -2
169 
218 #ifdef DOXYGEN
219 
227 #else
228 #define mock() _mock(__func__, __FILE__, __LINE__)
229 #endif
230 
231 #ifdef DOXYGEN
232 
252 #type mock_type(#type);
253 #else
254 #define mock_type(type) ((type) mock())
255 #endif
256 
257 #ifdef DOXYGEN
258 
279 type mock_ptr_type(#type);
280 #else
281 #define mock_ptr_type(type) ((type) (uintptr_t) mock())
282 #endif
283 
284 
285 #ifdef DOXYGEN
286 
310 void will_return(#function, LargestIntegralType value);
311 #else
312 #define will_return(function, value) \
313  _will_return(#function, __FILE__, __LINE__, \
314  cast_to_largest_integral_type(value), 1)
315 #endif
316 
317 #ifdef DOXYGEN
318 
333 void will_return_count(#function, LargestIntegralType value, int count);
334 #else
335 #define will_return_count(function, value, count) \
336  _will_return(#function, __FILE__, __LINE__, \
337  cast_to_largest_integral_type(value), count)
338 #endif
339 
340 #ifdef DOXYGEN
341 
356 void will_return_always(#function, LargestIntegralType value);
357 #else
358 #define will_return_always(function, value) \
359  will_return_count(function, (value), WILL_RETURN_ALWAYS)
360 #endif
361 
362 #ifdef DOXYGEN
363 
384 void will_return_maybe(#function, LargestIntegralType value);
385 #else
386 #define will_return_maybe(function, value) \
387  will_return_count(function, (value), WILL_RETURN_ONCE)
388 #endif
389 
435 /*
436  * Add a custom parameter checking function. If the event parameter is NULL
437  * the event structure is allocated internally by this function. If event
438  * parameter is provided it must be allocated on the heap and doesn't need to
439  * be deallocated by the caller.
440  */
441 #ifdef DOXYGEN
442 
458 void expect_check(#function, #parameter, #check_function, const void *check_data);
459 #else
460 #define expect_check(function, parameter, check_function, check_data) \
461  _expect_check(#function, #parameter, __FILE__, __LINE__, check_function, \
462  cast_to_largest_integral_type(check_data), NULL, 1)
463 #endif
464 
465 #ifdef DOXYGEN
466 
480 void expect_in_set(#function, #parameter, LargestIntegralType value_array[]);
481 #else
482 #define expect_in_set(function, parameter, value_array) \
483  expect_in_set_count(function, parameter, value_array, 1)
484 #endif
485 
486 #ifdef DOXYGEN
487 
505 void expect_in_set_count(#function, #parameter, LargestIntegralType value_array[], size_t count);
506 #else
507 #define expect_in_set_count(function, parameter, value_array, count) \
508  _expect_in_set(#function, #parameter, __FILE__, __LINE__, value_array, \
509  sizeof(value_array) / sizeof((value_array)[0]), count)
510 #endif
511 
512 #ifdef DOXYGEN
513 
527 void expect_not_in_set(#function, #parameter, LargestIntegralType value_array[]);
528 #else
529 #define expect_not_in_set(function, parameter, value_array) \
530  expect_not_in_set_count(function, parameter, value_array, 1)
531 #endif
532 
533 #ifdef DOXYGEN
534 
552 void expect_not_in_set_count(#function, #parameter, LargestIntegralType value_array[], size_t count);
553 #else
554 #define expect_not_in_set_count(function, parameter, value_array, count) \
555  _expect_not_in_set( \
556  #function, #parameter, __FILE__, __LINE__, value_array, \
557  sizeof(value_array) / sizeof((value_array)[0]), count)
558 #endif
559 
560 
561 #ifdef DOXYGEN
562 
578 void expect_in_range(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum);
579 #else
580 #define expect_in_range(function, parameter, minimum, maximum) \
581  expect_in_range_count(function, parameter, minimum, maximum, 1)
582 #endif
583 
584 #ifdef DOXYGEN
585 
605 void expect_in_range_count(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum, size_t count);
606 #else
607 #define expect_in_range_count(function, parameter, minimum, maximum, count) \
608  _expect_in_range(#function, #parameter, __FILE__, __LINE__, minimum, \
609  maximum, count)
610 #endif
611 
612 #ifdef DOXYGEN
613 
629 void expect_not_in_range(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum);
630 #else
631 #define expect_not_in_range(function, parameter, minimum, maximum) \
632  expect_not_in_range_count(function, parameter, minimum, maximum, 1)
633 #endif
634 
635 #ifdef DOXYGEN
636 
656 void expect_not_in_range_count(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum, size_t count);
657 #else
658 #define expect_not_in_range_count(function, parameter, minimum, maximum, \
659  count) \
660  _expect_not_in_range(#function, #parameter, __FILE__, __LINE__, \
661  minimum, maximum, count)
662 #endif
663 
664 #ifdef DOXYGEN
665 
678 void expect_value(#function, #parameter, LargestIntegralType value);
679 #else
680 #define expect_value(function, parameter, value) \
681  expect_value_count(function, parameter, value, 1)
682 #endif
683 
684 #ifdef DOXYGEN
685 
702 void expect_value_count(#function, #parameter, LargestIntegralType value, size_t count);
703 #else
704 #define expect_value_count(function, parameter, value, count) \
705  _expect_value(#function, #parameter, __FILE__, __LINE__, \
706  cast_to_largest_integral_type(value), count)
707 #endif
708 
709 #ifdef DOXYGEN
710 
723 void expect_not_value(#function, #parameter, LargestIntegralType value);
724 #else
725 #define expect_not_value(function, parameter, value) \
726  expect_not_value_count(function, parameter, value, 1)
727 #endif
728 
729 #ifdef DOXYGEN
730 
747 void expect_not_value_count(#function, #parameter, LargestIntegralType value, size_t count);
748 #else
749 #define expect_not_value_count(function, parameter, value, count) \
750  _expect_not_value(#function, #parameter, __FILE__, __LINE__, \
751  cast_to_largest_integral_type(value), count)
752 #endif
753 
754 #ifdef DOXYGEN
755 
769 void expect_string(#function, #parameter, const char *string);
770 #else
771 #define expect_string(function, parameter, string) \
772  expect_string_count(function, parameter, string, 1)
773 #endif
774 
775 #ifdef DOXYGEN
776 
794 void expect_string_count(#function, #parameter, const char *string, size_t count);
795 #else
796 #define expect_string_count(function, parameter, string, count) \
797  _expect_string(#function, #parameter, __FILE__, __LINE__, \
798  (const char*)(string), count)
799 #endif
800 
801 #ifdef DOXYGEN
802 
816 void expect_not_string(#function, #parameter, const char *string);
817 #else
818 #define expect_not_string(function, parameter, string) \
819  expect_not_string_count(function, parameter, string, 1)
820 #endif
821 
822 #ifdef DOXYGEN
823 
841 void expect_not_string_count(#function, #parameter, const char *string, size_t count);
842 #else
843 #define expect_not_string_count(function, parameter, string, count) \
844  _expect_not_string(#function, #parameter, __FILE__, __LINE__, \
845  (const char*)(string), count)
846 #endif
847 
848 #ifdef DOXYGEN
849 
864 void expect_memory(#function, #parameter, void *memory, size_t size);
865 #else
866 #define expect_memory(function, parameter, memory, size) \
867  expect_memory_count(function, parameter, memory, size, 1)
868 #endif
869 
870 #ifdef DOXYGEN
871 
891 void expect_memory_count(#function, #parameter, void *memory, size_t size, size_t count);
892 #else
893 #define expect_memory_count(function, parameter, memory, size, count) \
894  _expect_memory(#function, #parameter, __FILE__, __LINE__, \
895  (const void*)(memory), size, count)
896 #endif
897 
898 #ifdef DOXYGEN
899 
915 void expect_not_memory(#function, #parameter, void *memory, size_t size);
916 #else
917 #define expect_not_memory(function, parameter, memory, size) \
918  expect_not_memory_count(function, parameter, memory, size, 1)
919 #endif
920 
921 #ifdef DOXYGEN
922 
942 void expect_not_memory_count(#function, #parameter, void *memory, size_t size, size_t count);
943 #else
944 #define expect_not_memory_count(function, parameter, memory, size, count) \
945  _expect_not_memory(#function, #parameter, __FILE__, __LINE__, \
946  (const void*)(memory), size, count)
947 #endif
948 
949 
950 #ifdef DOXYGEN
951 
962 void expect_any(#function, #parameter);
963 #else
964 #define expect_any(function, parameter) \
965  expect_any_count(function, parameter, 1)
966 #endif
967 
968 #ifdef DOXYGEN
969 
985 void expect_any_count(#function, #parameter, size_t count);
986 #else
987 #define expect_any_count(function, parameter, count) \
988  _expect_any(#function, #parameter, __FILE__, __LINE__, count)
989 #endif
990 
991 #ifdef DOXYGEN
992 
1002 void check_expected(#parameter);
1003 #else
1004 #define check_expected(parameter) \
1005  _check_expected(__func__, #parameter, __FILE__, __LINE__, \
1006  cast_to_largest_integral_type(parameter))
1007 #endif
1008 
1009 #ifdef DOXYGEN
1010 
1020 void check_expected_ptr(#parameter);
1021 #else
1022 #define check_expected_ptr(parameter) \
1023  _check_expected(__func__, #parameter, __FILE__, __LINE__, \
1024  cast_ptr_to_largest_integral_type(parameter))
1025 #endif
1026 
1048 #ifdef DOXYGEN
1049 
1061 void assert_true(scalar expression);
1062 #else
1063 #define assert_true(c) _assert_true(cast_to_largest_integral_type(c), #c, \
1064  __FILE__, __LINE__)
1065 #endif
1066 
1067 #ifdef DOXYGEN
1068 
1079 void assert_false(scalar expression);
1080 #else
1081 #define assert_false(c) _assert_true(!(cast_to_largest_integral_type(c)), #c, \
1082  __FILE__, __LINE__)
1083 #endif
1084 
1085 #ifdef DOXYGEN
1086 
1098 void assert_return_code(int rc, int error);
1099 #else
1100 #define assert_return_code(rc, error) \
1101  _assert_return_code(cast_to_largest_integral_type(rc), \
1102  sizeof(rc), \
1103  cast_to_largest_integral_type(error), \
1104  #rc, __FILE__, __LINE__)
1105 #endif
1106 
1107 #ifdef DOXYGEN
1108 
1118 void assert_non_null(void *pointer);
1119 #else
1120 #define assert_non_null(c) _assert_true(cast_ptr_to_largest_integral_type(c), #c, \
1121  __FILE__, __LINE__)
1122 #endif
1123 
1124 #ifdef DOXYGEN
1125 
1135 void assert_null(void *pointer);
1136 #else
1137 #define assert_null(c) _assert_true(!(cast_ptr_to_largest_integral_type(c)), #c, \
1138 __FILE__, __LINE__)
1139 #endif
1140 
1141 #ifdef DOXYGEN
1142 
1152 void assert_ptr_equal(void *a, void *b);
1153 #else
1154 #define assert_ptr_equal(a, b) \
1155  _assert_int_equal(cast_ptr_to_largest_integral_type(a), \
1156  cast_ptr_to_largest_integral_type(b), \
1157  __FILE__, __LINE__)
1158 #endif
1159 
1160 #ifdef DOXYGEN
1161 
1171 void assert_ptr_not_equal(void *a, void *b);
1172 #else
1173 #define assert_ptr_not_equal(a, b) \
1174  _assert_int_not_equal(cast_ptr_to_largest_integral_type(a), \
1175  cast_ptr_to_largest_integral_type(b), \
1176  __FILE__, __LINE__)
1177 #endif
1178 
1179 #ifdef DOXYGEN
1180 
1190 void assert_int_equal(int a, int b);
1191 #else
1192 #define assert_int_equal(a, b) \
1193  _assert_int_equal(cast_to_largest_integral_type(a), \
1194  cast_to_largest_integral_type(b), \
1195  __FILE__, __LINE__)
1196 #endif
1197 
1198 #ifdef DOXYGEN
1199 
1211 void assert_int_not_equal(int a, int b);
1212 #else
1213 #define assert_int_not_equal(a, b) \
1214  _assert_int_not_equal(cast_to_largest_integral_type(a), \
1215  cast_to_largest_integral_type(b), \
1216  __FILE__, __LINE__)
1217 #endif
1218 
1219 #ifdef DOXYGEN
1220 
1230 void assert_string_equal(const char *a, const char *b);
1231 #else
1232 #define assert_string_equal(a, b) \
1233  _assert_string_equal((const char*)(a), (const char*)(b), __FILE__, \
1234  __LINE__)
1235 #endif
1236 
1237 #ifdef DOXYGEN
1238 
1248 void assert_string_not_equal(const char *a, const char *b);
1249 #else
1250 #define assert_string_not_equal(a, b) \
1251  _assert_string_not_equal((const char*)(a), (const char*)(b), __FILE__, \
1252  __LINE__)
1253 #endif
1254 
1255 #ifdef DOXYGEN
1256 
1270 void assert_memory_equal(const void *a, const void *b, size_t size);
1271 #else
1272 #define assert_memory_equal(a, b, size) \
1273  _assert_memory_equal((const void*)(a), (const void*)(b), size, __FILE__, \
1274  __LINE__)
1275 #endif
1276 
1277 #ifdef DOXYGEN
1278 
1292 void assert_memory_not_equal(const void *a, const void *b, size_t size);
1293 #else
1294 #define assert_memory_not_equal(a, b, size) \
1295  _assert_memory_not_equal((const void*)(a), (const void*)(b), size, \
1296  __FILE__, __LINE__)
1297 #endif
1298 
1299 #ifdef DOXYGEN
1300 
1314 #else
1315 #define assert_in_range(value, minimum, maximum) \
1316  _assert_in_range( \
1317  cast_to_largest_integral_type(value), \
1318  cast_to_largest_integral_type(minimum), \
1319  cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
1320 #endif
1321 
1322 #ifdef DOXYGEN
1323 
1337 #else
1338 #define assert_not_in_range(value, minimum, maximum) \
1339  _assert_not_in_range( \
1340  cast_to_largest_integral_type(value), \
1341  cast_to_largest_integral_type(minimum), \
1342  cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
1343 #endif
1344 
1345 #ifdef DOXYGEN
1346 
1358 void assert_in_set(LargestIntegralType value, LargestIntegralType values[], size_t count);
1359 #else
1360 #define assert_in_set(value, values, number_of_values) \
1361  _assert_in_set(value, values, number_of_values, __FILE__, __LINE__)
1362 #endif
1363 
1364 #ifdef DOXYGEN
1365 
1377 void assert_not_in_set(LargestIntegralType value, LargestIntegralType values[], size_t count);
1378 #else
1379 #define assert_not_in_set(value, values, number_of_values) \
1380  _assert_not_in_set(value, values, number_of_values, __FILE__, __LINE__)
1381 #endif
1382 
1443 #ifdef DOXYGEN
1444 
1450 void function_called(void);
1451 #else
1452 #define function_called() _function_called(__func__, __FILE__, __LINE__)
1453 #endif
1454 
1455 #ifdef DOXYGEN
1456 
1466 void expect_function_calls(#function, const int times);
1467 #else
1468 #define expect_function_calls(function, times) \
1469  _expect_function_call(#function, __FILE__, __LINE__, times)
1470 #endif
1471 
1472 #ifdef DOXYGEN
1473 
1481 void expect_function_call(#function);
1482 #else
1483 #define expect_function_call(function) \
1484  _expect_function_call(#function, __FILE__, __LINE__, 1)
1485 #endif
1486 
1487 #ifdef DOXYGEN
1488 
1495 void expect_function_call_any(#function);
1496 #else
1497 #define expect_function_call_any(function) \
1498  _expect_function_call(#function, __FILE__, __LINE__, -1)
1499 #endif
1500 
1501 #ifdef DOXYGEN
1502 
1509 void ignore_function_calls(#function);
1510 #else
1511 #define ignore_function_calls(function) \
1512  _expect_function_call(#function, __FILE__, __LINE__, -2)
1513 #endif
1514 
1543 #ifdef DOXYGEN
1544 
1547 void fail(void);
1548 #else
1549 #define fail() _fail(__FILE__, __LINE__)
1550 #endif
1551 
1552 #ifdef DOXYGEN
1553 
1556 void skip(void);
1557 #else
1558 #define skip() _skip(__FILE__, __LINE__)
1559 #endif
1560 
1561 #ifdef DOXYGEN
1562 
1576 void fail_msg(const char *msg, ...);
1577 #else
1578 #define fail_msg(msg, ...) do { \
1579  print_error("ERROR: " msg "\n", ##__VA_ARGS__); \
1580  fail(); \
1581 } while (0)
1582 #endif
1583 
1584 #ifdef DOXYGEN
1585 
1604 int run_test(#function);
1605 #else
1606 #define run_test(f) _run_test(#f, f, NULL, UNIT_TEST_FUNCTION_TYPE_TEST, NULL)
1607 #endif
1608 
1609 static inline void _unit_test_dummy(void **state) {
1610  (void)state;
1611 }
1612 
1617 #define unit_test(f) { #f, f, UNIT_TEST_FUNCTION_TYPE_TEST }
1618 
1619 #define _unit_test_setup(test, setup) \
1620  { #test "_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_SETUP }
1621 
1626 #define unit_test_setup(test, setup) \
1627  _unit_test_setup(test, setup), \
1628  unit_test(test), \
1629  _unit_test_teardown(test, _unit_test_dummy)
1630 
1631 #define _unit_test_teardown(test, teardown) \
1632  { #test "_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_TEARDOWN }
1633 
1638 #define unit_test_teardown(test, teardown) \
1639  _unit_test_setup(test, _unit_test_dummy), \
1640  unit_test(test), \
1641  _unit_test_teardown(test, teardown)
1642 
1647 #define group_test_setup(setup) \
1648  { "group_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_GROUP_SETUP }
1649 
1654 #define group_test_teardown(teardown) \
1655  { "group_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_GROUP_TEARDOWN }
1656 
1664 #define unit_test_setup_teardown(test, setup, teardown) \
1665  _unit_test_setup(test, setup), \
1666  unit_test(test), \
1667  _unit_test_teardown(test, teardown)
1668 
1669 
1671 #define cmocka_unit_test(f) { #f, f, NULL, NULL, NULL }
1672 
1674 #define cmocka_unit_test_setup(f, setup) { #f, f, setup, NULL, NULL }
1675 
1677 #define cmocka_unit_test_teardown(f, teardown) { #f, f, NULL, teardown, NULL }
1678 
1683 #define cmocka_unit_test_setup_teardown(f, setup, teardown) { #f, f, setup, teardown, NULL }
1684 
1692 #define cmocka_unit_test_prestate(f, state) { #f, f, NULL, NULL, state }
1693 
1701 #define cmocka_unit_test_prestate_setup_teardown(f, setup, teardown, state) { #f, f, setup, teardown, state }
1702 
1703 #define run_tests(tests) _run_tests(tests, sizeof(tests) / sizeof(tests)[0])
1704 #define run_group_tests(tests) _run_group_tests(tests, sizeof(tests) / sizeof(tests)[0])
1705 
1706 #ifdef DOXYGEN
1707 
1763 int cmocka_run_group_tests(const struct CMUnitTest group_tests[],
1764  CMFixtureFunction group_setup,
1765  CMFixtureFunction group_teardown);
1766 #else
1767 # define cmocka_run_group_tests(group_tests, group_setup, group_teardown) \
1768  _cmocka_run_group_tests(#group_tests, group_tests, sizeof(group_tests) / sizeof(group_tests)[0], group_setup, group_teardown)
1769 #endif
1770 
1771 #ifdef DOXYGEN
1772 
1831 int cmocka_run_group_tests_name(const char *group_name,
1832  const struct CMUnitTest group_tests[],
1833  CMFixtureFunction group_setup,
1834  CMFixtureFunction group_teardown);
1835 #else
1836 # define cmocka_run_group_tests_name(group_name, group_tests, group_setup, group_teardown) \
1837  _cmocka_run_group_tests(group_name, group_tests, sizeof(group_tests) / sizeof(group_tests)[0], group_setup, group_teardown)
1838 #endif
1839 
1865 #ifdef DOXYGEN
1866 
1888 void *test_malloc(size_t size);
1889 #else
1890 #define test_malloc(size) _test_malloc(size, __FILE__, __LINE__)
1891 #endif
1892 
1893 #ifdef DOXYGEN
1894 
1907 void *test_calloc(size_t nmemb, size_t size);
1908 #else
1909 #define test_calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__)
1910 #endif
1911 
1912 #ifdef DOXYGEN
1913 
1923 void *test_realloc(void *ptr, size_t size);
1924 #else
1925 #define test_realloc(ptr, size) _test_realloc(ptr, size, __FILE__, __LINE__)
1926 #endif
1927 
1928 #ifdef DOXYGEN
1929 
1936 void test_free(void *ptr);
1937 #else
1938 #define test_free(ptr) _test_free(ptr, __FILE__, __LINE__)
1939 #endif
1940 
1941 /* Redirect malloc, calloc and free to the unit test allocators. */
1942 #ifdef UNIT_TESTING
1943 #define malloc test_malloc
1944 #define realloc test_realloc
1945 #define calloc test_calloc
1946 #define free test_free
1947 #endif /* UNIT_TESTING */
1948 
2002 void mock_assert(const int result, const char* const expression,
2003  const char * const file, const int line);
2004 
2005 #ifdef DOXYGEN
2006 
2028 void expect_assert_failure(function fn_call);
2029 #else
2030 #define expect_assert_failure(function_call) \
2031  { \
2032  const int result = setjmp(global_expect_assert_env); \
2033  global_expecting_assert = 1; \
2034  if (result) { \
2035  print_message("Expected assertion %s occurred\n", \
2036  global_last_failed_assert); \
2037  global_expecting_assert = 0; \
2038  } else { \
2039  function_call ; \
2040  global_expecting_assert = 0; \
2041  print_error("Expected assert in %s\n", #function_call); \
2042  _fail(__FILE__, __LINE__); \
2043  } \
2044  }
2045 #endif
2046 
2049 /* Function prototype for setup, test and teardown functions. */
2050 typedef void (*UnitTestFunction)(void **state);
2051 
2052 /* Function that determines whether a function parameter value is correct. */
2053 typedef int (*CheckParameterValue)(const LargestIntegralType value,
2054  const LargestIntegralType check_value_data);
2055 
2056 /* Type of the unit test function. */
2057 typedef enum UnitTestFunctionType {
2058  UNIT_TEST_FUNCTION_TYPE_TEST = 0,
2059  UNIT_TEST_FUNCTION_TYPE_SETUP,
2060  UNIT_TEST_FUNCTION_TYPE_TEARDOWN,
2061  UNIT_TEST_FUNCTION_TYPE_GROUP_SETUP,
2062  UNIT_TEST_FUNCTION_TYPE_GROUP_TEARDOWN,
2063 } UnitTestFunctionType;
2064 
2065 /*
2066  * Stores a unit test function with its name and type.
2067  * NOTE: Every setup function must be paired with a teardown function. It's
2068  * possible to specify NULL function pointers.
2069  */
2070 typedef struct UnitTest {
2071  const char* name;
2072  UnitTestFunction function;
2073  UnitTestFunctionType function_type;
2074 } UnitTest;
2075 
2076 typedef struct GroupTest {
2077  UnitTestFunction setup;
2078  UnitTestFunction teardown;
2079  const UnitTest *tests;
2080  const size_t number_of_tests;
2081 } GroupTest;
2082 
2083 /* Function prototype for test functions. */
2084 typedef void (*CMUnitTestFunction)(void **state);
2085 
2086 /* Function prototype for setup and teardown functions. */
2087 typedef int (*CMFixtureFunction)(void **state);
2088 
2089 struct CMUnitTest {
2090  const char *name;
2091  CMUnitTestFunction test_func;
2092  CMFixtureFunction setup_func;
2093  CMFixtureFunction teardown_func;
2094  void *initial_state;
2095 };
2096 
2097 /* Location within some source code. */
2098 typedef struct SourceLocation {
2099  const char* file;
2100  int line;
2101 } SourceLocation;
2102 
2103 /* Event that's called to check a parameter value. */
2104 typedef struct CheckParameterEvent {
2105  SourceLocation location;
2106  const char *parameter_name;
2107  CheckParameterValue check_value;
2108  LargestIntegralType check_value_data;
2109 } CheckParameterEvent;
2110 
2111 /* Used by expect_assert_failure() and mock_assert(). */
2112 extern int global_expecting_assert;
2113 extern jmp_buf global_expect_assert_env;
2114 extern const char * global_last_failed_assert;
2115 
2116 /* Retrieves a value for the given function, as set by "will_return". */
2117 LargestIntegralType _mock(const char * const function, const char* const file,
2118  const int line);
2119 
2120 void _expect_function_call(
2121  const char * const function_name,
2122  const char * const file,
2123  const int line,
2124  const int count);
2125 
2126 void _function_called(const char * const function, const char* const file,
2127  const int line);
2128 
2129 void _expect_check(
2130  const char* const function, const char* const parameter,
2131  const char* const file, const int line,
2132  const CheckParameterValue check_function,
2133  const LargestIntegralType check_data, CheckParameterEvent * const event,
2134  const int count);
2135 
2136 void _expect_in_set(
2137  const char* const function, const char* const parameter,
2138  const char* const file, const int line, const LargestIntegralType values[],
2139  const size_t number_of_values, const int count);
2140 void _expect_not_in_set(
2141  const char* const function, const char* const parameter,
2142  const char* const file, const int line, const LargestIntegralType values[],
2143  const size_t number_of_values, const int count);
2144 
2145 void _expect_in_range(
2146  const char* const function, const char* const parameter,
2147  const char* const file, const int line,
2148  const LargestIntegralType minimum,
2149  const LargestIntegralType maximum, const int count);
2150 void _expect_not_in_range(
2151  const char* const function, const char* const parameter,
2152  const char* const file, const int line,
2153  const LargestIntegralType minimum,
2154  const LargestIntegralType maximum, const int count);
2155 
2156 void _expect_value(
2157  const char* const function, const char* const parameter,
2158  const char* const file, const int line, const LargestIntegralType value,
2159  const int count);
2160 void _expect_not_value(
2161  const char* const function, const char* const parameter,
2162  const char* const file, const int line, const LargestIntegralType value,
2163  const int count);
2164 
2165 void _expect_string(
2166  const char* const function, const char* const parameter,
2167  const char* const file, const int line, const char* string,
2168  const int count);
2169 void _expect_not_string(
2170  const char* const function, const char* const parameter,
2171  const char* const file, const int line, const char* string,
2172  const int count);
2173 
2174 void _expect_memory(
2175  const char* const function, const char* const parameter,
2176  const char* const file, const int line, const void* const memory,
2177  const size_t size, const int count);
2178 void _expect_not_memory(
2179  const char* const function, const char* const parameter,
2180  const char* const file, const int line, const void* const memory,
2181  const size_t size, const int count);
2182 
2183 void _expect_any(
2184  const char* const function, const char* const parameter,
2185  const char* const file, const int line, const int count);
2186 
2187 void _check_expected(
2188  const char * const function_name, const char * const parameter_name,
2189  const char* file, const int line, const LargestIntegralType value);
2190 
2191 void _will_return(const char * const function_name, const char * const file,
2192  const int line, const LargestIntegralType value,
2193  const int count);
2194 void _assert_true(const LargestIntegralType result,
2195  const char* const expression,
2196  const char * const file, const int line);
2197 void _assert_return_code(const LargestIntegralType result,
2198  size_t rlen,
2199  const LargestIntegralType error,
2200  const char * const expression,
2201  const char * const file,
2202  const int line);
2203 void _assert_int_equal(
2204  const LargestIntegralType a, const LargestIntegralType b,
2205  const char * const file, const int line);
2206 void _assert_int_not_equal(
2207  const LargestIntegralType a, const LargestIntegralType b,
2208  const char * const file, const int line);
2209 void _assert_string_equal(const char * const a, const char * const b,
2210  const char * const file, const int line);
2211 void _assert_string_not_equal(const char * const a, const char * const b,
2212  const char *file, const int line);
2213 void _assert_memory_equal(const void * const a, const void * const b,
2214  const size_t size, const char* const file,
2215  const int line);
2216 void _assert_memory_not_equal(const void * const a, const void * const b,
2217  const size_t size, const char* const file,
2218  const int line);
2219 void _assert_in_range(
2220  const LargestIntegralType value, const LargestIntegralType minimum,
2221  const LargestIntegralType maximum, const char* const file, const int line);
2222 void _assert_not_in_range(
2223  const LargestIntegralType value, const LargestIntegralType minimum,
2224  const LargestIntegralType maximum, const char* const file, const int line);
2225 void _assert_in_set(
2226  const LargestIntegralType value, const LargestIntegralType values[],
2227  const size_t number_of_values, const char* const file, const int line);
2228 void _assert_not_in_set(
2229  const LargestIntegralType value, const LargestIntegralType values[],
2230  const size_t number_of_values, const char* const file, const int line);
2231 
2232 void* _test_malloc(const size_t size, const char* file, const int line);
2233 void* _test_realloc(void *ptr, const size_t size, const char* file, const int line);
2234 void* _test_calloc(const size_t number_of_elements, const size_t size,
2235  const char* file, const int line);
2236 void _test_free(void* const ptr, const char* file, const int line);
2237 
2238 void _fail(const char * const file, const int line);
2239 
2240 void _skip(const char * const file, const int line);
2241 
2242 int _run_test(
2243  const char * const function_name, const UnitTestFunction Function,
2244  void ** const volatile state, const UnitTestFunctionType function_type,
2245  const void* const heap_check_point);
2246 CMOCKA_DEPRECATED int _run_tests(const UnitTest * const tests,
2247  const size_t number_of_tests);
2248 CMOCKA_DEPRECATED int _run_group_tests(const UnitTest * const tests,
2249  const size_t number_of_tests);
2250 
2251 /* Test runner */
2252 int _cmocka_run_group_tests(const char *group_name,
2253  const struct CMUnitTest * const tests,
2254  const size_t num_tests,
2255  CMFixtureFunction group_setup,
2256  CMFixtureFunction group_teardown);
2257 
2258 /* Standard output and error print methods. */
2259 void print_message(const char* const format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2);
2260 void print_error(const char* const format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2);
2261 void vprint_message(const char* const format, va_list args) CMOCKA_PRINTF_ATTRIBUTE(1, 0);
2262 void vprint_error(const char* const format, va_list args) CMOCKA_PRINTF_ATTRIBUTE(1, 0);
2263 
2264 enum cm_message_output {
2265  CM_OUTPUT_STDOUT,
2266  CM_OUTPUT_SUBUNIT,
2267  CM_OUTPUT_TAP,
2268  CM_OUTPUT_XML,
2269 };
2270 
2282 void cmocka_set_message_output(enum cm_message_output output);
2283 
2286 #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.
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:1571
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.
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.
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
Largest integral type.
Definition: cmocka.h:73
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.
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.
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.
void cmocka_set_message_output(enum cm_message_output output)
Function to set the output format for a test.
Definition: cmocka.c:2513
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.
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.
void expect_value_count(#function, #parameter, LargestIntegralType value, size_t count)
Add an event to repeatedly check if a parameter is the given value.
void assert_in_set(LargestIntegralType value, LargestIntegralType values[], size_t count)
Assert that the specified value is within a set.