cmocka: 🛡️ Assert Macros

cmocka 2.0.2 Unit testing library with mock support

Loading...
Searching...
No Matches
Functions
🛡️ Assert Macros
📚 The CMocka API

Verify conditions and fail tests when assertions don't hold. More...

## Functions
void | assert_true (scalar expression)
| Assert that the given expression is true.
void | assert_false (scalar expression)
| Assert that the given expression is false.
void | assert_return_code (intmax_t rc, int32_t error)
| Assert that the return_code is greater than or equal to 0.
void | assert_non_null (void *pointer)
| Assert that the given pointer is non-NULL.
void | assert_non_null_msg (void *pointer, const char *const message)
| Assert that the given pointer is non-NULL.
void | assert_null (void *pointer)
| Assert that the given pointer is NULL.
void | assert_null_msg (void *pointer, const char *const message)
| Assert that the given pointer is NULL.
void | assert_ptr_equal (void *a, void *b)
| Assert that the two given pointers are equal.
void | assert_ptr_equal_msg (void *a, void *b, const char *const msg)
| Assert that the two given pointers are equal.
void | assert_ptr_not_equal (void *a, void *b)
| Assert that the two given pointers are not equal.
void | assert_ptr_not_equal_msg (void *a, void *b, const char *const msg)
| Assert that the two given pointers are not equal.
void | assert_int_equal (intmax_t a, intmax_t b)
| Assert that the two given integers are equal.
void | assert_uint_equal (uintmax_t a, uintmax_t b)
| Assert that the two given unsigned integers are equal.
void | assert_int_not_equal (intmax_t a, intmax_t b)
| Assert that the two given integers are not equal.
void | assert_uint_not_equal (uintmax_t a, uintmax_t b)
| Assert that the two given unsigned integers are not equal.
void | assert_float_equal (float a, float b, float epsilon)
| Assert that the two given float are equal given an epsilon.
void | assert_float_not_equal (float a, float b, float epsilon)
| Assert that the two given float are not equal given an epsilon.
void | assert_double_equal (double a, double b, double epsilon)
| Assert that the two given double are equal given an epsilon.
void | assert_double_not_equal (double a, double b, double epsilon)
| Assert that the two given double are not equal given an epsilon.
void | assert_string_equal (const char *a, const char *b)
| Assert that the two given strings are equal.
void | assert_string_not_equal (const char *a, const char *b)
| Assert that the two given strings are not 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 | assert_memory_not_equal (const void *a, const void *b, size_t size)
| Assert that the two given areas of memory are not equal.
void | assert_int_in_range (intmax_t value, intmax_t minimum, intmax_t maximum)
| Assert that the specified integer value is not smaller than the minimum and and not greater than the maximum.
void | assert_uint_in_range (uintmax_t value, uintmax_t minimum, uintmax_t maximum)
| Assert that the specified unsigned integer value is not smaller than the minimum and and not greater than the maximum.
void | assert_int_not_in_range (intmax_t value, intmax_t minimum, intmax_t maximum)
| Assert that the specified value is smaller than the minimum or greater than the maximum.
void | assert_uint_not_in_range (uintmax_t value, uintmax_t minimum, uintmax_t maximum)
| Assert that the specified value is smaller than the minimum or greater than the maximum.
void | assert_in_range (uintmax_t value, uintmax_t minimum, uintmax_t maximum)
void | assert_not_in_range (uintmax_t value, uintmax_t minimum, uintmax_t maximum)
void | assert_float_not_in_range (double value, double minimum, double maximum, double epsilon)
| Assert that the specified float value is smaller than the minimum or greater than the maximum.
void | assert_float_in_range (double value, double minimum, double maximum, double epsilon)
| Assert that the specified float value is not smaller than the minimum and and not greater than the maximum.
void | assert_in_set (uintmax_t value, uintmax_t values[], size_t count)
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 | assert_int_in_set (intmax_t value, intmax_t values[], size_t count)
| Assert that the specified integer value is within a set.
void | assert_int_not_in_set (intmax_t value, intmax_t values[], size_t count)
| Assert that the specified value is not within a set.
void | assert_uint_in_set (uintmax_t value, uintmax_t values[], size_t count)
| Assert that the specified unsigned integer value is within a set.
void | assert_uint_not_in_set (uintmax_t value, uintmax_t values[], size_t count)
| Assert that the specified unsigned integer value is not within a set.
void | assert_float_in_set (double value, double values[], size_t count, double epsilon)
| Assert that the specified float value is within a set.
void | assert_float_not_in_set (double value, double values[], size_t count, double epsilon)
| Assert that the specified float value is not within a set.

