cmocka  1.0.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
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. */
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 /* Perform an unsigned cast to LargestIntegralType. */
96 #define cast_to_largest_integral_type(value) \
97  ((LargestIntegralType)(value))
98 
99 /* Smallest integral type capable of holding a pointer. */
100 #if !defined(_UINTPTR_T) && !defined(_UINTPTR_T_DEFINED)
101 # if defined(_WIN32)
102  /* WIN32 is an ILP32 platform */
103  typedef unsigned int uintptr_t;
104 # elif defined(_WIN64)
105  typedef unsigned long int uintptr_t
106 # else /* _WIN32 */
107 
108 /* ILP32 and LP64 platforms */
109 # ifdef __WORDSIZE /* glibc */
110 # if __WORDSIZE == 64
111  typedef unsigned long int uintptr_t;
112 # else
113  typedef unsigned int uintptr_t;
114 # endif /* __WORDSIZE == 64 */
115 # else /* __WORDSIZE */
116 # if defined(_LP64) || defined(_I32LPx)
117  typedef unsigned long int uintptr_t;
118 # else
119  typedef unsigned int uintptr_t;
120 # endif
121 # endif /* __WORDSIZE */
122 # endif /* _WIN32 */
123 
124 # define _UINTPTR_T
125 # define _UINTPTR_T_DEFINED
126 #endif /* !defined(_UINTPTR_T) || !defined(_UINTPTR_T_DEFINED) */
127 
128 /* Perform an unsigned cast to uintptr_t. */
129 #define cast_to_pointer_integral_type(value) \
130  ((uintptr_t)((size_t)(value)))
131 
132 /* Perform a cast of a pointer to LargestIntegralType */
133 #define cast_ptr_to_largest_integral_type(value) \
134 cast_to_largest_integral_type(cast_to_pointer_integral_type(value))
135 
136 /* GCC have printf type attribute check. */
137 #ifdef __GNUC__
138 #define CMOCKA_PRINTF_ATTRIBUTE(a,b) \
139  __attribute__ ((__format__ (__printf__, a, b)))
140 #else
141 #define CMOCKA_PRINTF_ATTRIBUTE(a,b)
142 #endif /* __GNUC__ */
143 
144 #if defined(__GNUC__)
145 #define CMOCKA_DEPRECATED __attribute__ ((deprecated))
146 #elif defined(_MSC_VER)
147 #define CMOCKA_DEPRECATED __declspec(deprecated)
148 #else
149 #define CMOCKA_DEPRECATED
150 #endif
151 
200 #ifdef DOXYGEN
201 
209 #else
210 #define mock() _mock(__func__, __FILE__, __LINE__)
211 #endif
212 
213 #ifdef DOXYGEN
214 
234 #type mock_type(#type);
235 #else
236 #define mock_type(type) ((type) mock())
237 #endif
238 
239 #ifdef DOXYGEN
240 
261 type mock_ptr_type(#type);
262 #else
263 #define mock_ptr_type(type) ((type) (uintptr_t) mock())
264 #endif
265 
266 
267 #ifdef DOXYGEN
268 
292 void will_return(#function, LargestIntegralType value);
293 #else
294 #define will_return(function, value) \
295  _will_return(#function, __FILE__, __LINE__, \
296  cast_to_largest_integral_type(value), 1)
297 #endif
298 
299 #ifdef DOXYGEN
300 
313 void will_return_count(#function, LargestIntegralType value, int count);
314 #else
315 #define will_return_count(function, value, count) \
316  _will_return(#function, __FILE__, __LINE__, \
317  cast_to_largest_integral_type(value), count)
318 #endif
319 
320 #ifdef DOXYGEN
321 
336 void will_return_always(#function, LargestIntegralType value);
337 #else
338 #define will_return_always(function, value) \
339  will_return_count(function, (value), -1)
340 #endif
341 
388 /*
389  * Add a custom parameter checking function. If the event parameter is NULL
390  * the event structure is allocated internally by this function. If event
391  * parameter is provided it must be allocated on the heap and doesn't need to
392  * be deallocated by the caller.
393  */
394 #ifdef DOXYGEN
395 
411 void expect_check(#function, #parameter, #check_function, const void *check_data);
412 #else
413 #define expect_check(function, parameter, check_function, check_data) \
414  _expect_check(#function, #parameter, __FILE__, __LINE__, check_function, \
415  cast_to_largest_integral_type(check_data), NULL, 1)
416 #endif
417 
418 #ifdef DOXYGEN
419 
433 void expect_in_set(#function, #parameter, LargestIntegralType value_array[]);
434 #else
435 #define expect_in_set(function, parameter, value_array) \
436  expect_in_set_count(function, parameter, value_array, 1)
437 #endif
438 
439 #ifdef DOXYGEN
440 
458 void expect_in_set_count(#function, #parameter, LargestIntegralType value_array[], size_t count);
459 #else
460 #define expect_in_set_count(function, parameter, value_array, count) \
461  _expect_in_set(#function, #parameter, __FILE__, __LINE__, value_array, \
462  sizeof(value_array) / sizeof((value_array)[0]), count)
463 #endif
464 
465 #ifdef DOXYGEN
466 
480 void expect_not_in_set(#function, #parameter, LargestIntegralType value_array[]);
481 #else
482 #define expect_not_in_set(function, parameter, value_array) \
483  expect_not_in_set_count(function, parameter, value_array, 1)
484 #endif
485 
486 #ifdef DOXYGEN
487 
505 void expect_not_in_set_count(#function, #parameter, LargestIntegralType value_array[], size_t count);
506 #else
507 #define expect_not_in_set_count(function, parameter, value_array, count) \
508  _expect_not_in_set( \
509  #function, #parameter, __FILE__, __LINE__, value_array, \
510  sizeof(value_array) / sizeof((value_array)[0]), count)
511 #endif
512 
513 
514 #ifdef DOXYGEN
515 
531 void expect_in_range(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum);
532 #else
533 #define expect_in_range(function, parameter, minimum, maximum) \
534  expect_in_range_count(function, parameter, minimum, maximum, 1)
535 #endif
536 
537 #ifdef DOXYGEN
538 
558 void expect_in_range_count(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum, size_t count);
559 #else
560 #define expect_in_range_count(function, parameter, minimum, maximum, count) \
561  _expect_in_range(#function, #parameter, __FILE__, __LINE__, minimum, \
562  maximum, count)
563 #endif
564 
565 #ifdef DOXYGEN
566 
582 void expect_not_in_range(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum);
583 #else
584 #define expect_not_in_range(function, parameter, minimum, maximum) \
585  expect_not_in_range_count(function, parameter, minimum, maximum, 1)
586 #endif
587 
588 #ifdef DOXYGEN
589 
609 void expect_not_in_range_count(#function, #parameter, LargestIntegralType minimum, LargestIntegralType maximum, size_t count);
610 #else
611 #define expect_not_in_range_count(function, parameter, minimum, maximum, \
612  count) \
613  _expect_not_in_range(#function, #parameter, __FILE__, __LINE__, \
614  minimum, maximum, count)
615 #endif
616 
617 #ifdef DOXYGEN
618 
631 void expect_value(#function, #parameter, LargestIntegralType value);
632 #else
633 #define expect_value(function, parameter, value) \
634  expect_value_count(function, parameter, value, 1)
635 #endif
636 
637 #ifdef DOXYGEN
638 
655 void expect_value_count(#function, #parameter, LargestIntegralType value, size_t count);
656 #else
657 #define expect_value_count(function, parameter, value, count) \
658  _expect_value(#function, #parameter, __FILE__, __LINE__, \
659  cast_to_largest_integral_type(value), count)
660 #endif
661 
662 #ifdef DOXYGEN
663 
676 void expect_not_value(#function, #parameter, LargestIntegralType value);
677 #else
678 #define expect_not_value(function, parameter, value) \
679  expect_not_value_count(function, parameter, value, 1)
680 #endif
681 
682 #ifdef DOXYGEN
683 
700 void expect_not_value_count(#function, #parameter, LargestIntegralType value, size_t count);
701 #else
702 #define expect_not_value_count(function, parameter, value, count) \
703  _expect_not_value(#function, #parameter, __FILE__, __LINE__, \
704  cast_to_largest_integral_type(value), count)
705 #endif
706 
707 #ifdef DOXYGEN
708 
722 void expect_string(#function, #parameter, const char *string);
723 #else
724 #define expect_string(function, parameter, string) \
725  expect_string_count(function, parameter, string, 1)
726 #endif
727 
728 #ifdef DOXYGEN
729 
747 void expect_string_count(#function, #parameter, const char *string, size_t count);
748 #else
749 #define expect_string_count(function, parameter, string, count) \
750  _expect_string(#function, #parameter, __FILE__, __LINE__, \
751  (const char*)(string), count)
752 #endif
753 
754 #ifdef DOXYGEN
755 
769 void expect_not_string(#function, #parameter, const char *string);
770 #else
771 #define expect_not_string(function, parameter, string) \
772  expect_not_string_count(function, parameter, string, 1)
773 #endif
774 
775 #ifdef DOXYGEN
776 
794 void expect_not_string_count(#function, #parameter, const char *string, size_t count);
795 #else
796 #define expect_not_string_count(function, parameter, string, count) \
797  _expect_not_string(#function, #parameter, __FILE__, __LINE__, \
798  (const char*)(string), count)
799 #endif
800 
801 #ifdef DOXYGEN
802 
817 void expect_memory(#function, #parameter, void *memory, size_t size);
818 #else
819 #define expect_memory(function, parameter, memory, size) \
820  expect_memory_count(function, parameter, memory, size, 1)
821 #endif
822 
823 #ifdef DOXYGEN
824 
844 void expect_memory_count(#function, #parameter, void *memory, size_t size, size_t count);
845 #else
846 #define expect_memory_count(function, parameter, memory, size, count) \
847  _expect_memory(#function, #parameter, __FILE__, __LINE__, \
848  (const void*)(memory), size, count)
849 #endif
850 
851 #ifdef DOXYGEN
852 
868 void expect_not_memory(#function, #parameter, void *memory, size_t size);
869 #else
870 #define expect_not_memory(function, parameter, memory, size) \
871  expect_not_memory_count(function, parameter, memory, size, 1)
872 #endif
873 
874 #ifdef DOXYGEN
875 
895 void expect_not_memory_count(#function, #parameter, void *memory, size_t size, size_t count);
896 #else
897 #define expect_not_memory_count(function, parameter, memory, size, count) \
898  _expect_not_memory(#function, #parameter, __FILE__, __LINE__, \
899  (const void*)(memory), size, count)
900 #endif
901 
902 
903 #ifdef DOXYGEN
904 
915 void expect_any(#function, #parameter);
916 #else
917 #define expect_any(function, parameter) \
918  expect_any_count(function, parameter, 1)
919 #endif
920 
921 #ifdef DOXYGEN
922 
938 void expect_any_count(#function, #parameter, size_t count);
939 #else
940 #define expect_any_count(function, parameter, count) \
941  _expect_any(#function, #parameter, __FILE__, __LINE__, count)
942 #endif
943 
944 #ifdef DOXYGEN
945 
955 void check_expected(#parameter);
956 #else
957 #define check_expected(parameter) \
958  _check_expected(__func__, #parameter, __FILE__, __LINE__, \
959  cast_to_largest_integral_type(parameter))
960 #endif
961 
962 #ifdef DOXYGEN
963 
973 void check_expected_ptr(#parameter);
974 #else
975 #define check_expected_ptr(parameter) \
976  _check_expected(__func__, #parameter, __FILE__, __LINE__, \
977  cast_ptr_to_largest_integral_type(parameter))
978 #endif
979 
1001 #ifdef DOXYGEN
1002 
1014 void assert_true(scalar expression);
1015 #else
1016 #define assert_true(c) _assert_true(cast_to_largest_integral_type(c), #c, \
1017  __FILE__, __LINE__)
1018 #endif
1019 
1020 #ifdef DOXYGEN
1021 
1032 void assert_false(scalar expression);
1033 #else
1034 #define assert_false(c) _assert_true(!(cast_to_largest_integral_type(c)), #c, \
1035  __FILE__, __LINE__)
1036 #endif
1037 
1038 #ifdef DOXYGEN
1039 
1051 void assert_return_code(int rc, int error);
1052 #else
1053 #define assert_return_code(rc, error) \
1054  _assert_return_code(cast_to_largest_integral_type(rc), \
1055  sizeof(rc), \
1056  cast_to_largest_integral_type(error), \
1057  #rc, __FILE__, __LINE__)
1058 #endif
1059 
1060 #ifdef DOXYGEN
1061 
1071 void assert_non_null(void *pointer);
1072 #else
1073 #define assert_non_null(c) _assert_true(cast_ptr_to_largest_integral_type(c), #c, \
1074  __FILE__, __LINE__)
1075 #endif
1076 
1077 #ifdef DOXYGEN
1078 
1088 void assert_null(void *pointer);
1089 #else
1090 #define assert_null(c) _assert_true(!(cast_ptr_to_largest_integral_type(c)), #c, \
1091 __FILE__, __LINE__)
1092 #endif
1093 
1094 #ifdef DOXYGEN
1095 
1105 void assert_ptr_equal(void *a, void *b);
1106 #else
1107 #define assert_ptr_equal(a, b) \
1108  _assert_int_equal(cast_ptr_to_largest_integral_type(a), \
1109  cast_ptr_to_largest_integral_type(b), \
1110  __FILE__, __LINE__)
1111 #endif
1112 
1113 #ifdef DOXYGEN
1114 
1124 void assert_ptr_not_equal(void *a, void *b);
1125 #else
1126 #define assert_ptr_not_equal(a, b) \
1127  _assert_int_not_equal(cast_ptr_to_largest_integral_type(a), \
1128  cast_ptr_to_largest_integral_type(b), \
1129  __FILE__, __LINE__)
1130 #endif
1131 
1132 #ifdef DOXYGEN
1133 
1143 void assert_int_equal(int a, int b);
1144 #else
1145 #define assert_int_equal(a, b) \
1146  _assert_int_equal(cast_to_largest_integral_type(a), \
1147  cast_to_largest_integral_type(b), \
1148  __FILE__, __LINE__)
1149 #endif
1150 
1151 #ifdef DOXYGEN
1152 
1164 void assert_int_not_equal(int a, int b);
1165 #else
1166 #define assert_int_not_equal(a, b) \
1167  _assert_int_not_equal(cast_to_largest_integral_type(a), \
1168  cast_to_largest_integral_type(b), \
1169  __FILE__, __LINE__)
1170 #endif
1171 
1172 #ifdef DOXYGEN
1173 
1183 void assert_string_equal(const char *a, const char *b);
1184 #else
1185 #define assert_string_equal(a, b) \
1186  _assert_string_equal((const char*)(a), (const char*)(b), __FILE__, \
1187  __LINE__)
1188 #endif
1189 
1190 #ifdef DOXYGEN
1191 
1201 void assert_string_not_equal(const char *a, const char *b);
1202 #else
1203 #define assert_string_not_equal(a, b) \
1204  _assert_string_not_equal((const char*)(a), (const char*)(b), __FILE__, \
1205  __LINE__)
1206 #endif
1207 
1208 #ifdef DOXYGEN
1209 
1223 void assert_memory_equal(const void *a, const void *b, size_t size);
1224 #else
1225 #define assert_memory_equal(a, b, size) \
1226  _assert_memory_equal((const void*)(a), (const void*)(b), size, __FILE__, \
1227  __LINE__)
1228 #endif
1229 
1230 #ifdef DOXYGEN
1231 
1245 void assert_memory_not_equal(const void *a, const void *b, size_t size);
1246 #else
1247 #define assert_memory_not_equal(a, b, size) \
1248  _assert_memory_not_equal((const void*)(a), (const void*)(b), size, \
1249  __FILE__, __LINE__)
1250 #endif
1251 
1252 #ifdef DOXYGEN
1253 
1267 #else
1268 #define assert_in_range(value, minimum, maximum) \
1269  _assert_in_range( \
1270  cast_to_largest_integral_type(value), \
1271  cast_to_largest_integral_type(minimum), \
1272  cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
1273 #endif
1274 
1275 #ifdef DOXYGEN
1276 
1290 #else
1291 #define assert_not_in_range(value, minimum, maximum) \
1292  _assert_not_in_range( \
1293  cast_to_largest_integral_type(value), \
1294  cast_to_largest_integral_type(minimum), \
1295  cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
1296 #endif
1297 
1298 #ifdef DOXYGEN
1299 
1311 void assert_in_set(LargestIntegralType value, LargestIntegralType values[], size_t count);
1312 #else
1313 #define assert_in_set(value, values, number_of_values) \
1314  _assert_in_set(value, values, number_of_values, __FILE__, __LINE__)
1315 #endif
1316 
1317 #ifdef DOXYGEN
1318 
1330 void assert_not_in_set(LargestIntegralType value, LargestIntegralType values[], size_t count);
1331 #else
1332 #define assert_not_in_set(value, values, number_of_values) \
1333  _assert_not_in_set(value, values, number_of_values, __FILE__, __LINE__)
1334 #endif
1335 
1364 #ifdef DOXYGEN
1365 
1368 void fail(void);
1369 #else
1370 #define fail() _fail(__FILE__, __LINE__)
1371 #endif
1372 
1373 #ifdef DOXYGEN
1374 
1377 void skip(void);
1378 #else
1379 #define skip() _skip(__FILE__, __LINE__)
1380 #endif
1381 
1382 #ifdef DOXYGEN
1383 
1397 void fail_msg(const char *msg, ...);
1398 #else
1399 #define fail_msg(msg, ...) do { \
1400  print_error("ERROR: " msg "\n", ##__VA_ARGS__); \
1401  fail(); \
1402 } while (0)
1403 #endif
1404 
1405 #ifdef DOXYGEN
1406 
1425 int run_test(#function);
1426 #else
1427 #define run_test(f) _run_test(#f, f, NULL, UNIT_TEST_FUNCTION_TYPE_TEST, NULL)
1428 #endif
1429 
1430 static inline void _unit_test_dummy(void **state) {
1431  (void)state;
1432 }
1433 
1438 #define unit_test(f) { #f, f, UNIT_TEST_FUNCTION_TYPE_TEST }
1439 
1440 #define _unit_test_setup(test, setup) \
1441  { #test "_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_SETUP }
1442 
1447 #define unit_test_setup(test, setup) \
1448  _unit_test_setup(test, setup), \
1449  unit_test(test), \
1450  _unit_test_teardown(test, _unit_test_dummy)
1451 
1452 #define _unit_test_teardown(test, teardown) \
1453  { #test "_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_TEARDOWN }
1454 
1459 #define unit_test_teardown(test, teardown) \
1460  _unit_test_setup(test, _unit_test_dummy), \
1461  unit_test(test), \
1462  _unit_test_teardown(test, teardown)
1463 
1468 #define group_test_setup(setup) \
1469  { "group_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_GROUP_SETUP }
1470 
1475 #define group_test_teardown(teardown) \
1476  { "group_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_GROUP_TEARDOWN }
1477 
1485 #define unit_test_setup_teardown(test, setup, teardown) \
1486  _unit_test_setup(test, setup), \
1487  unit_test(test), \
1488  _unit_test_teardown(test, teardown)
1489 
1490 
1492 #define cmocka_unit_test(f) { #f, f, NULL, NULL }
1493 
1495 #define cmocka_unit_test_setup(f, setup) { #f, f, setup, NULL }
1496 
1498 #define cmocka_unit_test_teardown(f, teardown) { #f, f, NULL, teardown }
1499 
1504 #define cmocka_unit_test_setup_teardown(f, setup, teardown) { #f, f, setup, teardown }
1505 
1506 #define run_tests(tests) _run_tests(tests, sizeof(tests) / sizeof(tests)[0])
1507 #define run_group_tests(tests) _run_group_tests(tests, sizeof(tests) / sizeof(tests)[0])
1508 
1509 #ifdef DOXYGEN
1510 
1566 int cmocka_run_group_tests(const struct CMUnitTest group_tests[],
1567  CMFixtureFunction group_setup,
1568  CMFixtureFunction group_teardown);
1569 #else
1570 # define cmocka_run_group_tests(group_tests, group_setup, group_teardown) \
1571  _cmocka_run_group_tests(#group_tests, group_tests, sizeof(group_tests) / sizeof(group_tests)[0], group_setup, group_teardown)
1572 #endif
1573 
1574 #ifdef DOXYGEN
1575 
1634 int cmocka_run_group_tests_name(const char *group_name,
1635  const struct CMUnitTest group_tests[],
1636  CMFixtureFunction group_setup,
1637  CMFixtureFunction group_teardown);
1638 #else
1639 # define cmocka_run_group_tests_name(group_name, group_tests, group_setup, group_teardown) \
1640  _cmocka_run_group_tests(group_name, group_tests, sizeof(group_tests) / sizeof(group_tests)[0], group_setup, group_teardown)
1641 #endif
1642 
1668 #ifdef DOXYGEN
1669 
1691 void *test_malloc(size_t size);
1692 #else
1693 #define test_malloc(size) _test_malloc(size, __FILE__, __LINE__)
1694 #endif
1695 
1696 #ifdef DOXYGEN
1697 
1710 void *test_calloc(size_t nmemb, size_t size);
1711 #else
1712 #define test_calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__)
1713 #endif
1714 
1715 #ifdef DOXYGEN
1716 
1726 void *test_realloc(void *ptr, size_t size);
1727 #else
1728 #define test_realloc(ptr, size) _test_realloc(ptr, size, __FILE__, __LINE__)
1729 #endif
1730 
1731 #ifdef DOXYGEN
1732 
1739 void test_free(void *ptr);
1740 #else
1741 #define test_free(ptr) _test_free(ptr, __FILE__, __LINE__)
1742 #endif
1743 
1744 /* Redirect malloc, calloc and free to the unit test allocators. */
1745 #ifdef UNIT_TESTING
1746 #define malloc test_malloc
1747 #define realloc test_realloc
1748 #define calloc test_calloc
1749 #define free test_free
1750 #endif /* UNIT_TESTING */
1751 
1805 void mock_assert(const int result, const char* const expression,
1806  const char * const file, const int line);
1807 
1808 #ifdef DOXYGEN
1809 
1831 void expect_assert_failure(function fn_call);
1832 #else
1833 #define expect_assert_failure(function_call) \
1834  { \
1835  const int result = setjmp(global_expect_assert_env); \
1836  global_expecting_assert = 1; \
1837  if (result) { \
1838  print_message("Expected assertion %s occurred\n", \
1839  global_last_failed_assert); \
1840  global_expecting_assert = 0; \
1841  } else { \
1842  function_call ; \
1843  global_expecting_assert = 0; \
1844  print_error("Expected assert in %s\n", #function_call); \
1845  _fail(__FILE__, __LINE__); \
1846  } \
1847  }
1848 #endif
1849 
1852 /* Function prototype for setup, test and teardown functions. */
1853 typedef void (*UnitTestFunction)(void **state);
1854 
1855 /* Function that determines whether a function parameter value is correct. */
1856 typedef int (*CheckParameterValue)(const LargestIntegralType value,
1857  const LargestIntegralType check_value_data);
1858 
1859 /* Type of the unit test function. */
1860 typedef enum UnitTestFunctionType {
1861  UNIT_TEST_FUNCTION_TYPE_TEST = 0,
1862  UNIT_TEST_FUNCTION_TYPE_SETUP,
1863  UNIT_TEST_FUNCTION_TYPE_TEARDOWN,
1864  UNIT_TEST_FUNCTION_TYPE_GROUP_SETUP,
1865  UNIT_TEST_FUNCTION_TYPE_GROUP_TEARDOWN,
1866 } UnitTestFunctionType;
1867 
1868 /*
1869  * Stores a unit test function with its name and type.
1870  * NOTE: Every setup function must be paired with a teardown function. It's
1871  * possible to specify NULL function pointers.
1872  */
1873 typedef struct UnitTest {
1874  const char* name;
1875  UnitTestFunction function;
1876  UnitTestFunctionType function_type;
1877 } UnitTest;
1878 
1879 typedef struct GroupTest {
1880  UnitTestFunction setup;
1881  UnitTestFunction teardown;
1882  const UnitTest *tests;
1883  const size_t number_of_tests;
1884 } GroupTest;
1885 
1886 /* Function prototype for test functions. */
1887 typedef void (*CMUnitTestFunction)(void **state);
1888 
1889 /* Function prototype for setup and teardown functions. */
1890 typedef int (*CMFixtureFunction)(void **state);
1891 
1892 struct CMUnitTest {
1893  const char *name;
1894  CMUnitTestFunction test_func;
1895  CMFixtureFunction setup_func;
1896  CMFixtureFunction teardown_func;
1897 };
1898 
1899 /* Location within some source code. */
1900 typedef struct SourceLocation {
1901  const char* file;
1902  int line;
1903 } SourceLocation;
1904 
1905 /* Event that's called to check a parameter value. */
1906 typedef struct CheckParameterEvent {
1907  SourceLocation location;
1908  const char *parameter_name;
1909  CheckParameterValue check_value;
1910  LargestIntegralType check_value_data;
1911 } CheckParameterEvent;
1912 
1913 /* Used by expect_assert_failure() and mock_assert(). */
1914 extern int global_expecting_assert;
1915 extern jmp_buf global_expect_assert_env;
1916 extern const char * global_last_failed_assert;
1917 
1918 /* Retrieves a value for the given function, as set by "will_return". */
1919 LargestIntegralType _mock(const char * const function, const char* const file,
1920  const int line);
1921 
1922 void _expect_check(
1923  const char* const function, const char* const parameter,
1924  const char* const file, const int line,
1925  const CheckParameterValue check_function,
1926  const LargestIntegralType check_data, CheckParameterEvent * const event,
1927  const int count);
1928 
1929 void _expect_in_set(
1930  const char* const function, const char* const parameter,
1931  const char* const file, const int line, const LargestIntegralType values[],
1932  const size_t number_of_values, const int count);
1933 void _expect_not_in_set(
1934  const char* const function, const char* const parameter,
1935  const char* const file, const int line, const LargestIntegralType values[],
1936  const size_t number_of_values, const int count);
1937 
1938 void _expect_in_range(
1939  const char* const function, const char* const parameter,
1940  const char* const file, const int line,
1941  const LargestIntegralType minimum,
1942  const LargestIntegralType maximum, const int count);
1943 void _expect_not_in_range(
1944  const char* const function, const char* const parameter,
1945  const char* const file, const int line,
1946  const LargestIntegralType minimum,
1947  const LargestIntegralType maximum, const int count);
1948 
1949 void _expect_value(
1950  const char* const function, const char* const parameter,
1951  const char* const file, const int line, const LargestIntegralType value,
1952  const int count);
1953 void _expect_not_value(
1954  const char* const function, const char* const parameter,
1955  const char* const file, const int line, const LargestIntegralType value,
1956  const int count);
1957 
1958 void _expect_string(
1959  const char* const function, const char* const parameter,
1960  const char* const file, const int line, const char* string,
1961  const int count);
1962 void _expect_not_string(
1963  const char* const function, const char* const parameter,
1964  const char* const file, const int line, const char* string,
1965  const int count);
1966 
1967 void _expect_memory(
1968  const char* const function, const char* const parameter,
1969  const char* const file, const int line, const void* const memory,
1970  const size_t size, const int count);
1971 void _expect_not_memory(
1972  const char* const function, const char* const parameter,
1973  const char* const file, const int line, const void* const memory,
1974  const size_t size, const int count);
1975 
1976 void _expect_any(
1977  const char* const function, const char* const parameter,
1978  const char* const file, const int line, const int count);
1979 
1980 void _check_expected(
1981  const char * const function_name, const char * const parameter_name,
1982  const char* file, const int line, const LargestIntegralType value);
1983 
1984 void _will_return(const char * const function_name, const char * const file,
1985  const int line, const LargestIntegralType value,
1986  const int count);
1987 void _assert_true(const LargestIntegralType result,
1988  const char* const expression,
1989  const char * const file, const int line);
1990 void _assert_return_code(const LargestIntegralType result,
1991  size_t rlen,
1992  const LargestIntegralType error,
1993  const char * const expression,
1994  const char * const file,
1995  const int line);
1996 void _assert_int_equal(
1997  const LargestIntegralType a, const LargestIntegralType b,
1998  const char * const file, const int line);
1999 void _assert_int_not_equal(
2000  const LargestIntegralType a, const LargestIntegralType b,
2001  const char * const file, const int line);
2002 void _assert_string_equal(const char * const a, const char * const b,
2003  const char * const file, const int line);
2004 void _assert_string_not_equal(const char * const a, const char * const b,
2005  const char *file, const int line);
2006 void _assert_memory_equal(const void * const a, const void * const b,
2007  const size_t size, const char* const file,
2008  const int line);
2009 void _assert_memory_not_equal(const void * const a, const void * const b,
2010  const size_t size, const char* const file,
2011  const int line);
2012 void _assert_in_range(
2013  const LargestIntegralType value, const LargestIntegralType minimum,
2014  const LargestIntegralType maximum, const char* const file, const int line);
2015 void _assert_not_in_range(
2016  const LargestIntegralType value, const LargestIntegralType minimum,
2017  const LargestIntegralType maximum, const char* const file, const int line);
2018 void _assert_in_set(
2019  const LargestIntegralType value, const LargestIntegralType values[],
2020  const size_t number_of_values, const char* const file, const int line);
2021 void _assert_not_in_set(
2022  const LargestIntegralType value, const LargestIntegralType values[],
2023  const size_t number_of_values, const char* const file, const int line);
2024 
2025 void* _test_malloc(const size_t size, const char* file, const int line);
2026 void* _test_realloc(void *ptr, const size_t size, const char* file, const int line);
2027 void* _test_calloc(const size_t number_of_elements, const size_t size,
2028  const char* file, const int line);
2029 void _test_free(void* const ptr, const char* file, const int line);
2030 
2031 void _fail(const char * const file, const int line);
2032 
2033 void _skip(const char * const file, const int line);
2034 
2035 int _run_test(
2036  const char * const function_name, const UnitTestFunction Function,
2037  void ** const volatile state, const UnitTestFunctionType function_type,
2038  const void* const heap_check_point);
2039 CMOCKA_DEPRECATED int _run_tests(const UnitTest * const tests,
2040  const size_t number_of_tests);
2041 CMOCKA_DEPRECATED int _run_group_tests(const UnitTest * const tests,
2042  const size_t number_of_tests);
2043 
2044 /* Test runner */
2045 int _cmocka_run_group_tests(const char *group_name,
2046  const struct CMUnitTest * const tests,
2047  const size_t num_tests,
2048  CMFixtureFunction group_setup,
2049  CMFixtureFunction group_teardown);
2050 
2051 /* Standard output and error print methods. */
2052 void print_message(const char* const format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2);
2053 void print_error(const char* const format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2);
2054 void vprint_message(const char* const format, va_list args) CMOCKA_PRINTF_ATTRIBUTE(1, 0);
2055 void vprint_error(const char* const format, va_list args) CMOCKA_PRINTF_ATTRIBUTE(1, 0);
2056 
2057 enum cm_message_output {
2058  CM_OUTPUT_STDOUT,
2059  CM_OUTPUT_SUBUNIT,
2060  CM_OUTPUT_TAP,
2061  CM_OUTPUT_XML,
2062 };
2063 
2075 void cmocka_set_message_output(enum cm_message_output output);
2076 
2079 #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 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:1331
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 expect_string(#function,#parameter, const char *string)
Add an event to check if the parameter value is equal to the provided string.
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_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'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'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'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.
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 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:2202
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 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'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'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.