This commit is contained in:
Alex Kotov 2022-12-13 09:45:17 +04:00
parent e154bdf7c4
commit 3fae5cafc2
Signed by: kotovalexarian
GPG Key ID: 553C0EBBEB5D5F08
5 changed files with 57 additions and 24 deletions

View File

@ -2,9 +2,11 @@
#include <kernaux/assert.h>
#include <assert.h>
#include <setjmp.h>
#include <stddef.h>
#include <string.h>
static jmp_buf jmpbuf;
static unsigned int count = 0;
static const char *last_file = NULL;
static int last_line = 0;
@ -19,34 +21,44 @@ static void assert_cb(
last_file = file;
last_line = line;
last_str = str;
longjmp(jmpbuf, 1);
}
int main()
{
assert(setjmp(jmpbuf) == 0);
kernaux_assert_cb = assert_cb;
// cppcheck-suppress duplicateExpression
KERNAUX_ASSERT(1 == 1);
if (setjmp(jmpbuf) == 0) {
// cppcheck-suppress duplicateExpression
KERNAUX_ASSERT(1 == 1);
}
assert(count == 0);
assert(last_file == NULL);
assert(last_line == 0);
assert(last_str == NULL);
// cppcheck-suppress duplicateExpression
KERNAUX_ASSERT(1 != 1);
if (setjmp(jmpbuf) == 0) {
// cppcheck-suppress duplicateExpression
KERNAUX_ASSERT(1 != 1);
}
assert(count == 1);
assert(strcmp(last_file, __FILE__) == 0);
assert(last_line == __LINE__ - 4);
assert(last_line == __LINE__ - 5);
assert(strcmp(last_str, "1 != 1") == 0);
// cppcheck-suppress staticStringCompare
KERNAUX_ASSERT(strcmp("qwe", "rty") == 0);
if (setjmp(jmpbuf) == 0) {
// cppcheck-suppress staticStringCompare
KERNAUX_ASSERT(strcmp("qwe", "rty") == 0);
}
assert(count == 2);
assert(strcmp(last_file, __FILE__) == 0);
assert(last_line == __LINE__ - 4);
assert(last_line == __LINE__ - 5);
assert(strcmp(last_str, "strcmp(\"qwe\", \"rty\") == 0") == 0);
return 0;

View File

@ -2,9 +2,11 @@
#include <kernaux/assert.h>
#include <assert.h>
#include <setjmp.h>
#include <stddef.h>
#include <string.h>
static jmp_buf jmpbuf;
static unsigned int count = 0;
static const char *last_file = NULL;
static int last_line = 0;
@ -19,30 +21,31 @@ static void assert_cb(
last_file = file;
last_line = line;
last_str = str;
longjmp(jmpbuf, 1);
}
int main()
{
KERNAUX_PANIC("foo");
assert(count == 0);
assert(last_file == NULL);
assert(last_line == 0);
assert(last_str == NULL);
assert(setjmp(jmpbuf) == 0);
kernaux_assert_cb = assert_cb;
KERNAUX_PANIC("bar");
if (setjmp(jmpbuf) == 0) {
KERNAUX_PANIC("bar");
}
assert(count == 1);
assert(strcmp(last_file, __FILE__) == 0);
assert(last_line == __LINE__ - 4);
assert(last_line == __LINE__ - 5);
assert(strcmp(last_str, "bar") == 0);
KERNAUX_PANIC("car");
if (setjmp(jmpbuf) == 0) {
KERNAUX_PANIC("car");
}
assert(count == 2);
assert(strcmp(last_file, __FILE__) == 0);
assert(last_line == __LINE__ - 4);
assert(last_line == __LINE__ - 5);
assert(strcmp(last_str, "car") == 0);
}

View File

@ -5,6 +5,8 @@
extern "C" {
#endif
#include <kernaux/macro.h>
#ifdef KERNAUX_DISABLE_ASSERT
#define KERNAUX_PANIC(msg) ((void)0)
#define KERNAUX_ASSERT(cond) ((void)0)
@ -15,9 +17,12 @@ extern "C" {
#define KERNAUX_NOTNULL(cond) KERNAUX_ASSERT(cond)
void kernaux_assert_do(const char *file, int line, const char *msg);
typedef void (*KernAux_Assert_Cb)(const char *file, int line, const char *msg);
extern void (*kernaux_assert_cb)(const char *file, int line, const char *msg);
extern KernAux_Assert_Cb kernaux_assert_cb;
KERNAUX_NORETURN
void kernaux_assert_do(const char *file, int line, const char *msg);
#ifdef __cplusplus
}

View File

@ -6,7 +6,7 @@
#include <stddef.h>
void (*kernaux_assert_cb)(const char *file, int line, const char *msg) = NULL;
KernAux_Assert_Cb kernaux_assert_cb = NULL;
void kernaux_assert_do(
const char *const file,
@ -14,4 +14,8 @@ void kernaux_assert_do(
const char *const msg
) {
if (kernaux_assert_cb) kernaux_assert_cb(file, line, msg);
// Enforce noreturn.
volatile int x = 0;
for (;;) ++x;
}

View File

@ -9,15 +9,16 @@
#include <kernaux/memmap.h>
#include <assert.h>
#include <setjmp.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
static KernAux_MemMap memmap;
static jmp_buf jmpbuf;
static unsigned int assert_count_exp = 0;
static unsigned int assert_count_ctr = 0;
static const char *assert_last_file = NULL;
static void assert_cb(
@ -27,6 +28,8 @@ static void assert_cb(
) {
++assert_count_ctr;
assert_last_file = file;
longjmp(jmpbuf, 1);
}
static void before_assert()
@ -52,6 +55,8 @@ static void expect_assert()
void test_main()
{
assert(setjmp(jmpbuf) == 0);
kernaux_assert_cb = assert_cb;
{
@ -71,7 +76,9 @@ void test_main()
assert(KernAux_MemMap_entry_by_index(memmap, 0) == NULL);
before_assert();
assert(!KernAux_MemMap_finish(memmap));
if (setjmp(jmpbuf) == 0) {
assert(!KernAux_MemMap_finish(memmap));
}
expect_assert();
}
@ -183,7 +190,9 @@ void test_main()
assert(MEMMAP.entries[0].limit == 2);
before_assert();
assert(KernAux_MemMap_entry_by_index(memmap, 0) == NULL);
if (setjmp(jmpbuf) == 0) {
assert(KernAux_MemMap_entry_by_index(memmap, 0) == NULL);
}
expect_assert();
}