CLibs
Loading...
Searching...
No Matches
assert_that.h File Reference
#include "errors.h"
#include <stdlib.h>
#include <assert.h>

Go to the source code of this file.

Macros

#define CLIBS_ASSERT_THAT_EXIT_VAL   RV_EXCEPTION
#define assert_that(EXPRESSION, ...)
 Asserts a given expression and exits with an error message if the assertion fails.

Macro Definition Documentation

◆ assert_that

#define assert_that ( EXPRESSION,
... )
Value:
do \
{ \
if ( !( EXPRESSION ) ) \
exit( ( int ) fflwarnx_ret( \
"Assertion error: " COLOR_DEFAULT #EXPRESSION COLOR_WARNING \
": " __VA_ARGS__ ) ); \
} \
while ( 0 )
#define CLIBS_ASSERT_THAT_EXIT_VAL
Definition assert_that.h:21
#define COLOR_WARNING
Definition errors.h:117
#define fflwarnx_ret(RETVAL,...)
Definition errors.h:290
#define COLOR_DEFAULT
Definition terminal_colors.h:33

Asserts a given expression and exits with an error message if the assertion fails.

Parameters
EXPRESSIONThe condition to be evaluated. If false, the assertion fails.
...Additional arguments for the formatted error message (format string + varargs). These are passed to fflwarnx_ret for constructing the error message.

Example:

int x = 5;
assert_that(x > 10, "x must be greater than 10, but got %d", x);
#define assert_that(EXPRESSION,...)
Asserts a given expression and exits with an error message if the assertion fails.
Definition assert_that.h:55

In this example, if x > 10 evaluates to false, the program will exit and print an error message (the expression is printed in COLOR_DEFAULT, the rest in COLOR_WARNING):

"Assertion error: x > 10: x must be greater than 10, but got 5".
Note
The error code returned is – by default – RV_EXCEPTION from errors.h, but may be redefined. Either
// like this:
#define CLIBS_ASSERT_THAT_EXIT_VAL 123
#include "assert_that.h"
Or -DCLIBS_ASSERT_THAT_EXIT_VAL=123 during compilation

◆ CLIBS_ASSERT_THAT_EXIT_VAL

#define CLIBS_ASSERT_THAT_EXIT_VAL   RV_EXCEPTION