From e3a1284aafd5af99d8c5b97619eceb2afdd94f4c Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sat, 18 Dec 2021 05:51:12 +0500 Subject: [PATCH] Add example for return asserts --- .gitignore | 3 +- Makefile.am | 12 +++- README.md | 3 +- examples/assert_return.c | 77 ++++++++++++++++++++++++++ examples/{assert.c => assert_simple.c} | 0 5 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 examples/assert_return.c rename examples/{assert.c => assert_simple.c} (100%) diff --git a/.gitignore b/.gitignore index ee2316a..02515a1 100644 --- a/.gitignore +++ b/.gitignore @@ -34,7 +34,8 @@ /tests/test*.log /tests/test*.trs -/examples/assert +/examples/assert_return +/examples/assert_simple /examples/cmdline /examples/pfa /examples/printf diff --git a/Makefile.am b/Makefile.am index e88a68e..39e4aeb 100644 --- a/Makefile.am +++ b/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) \ diff --git a/README.md b/README.md index b0cfa76..f1daf50 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/examples/assert_return.c b/examples/assert_return.c new file mode 100644 index 0000000..794833c --- /dev/null +++ b/examples/assert_return.c @@ -0,0 +1,77 @@ +#define KERNAUX_ENABLE_ASSERT +#include + +#include +#include +#include +#include + +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; +} diff --git a/examples/assert.c b/examples/assert_simple.c similarity index 100% rename from examples/assert.c rename to examples/assert_simple.c