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