## Detailed Description

Verify conditions and fail tests when assertions don't hold.

Assertion macros for validating test conditions.

CMocka provides type-specific assertion macros that display detailed information about failures, making debugging easier than the standard C library's assert(3) macro.

On an assertion failure a cmocka assert macro will write the failure to the standard error stream and signal a test failure. Due to limitations of the C language the general C standard library assert() and cmocka's assert_true() and assert_false() macros can only display the expression that caused the assert failure. cmocka's type specific assert macros, assert_{type}_equal() and assert_{type}_not_equal(), display the data that caused the assertion failure which increases data visibility aiding debugging of failing test cases.

## Function Documentation

## ◆ assert_double_equal()

void assert_double_equal | ( | double | a,
|  | double | b,
|  | double | epsilon )

Assert that the two given double are equal given an epsilon.

The function prints an error message to standard error and terminates the test by calling fail() if the double are not equal (given an epsilon).

Parameters

  [in] | a | The first double to compare.
  [in] | b | The double to compare against the first one.
  [in] | epsilon | The epsilon used as margin for double comparison.

## ◆ assert_double_not_equal()

void assert_double_not_equal | ( | double | a,
|  | double | b,
|  | double | epsilon )

Assert that the two given double are not equal given an epsilon.

The function prints an error message to standard error and terminates the test by calling fail() if the double are not equal (given an epsilon).

Parameters

  [in] | a | The first double to compare.
  [in] | b | The double to compare against the first one.
  [in] | epsilon | The epsilon used as margin for double comparison.

## ◆ assert_false()

void assert_false | ( | scalar | expression | ) |

Assert that the given expression is false.

The function prints an error message to standard error and terminates the test by calling fail() if expression is true.

Parameters

  [in] | expression | The expression to evaluate.

See also
  assert_int_equal()
  assert_string_equal()

## ◆ assert_float_equal()

void assert_float_equal | ( | float | a,
|  | float | b,
|  | float | epsilon )

Assert that the two given float are equal given an epsilon.

The function prints an error message to standard error and terminates the test by calling fail() if the float are not equal (given an epsilon).

Parameters

  [in] | a | The first float to compare.
  [in] | b | The float to compare against the first one.
  [in] | epsilon | The epsilon used as margin for float comparison.

## ◆ assert_float_in_range()

void assert_float_in_range | ( | double | value,
|  | double | minimum,
|  | double | maximum,
|  | double | epsilon )

Assert that the specified float value is not smaller than the minimum and and not greater than the maximum.

The function prints an error message to standard error and terminates the test by calling fail() if value is not in range.

Parameters

  [in] | value | The value to check.
  [in] | minimum | The minimum value allowed.
  [in] | maximum | The maximum value allowed.
  [in] | epsilon | The epsilon used as margin for float comparison.

## ◆ assert_float_in_set()

void assert_float_in_set | ( | double | value,
|  | double | values[],
|  | size_t | count,
|  | double | epsilon )

Assert that the specified float value is within a set.

The function prints an error message to standard error and terminates the test by calling fail() if value is not within a set.

Parameters

  [in] | value | The value to look up
  [in] | values[] | The array to check for the value.
  [in] | count | The size of the values array.
  [in] | epsilon | The epsilon used as margin for float comparison.

## ◆ assert_float_not_equal()

void assert_float_not_equal | ( | float | a,
|  | float | b,
|  | float | epsilon )

Assert that the two given float are not equal given an epsilon.

The function prints an error message to standard error and terminates the test by calling fail() if the float are not equal (given an epsilon).

Parameters

  [in] | a | The first float to compare.
  [in] | b | The float to compare against the first one.
  [in] | epsilon | The epsilon used as margin for float comparison.

