mirror of https://github.com/tailix/libkernaux.git
Add example for return asserts
This commit is contained in:
parent
3e071577f3
commit
e3a1284aaf
|
@ -34,7 +34,8 @@
|
|||
/tests/test*.log
|
||||
/tests/test*.trs
|
||||
|
||||
/examples/assert
|
||||
/examples/assert_return
|
||||
/examples/assert_simple
|
||||
/examples/cmdline
|
||||
/examples/pfa
|
||||
/examples/printf
|
||||
|
|
12
Makefile.am
12
Makefile.am
|
@ -32,7 +32,9 @@ libkernaux_a_SOURCES += src/arch/x86_64.S
|
|||
endif
|
||||
|
||||
if ENABLE_ASSERT
|
||||
TESTS += examples/assert
|
||||
TESTS += \
|
||||
examples/assert_return \
|
||||
examples/assert_simple
|
||||
endif
|
||||
|
||||
if ENABLE_CMDLINE
|
||||
|
@ -80,9 +82,13 @@ TESTS += \
|
|||
tests/test_units_human
|
||||
endif
|
||||
|
||||
examples_assert_SOURCES = \
|
||||
examples_assert_return_SOURCES = \
|
||||
$(libkernaux_a_SOURCES) \
|
||||
examples/assert.c
|
||||
examples/assert_return.c
|
||||
|
||||
examples_assert_simple_SOURCES = \
|
||||
$(libkernaux_a_SOURCES) \
|
||||
examples/assert_simple.c
|
||||
|
||||
examples_cmdline_SOURCES = \
|
||||
$(libkernaux_a_SOURCES) \
|
||||
|
|
|
@ -30,7 +30,8 @@ API
|
|||
|
||||
* Runtime environment
|
||||
* [Assertions](/include/kernaux/assert.h)
|
||||
* [Example](/examples/assert.c)
|
||||
* [Simple](/examples/assert_simple.c)
|
||||
* [With return](/example/assert_return.c)
|
||||
* [Architecture-specific helpers](/include/kernaux/arch/)
|
||||
* Device drivers (for debugging only)
|
||||
* [Serial console](/include/kernaux/console.h)
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
#define KERNAUX_ENABLE_ASSERT
|
||||
#include <kernaux/assert.h>
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
|
||||
static unsigned int count = 0;
|
||||
static const char *last_file = NULL;
|
||||
static int last_line = 0;
|
||||
static const char *last_str = NULL;
|
||||
|
||||
static unsigned int noreturn_count = 0;
|
||||
|
||||
static void assert_cb(
|
||||
const char *const file,
|
||||
const int line,
|
||||
const char *const str
|
||||
) {
|
||||
++count;
|
||||
last_file = file;
|
||||
last_line = line;
|
||||
last_str = str;
|
||||
}
|
||||
|
||||
static void test_return(const int value)
|
||||
{
|
||||
KERNAUX_ASSERT_RETURN(value > 100);
|
||||
++noreturn_count;
|
||||
}
|
||||
|
||||
static bool test_retval(const int value)
|
||||
{
|
||||
KERNAUX_ASSERT_RETVAL(value > 100, false);
|
||||
++noreturn_count;
|
||||
return true;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
kernaux_assert_cb = assert_cb;
|
||||
|
||||
test_return(123);
|
||||
|
||||
assert(count == 0);
|
||||
assert(last_file == NULL);
|
||||
assert(last_line == 0);
|
||||
assert(last_str == NULL);
|
||||
assert(noreturn_count == 1);
|
||||
|
||||
assert(test_retval(123));
|
||||
|
||||
assert(count == 0);
|
||||
assert(last_file == NULL);
|
||||
assert(last_line == 0);
|
||||
assert(last_str == NULL);
|
||||
assert(noreturn_count == 2);
|
||||
|
||||
test_return(0);
|
||||
|
||||
assert(count == 1);
|
||||
assert(strcmp(last_file, __FILE__) == 0);
|
||||
assert(last_line == 29);
|
||||
assert(strcmp(last_str, "value > 100") == 0);
|
||||
assert(noreturn_count == 2);
|
||||
|
||||
assert(!test_retval(0));
|
||||
|
||||
assert(count == 2);
|
||||
assert(strcmp(last_file, __FILE__) == 0);
|
||||
assert(last_line == 35);
|
||||
assert(strcmp(last_str, "value > 100") == 0);
|
||||
assert(noreturn_count == 2);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue