Loading...
Searching...
No Matches
expect() utility function

Detailed Description

Files

file  expect.h
 test "expect condition" utility function
 

Macros

#define expect(cond)   (likely(cond) ? (void)0 : _expect_failure(__FILE__, __LINE__))
 abort the program if condition is false
 

Functions

static NORETURN void _expect_failure (const char *file, unsigned line)
 Function to handle failed expectation.
 

Macro Definition Documentation

◆ expect

#define expect (   cond)    (likely(cond) ? (void)0 : _expect_failure(__FILE__, __LINE__))

abort the program if condition is false

This is similar to assert(), but will not be excluded from a build even if NDEBUG is set. Use e.g., in test application to "assert" conditions, in order to prevent a different compilation mode (a release build?) from making the test non-functional.

Otherwise, the macro expect() prints an error message to standard error and terminates the application by calling core_panic().

The purpose of this macro is to help programmers find bugs in their programs.

A failed condition generates output similar to:

0x89abcdef
*** RIOT kernel panic:
FAILED CONDITION.

...

Where 0x89abcdef is an address. This address can be used with tools like addr2line (or e.g. arm-none-eabi-addr2line for ARM-based code), objdump, or gdb (with the command info line *(0x89abcdef)) to identify the line the condition failed in.

Definition at line 80 of file expect.h.

Function Documentation

◆ _expect_failure()

static NORETURN void _expect_failure ( const char *  file,
unsigned  line 
)
inlinestatic

Function to handle failed expectation.

Note
This function was introduced for memory size optimization
Warning
this function NEVER returns!
Parameters
[in]fileThe file name of the file the expectation failed in
[in]lineThe code line of file the expectation failed in

Definition at line 46 of file expect.h.