## ◆ assert_float_not_in_range()

void assert_float_not_in_range | ( | double | value,
|  | double | minimum,
|  | double | maximum,
|  | double | epsilon )

Assert that the specified float value is smaller than the minimum or greater than the maximum.

The function prints an error message to standard error and terminates the test by calling fail() if value is in range.

The function prints an error message to standard error and terminates the test by calling fail() if value is not in range.

Parameters

  [in] | value | The value to check.
  [in] | minimum | The minimum value allowed.
  [in] | maximum | The maximum value allowed.
  [in] | epsilon | The epsilon used as margin for float comparison.

## ◆ assert_float_not_in_set()

void assert_float_not_in_set | ( | double | value,
|  | double | values[],
|  | size_t | count,
|  | double | epsilon )

Assert that the specified float value is not within a set.

The function prints an error message to standard error and terminates the test by calling fail() if value is not within a set.

Parameters

  [in] | value | The value to look up
  [in] | values[] | The array to check for the value.
  [in] | count | The size of the values array.
  [in] | epsilon | The epsilon used as margin for float comparison.

## ◆ assert_in_range()

void assert_in_range | ( | uintmax_t | value,
|  | uintmax_t | minimum,
|  | uintmax_t | maximum )

Deprecated
  Use assert_int_in_range() and assert_uint_in_range()

## ◆ assert_in_set()

void assert_in_set | ( | uintmax_t | value,
|  | uintmax_t | values[],
|  | size_t | count )

Deprecated
  Use assert_int_in_set() or assert_uint_in_set()

## ◆ assert_int_equal()

void assert_int_equal | ( | intmax_t | a,
|  | intmax_t | b )

Assert that the two given integers are equal.

The function prints an error message to standard error and terminates the test by calling fail() if the integers are not equal.

Parameters

  [in] | a | The first integer to compare.
  [in] | b | The integer to compare against the first one.

## ◆ assert_int_in_range()

void assert_int_in_range | ( | intmax_t | value,
|  | intmax_t | minimum,
|  | intmax_t | maximum )

Assert that the specified integer value is not smaller than the minimum and and not greater than the maximum.

The function prints an error message to standard error and terminates the test by calling fail() if value is not in range.

Parameters

  [in] | value | The value to check.
  [in] | minimum | The minimum value allowed.
  [in] | maximum | The maximum value allowed.

## ◆ assert_int_in_set()

void assert_int_in_set | ( | intmax_t | value,
|  | intmax_t | values[],
|  | size_t | count )

Assert that the specified integer value is within a set.

The function prints an error message to standard error and terminates the test by calling fail() if value is not within a set.

Parameters

  [in] | value | The value to look up
  [in] | values[] | The array to check for the value.
  [in] | count | The size of the values array.

## ◆ assert_int_not_equal()

void assert_int_not_equal | ( | intmax_t | a,
|  | intmax_t | b )

Assert that the two given integers are not equal.

The function prints an error message to standard error and terminates the test by calling fail() if the integers are equal.

Parameters

  [in] | a | The first integer to compare.
  [in] | b | The integer to compare against the first one.

See also
  assert_int_equal()

## ◆ assert_int_not_in_range()

void assert_int_not_in_range | ( | intmax_t | value,
|  | intmax_t | minimum,
|  | intmax_t | maximum )

Assert that the specified value is smaller than the minimum or greater than the maximum.

The function prints an error message to standard error and terminates the test by calling fail() if value is in range.

The function prints an error message to standard error and terminates the test by calling fail() if value is not in range.

Parameters

  [in] | value | The value to check.
  [in] | minimum | The minimum value allowed.
  [in] | maximum | The maximum value allowed.

## ◆ assert_int_not_in_set()

void assert_int_not_in_set | ( | intmax_t | value,
|  | intmax_t | values[],
|  | size_t | count )

Assert that the specified value is not within a set.

The function prints an error message to standard error and terminates the test by calling fail() if value is within a set.

Parameters

  [in] | value | The value to look up
  [in] | values[] | The array to check for the value.
  [in] | count | The size of the values array.

## ◆ assert_memory_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.

The function prints an error message to standard error and terminates the test by calling fail() if the memory is not equal.

