Macros | Functions
Standard Assertions

How to handle assert(3) of the standard C library. More...

Collaboration diagram for Standard Assertions:


#define expect_assert_failure(function_call)


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. More...

Detailed Description

How to handle assert(3) of the standard C library.

Runtime assert macros like the standard C library's assert() should be redefined in modules being tested to use cmocka's mock_assert() function. Normally mock_assert() signals a test failure. If a function is called using the expect_assert_failure() macro, any calls to mock_assert() within the function will result in the execution of the test. If no calls to mock_assert() occur during the function called via expect_assert_failure() a test failure is signalled.

Macro Definition Documentation

◆ expect_assert_failure

#define expect_assert_failure (   function_call)
{ \
const int result = setjmp(global_expect_assert_env); \
global_expecting_assert = 1; \
if (result) { \
print_message("Expected assertion %s occurred\n", \
global_expecting_assert = 0; \
} else { \
function_call ; \
global_expecting_assert = 0; \
print_error("Expected assert in %s\n", #function_call); \
_fail(__FILE__, __LINE__); \
} \
jmp_buf global_expect_assert_env
Definition: cmocka.c:277
const char * global_last_failed_assert
Definition: cmocka.c:279

Definition at line 2012 of file cmocka.h.

Referenced by decrement_value_fail(), increment_value_assert(), test_divide_by_zero(), test_find_operator_function_by_string_null_functions(), test_find_operator_function_by_string_null_string(), test_perform_operation_null_args(), test_perform_operation_null_intermediate_values(), test_perform_operation_null_number_of_intermediate_values(), and test_perform_operation_null_operator_functions().

Function Documentation

◆ mock_assert()

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.

In conjuction with check_assert() it's possible to determine whether an assert condition has failed without stopping a test.

[in]resultThe expression to assert.
[in]expressionThe expression as string.
[in]fileThe file mock_assert() is called.
[in]lineThe line mock_assert() is called.
extern void mock_assert(const int result, const char* const expression,
const char * const file, const int line);
#undef assert
#define assert(expression) \
mock_assert((int)(expression), #expression, __FILE__, __LINE__);
void increment_value(int * const value) {
(*value) ++;
See also

Definition at line 1520 of file cmocka.c.

References _fail(), cm_print_error(), and global_last_failed_assert.

Referenced by assert_failed().