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

124 lines
2.6 KiB
C
Raw Normal View History

2021-12-17 19:51:12 -05:00
#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;
}
2021-12-18 00:55:48 -05:00
static void test_assert_return(const int value)
2021-12-17 19:51:12 -05:00
{
KERNAUX_ASSERT_RETURN(value > 100);
++noreturn_count;
}
2021-12-18 00:55:48 -05:00
static bool test_assert_retval(const int value)
2021-12-17 19:51:12 -05:00
{
KERNAUX_ASSERT_RETVAL(value > 100, false);
++noreturn_count;
return true;
}
2021-12-18 00:55:48 -05:00
static void test_notnull_return(const int *const value)
{
KERNAUX_NOTNULL_RETURN(value);
++noreturn_count;
}
static bool test_notnull_retval(const int *const value)
{
KERNAUX_NOTNULL_RETVAL(value, false);
++noreturn_count;
return true;
}
2021-12-17 19:51:12 -05:00
int main()
{
kernaux_assert_cb = assert_cb;
2021-12-18 00:55:48 -05:00
int value;
test_assert_return(123);
2021-12-17 19:51:12 -05:00
assert(count == 0);
assert(last_file == NULL);
assert(last_line == 0);
assert(last_str == NULL);
assert(noreturn_count == 1);
2021-12-18 00:55:48 -05:00
assert(test_assert_retval(123));
2021-12-17 19:51:12 -05:00
assert(count == 0);
assert(last_file == NULL);
assert(last_line == 0);
assert(last_str == NULL);
assert(noreturn_count == 2);
2021-12-18 00:55:48 -05:00
test_notnull_return(&value);
assert(count == 0);
assert(last_file == NULL);
assert(last_line == 0);
assert(last_str == NULL);
assert(noreturn_count == 3);
assert(test_notnull_retval(&value));
assert(count == 0);
assert(last_file == NULL);
assert(last_line == 0);
assert(last_str == NULL);
assert(noreturn_count == 4);
test_assert_return(0);
2021-12-17 19:51:12 -05:00
assert(count == 1);
assert(strcmp(last_file, __FILE__) == 0);
assert(last_line == 28);
2021-12-17 19:51:12 -05:00
assert(strcmp(last_str, "value > 100") == 0);
2021-12-18 00:55:48 -05:00
assert(noreturn_count == 4);
2021-12-17 19:51:12 -05:00
2021-12-18 00:55:48 -05:00
assert(!test_assert_retval(0));
2021-12-17 19:51:12 -05:00
assert(count == 2);
assert(strcmp(last_file, __FILE__) == 0);
assert(last_line == 34);
2021-12-17 19:51:12 -05:00
assert(strcmp(last_str, "value > 100") == 0);
2021-12-18 00:55:48 -05:00
assert(noreturn_count == 4);
test_notnull_return(NULL);
assert(count == 3);
assert(strcmp(last_file, __FILE__) == 0);
assert(last_line == 41);
2021-12-18 00:55:48 -05:00
assert(strcmp(last_str, "value") == 0);
assert(noreturn_count == 4);
assert(!test_notnull_retval(NULL));
assert(count == 4);
assert(strcmp(last_file, __FILE__) == 0);
assert(last_line == 47);
2021-12-18 00:55:48 -05:00
assert(strcmp(last_str, "value") == 0);
assert(noreturn_count == 4);
2021-12-17 19:51:12 -05:00
return 0;
}