Parameters

  [in] | a | The first memory area to compare (interpreted as unsigned char).
  [in] | b | The second memory area to compare (interpreted as unsigned char).
  [in] | size | The first n bytes of the memory areas to compare.

## ◆ assert_memory_not_equal()

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.

The function prints an error message to standard error and terminates the test by calling fail() if the memory is equal.

Parameters

  [in] | a | The first memory area to compare (interpreted as unsigned char).
  [in] | b | The second memory area to compare (interpreted as unsigned char).
  [in] | size | The first n bytes of the memory areas to compare.

## ◆ assert_non_null()

void assert_non_null | ( | void * | pointer | ) |

Assert that the given pointer is non-NULL.

The function prints an error message to standard error and terminates the test by calling fail() if the pointer is NULL.

Parameters

  [in] | pointer | The pointer to evaluate.

See also
  assert_null()

## ◆ assert_non_null_msg()

void assert_non_null_msg | ( | void * | pointer,
|  | const char *const | message )

Assert that the given pointer is non-NULL.

The function prints an error message extended by message to standard error and terminates the test by calling fail() if the pointer is NULL.

Parameters

  [in] | pointer | The pointer to evaluate.
  [in] | message | The message to print when the pointer is NULL.

See also
  assert_null_msg()

## ◆ assert_not_in_range()

void assert_not_in_range | ( | uintmax_t | value,
|  | uintmax_t | minimum,
|  | uintmax_t | maximum )

Deprecated
  Use assert_int_not_in_range() or assert_uint_not_in_range()

## ◆ assert_not_in_set()

void assert_not_in_set | ( | uintmax_t | value,
|  | uintmax_t | values[],
|  | size_t | count )

Assert that the specified value is not within a set.

The function prints an error message to standard error and terminates the test by calling fail() if value is within a set.

Parameters

  [in] | value | The value to look up
  [in] | values[] | The array to check for the value.
  [in] | count | The size of the values array.

## ◆ assert_null()

void assert_null | ( | void * | pointer | ) |

Assert that the given pointer is NULL.

The function prints an error message to standard error and terminates the test by calling fail() if the pointer is non-NULL.

Parameters

  [in] | pointer | The pointer to evaluate.

See also
  assert_non_null()

## ◆ assert_null_msg()

void assert_null_msg | ( | void * | pointer,
|  | const char *const | message )

Assert that the given pointer is NULL.

The function prints an error message extended by message to standard error and terminates the test by calling fail() if the pointer is non-NULL.

Parameters

  [in] | pointer | The pointer to evaluate.
  [in] | message | The message to print when the pointer is not NULL.

See also
  assert_non_null_msg()

## ◆ assert_ptr_equal()

void assert_ptr_equal | ( | void * | a,
|  | void * | b )

Assert that the two given pointers are equal.

The function prints an error message and terminates the test by calling fail() if the pointers are not equal.

Parameters

  [in] | a | The first pointer to compare.
  [in] | b | The pointer to compare against the first one.

## ◆ assert_ptr_equal_msg()

void assert_ptr_equal_msg | ( | void * | a,
|  | void * | b,
|  | const char *const | msg )

Assert that the two given pointers are equal.

The function prints the failing comparison and the error message given by the user and then terminates the test by calling fail() if the pointers are not equal.

Parameters

  [in] | a | The first pointer to compare.
  [in] | b | The pointer to compare against the first one.
  [in] | msg | The error message to print when a & b are not equal.

## ◆ assert_ptr_not_equal()

void assert_ptr_not_equal | ( | void * | a,
|  | void * | b )

Assert that the two given pointers are not equal.

The function prints an error message and terminates the test by calling fail() if the pointers are equal.

Parameters

  [in] | a | The first pointer to compare.
  [in] | b | The pointer to compare against the first one.

## ◆ assert_ptr_not_equal_msg()

void assert_ptr_not_equal_msg | ( | void * | a,
|  | void * | b,
|  | const char *const | msg )

Assert that the two given pointers are not equal.

The function prints the failing comparison and the error message given by the user and then terminates the test by calling fail() if the pointers are equal.

Parameters

  [in] | a | The first pointer to compare.
  [in] | b | The pointer to compare against the first one.
  [in] | msg | The error message to print when a & b are equal.

## ◆ assert_return_code()

void assert_return_code | ( | intmax_t | rc,
|  | int32_t | error )

Assert that the return_code is greater than or equal to 0.

The function prints an error message to standard error and terminates the test by calling fail() if the return code is smaller than 0. If the function you check sets an errno if it fails you can pass it to the function and it will be printed as part of the error message.

Parameters

  [in] | rc | The return code to evaluate.
  [in] | error | Pass errno here or 0.

## ◆ assert_string_equal()

void assert_string_equal | ( | const char * | a,
|  | const char * | b )

Assert that the two given strings are equal.

The function prints an error message to standard error and terminates the test by calling fail() if the strings are not equal.

Parameters

  [in] | a | The string to check.
  [in] | b | The other string to compare.

## ◆ assert_string_not_equal()

void assert_string_not_equal | ( | const char * | a,
|  | const char * | b )

Assert that the two given strings are not equal.

The function prints an error message to standard error and terminates the test by calling fail() if the strings are equal.

Parameters

  [in] | a | The string to check.
  [in] | b | The other string to compare.

## ◆ assert_true()

void assert_true | ( | scalar | expression | ) |

Assert that the given expression is true.

The function prints an error message to standard error and terminates the test by calling fail() if expression is false (i.e., compares equal to zero).

Parameters

  [in] | expression | The expression to evaluate.

See also
  assert_int_equal()
  assert_string_equal()

## ◆ assert_uint_equal()

void assert_uint_equal | ( | uintmax_t | a,
|  | uintmax_t | b )

Assert that the two given unsigned integers are equal.

The function prints an error message to standard error and terminates the test by calling fail() if the integers are not equal.

Parameters

  [in] | a | The first unsigned integer to compare.
  [in] | b | The unsigned integer to compare against the first one.

## ◆ assert_uint_in_range()

void assert_uint_in_range | ( | uintmax_t | value,
|  | uintmax_t | minimum,
|  | uintmax_t | maximum )

Assert that the specified unsigned integer value is not smaller than the minimum and and not greater than the maximum.

The function prints an error message to standard error and terminates the test by calling fail() if value is not in range.

Parameters

  [in] | value | The value to check.
  [in] | minimum | The minimum value allowed.
  [in] | maximum | The maximum value allowed.

## ◆ assert_uint_in_set()

void assert_uint_in_set | ( | uintmax_t | value,
|  | uintmax_t | values[],
|  | size_t | count )

Assert that the specified unsigned integer value is within a set.

The function prints an error message to standard error and terminates the test by calling fail() if value is not within a set.

Parameters

  [in] | value | The value to look up
  [in] | values[] | The array to check for the value.
  [in] | count | The size of the values array.

## ◆ assert_uint_not_equal()

void assert_uint_not_equal | ( | uintmax_t | a,
|  | uintmax_t | b )

Assert that the two given unsigned integers are not equal.

The function prints an error message to standard error and terminates the test by calling fail() if the integers are not equal.

Parameters

  [in] | a | The first unsigned integer to compare.
  [in] | b | The unsigned integer to compare against the first one.

## ◆ assert_uint_not_in_range()

void assert_uint_not_in_range | ( | uintmax_t | value,
|  | uintmax_t | minimum,
|  | uintmax_t | maximum )

Assert that the specified value is smaller than the minimum or greater than the maximum.

The function prints an error message to standard error and terminates the test by calling fail() if value is in range.

The function prints an error message to standard error and terminates the test by calling fail() if value is not in range.

Parameters

  [in] | value | The value to check.
  [in] | minimum | The minimum value allowed.
  [in] | maximum | The maximum value allowed.

## ◆ assert_uint_not_in_set()

void assert_uint_not_in_set | ( | uintmax_t | value,
|  | uintmax_t | values[],
|  | size_t | count )

Assert that the specified unsigned integer value is not within a set.

The function prints an error message to standard error and terminates the test by calling fail() if value is within a set.

Parameters

  [in] | value | The value to look up
  [in] | values[] | The array to check for the value.
  [in] | count | The size of the values array.