1
0
Fork 0
mirror of https://github.com/tailix/libkernaux.git synced 2024-11-13 11:04:27 -05:00

Add example for return asserts

This commit is contained in:
Alex Kotov 2021-12-18 05:51:12 +05:00
parent 3e071577f3
commit e3a1284aaf
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
5 changed files with 90 additions and 5 deletions

3
.gitignore vendored
View file

@ -34,7 +34,8 @@
/tests/test*.log /tests/test*.log
/tests/test*.trs /tests/test*.trs
/examples/assert /examples/assert_return
/examples/assert_simple
/examples/cmdline /examples/cmdline
/examples/pfa /examples/pfa
/examples/printf /examples/printf

View file

@ -32,7 +32,9 @@ libkernaux_a_SOURCES += src/arch/x86_64.S
endif endif
if ENABLE_ASSERT if ENABLE_ASSERT
TESTS += examples/assert TESTS += \
examples/assert_return \
examples/assert_simple
endif endif
if ENABLE_CMDLINE if ENABLE_CMDLINE
@ -80,9 +82,13 @@ TESTS += \
tests/test_units_human tests/test_units_human
endif endif
examples_assert_SOURCES = \ examples_assert_return_SOURCES = \
$(libkernaux_a_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 = \ examples_cmdline_SOURCES = \
$(libkernaux_a_SOURCES) \ $(libkernaux_a_SOURCES) \

View file

@ -30,7 +30,8 @@ API
* Runtime environment * Runtime environment
* [Assertions](/include/kernaux/assert.h) * [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/) * [Architecture-specific helpers](/include/kernaux/arch/)
* Device drivers (for debugging only) * Device drivers (for debugging only)
* [Serial console](/include/kernaux/console.h) * [Serial console](/include/kernaux/console.h)

77
examples/assert_return.c Normal file
View file

@ -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;
}