cmocka  0.3.2
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 #if _MSC_VER < 1500
21 #ifdef __cplusplus
22 extern "C" {
23 #endif /* __cplusplus */
24 int __stdcall IsDebuggerPresent();
25 #ifdef __cplusplus
26 } /* extern "C" */
27 #endif /* __cplusplus */
28 #endif /* _MSC_VER < 1500 */
29 #endif /* _WIN32 */
30 
31 /*
32  * These headers or their equivalents should be included prior to including
33  * this header file.
34  *
35  * #include <stdarg.h>
36  * #include <stddef.h>
37  * #include <setjmp.h>
38  *
39  * This allows test applications to use custom definitions of C standard
40  * library functions and types.
41  */
42 
43 /* For those who are used to __func__ from gcc. */
44 #ifndef __func__
45 #define __func__ __FUNCTION__
46 #endif
47 
48 /* GCC have printf type attribute check. */
49 #ifdef __GNUC__
50 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
51 #else
52 #define PRINTF_ATTRIBUTE(a,b)
53 #endif /* __GNUC__ */
54 
63 /*
64  * Largest integral type. This type should be large enough to hold any
65  * pointer or integer supported by the compiler.
66  */
67 #ifndef LargestIntegralType
68 #define LargestIntegralType unsigned long long
69 #endif /* LargestIntegralType */
70 
71 /* Printf format used to display LargestIntegralType. */
72 #ifndef LargestIntegralTypePrintfFormat
73 #ifdef _WIN32
74 #define LargestIntegralTypePrintfFormat "%I64x"
75 #else
76 #define LargestIntegralTypePrintfFormat "%llx"
77 #endif /* _WIN32 */
78 #endif /* LargestIntegralTypePrintfFormat */
79 
80 /* Perform an unsigned cast to LargestIntegralType. */
81 #define cast_to_largest_integral_type(value) \
82  ((LargestIntegralType)((size_t)(value)))
83 
84 /* Smallest integral type capable of holding a pointer. */
85 #if !defined(_UINTPTR_T) && !defined(_UINTPTR_T_DEFINED)
86 # if defined(_WIN32)
87  /* WIN32 is an ILP32 platform */
88  typedef unsigned int uintptr_t;
89 # elif defined(_WIN64)
90  typedef unsigned long int uintptr_t
91 # else /* _WIN32 */
92 
93 /* ILP32 and LP64 platforms */
94 # ifdef __WORDSIZE /* glibc */
95 # if __WORDSIZE == 64
96  typedef unsigned long int uintptr_t;
97 # else
98  typedef unsigned int uintptr_t;
99 # endif /* __WORDSIZE == 64 */
100 # else /* __WORDSIZE */
101 # if defined(_LP64) || defined(_I32LPx)
102  typedef unsigned long int uintptr_t;
103 # else
104  typedef unsigned int uintptr_t;
105 # endif
106 # endif /* __WORDSIZE */
107 # endif /* _WIN32 */
108 
109 # define _UINTPTR_T
110 # define _UINTPTR_T_DEFINED
111 #endif /* !defined(_UINTPTR_T) || !defined(_UINTPTR_T_DEFINED) */
112 
113 /* Perform an unsigned cast to uintptr_t. */
114 #define cast_to_pointer_integral_type(value) \
115  ((uintptr_t)(value))
116 
117 /* Perform a cast of a pointer to uintmax_t */
118 #define cast_ptr_to_largest_integral_type(value) \
119 cast_to_largest_integral_type(cast_to_pointer_integral_type(value))
120 
169 #ifdef DOXYGEN
170 
177 void *mock(void);
178 #else
179 #define mock() _mock(__func__, __FILE__, __LINE__)
180 #endif
181 
182 #ifdef DOXYGEN
183 
203 void *mock_type(#type);
204 #else
205 #define mock_type(type) ((type) mock())
206 #endif
207 
208 #ifdef DOXYGEN
209 
230 void *mock_ptr_type(#type);
231 #else
232 #define mock_ptr_type(type) ((type) (uintptr_t) mock())
233 #endif
234 
235 
236 #ifdef DOXYGEN
237 
261 void will_return(#function, void *value);
262 #else
263 #define will_return(function, value) \
264  _will_return(#function, __FILE__, __LINE__, \
265  cast_to_largest_integral_type(value), 1)
266 #endif
267 
268 #ifdef DOXYGEN
269 
282 void will_return_count(#function, void *value, int count);
283 #else
284 #define will_return_count(function, value, count) \
285  _will_return(#function, __FILE__, __LINE__, \
286  cast_to_largest_integral_type(value), count)
287 #endif
288 
289 #ifdef DOXYGEN
290 
305 void will_return_always(#function, void *value);
306 #else
307 #define will_return_always(function, value) \
308  will_return_count(function, (value), -1)
309 #endif
310 
357 /*
358  * Add a custom parameter checking function. If the event parameter is NULL
359  * the event structure is allocated internally by this function. If event
360  * parameter is provided it must be allocated on the heap and doesn't need to
361  * be deallocated by the caller.
362  */
363 #ifdef DOXYGEN
364 
380 void expect_check(#function, #parameter, #check_function, const void *check_data);
381 #else
382 #define expect_check(function, parameter, check_function, check_data) \
383  _expect_check(#function, #parameter, __FILE__, __LINE__, check_function, \
384  cast_to_largest_integral_type(check_data), NULL, 0)
385 #endif
386 
387 #if DOXYGEN
388 
402 void expect_in_set(#function, #parameter, uintmax_t value_array[]);
403 #else
404 #define expect_in_set(function, parameter, value_array) \
405  expect_in_set_count(function, parameter, value_array, 1)
406 #endif
407 
408 #if DOXYGEN
409 
427 void expect_in_set_count(#function, #parameter, uintmax_t value_array[], size_t count);
428 #else
429 #define expect_in_set_count(function, parameter, value_array, count) \
430  _expect_in_set(#function, #parameter, __FILE__, __LINE__, value_array, \
431  sizeof(value_array) / sizeof((value_array)[0]), count)
432 #endif
433 
434 #if DOXYGEN
435 
449 void expect_not_in_set(#function, #parameter, uintmax_t value_array[]);
450 #else
451 #define expect_not_in_set(function, parameter, value_array) \
452  expect_not_in_set_count(function, parameter, value_array, 1)
453 #endif
454 
455 #if DOXYGEN
456 
474 void expect_not_in_set_count(#function, #parameter, uintmax_t value_array[], size_t count);
475 #else
476 #define expect_not_in_set_count(function, parameter, value_array, count) \
477  _expect_not_in_set( \
478  #function, #parameter, __FILE__, __LINE__, value_array, \
479  sizeof(value_array) / sizeof((value_array)[0]), count)
480 #endif
481 
482 
483 #if DOXYGEN
484 
500 void expect_in_range(#function, #parameter, uintmax_t minimum, uintmax_t maximum);
501 #else
502 #define expect_in_range(function, parameter, minimum, maximum) \
503  expect_in_range_count(function, parameter, minimum, maximum, 1)
504 #endif
505 
506 #if DOXYGEN
507 
527 void expect_in_range_count(#function, #parameter, uintmax_t minimum, uintmax_t maximum, size_t count);
528 #else
529 #define expect_in_range_count(function, parameter, minimum, maximum, count) \
530  _expect_in_range(#function, #parameter, __FILE__, __LINE__, minimum, \
531  maximum, count)
532 #endif
533 
534 #if DOXYGEN
535 
551 void expect_not_in_range(#function, #parameter, uintmax_t minimum, uintmax_t maximum);
552 #else
553 #define expect_not_in_range(function, parameter, minimum, maximum) \
554  expect_not_in_range_count(function, parameter, minimum, maximum, 1)
555 #endif
556 
557 #if DOXYGEN
558 
578 void expect_not_in_range_count(#function, #parameter, uintmax_t minimum, uintmax_t maximum, size_t count);
579 #else
580 #define expect_not_in_range_count(function, parameter, minimum, maximum, \
581  count) \
582  _expect_not_in_range(#function, #parameter, __FILE__, __LINE__, \
583  minimum, maximum, count)
584 #endif
585 
586 #if DOXYGEN
587 
600 void expect_value(#function, #parameter, uintmax_t value);
601 #else
602 #define expect_value(function, parameter, value) \
603  expect_value_count(function, parameter, value, 1)
604 #endif
605 
606 #if DOXYGEN
607 
624 void expect_value_count(#function, #parameter, uintmax_t value, size_t count);
625 #else
626 #define expect_value_count(function, parameter, value, count) \
627  _expect_value(#function, #parameter, __FILE__, __LINE__, \
628  cast_to_largest_integral_type(value), count)
629 #endif
630 
631 #if DOXYGEN
632 
645 void expect_not_value(#function, #parameter, uintmax_t value);
646 #else
647 #define expect_not_value(function, parameter, value) \
648  expect_not_value_count(function, parameter, value, 1)
649 #endif
650 
651 #if DOXYGEN
652 
669 void expect_not_value_count(#function, #parameter, uintmax_t value, size_t count);
670 #else
671 #define expect_not_value_count(function, parameter, value, count) \
672  _expect_not_value(#function, #parameter, __FILE__, __LINE__, \
673  cast_to_largest_integral_type(value), count)
674 #endif
675 
676 #if DOXYGEN
677 
691 void expect_string(#function, #parameter, const char *string);
692 #else
693 #define expect_string(function, parameter, string) \
694  expect_string_count(function, parameter, string, 1)
695 #endif
696 
697 #if DOXYGEN
698 
716 void expect_string_count(#function, #parameter, const char *string, size_t count);
717 #else
718 #define expect_string_count(function, parameter, string, count) \
719  _expect_string(#function, #parameter, __FILE__, __LINE__, \
720  (const char*)(string), count)
721 #endif
722 
723 #if DOXYGEN
724 
738 void expect_not_string(#function, #parameter, const char *string);
739 #else
740 #define expect_not_string(function, parameter, string) \
741  expect_not_string_count(function, parameter, string, 1)
742 #endif
743 
744 #if DOXYGEN
745 
763 void expect_not_string_count(#function, #parameter, const char *string, size_t count);
764 #else
765 #define expect_not_string_count(function, parameter, string, count) \
766  _expect_not_string(#function, #parameter, __FILE__, __LINE__, \
767  (const char*)(string), count)
768 #endif
769 
770 #if DOXYGEN
771 
786 void expect_memory(#function, #parameter, void *memory, size_t size);
787 #else
788 #define expect_memory(function, parameter, memory, size) \
789  expect_memory_count(function, parameter, memory, size, 1)
790 #endif
791 
792 #if DOXYGEN
793 
813 void expect_memory_count(#function, #parameter, void *memory, size_t size, size_t count);
814 #else
815 #define expect_memory_count(function, parameter, memory, size, count) \
816  _expect_memory(#function, #parameter, __FILE__, __LINE__, \
817  (const void*)(memory), size, count)
818 #endif
819 
820 #if DOXYGEN
821 
837 void expect_not_memory(#function, #parameter, void *memory, size_t size);
838 #else
839 #define expect_not_memory(function, parameter, memory, size) \
840  expect_not_memory_count(function, parameter, memory, size, 1)
841 #endif
842 
843 #if DOXYGEN
844 
864 void expect_not_memory_count(#function, #parameter, void *memory, size_t size, size_t count);
865 #else
866 #define expect_not_memory_count(function, parameter, memory, size, count) \
867  _expect_not_memory(#function, #parameter, __FILE__, __LINE__, \
868  (const void*)(memory), size, count)
869 #endif
870 
871 
872 #if DOXYGEN
873 
884 void expect_any(#function, #parameter);
885 #else
886 #define expect_any(function, parameter) \
887  expect_any_count(function, parameter, 1)
888 #endif
889 
890 #if DOXYGEN
891 
907 void expect_any_count(#function, #parameter, size_t count);
908 #else
909 #define expect_any_count(function, parameter, count) \
910  _expect_any(#function, #parameter, __FILE__, __LINE__, count)
911 #endif
912 
913 #if DOXYGEN
914 
924 void check_expected(#parameter);
925 #else
926 #define check_expected(parameter) \
927  _check_expected(__func__, #parameter, __FILE__, __LINE__, \
928  cast_to_largest_integral_type(parameter))
929 #endif
930 
952 #ifdef DOXYGEN
953 
965 void assert_true(scalar expression);
966 #else
967 #define assert_true(c) _assert_true(cast_to_largest_integral_type(c), #c, \
968  __FILE__, __LINE__)
969 #endif
970 
971 #ifdef DOXYGEN
972 
983 void assert_false(scalar expression);
984 #else
985 #define assert_false(c) _assert_true(!(cast_to_largest_integral_type(c)), #c, \
986  __FILE__, __LINE__)
987 #endif
988 
989 #ifdef DOXYGEN
990 
1000 void assert_non_null(void *pointer);
1001 #else
1002 #define assert_non_null(c) _assert_true(cast_ptr_to_largest_integral_type(c), #c, \
1003  __FILE__, __LINE__)
1004 #endif
1005 
1006 #ifdef DOXYGEN
1007 
1017 void assert_null(void *pointer);
1018 #else
1019 #define assert_null(c) _assert_true(!(cast_ptr_to_largest_integral_type(c)), #c, \
1020 __FILE__, __LINE__)
1021 #endif
1022 
1023 #ifdef DOXYGEN
1024 
1034 void assert_int_equal(int a, int b);
1035 #else
1036 #define assert_int_equal(a, b) \
1037  _assert_int_equal(cast_to_largest_integral_type(a), \
1038  cast_to_largest_integral_type(b), \
1039  __FILE__, __LINE__)
1040 #endif
1041 
1042 #ifdef DOXYGEN
1043 
1055 void assert_int_not_equal(int a, int b);
1056 #else
1057 #define assert_int_not_equal(a, b) \
1058  _assert_int_not_equal(cast_to_largest_integral_type(a), \
1059  cast_to_largest_integral_type(b), \
1060  __FILE__, __LINE__)
1061 #endif
1062 
1063 #ifdef DOXYGEN
1064 
1074 void assert_string_equal(const char *a, const char *b);
1075 #else
1076 #define assert_string_equal(a, b) \
1077  _assert_string_equal((const char*)(a), (const char*)(b), __FILE__, \
1078  __LINE__)
1079 #endif
1080 
1081 #ifdef DOXYGEN
1082 
1092 void assert_string_not_equal(const char *a, const char *b);
1093 #else
1094 #define assert_string_not_equal(a, b) \
1095  _assert_string_not_equal((const char*)(a), (const char*)(b), __FILE__, \
1096  __LINE__)
1097 #endif
1098 
1099 #ifdef DOXYGEN
1100 
1114 void assert_memory_equal(const void *a, const void *b, size_t size);
1115 #else
1116 #define assert_memory_equal(a, b, size) \
1117  _assert_memory_equal((const void*)(a), (const void*)(b), size, __FILE__, \
1118  __LINE__)
1119 #endif
1120 
1121 #ifdef DOXYGEN
1122 
1136 void assert_memory_not_equal(const void *a, const void *b, size_t size);
1137 #else
1138 #define assert_memory_not_equal(a, b, size) \
1139  _assert_memory_not_equal((const void*)(a), (const void*)(b), size, \
1140  __FILE__, __LINE__)
1141 #endif
1142 
1143 #ifdef DOXYGEN
1144 
1157 void assert_in_range(uintmax_t value, uintmax_t minimum, uintmax_t maximum);
1158 #else
1159 #define assert_in_range(value, minimum, maximum) \
1160  _assert_in_range( \
1161  cast_to_largest_integral_type(value), \
1162  cast_to_largest_integral_type(minimum), \
1163  cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
1164 #endif
1165 
1166 #ifdef DOXYGEN
1167 
1180 void assert_not_in_range(uintmax_t value, uintmax_t minimum, uintmax_t maximum);
1181 #else
1182 #define assert_not_in_range(value, minimum, maximum) \
1183  _assert_not_in_range( \
1184  cast_to_largest_integral_type(value), \
1185  cast_to_largest_integral_type(minimum), \
1186  cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
1187 #endif
1188 
1189 #ifdef DOXYGEN
1190 
1202 void assert_in_set(uintmax_t value, uintmax_t values[], size_t count);
1203 #else
1204 #define assert_in_set(value, values, number_of_values) \
1205  _assert_in_set(value, values, number_of_values, __FILE__, __LINE__)
1206 #endif
1207 
1208 #ifdef DOXYGEN
1209 
1221 void assert_not_in_set(uintmax_t value, uintmax_t values[], size_t count);
1222 #else
1223 #define assert_not_in_set(value, values, number_of_values) \
1224  _assert_not_in_set(value, values, number_of_values, __FILE__, __LINE__)
1225 #endif
1226 
1255 #ifdef DOXYGEN
1256 
1259 void fail(void);
1260 #else
1261 #define fail() _fail(__FILE__, __LINE__)
1262 #endif
1263 
1264 #ifdef DOXYGEN
1265 
1268 void fail_msg(const char *msg, ...);
1269 #else
1270 #define fail_msg(msg, ...) do { \
1271  print_error("ERROR: " msg "\n", ##__VA_ARGS__); \
1272  fail(); \
1273 } while (0)
1274 #endif
1275 
1276 #ifdef DOXYGEN
1277 
1294 int run_test(#function);
1295 #else
1296 #define run_test(f) _run_test(#f, f, NULL, UNIT_TEST_FUNCTION_TYPE_TEST, NULL)
1297 #endif
1298 
1300 #define unit_test(f) { #f, f, UNIT_TEST_FUNCTION_TYPE_TEST }
1301 
1302 #define _unit_test_setup(test, setup) \
1303  { #test "_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_SETUP }
1304 
1306 #define unit_test_setup(test, setup) \
1307  _unit_test_setup(test, setup), \
1308  unit_test(test)
1309 
1310 #define _unit_test_teardown(test, teardown) \
1311  { #test "_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_TEARDOWN }
1312 
1314 #define unit_test_teardown(test, teardown) \
1315  unit_test(test), \
1316  _unit_test_teardown(test, teardown)
1317 
1322 #define unit_test_setup_teardown(test, setup, teardown) \
1323  _unit_test_setup(test, setup), \
1324  unit_test(test), \
1325  _unit_test_teardown(test, teardown)
1326 
1327 #ifdef DOXYGEN
1328 
1373 int run_tests(const UnitTest tests[]);
1374 #else
1375 #define run_tests(tests) _run_tests(tests, sizeof(tests) / sizeof(tests)[0])
1376 #endif
1377 
1403 #ifdef DOXYGEN
1404 
1426 void *test_malloc(size_t size);
1427 #else
1428 #define test_malloc(size) _test_malloc(size, __FILE__, __LINE__)
1429 #endif
1430 
1431 #ifdef DOXYGEN
1432 
1445 void *test_calloc(size_t nmemb, size_t size);
1446 #else
1447 #define test_calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__)
1448 #endif
1449 
1450 #ifdef DOXYGEN
1451 
1458 void test_free(void *ptr);
1459 #else
1460 #define test_free(ptr) _test_free(ptr, __FILE__, __LINE__)
1461 #endif
1462 
1463 /* Redirect malloc, calloc and free to the unit test allocators. */
1464 #if UNIT_TESTING
1465 #define malloc test_malloc
1466 #define calloc test_calloc
1467 #define free test_free
1468 #endif /* UNIT_TESTING */
1469 
1523 void mock_assert(const int result, const char* const expression,
1524  const char * const file, const int line);
1525 
1526 #ifdef DOXYGEN
1527 
1549 void expect_assert_failure(function fn_call);
1550 #else
1551 #define expect_assert_failure(function_call) \
1552  { \
1553  const int result = setjmp(global_expect_assert_env); \
1554  global_expecting_assert = 1; \
1555  if (result) { \
1556  print_message("Expected assertion %s occurred\n", \
1557  global_last_failed_assert); \
1558  global_expecting_assert = 0; \
1559  } else { \
1560  function_call ; \
1561  global_expecting_assert = 0; \
1562  print_error("Expected assert in %s\n", #function_call); \
1563  _fail(__FILE__, __LINE__); \
1564  } \
1565  }
1566 #endif
1567 
1570 /* Function prototype for setup, test and teardown functions. */
1571 typedef void (*UnitTestFunction)(void **state);
1572 
1573 /* Function that determines whether a function parameter value is correct. */
1574 typedef int (*CheckParameterValue)(const LargestIntegralType value,
1575  const LargestIntegralType check_value_data);
1576 
1577 /* Type of the unit test function. */
1578 typedef enum UnitTestFunctionType {
1579  UNIT_TEST_FUNCTION_TYPE_TEST = 0,
1580  UNIT_TEST_FUNCTION_TYPE_SETUP,
1581  UNIT_TEST_FUNCTION_TYPE_TEARDOWN,
1582 } UnitTestFunctionType;
1583 
1584 /*
1585  * Stores a unit test function with its name and type.
1586  * NOTE: Every setup function must be paired with a teardown function. It's
1587  * possible to specify NULL function pointers.
1588  */
1589 typedef struct UnitTest {
1590  const char* name;
1591  UnitTestFunction function;
1592  UnitTestFunctionType function_type;
1593 } UnitTest;
1594 
1595 
1596 /* Location within some source code. */
1597 typedef struct SourceLocation {
1598  const char* file;
1599  int line;
1600 } SourceLocation;
1601 
1602 /* Event that's called to check a parameter value. */
1603 typedef struct CheckParameterEvent {
1604  SourceLocation location;
1605  const char *parameter_name;
1606  CheckParameterValue check_value;
1607  LargestIntegralType check_value_data;
1608 } CheckParameterEvent;
1609 
1610 /* Used by expect_assert_failure() and mock_assert(). */
1611 extern int global_expecting_assert;
1612 extern jmp_buf global_expect_assert_env;
1613 extern const char * global_last_failed_assert;
1614 
1615 /* Retrieves a value for the given function, as set by "will_return". */
1616 LargestIntegralType _mock(const char * const function, const char* const file,
1617  const int line);
1618 
1619 void _expect_check(
1620  const char* const function, const char* const parameter,
1621  const char* const file, const int line,
1622  const CheckParameterValue check_function,
1623  const LargestIntegralType check_data, CheckParameterEvent * const event,
1624  const int count);
1625 
1626 void _expect_in_set(
1627  const char* const function, const char* const parameter,
1628  const char* const file, const int line, const LargestIntegralType values[],
1629  const size_t number_of_values, const int count);
1630 void _expect_not_in_set(
1631  const char* const function, const char* const parameter,
1632  const char* const file, const int line, const LargestIntegralType values[],
1633  const size_t number_of_values, const int count);
1634 
1635 void _expect_in_range(
1636  const char* const function, const char* const parameter,
1637  const char* const file, const int line,
1638  const LargestIntegralType minimum,
1639  const LargestIntegralType maximum, const int count);
1640 void _expect_not_in_range(
1641  const char* const function, const char* const parameter,
1642  const char* const file, const int line,
1643  const LargestIntegralType minimum,
1644  const LargestIntegralType maximum, const int count);
1645 
1646 void _expect_value(
1647  const char* const function, const char* const parameter,
1648  const char* const file, const int line, const LargestIntegralType value,
1649  const int count);
1650 void _expect_not_value(
1651  const char* const function, const char* const parameter,
1652  const char* const file, const int line, const LargestIntegralType value,
1653  const int count);
1654 
1655 void _expect_string(
1656  const char* const function, const char* const parameter,
1657  const char* const file, const int line, const char* string,
1658  const int count);
1659 void _expect_not_string(
1660  const char* const function, const char* const parameter,
1661  const char* const file, const int line, const char* string,
1662  const int count);
1663 
1664 void _expect_memory(
1665  const char* const function, const char* const parameter,
1666  const char* const file, const int line, const void* const memory,
1667  const size_t size, const int count);
1668 void _expect_not_memory(
1669  const char* const function, const char* const parameter,
1670  const char* const file, const int line, const void* const memory,
1671  const size_t size, const int count);
1672 
1673 void _expect_any(
1674  const char* const function, const char* const parameter,
1675  const char* const file, const int line, const int count);
1676 
1677 void _check_expected(
1678  const char * const function_name, const char * const parameter_name,
1679  const char* file, const int line, const LargestIntegralType value);
1680 
1681 void _will_return(const char * const function_name, const char * const file,
1682  const int line, const LargestIntegralType value,
1683  const int count);
1684 void _assert_true(const LargestIntegralType result,
1685  const char* const expression,
1686  const char * const file, const int line);
1687 void _assert_int_equal(
1688  const LargestIntegralType a, const LargestIntegralType b,
1689  const char * const file, const int line);
1690 void _assert_int_not_equal(
1691  const LargestIntegralType a, const LargestIntegralType b,
1692  const char * const file, const int line);
1693 void _assert_string_equal(const char * const a, const char * const b,
1694  const char * const file, const int line);
1695 void _assert_string_not_equal(const char * const a, const char * const b,
1696  const char *file, const int line);
1697 void _assert_memory_equal(const void * const a, const void * const b,
1698  const size_t size, const char* const file,
1699  const int line);
1700 void _assert_memory_not_equal(const void * const a, const void * const b,
1701  const size_t size, const char* const file,
1702  const int line);
1703 void _assert_in_range(
1704  const LargestIntegralType value, const LargestIntegralType minimum,
1705  const LargestIntegralType maximum, const char* const file, const int line);
1706 void _assert_not_in_range(
1707  const LargestIntegralType value, const LargestIntegralType minimum,
1708  const LargestIntegralType maximum, const char* const file, const int line);
1709 void _assert_in_set(
1710  const LargestIntegralType value, const LargestIntegralType values[],
1711  const size_t number_of_values, const char* const file, const int line);
1712 void _assert_not_in_set(
1713  const LargestIntegralType value, const LargestIntegralType values[],
1714  const size_t number_of_values, const char* const file, const int line);
1715 
1716 void* _test_malloc(const size_t size, const char* file, const int line);
1717 void* _test_calloc(const size_t number_of_elements, const size_t size,
1718  const char* file, const int line);
1719 void _test_free(void* const ptr, const char* file, const int line);
1720 
1721 void _fail(const char * const file, const int line);
1722 int _run_test(
1723  const char * const function_name, const UnitTestFunction Function,
1724  void ** const volatile state, const UnitTestFunctionType function_type,
1725  const void* const heap_check_point);
1726 int _run_tests(const UnitTest * const tests, const size_t number_of_tests);
1727 
1728 /* Standard output and error print methods. */
1729 void print_message(const char* const format, ...) PRINTF_ATTRIBUTE(1, 2);
1730 void print_error(const char* const format, ...) PRINTF_ATTRIBUTE(1, 2);
1731 void vprint_message(const char* const format, va_list args) PRINTF_ATTRIBUTE(1, 0);
1732 void vprint_error(const char* const format, va_list args) PRINTF_ATTRIBUTE(1, 0);
1733 
1736 #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_not_in_range_count(#function,#parameter, uintmax_t minimum, uintmax_t maximum, size_t count)
Add an event to repeatedly check a parameter is outside a numerical range.
void expect_not_in_range(#function,#parameter, uintmax_t minimum, uintmax_t maximum)
Add an event to check a parameter is outside a numerical range.
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.
int run_tests(const UnitTest tests[])
Run tests specified by an array of UnitTest structures.
void * mock_ptr_type(#type)
Retrieve a typed return value of the current function.
void expect_check(#function,#parameter,#check_function, const void *check_data)
Add a custom parameter checking function.
void * mock(void)
Retrieve a return value of the current function.
void assert_in_set(uintmax_t value, uintmax_t values[], size_t count)
Assert that the specified value is within a set.
void fail_msg(const char *msg,...)
Forces the test to fail immediately and quit, printing the reason.
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 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:1277
void expect_any(#function,#parameter)
Add an event to check if a parameter (of any value) has been passed.
void assert_not_in_range(uintmax_t value, uintmax_t minimum, uintmax_t maximum)
Assert that the specified value is smaller than the minimum and bigger than the maximum.
void assert_in_range(uintmax_t value, uintmax_t minimum, uintmax_t maximum)
Assert that the specified value is bigger than the minimum and smaller than the maximum.
void expect_string(#function,#parameter, const char *string)
Add an event to check if the parameter value is equal to the provided string.
void will_return_always(#function, void *value)
Store a value that will be always returned by mock().
void assert_not_in_set(uintmax_t value, uintmax_t values[], size_t count)
Assert that the specified value is not within a set.
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_value(#function,#parameter, uintmax_t value)
Add an event to check if a parameter is the given value.
void expect_not_value(#function,#parameter, uintmax_t value)
Add an event to check if a parameter isn't the given value.
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_non_null(void *pointer)
Assert that the given pointer is non-NULL.
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 assert_false(scalar expression)
Assert that the given expression is false.
void expect_not_in_set_count(#function,#parameter, uintmax_t value_array[], size_t count)
Add an event to check if the parameter value is not part of the provided array.
void assert_int_not_equal(int a, int b)
Assert that the two given integers are not equal.
void expect_in_range(#function,#parameter, uintmax_t minimum, uintmax_t maximum)
Add an event to check a parameter is inside a numerical range.
int run_test(#function)
Generic method to run a single test.
void will_return_count(#function, void *value, int count)
Store a value to be returned by mock() later.
void expect_not_in_set(#function,#parameter, uintmax_t value_array[])
Add an event to check if the parameter value is not part of the provided array.
void expect_not_value_count(#function,#parameter, uintmax_t value, size_t count)
Add an event to repeatedly check if a parameter isn't the given value.
void * test_calloc(size_t nmemb, size_t size)
Test function overriding calloc.
void expect_memory(#function,#parameter, void *memory, size_t size)
Add an event to check if the parameter does match an area of memory.
void expect_in_set_count(#function,#parameter, uintmax_t value_array[], size_t count)
Add an event to check if the parameter value is part of the provided array.
void will_return(#function, void *value)
Store a value to be returned by mock() later.
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_in_range_count(#function,#parameter, uintmax_t minimum, uintmax_t maximum, size_t count)
Add an event to repeatedly check a parameter is inside a numerical range.
void * mock_type(#type)
Retrieve a typed return value of the current function.
void expect_value_count(#function,#parameter, uintmax_t value, size_t count)
Add an event to repeatedly check if a parameter is 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 expect_in_set(#function,#parameter, uintmax_t value_array[])
Add an event to check if the parameter value is part of the provided array.