24 int __stdcall IsDebuggerPresent();
45 #define __func__ __FUNCTION__
50 #define PRINTF_ATTRIBUTE(a,b) __attribute__ ((__format__ (__printf__, a, b)))
52 #define PRINTF_ATTRIBUTE(a,b)
67 #ifndef LargestIntegralType
68 #define LargestIntegralType unsigned long long
72 #ifndef LargestIntegralTypePrintfFormat
74 #define LargestIntegralTypePrintfFormat "%I64x"
76 #define LargestIntegralTypePrintfFormat "%llx"
81 #define cast_to_largest_integral_type(value) \
82 ((LargestIntegralType)((size_t)(value)))
85 #if !defined(_UINTPTR_T) && !defined(_UINTPTR_T_DEFINED)
88 typedef unsigned int uintptr_t;
89 # elif defined(_WIN64)
90 typedef unsigned long int uintptr_t
96 typedef unsigned long int uintptr_t;
98 typedef unsigned int uintptr_t;
101 # if defined(_LP64) || defined(_I32LPx)
102 typedef unsigned long int uintptr_t;
104 typedef unsigned int uintptr_t;
110 # define _UINTPTR_T_DEFINED
114 #define cast_to_pointer_integral_type(value) \
118 #define cast_ptr_to_largest_integral_type(value) \
119 cast_to_largest_integral_type(cast_to_pointer_integral_type(value))
179 #define mock() _mock(__func__, __FILE__, __LINE__)
205 #define mock_type(type) ((type) mock())
232 #define mock_ptr_type(type) ((type) (uintptr_t) mock())
263 #define will_return(function, value) \
264 _will_return(#function, __FILE__, __LINE__, \
265 cast_to_largest_integral_type(value), 1)
284 #define will_return_count(function, value, count) \
285 _will_return(#function, __FILE__, __LINE__, \
286 cast_to_largest_integral_type(value), count)
307 #define will_return_always(function, value) \
308 will_return_count(function, (value), -1)
380 void expect_check(#
function, #parameter, #check_function,
const void *check_data);
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)
402 void expect_in_set(#
function, #parameter, uintmax_t value_array[]);
404 #define expect_in_set(function, parameter, value_array) \
405 expect_in_set_count(function, parameter, value_array, 1)
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)
451 #define expect_not_in_set(function, parameter, value_array) \
452 expect_not_in_set_count(function, parameter, value_array, 1)
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)
500 void expect_in_range(#
function, #parameter, uintmax_t minimum, uintmax_t maximum);
502 #define expect_in_range(function, parameter, minimum, maximum) \
503 expect_in_range_count(function, parameter, minimum, maximum, 1)
527 void expect_in_range_count(#
function, #parameter, uintmax_t minimum, uintmax_t maximum,
size_t count);
529 #define expect_in_range_count(function, parameter, minimum, maximum, count) \
530 _expect_in_range(#function, #parameter, __FILE__, __LINE__, minimum, \
553 #define expect_not_in_range(function, parameter, minimum, maximum) \
554 expect_not_in_range_count(function, parameter, minimum, maximum, 1)
580 #define expect_not_in_range_count(function, parameter, minimum, maximum, \
582 _expect_not_in_range(#function, #parameter, __FILE__, __LINE__, \
583 minimum, maximum, count)
600 void expect_value(#
function, #parameter, uintmax_t value);
602 #define expect_value(function, parameter, value) \
603 expect_value_count(function, parameter, value, 1)
626 #define expect_value_count(function, parameter, value, count) \
627 _expect_value(#function, #parameter, __FILE__, __LINE__, \
628 cast_to_largest_integral_type(value), count)
647 #define expect_not_value(function, parameter, value) \
648 expect_not_value_count(function, parameter, value, 1)
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)
691 void expect_string(#
function, #parameter,
const char *
string);
693 #define expect_string(function, parameter, string) \
694 expect_string_count(function, parameter, string, 1)
718 #define expect_string_count(function, parameter, string, count) \
719 _expect_string(#function, #parameter, __FILE__, __LINE__, \
720 (const char*)(string), count)
740 #define expect_not_string(function, parameter, string) \
741 expect_not_string_count(function, parameter, string, 1)
765 #define expect_not_string_count(function, parameter, string, count) \
766 _expect_not_string(#function, #parameter, __FILE__, __LINE__, \
767 (const char*)(string), count)
786 void expect_memory(#
function, #parameter,
void *memory,
size_t size);
788 #define expect_memory(function, parameter, memory, size) \
789 expect_memory_count(function, parameter, memory, size, 1)
815 #define expect_memory_count(function, parameter, memory, size, count) \
816 _expect_memory(#function, #parameter, __FILE__, __LINE__, \
817 (const void*)(memory), size, count)
839 #define expect_not_memory(function, parameter, memory, size) \
840 expect_not_memory_count(function, parameter, memory, size, 1)
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)
886 #define expect_any(function, parameter) \
887 expect_any_count(function, parameter, 1)
909 #define expect_any_count(function, parameter, count) \
910 _expect_any(#function, #parameter, __FILE__, __LINE__, count)
926 #define check_expected(parameter) \
927 _check_expected(__func__, #parameter, __FILE__, __LINE__, \
928 cast_to_largest_integral_type(parameter))
967 #define assert_true(c) _assert_true(cast_to_largest_integral_type(c), #c, \
985 #define assert_false(c) _assert_true(!(cast_to_largest_integral_type(c)), #c, \
1002 #define assert_non_null(c) _assert_true(cast_ptr_to_largest_integral_type(c), #c, \
1019 #define assert_null(c) _assert_true(!(cast_ptr_to_largest_integral_type(c)), #c, \
1036 #define assert_int_equal(a, b) \
1037 _assert_int_equal(cast_to_largest_integral_type(a), \
1038 cast_to_largest_integral_type(b), \
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), \
1076 #define assert_string_equal(a, b) \
1077 _assert_string_equal((const char*)(a), (const char*)(b), __FILE__, \
1094 #define assert_string_not_equal(a, b) \
1095 _assert_string_not_equal((const char*)(a), (const char*)(b), __FILE__, \
1116 #define assert_memory_equal(a, b, size) \
1117 _assert_memory_equal((const void*)(a), (const void*)(b), size, __FILE__, \
1138 #define assert_memory_not_equal(a, b, size) \
1139 _assert_memory_not_equal((const void*)(a), (const void*)(b), size, \
1157 void assert_in_range(uintmax_t value, uintmax_t minimum, uintmax_t maximum);
1159 #define assert_in_range(value, minimum, maximum) \
1161 cast_to_largest_integral_type(value), \
1162 cast_to_largest_integral_type(minimum), \
1163 cast_to_largest_integral_type(maximum), __FILE__, __LINE__)
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__)
1202 void assert_in_set(uintmax_t value, uintmax_t values[],
size_t count);
1204 #define assert_in_set(value, values, number_of_values) \
1205 _assert_in_set(value, values, number_of_values, __FILE__, __LINE__)
1223 #define assert_not_in_set(value, values, number_of_values) \
1224 _assert_not_in_set(value, values, number_of_values, __FILE__, __LINE__)
1261 #define fail() _fail(__FILE__, __LINE__)
1268 void fail_msg(
const char *msg, ...);
1270 #define fail_msg(msg, ...) do { \
1271 print_error("ERROR: " msg "\n", ##__VA_ARGS__); \
1296 #define run_test(f) _run_test(#f, f, NULL, UNIT_TEST_FUNCTION_TYPE_TEST, NULL)
1300 #define unit_test(f) { #f, f, UNIT_TEST_FUNCTION_TYPE_TEST }
1302 #define _unit_test_setup(test, setup) \
1303 { #test "_" #setup, setup, UNIT_TEST_FUNCTION_TYPE_SETUP }
1306 #define unit_test_setup(test, setup) \
1307 _unit_test_setup(test, setup), \
1310 #define _unit_test_teardown(test, teardown) \
1311 { #test "_" #teardown, teardown, UNIT_TEST_FUNCTION_TYPE_TEARDOWN }
1314 #define unit_test_teardown(test, teardown) \
1316 _unit_test_teardown(test, teardown)
1322 #define unit_test_setup_teardown(test, setup, teardown) \
1323 _unit_test_setup(test, setup), \
1325 _unit_test_teardown(test, teardown)
1375 #define run_tests(tests) _run_tests(tests, sizeof(tests) / sizeof(tests)[0])
1428 #define test_malloc(size) _test_malloc(size, __FILE__, __LINE__)
1447 #define test_calloc(num, size) _test_calloc(num, size, __FILE__, __LINE__)
1460 #define test_free(ptr) _test_free(ptr, __FILE__, __LINE__)
1465 #define malloc test_malloc
1466 #define calloc test_calloc
1467 #define free test_free
1523 void mock_assert(
const int result,
const char*
const expression,
1524 const char *
const file,
const int line);
1551 #define expect_assert_failure(function_call) \
1553 const int result = setjmp(global_expect_assert_env); \
1554 global_expecting_assert = 1; \
1556 print_message("Expected assertion %s occurred\n", \
1557 global_last_failed_assert); \
1558 global_expecting_assert = 0; \
1561 global_expecting_assert = 0; \
1562 print_error("Expected assert in %s\n", #function_call); \
1563 _fail(__FILE__, __LINE__); \
1571 typedef void (*UnitTestFunction)(
void **state);
1574 typedef int (*CheckParameterValue)(
const LargestIntegralType value,
1575 const LargestIntegralType check_value_data);
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;
1589 typedef struct UnitTest {
1591 UnitTestFunction
function;
1592 UnitTestFunctionType function_type;
1597 typedef struct SourceLocation {
1603 typedef struct CheckParameterEvent {
1604 SourceLocation location;
1605 const char *parameter_name;
1606 CheckParameterValue check_value;
1607 LargestIntegralType check_value_data;
1608 } CheckParameterEvent;
1611 extern int global_expecting_assert;
1612 extern jmp_buf global_expect_assert_env;
1613 extern const char * global_last_failed_assert;
1616 LargestIntegralType _mock(
const char *
const function,
const char*
const file,
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,
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);
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);
1647 const char*
const function,
const char*
const parameter,
1648 const char*
const file,
const int line,
const LargestIntegralType value,
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,
1655 void _expect_string(
1656 const char*
const function,
const char*
const parameter,
1657 const char*
const file,
const int line,
const char*
string,
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,
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);
1674 const char*
const function,
const char*
const parameter,
1675 const char*
const file,
const int line,
const int count);
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);
1681 void _will_return(
const char *
const function_name,
const char *
const file,
1682 const int line,
const LargestIntegralType value,
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,
1700 void _assert_memory_not_equal(
const void *
const a,
const void *
const b,
1701 const size_t size,
const char*
const file,
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);
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);
1721 void _fail(
const char *
const file,
const int line);
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);
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);
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.