23 #define inline __inline
30 int __stdcall IsDebuggerPresent();
52 #define __func__ __FUNCTION__
57 #define CMOCKA_PRINTF_ATTRIBUTE(a,b) \
58 __attribute__ ((__format__ (__printf__, a, b)))
60 #define CMOCKA_PRINTF_ATTRIBUTE(a,b)
75 #ifndef LargestIntegralType
76 #define LargestIntegralType unsigned long long
80 #ifndef LargestIntegralTypePrintfFormat
82 #define LargestIntegralTypePrintfFormat "0x%I64x"
84 #define LargestIntegralTypePrintfFormat "%#llx"
89 #define cast_to_largest_integral_type(value) \
90 ((LargestIntegralType)((size_t)(value)))
93 #if !defined(_UINTPTR_T) && !defined(_UINTPTR_T_DEFINED)
96 typedef unsigned int uintptr_t;
97 # elif defined(_WIN64)
98 typedef unsigned long int uintptr_t
103 # if __WORDSIZE == 64
104 typedef unsigned long int uintptr_t;
106 typedef unsigned int uintptr_t;
109 # if defined(_LP64) || defined(_I32LPx)
110 typedef unsigned long int uintptr_t;
112 typedef unsigned int uintptr_t;
118 # define _UINTPTR_T_DEFINED
122 #define cast_to_pointer_integral_type(value) \
126 #define cast_ptr_to_largest_integral_type(value) \
127 cast_to_largest_integral_type(cast_to_pointer_integral_type(value))
187 #define mock() _mock(__func__, __FILE__, __LINE__)
213 #define mock_type(type) ((type) mock())
240 #define mock_ptr_type(type) ((type) (uintptr_t) mock())
271 #define will_return(function, value) \
272 _will_return(#function, __FILE__, __LINE__, \
273 cast_to_largest_integral_type(value), 1)
292 #define will_return_count(function, value, count) \
293 _will_return(#function, __FILE__, __LINE__, \
294 cast_to_largest_integral_type(value), count)
315 #define will_return_always(function, value) \
316 will_return_count(function, (value), -1)
388 void expect_check(#
function, #parameter, #check_function,
const void *check_data);
390 #define expect_check(function, parameter, check_function, check_data) \
391 _expect_check(#function, #parameter, __FILE__, __LINE__, check_function, \
392 cast_to_largest_integral_type(check_data), NULL, 0)
410 void expect_in_set(#
function, #parameter, uintmax_t value_array[]);
412 #define expect_in_set(function, parameter, value_array) \
413 expect_in_set_count(function, parameter, value_array, 1)
437 #define expect_in_set_count(function, parameter, value_array, count) \
438 _expect_in_set(#function, #parameter, __FILE__, __LINE__, value_array, \
439 sizeof(value_array) / sizeof((value_array)[0]), count)
459 #define expect_not_in_set(function, parameter, value_array) \
460 expect_not_in_set_count(function, parameter, value_array, 1)
484 #define expect_not_in_set_count(function, parameter, value_array, count) \
485 _expect_not_in_set( \
486 #function, #parameter, __FILE__, __LINE__, value_array, \
487 sizeof(value_array) / sizeof((value_array)[0]), count)
508 void expect_in_range(#
function, #parameter, uintmax_t minimum, uintmax_t maximum);
510 #define expect_in_range(function, parameter, minimum, maximum) \
511 expect_in_range_count(function, parameter, minimum, maximum, 1)
535 void expect_in_range_count(#
function, #parameter, uintmax_t minimum, uintmax_t maximum,
size_t count);
537 #define expect_in_range_count(function, parameter, minimum, maximum, count) \
538 _expect_in_range(#function, #parameter, __FILE__, __LINE__, minimum, \
561 #define expect_not_in_range(function, parameter, minimum, maximum) \
562 expect_not_in_range_count(function, parameter, minimum, maximum, 1)
588 #define expect_not_in_range_count(function, parameter, minimum, maximum, \
590 _expect_not_in_range(#function, #parameter, __FILE__, __LINE__, \
591 minimum, maximum, count)
608 void expect_value(#
function, #parameter, uintmax_t value);
610 #define expect_value(function, parameter, value) \
611 expect_value_count(function, parameter, value, 1)
634 #define expect_value_count(function, parameter, value, count) \
635 _expect_value(#function, #parameter, __FILE__, __LINE__, \
636 cast_to_largest_integral_type(value), count)
655 #define expect_not_value(function, parameter, value) \
656 expect_not_value_count(function, parameter, value, 1)
679 #define expect_not_value_count(function, parameter, value, count) \
680 _expect_not_value(#function, #parameter, __FILE__, __LINE__, \
681 cast_to_largest_integral_type(value), count)
699 void expect_string(#
function, #parameter,
const char *
string);
701 #define expect_string(function, parameter, string) \
702 expect_string_count(function, parameter, string, 1)
726 #define expect_string_count(function, parameter, string, count) \
727 _expect_string(#function, #parameter, __FILE__, __LINE__, \
728 (const char*)(string), count)
748 #define expect_not_string(function, parameter, string) \
749 expect_not_string_count(function, parameter, string, 1)
773 #define expect_not_string_count(function, parameter, string, count) \
774 _expect_not_string(#function, #parameter, __FILE__, __LINE__, \
775 (const char*)(string), count)
794 void expect_memory(#
function, #parameter,
void *memory,
size_t size);
796 #define expect_memory(function, parameter, memory, size) \
797 expect_memory_count(function, parameter, memory, size, 1)
823 #define expect_memory_count(function, parameter, memory, size, count) \
824 _expect_memory(#function, #parameter, __FILE__, __LINE__, \
825 (const void*)(memory), size, count)
847 #define expect_not_memory(function, parameter, memory, size) \
848 expect_not_memory_count(function, parameter, memory, size, 1)
874 #define expect_not_memory_count(function, parameter, memory, size, count) \
875 _expect_not_memory(#function, #parameter, __FILE__, __LINE__, \
876 (const void*)(memory), size, count)
894 #define expect_any(function, parameter) \
895 expect_any_count(function, parameter, 1)
917 #define expect_any_count(function, parameter, count) \
918 _expect_any(#function, #parameter, __FILE__, __LINE__, count)
934 #define check_expected(parameter) \
935 _check_expected(__func__, #parameter, __FILE__, __LINE__, \
936 cast_to_largest_integral_type(parameter))
975 #define assert_true(c) _assert_true(cast_to_largest_integral_type(c), #c, \
993 #define assert_false(c) _assert_true(!(cast_to_largest_integral_type(c)), #c, \
1012 #define assert_return_code(rc, error) \
1013 _assert_return_code(cast_to_largest_integral_type(rc), \
1015 cast_to_largest_integral_type(error), \
1016 #rc, __FILE__, __LINE__)
1032 #define assert_non_null(c) _assert_true(cast_ptr_to_largest_integral_type(c), #c, \
1049 #define assert_null(c) _assert_true(!(cast_ptr_to_largest_integral_type(c)), #c, \
1066 #define assert_int_equal(a, b) \
1067 _assert_int_equal(cast_to_largest_integral_type(a), \
1068 cast_to_largest_integral_type(b), \
1087 #define assert_int_not_equal(a, b) \
1088 _assert_int_not_equal(cast_to_largest_integral_type(a), \
1089 cast_to_largest_integral_type(b), \
1106 #define assert_string_equal(a, b) \
1107 _assert_string_equal((const char*)(a), (const char*)(b), __FILE__, \
1124 #define assert_string_not_equal(a, b) \
1125 _assert_string_not_equal((const char*)(a), (const char*)(b), __FILE__, \
1146 #define assert_memory_equal(a, b, size) \
1147 _assert_memory_equal((const void*)(a), (const void*)(b), size, __FILE__, \
1168 #define assert_memory_not_equal(a, b, size) \
1169 _assert_memory_not_equal((const void*)(a), (const void*)(b), size, \
1187 void assert_in_range(uintmax_t value, uintmax_t minimum, uintmax_t maximum);
1189 #define assert_in_range(value, minimum, maximum) \
1191 cast_to_largest_integral_type(value), \
1192 cast_to_largest_integral_type(minimum), \
1193 cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
1212 #define assert_not_in_range(value, minimum, maximum) \
1213 _assert_not_in_range( \
1214 cast_to_largest_integral_type(value), \
1215 cast_to_largest_integral_type(minimum), \
1216 cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
1232 void assert_in_set(uintmax_t value, uintmax_t values[],
size_t count);
1234 #define assert_in_set(value, values, number_of_values) \
1235 _assert_in_set(value, values, number_of_values, __FILE__, __LINE__)
1253 #define assert_not_in_set(value, values, number_of_values) \
1254 _assert_not_in_set(value, values, number_of_values, __FILE__, __LINE__)
1291 #define fail() _fail(__FILE__, __LINE__)
1298 void fail_msg(
const char *msg, ...);
1300 #define fail_msg(msg, ...) do { \
1301 print_error("ERROR: " msg "\n", ##__VA_ARGS__); \
1326 #define run_test(f) _run_test(#f, f, NULL, UNIT_TEST_FUNCTION_TYPE_TEST, NULL)
1329 static inline void _unit_test_dummy(
void **state) {
1334 #define unit_test(f) { #f, f, UNIT_TEST_FUNCTION_TYPE_TEST }
1336 #define _unit_test_setup(test, setup) \
1337 { #test "_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_SETUP }
1340 #define unit_test_setup(test, setup) \
1341 _unit_test_setup(test, setup), \
1343 _unit_test_teardown(test, _unit_test_dummy)
1345 #define _unit_test_teardown(test, teardown) \
1346 { #test "_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_TEARDOWN }
1349 #define unit_test_teardown(test, teardown) \
1350 _unit_test_setup(test, _unit_test_dummy), \
1352 _unit_test_teardown(test, teardown)
1354 #define group_test_setup(setup) \
1355 { "group_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_GROUP_SETUP }
1357 #define group_test_teardown(teardown) \
1358 { "group_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_GROUP_TEARDOWN }
1364 #define unit_test_setup_teardown(test, setup, teardown) \
1365 _unit_test_setup(test, setup), \
1367 _unit_test_teardown(test, teardown)
1417 #define run_tests(tests) _run_tests(tests, sizeof(tests) / sizeof(tests)[0])
1420 #define run_group_tests(tests) _run_group_tests(tests, sizeof(tests) / sizeof(tests)[0])
1472 #define test_malloc(size) _test_malloc(size, __FILE__, __LINE__)
1491 #define test_calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__)
1504 #define test_free(ptr) _test_free(ptr, __FILE__, __LINE__)
1509 #define malloc test_malloc
1510 #define calloc test_calloc
1511 #define free test_free
1567 void mock_assert(
const int result,
const char*
const expression,
1568 const char *
const file,
const int line);
1595 #define expect_assert_failure(function_call) \
1597 const int result = setjmp(global_expect_assert_env); \
1598 global_expecting_assert = 1; \
1600 print_message("Expected assertion %s occurred\n", \
1601 global_last_failed_assert); \
1602 global_expecting_assert = 0; \
1605 global_expecting_assert = 0; \
1606 print_error("Expected assert in %s\n", #function_call); \
1607 _fail(__FILE__, __LINE__); \
1615 typedef void (*UnitTestFunction)(
void **state);
1618 typedef int (*CheckParameterValue)(
const LargestIntegralType value,
1619 const LargestIntegralType check_value_data);
1622 typedef enum UnitTestFunctionType {
1623 UNIT_TEST_FUNCTION_TYPE_TEST = 0,
1624 UNIT_TEST_FUNCTION_TYPE_SETUP,
1625 UNIT_TEST_FUNCTION_TYPE_TEARDOWN,
1626 UNIT_TEST_FUNCTION_TYPE_GROUP_SETUP,
1627 UNIT_TEST_FUNCTION_TYPE_GROUP_TEARDOWN,
1628 } UnitTestFunctionType;
1635 typedef struct UnitTest {
1637 UnitTestFunction
function;
1638 UnitTestFunctionType function_type;
1641 typedef struct GroupTest {
1642 UnitTestFunction setup;
1643 UnitTestFunction teardown;
1644 const UnitTest *tests;
1645 const size_t number_of_tests;
1649 typedef struct SourceLocation {
1655 typedef struct CheckParameterEvent {
1656 SourceLocation location;
1657 const char *parameter_name;
1658 CheckParameterValue check_value;
1659 LargestIntegralType check_value_data;
1660 } CheckParameterEvent;
1663 extern int global_expecting_assert;
1664 extern jmp_buf global_expect_assert_env;
1665 extern const char * global_last_failed_assert;
1668 LargestIntegralType _mock(
const char *
const function,
const char*
const file,
1672 const char*
const function,
const char*
const parameter,
1673 const char*
const file,
const int line,
1674 const CheckParameterValue check_function,
1675 const LargestIntegralType check_data, CheckParameterEvent *
const event,
1678 void _expect_in_set(
1679 const char*
const function,
const char*
const parameter,
1680 const char*
const file,
const int line,
const LargestIntegralType values[],
1681 const size_t number_of_values,
const int count);
1682 void _expect_not_in_set(
1683 const char*
const function,
const char*
const parameter,
1684 const char*
const file,
const int line,
const LargestIntegralType values[],
1685 const size_t number_of_values,
const int count);
1687 void _expect_in_range(
1688 const char*
const function,
const char*
const parameter,
1689 const char*
const file,
const int line,
1690 const LargestIntegralType minimum,
1691 const LargestIntegralType maximum,
const int count);
1692 void _expect_not_in_range(
1693 const char*
const function,
const char*
const parameter,
1694 const char*
const file,
const int line,
1695 const LargestIntegralType minimum,
1696 const LargestIntegralType maximum,
const int count);
1699 const char*
const function,
const char*
const parameter,
1700 const char*
const file,
const int line,
const LargestIntegralType value,
1702 void _expect_not_value(
1703 const char*
const function,
const char*
const parameter,
1704 const char*
const file,
const int line,
const LargestIntegralType value,
1707 void _expect_string(
1708 const char*
const function,
const char*
const parameter,
1709 const char*
const file,
const int line,
const char*
string,
1711 void _expect_not_string(
1712 const char*
const function,
const char*
const parameter,
1713 const char*
const file,
const int line,
const char*
string,
1716 void _expect_memory(
1717 const char*
const function,
const char*
const parameter,
1718 const char*
const file,
const int line,
const void*
const memory,
1719 const size_t size,
const int count);
1720 void _expect_not_memory(
1721 const char*
const function,
const char*
const parameter,
1722 const char*
const file,
const int line,
const void*
const memory,
1723 const size_t size,
const int count);
1726 const char*
const function,
const char*
const parameter,
1727 const char*
const file,
const int line,
const int count);
1729 void _check_expected(
1730 const char *
const function_name,
const char *
const parameter_name,
1731 const char* file,
const int line,
const LargestIntegralType value);
1733 void _will_return(
const char *
const function_name,
const char *
const file,
1734 const int line,
const LargestIntegralType value,
1736 void _assert_true(
const LargestIntegralType result,
1737 const char*
const expression,
1738 const char *
const file,
const int line);
1739 void _assert_return_code(
const LargestIntegralType result,
1741 const LargestIntegralType error,
1742 const char *
const expression,
1743 const char *
const file,
1745 void _assert_int_equal(
1746 const LargestIntegralType a,
const LargestIntegralType b,
1747 const char *
const file,
const int line);
1748 void _assert_int_not_equal(
1749 const LargestIntegralType a,
const LargestIntegralType b,
1750 const char *
const file,
const int line);
1751 void _assert_string_equal(
const char *
const a,
const char *
const b,
1752 const char *
const file,
const int line);
1753 void _assert_string_not_equal(
const char *
const a,
const char *
const b,
1754 const char *file,
const int line);
1755 void _assert_memory_equal(
const void *
const a,
const void *
const b,
1756 const size_t size,
const char*
const file,
1758 void _assert_memory_not_equal(
const void *
const a,
const void *
const b,
1759 const size_t size,
const char*
const file,
1761 void _assert_in_range(
1762 const LargestIntegralType value,
const LargestIntegralType minimum,
1763 const LargestIntegralType maximum,
const char*
const file,
const int line);
1764 void _assert_not_in_range(
1765 const LargestIntegralType value,
const LargestIntegralType minimum,
1766 const LargestIntegralType maximum,
const char*
const file,
const int line);
1767 void _assert_in_set(
1768 const LargestIntegralType value,
const LargestIntegralType values[],
1769 const size_t number_of_values,
const char*
const file,
const int line);
1770 void _assert_not_in_set(
1771 const LargestIntegralType value,
const LargestIntegralType values[],
1772 const size_t number_of_values,
const char*
const file,
const int line);
1774 void* _test_malloc(
const size_t size,
const char* file,
const int line);
1775 void* _test_calloc(
const size_t number_of_elements,
const size_t size,
1776 const char* file,
const int line);
1777 void _test_free(
void*
const ptr,
const char* file,
const int line);
1779 void _fail(
const char *
const file,
const int line);
1781 const char *
const function_name,
const UnitTestFunction Function,
1782 void **
const volatile state,
const UnitTestFunctionType function_type,
1783 const void*
const heap_check_point);
1784 int _run_tests(
const UnitTest *
const tests,
const size_t number_of_tests);
1785 int _run_group_tests(
const UnitTest *
const tests,
1786 const size_t number_of_tests);
1789 void print_message(
const char*
const format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2);
1790 void print_error(const
char* const format, ...) CMOCKA_PRINTF_ATTRIBUTE(1, 2);
1791 void vprint_message(const
char* const format, va_list args) CMOCKA_PRINTF_ATTRIBUTE(1, 0);
1792 void vprint_error(const
char* const format, va_list args) CMOCKA_PRINTF_ATTRIBUTE(1, 0);
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_return_code(int rc, int error)
Assert if the return_code is smaller than 0.
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.