cmocka  1.1.5
Unit testing library with mock support
cmocka.h
1 /*
2  * Copyright 2008 Google Inc.
3  * Copyright 2014-2018 Andreas Schneider <asn@cryptomilk.org>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #ifndef CMOCKA_H_
18 #define CMOCKA_H_
19 
20 #ifdef _WIN32
21 # ifdef _MSC_VER
22 
23 #define __func__ __FUNCTION__
24 
25 # ifndef inline
26 #define inline __inline
27 # endif /* inline */
28 
29 # if _MSC_VER < 1500
30 # ifdef __cplusplus
31 extern "C" {
32 # endif /* __cplusplus */
33 int __stdcall IsDebuggerPresent();
34 # ifdef __cplusplus
35 } /* extern "C" */
36 # endif /* __cplusplus */
37 # endif /* _MSC_VER < 1500 */
38 # endif /* _MSC_VER */
39 #endif /* _WIN32 */
40 
59 /* If __WORDSIZE is not set, try to figure it out and default to 32 bit. */
60 #ifndef __WORDSIZE
61 # if (defined(__x86_64__) && !defined(__ILP32__)) || defined(__sparc_v9__) || defined(__sparcv9)
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 #ifndef FloatPrintfFormat
111 # define FloatPrintfFormat "%f"
112 #endif /* FloatPrintfFormat */
113 
114 /* Perform an unsigned cast to LargestIntegralType. */
115 #define cast_to_largest_integral_type(value) \
116  ((LargestIntegralType)(value))
117 
118 /* Smallest integral type capable of holding a pointer. */
119 #if !defined(_UINTPTR_T) && !defined(_UINTPTR_T_DEFINED)
120 # if defined(_WIN32)
121  /* WIN32 is an ILP32 platform */
122  typedef unsigned int uintptr_t;
123 # elif defined(_WIN64)
124  typedef unsigned long int uintptr_t;
125 # else /* _WIN32 */
126 
127 /* ILP32 and LP64 platforms */
128 # ifdef __WORDSIZE /* glibc */
129 # if __WORDSIZE == 64
130  typedef unsigned long int uintptr_t;
131 # else
132  typedef unsigned int uintptr_t;
133 # endif /* __WORDSIZE == 64 */
134 # else /* __WORDSIZE */
135 # if defined(_LP64) || defined(_I32LPx)
136  typedef unsigned long int uintptr_t;
137 # else
138  typedef unsigned int uintptr_t;
139 # endif
140 # endif /* __WORDSIZE */
141 # endif /* _WIN32 */
142 
143 # define _UINTPTR_T
144 # define _UINTPTR_T_DEFINED
145 #endif /* !defined(_UINTPTR_T) || !defined(_UINTPTR_T_DEFINED) */
146 
147 /* Perform an unsigned cast to uintptr_t. */
148 #define cast_to_pointer_integral_type(value) \
149  ((uintptr_t)((size_t)(value)))
150 
151 /* Perform a cast of a pointer to LargestIntegralType */
152 #define cast_ptr_to_largest_integral_type(value) \
153 cast_to_largest_integral_type(cast_to_pointer_integral_type(value))
154 
155 /* GCC have printf type attribute check. */
156 #ifdef __GNUC__
157 #define CMOCKA_PRINTF_ATTRIBUTE(a,b) \
158  __attribute__ ((__format__ (__printf__, a, b)))
159 #else
160 #define CMOCKA_PRINTF_ATTRIBUTE(a,b)
161 #endif /* __GNUC__ */
162 
163 #if defined(__GNUC__)
164 #define CMOCKA_DEPRECATED __attribute__ ((deprecated))
165 #elif defined(_MSC_VER)
166 #define CMOCKA_DEPRECATED __declspec(deprecated)
167 #else
168 #define CMOCKA_DEPRECATED
169 #endif
170 
171 #define WILL_RETURN_ALWAYS -1
172 #define WILL_RETURN_ONCE -2
173 
222 #ifdef DOXYGEN
223 
231 #else
232 #define mock() _mock(__func__, __FILE__, __LINE__)
233 #endif
234 
235 #ifdef DOXYGEN
236 
256 #type mock_type(#type);
257 #else
258 #define mock_type(type) ((type) mock())
259 #endif
260 
261 #ifdef DOXYGEN
262 
283 type mock_ptr_type(#type);
284 #else
285 #define mock_ptr_type(type) ((type) (uintptr_t) mock())
286 #endif
287 
288 
289 #ifdef DOXYGEN
290 
314 void will_return(#function, LargestIntegralType value);
315 #else
316 #define will_return(function, value) \
317  _will_return(#function, __FILE__, __LINE__, \
318  cast_to_largest_integral_type(value), 1)
319 #endif
320 
321 #ifdef DOXYGEN
322 
337 void will_return_count(#function, LargestIntegralType value, int count);
338 #else
339 #define will_return_count(function, value, count) \
340  _will_return(#function, __FILE__, __LINE__, \
341  cast_to_largest_integral_type(value), count)
342 #endif
343 
344 #ifdef DOXYGEN
345 
360 void will_return_always(#function, LargestIntegralType value);
361 #else
362 #define will_return_always(function, value) \
363  will_return_count(function, (value), WILL_RETURN_ALWAYS)
364 #endif
365 
366 #ifdef DOXYGEN
367 
388 void will_return_maybe(#function, LargestIntegralType value);
389 #else
390 #define will_return_maybe(function, value) \
391  will_return_count(function, (value), WILL_RETURN_ONCE)
392 #endif
393 
439 /*
440  * Add a custom parameter checking function. If the event parameter is NULL
441  * the event structure is allocated internally by this function. If event
442  * parameter is provided it must be allocated on the heap and doesn't need to
443  * be deallocated by the caller.
444  */
445 #ifdef DOXYGEN
446 
462 void expect_check(#function, #parameter, #check_function, const void *check_data);
463 #else
464 #define expect_check(function, parameter, check_function, check_data) \
465  _expect_check(#function, #parameter, __FILE__, __LINE__, check_function, \
466  cast_to_largest_integral_type(check_data), NULL, 1)
467 #endif
468 
469 #ifdef DOXYGEN
470 
484 void expect_in_set(#function, #parameter, LargestIntegralType value_array[]);
485 #else
486 #define expect_in_set(function, parameter, value_array) \
487  expect_in_set_count(function, parameter, value_array, 1)
488 #endif
489 
490 #ifdef DOXYGEN
491 
509 void expect_in_set_count(#function, #parameter, LargestIntegralType value_array[], size_t count);
510 #else
511 #define expect_in_set_count(function, parameter, value_array, count) \
512  _expect_in_set(#function, #parameter, __FILE__, __LINE__, value_array, \
513  sizeof(value_array) / sizeof((value_array)[0]), count)
514 #endif
515 
516 #ifdef DOXYGEN
517 
531 void expect_not_in_set(#function, #parameter, LargestIntegralType value_array[]);
532 #else
533 #define expect_not_in_set(function, parameter, value_array) \
534  expect_not_in_set_count(function, parameter, value_array, 1)
535 #endif
536 
537 #ifdef DOXYGEN
538 
556 void expect_not_in_set_count(#function, #parameter, LargestIntegralType value_array[], size_t count);
557 #else
558 #define expect_not_in_set_count(function, parameter, value_array, count) \
559  _expect_not_in_set( \
560  #function, #parameter, __FILE__, __LINE__, value_array, \
561  sizeof(value_array) / sizeof((value_array)[0]), count)
562 #endif
563 
564 
565 #ifdef DOXYGEN
566 
582 void expect_in_range(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum);
583 #else
584 #define expect_in_range(function, parameter, minimum, maximum) \
585  expect_in_range_count(function, parameter, minimum, maximum, 1)
586 #endif
587 
588 #ifdef DOXYGEN
589 
609 void expect_in_range_count(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum, size_t count);
610 #else
611 #define expect_in_range_count(function, parameter, minimum, maximum, count) \
612  _expect_in_range(#function, #parameter, __FILE__, __LINE__, minimum, \
613  maximum, count)
614 #endif
615 
616 #ifdef DOXYGEN
617 
633 void expect_not_in_range(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum);
634 #else
635 #define expect_not_in_range(function, parameter, minimum, maximum) \
636  expect_not_in_range_count(function, parameter, minimum, maximum, 1)
637 #endif
638 
639 #ifdef DOXYGEN
640 
660 void expect_not_in_range_count(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum, size_t count);
661 #else
662 #define expect_not_in_range_count(function, parameter, minimum, maximum, \
663  count) \
664  _expect_not_in_range(#function, #parameter, __FILE__, __LINE__, \
665  minimum, maximum, count)
666 #endif
667 
668 #ifdef DOXYGEN
669 
682 void expect_value(#function, #parameter, LargestIntegralType value);
683 #else
684 #define expect_value(function, parameter, value) \
685  expect_value_count(function, parameter, value, 1)
686 #endif
687 
688 #ifdef DOXYGEN
689 
706 void expect_value_count(#function, #parameter, LargestIntegralType value, size_t count);
707 #else
708 #define expect_value_count(function, parameter, value, count) \
709  _expect_value(#function, #parameter, __FILE__, __LINE__, \
710  cast_to_largest_integral_type(value), count)
711 #endif
712 
713 #ifdef DOXYGEN
714 
727 void expect_not_value(#function, #parameter, LargestIntegralType value);
728 #else
729 #define expect_not_value(function, parameter, value) \
730  expect_not_value_count(function, parameter, value, 1)
731 #endif
732 
733 #ifdef DOXYGEN
734 
751 void expect_not_value_count(#function, #parameter, LargestIntegralType value, size_t count);
752 #else
753 #define expect_not_value_count(function, parameter, value, count) \
754  _expect_not_value(#function, #parameter, __FILE__, __LINE__, \
755  cast_to_largest_integral_type(value), count)
756 #endif
757 
758 #ifdef DOXYGEN
759 
773 void expect_string(#function, #parameter, const char *string);
774 #else
775 #define expect_string(function, parameter, string) \
776  expect_string_count(function, parameter, string, 1)
777 #endif
778 
779 #ifdef DOXYGEN
780 
798 void expect_string_count(#function, #parameter, const char *string, size_t count);
799 #else
800 #define expect_string_count(function, parameter, string, count) \
801  _expect_string(#function, #parameter, __FILE__, __LINE__, \
802  (const char*)(string), count)
803 #endif
804 
805 #ifdef DOXYGEN
806 
820 void expect_not_string(#function, #parameter, const char *string);
821 #else
822 #define expect_not_string(function, parameter, string) \
823  expect_not_string_count(function, parameter, string, 1)
824 #endif
825 
826 #ifdef DOXYGEN
827 
845 void expect_not_string_count(#function, #parameter, const char *string, size_t count);
846 #else
847 #define expect_not_string_count(function, parameter, string, count) \
848  _expect_not_string(#function, #parameter, __FILE__, __LINE__, \
849  (const char*)(string), count)
850 #endif
851 
852 #ifdef DOXYGEN
853 
868 void expect_memory(#function, #parameter, void *memory, size_t size);
869 #else
870 #define expect_memory(function, parameter, memory, size) \
871  expect_memory_count(function, parameter, memory, size, 1)
872 #endif
873 
874 #ifdef DOXYGEN
875 
895 void expect_memory_count(#function, #parameter, void *memory, size_t size, size_t count);
896 #else
897 #define expect_memory_count(function, parameter, memory, size, count) \
898  _expect_memory(#function, #parameter, __FILE__, __LINE__, \
899  (const void*)(memory), size, count)
900 #endif
901 
902 #ifdef DOXYGEN
903 
919 void expect_not_memory(#function, #parameter, void *memory, size_t size);
920 #else
921 #define expect_not_memory(function, parameter, memory, size) \
922  expect_not_memory_count(function, parameter, memory, size, 1)
923 #endif
924 
925 #ifdef DOXYGEN
926 
946 void expect_not_memory_count(#function, #parameter, void *memory, size_t size, size_t count);
947 #else
948 #define expect_not_memory_count(function, parameter, memory, size, count) \
949  _expect_not_memory(#function, #parameter, __FILE__, __LINE__, \
950  (const void*)(memory), size, count)
951 #endif
952 
953 
954 #ifdef DOXYGEN
955 
966 void expect_any(#function, #parameter);
967 #else
968 #define expect_any(function, parameter) \
969  expect_any_count(function, parameter, 1)
970 #endif
971 
972 #ifdef DOXYGEN
973 
984 void expect_any_always(#function, #parameter);
985 #else
986 #define expect_any_always(function, parameter) \
987  expect_any_count(function, parameter, WILL_RETURN_ALWAYS)
988 #endif
989 
990 #ifdef DOXYGEN
991 
1007 void expect_any_count(#function, #parameter, size_t count);
1008 #else
1009 #define expect_any_count(function, parameter, count) \
1010  _expect_any(#function, #parameter, __FILE__, __LINE__, count)
1011 #endif
1012 
1013 #ifdef DOXYGEN
1014 
1024 void check_expected(#parameter);
1025 #else
1026 #define check_expected(parameter) \
1027  _check_expected(__func__, #parameter, __FILE__, __LINE__, \
1028  cast_to_largest_integral_type(parameter))
1029 #endif
1030 
1031 #ifdef DOXYGEN
1032 
1042 void check_expected_ptr(#parameter);
1043 #else
1044 #define check_expected_ptr(parameter) \
1045  _check_expected(__func__, #parameter, __FILE__, __LINE__, \
1046  cast_ptr_to_largest_integral_type(parameter))
1047 #endif
1048 
1070 #ifdef DOXYGEN
1071 
1083 void assert_true(scalar expression);
1084 #else
1085 #define assert_true(c) _assert_true(cast_to_largest_integral_type(c), #c, \
1086  __FILE__, __LINE__)
1087 #endif
1088 
1089 #ifdef DOXYGEN
1090 
1101 void assert_false(scalar expression);
1102 #else
1103 #define assert_false(c) _assert_true(!(cast_to_largest_integral_type(c)), #c, \
1104  __FILE__, __LINE__)
1105 #endif
1106 
1107 #ifdef DOXYGEN
1108 
1120 void assert_return_code(int rc, int error);
1121 #else
1122 #define assert_return_code(rc, error) \
1123  _assert_return_code(cast_to_largest_integral_type(rc), \
1124  sizeof(rc), \
1125  cast_to_largest_integral_type(error), \
1126  #rc, __FILE__, __LINE__)
1127 #endif
1128 
1129 #ifdef DOXYGEN
1130 
1140 void assert_non_null(void *pointer);
1141 #else
1142 #define assert_non_null(c) _assert_true(cast_ptr_to_largest_integral_type(c), #c, \
1143  __FILE__, __LINE__)
1144 #endif
1145 
1146 #ifdef DOXYGEN
1147 
1157 void assert_null(void *pointer);
1158 #else
1159 #define assert_null(c) _assert_true(!(cast_ptr_to_largest_integral_type(c)), #c, \
1160 __FILE__, __LINE__)
1161 #endif
1162 
1163 #ifdef DOXYGEN
1164 
1174 void assert_ptr_equal(void *a, void *b);
1175 #else
1176 #define assert_ptr_equal(a, b) \
1177  _assert_int_equal(cast_ptr_to_largest_integral_type(a), \
1178  cast_ptr_to_largest_integral_type(b), \
1179  __FILE__, __LINE__)
1180 #endif
1181 
1182 #ifdef DOXYGEN
1183 
1193 void assert_ptr_not_equal(void *a, void *b);
1194 #else
1195 #define assert_ptr_not_equal(a, b) \
1196  _assert_int_not_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
1202 
1212 void assert_int_equal(int a, int b);
1213 #else
1214 #define assert_int_equal(a, b) \
1215  _assert_int_equal(cast_to_largest_integral_type(a), \
1216  cast_to_largest_integral_type(b), \
1217  __FILE__, __LINE__)
1218 #endif
1219 
1220 #ifdef DOXYGEN
1221 
1233 void assert_int_not_equal(int a, int b);
1234 #else
1235 #define assert_int_not_equal(a, b) \
1236  _assert_int_not_equal(cast_to_largest_integral_type(a), \
1237  cast_to_largest_integral_type(b), \
1238  __FILE__, __LINE__)
1239 #endif
1240 
1241 #ifdef DOXYGEN
1242 
1254 void assert_float_equal(float a, float b, float epsilon);
1255 #else
1256 #define assert_float_equal(a, b, epsilon) \
1257  _assert_float_equal((float)a, \
1258  (float)b, \
1259  (float)epsilon, \
1260  __FILE__, __LINE__)
1261 #endif
1262 
1263 #ifdef DOXYGEN
1264 
1276 void assert_float_not_equal(float a, float b, float epsilon);
1277 #else
1278 #define assert_float_not_equal(a, b, epsilon) \
1279  _assert_float_not_equal((float)a, \
1280  (float)b, \
1281  (float)epsilon, \
1282  __FILE__, __LINE__)
1283 #endif
1284 
1285 
1286 #ifdef DOXYGEN
1287 
1297 void assert_string_equal(const char *a, const char *b);
1298 #else
1299 #define assert_string_equal(a, b) \
1300  _assert_string_equal((const char*)(a), (const char*)(b), __FILE__, \
1301  __LINE__)
1302 #endif
1303 
1304 #ifdef DOXYGEN
1305 
1315 void assert_string_not_equal(const char *a, const char *b);
1316 #else
1317 #define assert_string_not_equal(a, b) \
1318  _assert_string_not_equal((const char*)(a), (const char*)(b), __FILE__, \
1319  __LINE__)
1320 #endif
1321 
1322 #ifdef DOXYGEN
1323 
1337 void assert_memory_equal(const void *a, const void *b, size_t size);
1338 #else
1339 #define assert_memory_equal(a, b, size) \
1340  _assert_memory_equal((const void*)(a), (const void*)(b), size, __FILE__, \
1341  __LINE__)
1342 #endif
1343 
1344 #ifdef DOXYGEN
1345 
1359 void assert_memory_not_equal(const void *a, const void *b, size_t size);
1360 #else
1361 #define assert_memory_not_equal(a, b, size) \
1362  _assert_memory_not_equal((const void*)(a), (const void*)(b), size, \
1363  __FILE__, __LINE__)
1364 #endif
1365 
1366 #ifdef DOXYGEN
1367 
1381 #else
1382 #define assert_in_range(value, minimum, maximum) \
1383  _assert_in_range( \
1384  cast_to_largest_integral_type(value), \
1385  cast_to_largest_integral_type(minimum), \
1386  cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
1387 #endif
1388 
1389 #ifdef DOXYGEN
1390 
1404 #else
1405 #define assert_not_in_range(value, minimum, maximum) \
1406  _assert_not_in_range( \
1407  cast_to_largest_integral_type(value), \
1408  cast_to_largest_integral_type(minimum), \
1409  cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
1410 #endif
1411 
1412 #ifdef DOXYGEN
1413 
1425 void assert_in_set(LargestIntegralType value, LargestIntegralType values[], size_t count);
1426 #else
1427 #define assert_in_set(value, values, number_of_values) \
1428  _assert_in_set(value, values, number_of_values, __FILE__, __LINE__)
1429 #endif
1430 
1431 #ifdef DOXYGEN
1432 
1444 void assert_not_in_set(LargestIntegralType value, LargestIntegralType values[], size_t count);
1445 #else
1446 #define assert_not_in_set(value, values, number_of_values) \
1447  _assert_not_in_set(value, values, number_of_values, __FILE__, __LINE__)
1448 #endif
1449 
1510 #ifdef DOXYGEN
1511 
1517 void function_called(void);
1518 #else
1519 #define function_called() _function_called(__func__, __FILE__, __LINE__)
1520 #endif
1521 
1522 #ifdef DOXYGEN
1523 
1533 void expect_function_calls(#function, const int times);
1534 #else
1535 #define expect_function_calls(function, times) \
1536  _expect_function_call(#function, __FILE__, __LINE__, times)
1537 #endif
1538 
1539 #ifdef DOXYGEN
1540 
1548 void expect_function_call(#function);
1549 #else
1550 #define expect_function_call(function) \
1551  _expect_function_call(#function, __FILE__, __LINE__, 1)
1552 #endif
1553 
1554 #ifdef DOXYGEN
1555 
1562 void expect_function_call_any(#function);
1563 #else
1564 #define expect_function_call_any(function) \
1565  _expect_function_call(#function, __FILE__, __LINE__, -1)
1566 #endif
1567 
1568 #ifdef DOXYGEN
1569 
1576 void ignore_function_calls(#function);
1577 #else
1578 #define ignore_function_calls(function) \
1579  _expect_function_call(#function, __FILE__, __LINE__, -2)
1580 #endif
1581 
1610 #ifdef DOXYGEN
1611 
1614 void fail(void);
1615 #else
1616 #define fail() _fail(__FILE__, __LINE__)
1617 #endif
1618 
1619 #ifdef DOXYGEN
1620 
1623 void skip(void);
1624 #else
1625 #define skip() _skip(__FILE__, __LINE__)
1626 #endif
1627 
1628 #ifdef DOXYGEN
1629 
1643 void fail_msg(const char *msg, ...);
1644 #else
1645 #define fail_msg(msg, ...) do { \
1646  print_error("ERROR: " msg "\n", ##__VA_ARGS__); \
1647  fail(); \
1648 } while (0)
1649 #endif
1650 
1651 #ifdef DOXYGEN
1652 
1671 int run_test(#function);
1672 #else
1673 #define run_test(f) _run_test(#f, f, NULL, UNIT_TEST_FUNCTION_TYPE_TEST, NULL)
1674 #endif
1675 
1676 static inline void _unit_test_dummy(void **state) {
1677  (void)state;
1678 }
1679 
1684 #define unit_test(f) { #f, f, UNIT_TEST_FUNCTION_TYPE_TEST }
1685 
1686 #define _unit_test_setup(test, setup) \
1687  { #test "_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_SETUP }
1688 
1693 #define unit_test_setup(test, setup) \
1694  _unit_test_setup(test, setup), \
1695  unit_test(test), \
1696  _unit_test_teardown(test, _unit_test_dummy)
1697 
1698 #define _unit_test_teardown(test, teardown) \
1699  { #test "_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_TEARDOWN }
1700 
1705 #define unit_test_teardown(test, teardown) \
1706  _unit_test_setup(test, _unit_test_dummy), \
1707  unit_test(test), \
1708  _unit_test_teardown(test, teardown)
1709 
1714 #define group_test_setup(setup) \
1715  { "group_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_GROUP_SETUP }
1716 
1721 #define group_test_teardown(teardown) \
1722  { "group_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_GROUP_TEARDOWN }
1723 
1731 #define unit_test_setup_teardown(test, setup, teardown) \
1732  _unit_test_setup(test, setup), \
1733  unit_test(test), \
1734  _unit_test_teardown(test, teardown)
1735 
1736 
1738 #define cmocka_unit_test(f) { #f, f, NULL, NULL, NULL }
1739 
1741 #define cmocka_unit_test_setup(f, setup) { #f, f, setup, NULL, NULL }
1742 
1744 #define cmocka_unit_test_teardown(f, teardown) { #f, f, NULL, teardown, NULL }
1745 
1750 #define cmocka_unit_test_setup_teardown(f, setup, teardown) { #f, f, setup, teardown, NULL }
1751 
1759 #define cmocka_unit_test_prestate(f, state) { #f, f, NULL, NULL, state }
1760 
1768 #define cmocka_unit_test_prestate_setup_teardown(f, setup, teardown, state) { #f, f, setup, teardown, state }
1769 
1770 #define run_tests(tests) _run_tests(tests, sizeof(tests) / sizeof((tests)[0]))
1771 #define run_group_tests(tests) _run_group_tests(tests, sizeof(tests) / sizeof((tests)[0]))
1772 
1773 #ifdef DOXYGEN
1774 
1830 int cmocka_run_group_tests(const struct CMUnitTest group_tests[],
1831  CMFixtureFunction group_setup,
1832  CMFixtureFunction group_teardown);
1833 #else
1834 # define cmocka_run_group_tests(group_tests, group_setup, group_teardown) \
1835  _cmocka_run_group_tests(#group_tests, group_tests, sizeof(group_tests) / sizeof((group_tests)[0]), group_setup, group_teardown)
1836 #endif
1837 
1838 #ifdef DOXYGEN
1839 
1898 int cmocka_run_group_tests_name(const char *group_name,
1899  const struct CMUnitTest group_tests[],
1900  CMFixtureFunction group_setup,
1901  CMFixtureFunction group_teardown);
1902 #else
1903 # define cmocka_run_group_tests_name(group_name, group_tests, group_setup, group_teardown) \
1904  _cmocka_run_group_tests(group_name, group_tests, sizeof(group_tests) / sizeof((group_tests)[0]), group_setup, group_teardown)
1905 #endif
1906 
1932 #ifdef DOXYGEN
1933 
1955 void *test_malloc(size_t size);
1956 #else
1957 #define test_malloc(size) _test_malloc(size, __FILE__, __LINE__)
1958 #endif
1959 
1960 #ifdef DOXYGEN
1961 
1974 void *test_calloc(size_t nmemb, size_t size);
1975 #else
1976 #define test_calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__)
1977 #endif
1978 
1979 #ifdef DOXYGEN
1980 
1990 void *test_realloc(void *ptr, size_t size);
1991 #else
1992 #define test_realloc(ptr, size) _test_realloc(ptr, size, __FILE__, __LINE__)
1993 #endif
1994 
1995 #ifdef DOXYGEN
1996 
2003 void test_free(void *ptr);
2004 #else
2005 #define test_free(ptr) _test_free(ptr, __FILE__, __LINE__)
2006 #endif
2007 
2008 /* Redirect malloc, calloc and free to the unit test allocators. */
2009 #ifdef UNIT_TESTING
2010 #define malloc test_malloc
2011 #define realloc test_realloc
2012 #define calloc test_calloc
2013 #define free test_free
2014 #endif /* UNIT_TESTING */
2015 
2069 void mock_assert(const int result, const char* const expression,
2070  const char * const file, const int line);
2071 
2072 #ifdef DOXYGEN
2073 
2095 void expect_assert_failure(function fn_call);
2096 #else
2097 #define expect_assert_failure(function_call) \
2098  { \
2099  const int result = setjmp(global_expect_assert_env); \
2100  global_expecting_assert = 1; \
2101  if (result) { \
2102  print_message("Expected assertion %s occurred\n", \
2103  global_last_failed_assert); \
2104  global_expecting_assert = 0; \
2105  } else { \
2106  function_call ; \
2107  global_expecting_assert = 0; \
2108  print_error("Expected assert in %s\n", #function_call); \
2109  _fail(__FILE__, __LINE__); \
2110  } \
2111  }
2112 #endif
2113 
2116 /* Function prototype for setup, test and teardown functions. */
2117 typedef void (*UnitTestFunction)(void **state);
2118 
2119 /* Function that determines whether a function parameter value is correct. */
2120 typedef int (*CheckParameterValue)(const LargestIntegralType value,
2121  const LargestIntegralType check_value_data);
2122 
2123 /* Type of the unit test function. */
2124 typedef enum UnitTestFunctionType {
2125  UNIT_TEST_FUNCTION_TYPE_TEST = 0,
2126  UNIT_TEST_FUNCTION_TYPE_SETUP,
2127  UNIT_TEST_FUNCTION_TYPE_TEARDOWN,
2128  UNIT_TEST_FUNCTION_TYPE_GROUP_SETUP,
2129  UNIT_TEST_FUNCTION_TYPE_GROUP_TEARDOWN,
2130 } UnitTestFunctionType;
2131 
2132 /*
2133  * Stores a unit test function with its name and type.
2134  * NOTE: Every setup function must be paired with a teardown function. It's
2135  * possible to specify NULL function pointers.
2136  */
2137 typedef struct UnitTest {
2138  const char* name;
2139  UnitTestFunction function;
2140  UnitTestFunctionType function_type;
2141 } UnitTest;
2142 
2143 typedef struct GroupTest {
2144  UnitTestFunction setup;
2145  UnitTestFunction teardown;
2146  const UnitTest *tests;
2147  const size_t number_of_tests;
2148 } GroupTest;
2149 
2150 /* Function prototype for test functions. */
2151 typedef void (*CMUnitTestFunction)(void **state);
2152 
2153 /* Function prototype for setup and teardown functions. */
2154 typedef int (*CMFixtureFunction)(void **state);
2155 
2156 struct CMUnitTest {
2157  const char *name;
2158  CMUnitTestFunction test_func;
2159  CMFixtureFunction setup_func;
2160  CMFixtureFunction teardown_func;
2161  void *initial_state;
2162 };
2163 
2164 /* Location within some source code. */
2165 typedef struct SourceLocation {
2166  const char* file;
2167  int line;
2168 } SourceLocation;
2169 
2170 /* Event that's called to check a parameter value. */
2171 typedef struct CheckParameterEvent {
2172  SourceLocation location;
2173  const char *parameter_name;
2174  CheckParameterValue check_value;
2175  LargestIntegralType check_value_data;
2177 
2178 /* Used by expect_assert_failure() and mock_assert(). */
2179 extern int global_expecting_assert;
2180 extern jmp_buf global_expect_assert_env;
2181 extern const char * global_last_failed_assert;
2182 
2183 /* Retrieves a value for the given function, as set by "will_return". */
2184 LargestIntegralType _mock(const char * const function, const char* const file,
2185  const int line);
2186 
2187 void _expect_function_call(
2188  const char * const function_name,
2189  const char * const file,
2190  const int line,
2191  const int count);
2192 
2193 void _function_called(const char * const function, const char* const file,
2194  const int line);
2195 
2196 void _expect_check(
2197  const char* const function, const char* const parameter,
2198  const char* const file, const int line,
2199  const CheckParameterValue check_function,
2200  const LargestIntegralType check_data, CheckParameterEvent * const event,
2201  const int count);
2202 
2203 void _expect_in_set(
2204  const char* const function, const char* const parameter,
2205  const char* const file, const int line, const LargestIntegralType values[],
2206  const size_t number_of_values, const int count);
2207 void _expect_not_in_set(
2208  const char* const function, const char* const parameter,
2209  const char* const file, const int line, const LargestIntegralType values[],
2210  const size_t number_of_values, const int count);
2211 
2212 void _expect_in_range(
2213  const char* const function, const char* const parameter,
2214  const char* const file, const int line,
2215  const LargestIntegralType minimum,
2216  const LargestIntegralType maximum, const int count);
2217 void _expect_not_in_range(
2218  const char* const function, const char* const parameter,
2219  const char* const file, const int line,
2220  const LargestIntegralType minimum,
2221  const LargestIntegralType maximum, const int count);
2222 
2223 void _expect_value(
2224  const char* const function, const char* const parameter,
2225  const char* const file, const int line, const LargestIntegralType value,
2226  const int count);
2227 void _expect_not_value(
2228  const char* const function, const char* const parameter,
2229  const char* const file, const int line, const LargestIntegralType value,
2230  const int count);
2231 
2232 void _expect_string(
2233  const char* const function, const char* const parameter,
2234  const char* const file, const int line, const char* string,
2235  const int count);
2236 void _expect_not_string(
2237  const char* const function, const char* const parameter,
2238  const char* const file, const int line, const char* string,
2239  const int count);
2240 
2241 void _expect_memory(
2242  const char* const function, const char* const parameter,
2243  const char* const file, const int line, const void* const memory,
2244  const size_t size, const int count);
2245 void _expect_not_memory(
2246  const char* const function, const char* const parameter,
2247  const char* const file, const int line, const void* const memory,
2248  const size_t size, const int count);
2249 
2250 void _expect_any(
2251  const char* const function, const char* const parameter,
2252  const char* const file, const int line, const int count);
2253 
2254 void _check_expected(
2255  const char * const function_name, const char * const parameter_name,
2256  const char* file, const int line, const LargestIntegralType value);
2257 
2258 void _will_return(const char * const function_name, const char * const file,
2259  const int line, const LargestIntegralType value,
2260  const int count);
2261 void _assert_true(const LargestIntegralType result,
2262  const char* const expression,
2263  const char * const file, const int line);
2264 void _assert_return_code(const LargestIntegralType result,
2265  size_t rlen,
2266  const LargestIntegralType error,
2267  const char * const expression,
2268  const char * const file,
2269  const int line);
2270 void _assert_float_equal(const float a, const float n,
2271  const float epsilon, const char* const file,
2272  const int line);
2273 void _assert_float_not_equal(const float a, const float n,
2274  const float epsilon, const char* const file,
2275  const int line);
2276 void _assert_int_equal(
2277  const LargestIntegralType a, const LargestIntegralType b,
2278  const char * const file, const int line);
2279 void _assert_int_not_equal(
2280  const LargestIntegralType a, const LargestIntegralType b,
2281  const char * const file, const int line);
2282 void _assert_string_equal(const char * const a, const char * const b,
2283  const char * const file, const int line);
2284 void _assert_string_not_equal(const char * const a, const char * const b,
2285  const char *file, const int line);
2286 void _assert_memory_equal(const void * const a, const void * const b,
2287  const size_t size, const char* const file,
2288  const int line);
2289 void _assert_memory_not_equal(const void * const a, const void * const b,
2290  const size_t size, const char* const file,
2291  const int line);
2292 void _assert_in_range(
2293  const LargestIntegralType value, const LargestIntegralType minimum,
2294  const LargestIntegralType maximum, const char* const file, const int line);
2295 void _assert_not_in_range(
2296  const LargestIntegralType value, const LargestIntegralType minimum,
2297  const LargestIntegralType maximum, const char* const file, const int line);
2298 void _assert_in_set(
2299  const LargestIntegralType value, const LargestIntegralType values[],
2300  const size_t number_of_values, const char* const file, const int line);
2301 void _assert_not_in_set(
2302  const LargestIntegralType value, const LargestIntegralType values[],
2303  const size_t number_of_values, const char* const file, const int line);
2304 
2305 void* _test_malloc(const size_t size, const char* file, const int line);
2306 void* _test_realloc(void *ptr, const size_t size, const char* file, const int line);
2307 void* _test_calloc(const size_t number_of_elements, const size_t size,
2308  const char* file, const int line);
2309 void _test_free(void* const ptr, const char* file, const int line);
2310 
2311 void _fail(const char * const file, const int line);
2312 
2313 void _skip(const char * const file, const int line);
2314 
2315 int _run_test(
2316  const char * const function_name, const UnitTestFunction Function,
2317  void ** const volatile state, const UnitTestFunctionType function_type,
2318  const void* const heap_check_point);
2319 CMOCKA_DEPRECATED int _run_tests(const UnitTest * const tests,
2320  const size_t number_of_tests);
2321 CMOCKA_DEPRECATED int _run_group_tests(const UnitTest * const tests,
2322  const size_t number_of_tests);
2323 
2324 /* Test runner */
2325 int _cmocka_run_group_tests(const char *group_name,
2326  const struct CMUnitTest * const tests,
2327  const size_t num_tests,
2328  CMFixtureFunction group_setup,
2329  CMFixtureFunction group_teardown);
2330 
2331 /* Standard output and error print methods. */
2332 void print_message(const char* const format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2);
2333 void print_error(const char* const format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2);
2334 void vprint_message(const char* const format, va_list args) CMOCKA_PRINTF_ATTRIBUTE(1, 0);
2335 void vprint_error(const char* const format, va_list args) CMOCKA_PRINTF_ATTRIBUTE(1, 0);
2336 
2337 enum cm_message_output {
2338  CM_OUTPUT_STDOUT,
2339  CM_OUTPUT_SUBUNIT,
2340  CM_OUTPUT_TAP,
2341  CM_OUTPUT_XML,
2342 };
2343 
2355 void cmocka_set_message_output(enum cm_message_output output);
2356 
2357 
2368 void cmocka_set_test_filter(const char *pattern);
2369 
2380 void cmocka_set_skip_filter(const char *pattern);
2381 
2384 #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'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'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 assert_float_not_equal(float a, float b, float epsilon)
Assert that the two given float are not equal given an epsilon.
void expect_check(#function, #parameter, #check_function, const void *check_data)
Add a custom parameter checking function.
void assert_memory_not_equal(const void *a, const void *b, size_t size)
Assert that the two given areas of memory are not equal.
void expect_in_set(#function, #parameter, LargestIntegralType value_array[])
Add an event to check if the parameter value is part of the provided array.
void expect_not_in_set_count(#function, #parameter, LargestIntegralType value_array[], size_t count)
Add an event to check if the parameter value is not part of the provided array.
void expect_memory(#function, #parameter, void *memory, size_t size)
Add an event to check if the parameter does match an area of memory.
LargestIntegralType mock(void)
Retrieve a return value of the current function.
void fail_msg(const char *msg,...)
Forces the test to fail immediately and quit, printing the reason.
void assert_in_range(LargestIntegralType value, LargestIntegralType minimum, LargestIntegralType maximum)
Assert that the specified value is not smaller than the minimum and and not greater than the maximum.
void expect_in_range(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum)
Add an event to check a parameter is inside a numerical range. The check would succeed if minimum <= ...
void will_return(#function, LargestIntegralType value)
Store a value to be returned by mock() later.
void assert_ptr_equal(void *a, void *b)
Assert that the two given pointers are equal.
void will_return_count(#function, LargestIntegralType value, int count)
Store a value to be returned by mock() later.
void expect_function_call(#function)
Store expected single call to a mock to be checked by function_called() later.
void mock_assert(const int result, const char *const expression, const char *const file, const int line)
Function to replace assert(3) in tested code.
Definition: cmocka.c:1711
int cmocka_run_group_tests(const struct CMUnitTest group_tests[], CMFixtureFunction group_setup, CMFixtureFunction group_teardown)
Run tests specified by an array of CMUnitTest structures.
void expect_in_range_count(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum, size_t count)
Add an event to repeatedly check a parameter is inside a numerical range. The check would succeed if ...
void will_return_maybe(#function, LargestIntegralType value)
Store a value that may be always returned by mock().
void expect_string_count(#function, #parameter, const char *string, size_t count)
Add an event to check if the parameter value is equal to the provided string.
void expect_function_call_any(#function)
Expects function_called() from given mock at least once.
void expect_not_string(#function, #parameter, const char *string)
Add an event to check if the parameter value isn't equal to the provided string.
void expect_not_in_range(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum)
Add an event to check a parameter is outside a numerical range. The check would succeed if minimum > ...
void assert_string_equal(const char *a, const char *b)
Assert that the two given strings are equal.
void * test_malloc(size_t size)
Test function overriding malloc.
void assert_return_code(int rc, int error)
Assert that the return_code is greater than or equal to 0.
void assert_float_equal(float a, float b, float epsilon)
Assert that the two given float are equal given an epsilon.
void assert_non_null(void *pointer)
Assert that the given pointer is non-NULL.
void assert_not_in_set(LargestIntegralType value, LargestIntegralType values[], size_t count)
Assert that the specified value is not within a set.
uintmax_t LargestIntegralType
Definition: cmocka.h: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.
Definition: cmocka.h:2143
void cmocka_set_skip_filter(const char *pattern)
Set a pattern to skip tests matching the pattern.
Definition: cmocka.c:2698
void expect_not_in_range_count(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum, size_t count)
Add an event to repeatedly check a parameter is outside a numerical range. The check would succeed if...
int cmocka_run_group_tests_name(const char *group_name, const struct CMUnitTest group_tests[], CMFixtureFunction group_setup, CMFixtureFunction group_teardown)
Run tests specified by an array of CMUnitTest structures and specify a name.
void assert_ptr_not_equal(void *a, void *b)
Assert that the two given pointers are not equal.
void assert_int_not_equal(int a, int b)
Assert that the two given integers are not equal.
void expect_memory_count(#function, #parameter, void *memory, size_t size, size_t count)
Add an event to repeatedly check if the parameter does match an area of memory.
int run_test(#function)
Generic method to run a single test.
void function_called(void)
Check that current mocked function is being called in the expected order.
void skip(void)
Forces the test to not be executed, but marked as skipped.
Definition: cmocka.h:2165
void cmocka_set_message_output(enum cm_message_output output)
Function to set the output format for a test.
Definition: cmocka.c:2688
void * test_calloc(size_t nmemb, size_t size)
Test function overriding calloc.
void assert_not_in_range(LargestIntegralType value, LargestIntegralType minimum, LargestIntegralType maximum)
Assert that the specified value is smaller than the minimum or greater than the maximum.
void ignore_function_calls(#function)
Ignores function_called() invocations from given mock function.
Definition: cmocka.h:2156
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't the given value.
void expect_any_always(#function, #parameter)
Add an event to always check if a parameter (of any value) has been passed.
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't the given value.
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_string(#function, #parameter, const char *string)
Add an event to check if the parameter value is equal to the provided string.
void expect_value(#function, #parameter, LargestIntegralType value)
Add an event to check if a parameter is the given value.
void test_free(void *ptr)
Test function overriding free(3).
void expect_assert_failure(function fn_call)
Ensure that mock_assert() is called.
Definition: cmocka.h:2137
void cmocka_set_test_filter(const char *pattern)
Set a pattern to only run the test matching the pattern.
Definition: cmocka.c:2693
void expect_value_count(#function, #parameter, LargestIntegralType value, size_t count)
Add an event to repeatedly check if a parameter is the given value.
Definition: cmocka.h:2171
void assert_in_set(LargestIntegralType value, LargestIntegralType values[], size_t count)
Assert that the specified value is within a set.