Loading...
Searching...
No Matches
🎯 Standard Assertions
📚 The CMocka API

Test code that uses assert(3) from the standard C library. More...

## Functions

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.
void | expect_assert_failure (function fn_call)
| Ensure that mock_assert() is called.

## Detailed Description

Test code that uses assert(3) from the standard C library.

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.

## Function Documentation

## expect_assert_failure()

void expect_assert_failure | ( | function | fn_call | ) |

Ensure that mock_assert() is called.

If mock_assert() is called the assert expression string is returned.

Parameters

  [in] | fn_call | The function will will call mock_assert().

#define assert mock_assert
void showmessage(const char *message) {
assert(message);
}
int main(int argc, const char* argv[]) {
expect_assert_failure(show_message(NULL));
printf("succeeded\n");
return 0;
}
expect_assert_failure
void expect_assert_failure(function fn_call)
Ensure that mock_assert() is called.

## 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 conjunction with check_assert() it's possible to determine whether an assert condition has failed without stopping a test.

Parameters

  [in] | result | The expression to assert.
  [in] | expression | The expression as string.
  [in] | file | The file mock_assert() is called.
  [in] | line | The line mock_assert() is called.

#ifdef UNIT_TESTING
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__);
#endif
void increment_value(int * const value) {
assert(value);
(*value) ++;
}
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.
Definition cmocka.c:3446

See also
  assert(3)
  expect_assert_failure