Add macro `KERNAUX_STATIC_TEST` (#138)

This commit is contained in:
Alex Kotov 2022-12-11 17:40:49 +04:00 committed by GitHub
parent 6d91acc75f
commit c19193c390
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 54 additions and 23 deletions

1
.gitignore vendored
View File

@ -86,6 +86,7 @@
/examples/macro_cast
/examples/macro_container_of
/examples/macro_packing
/examples/macro_static_test
/examples/memmap
/examples/multiboot2_header_macro
/examples/ntoa

View File

@ -1,3 +1,7 @@
2022-12-11 Alex Kotov <kotovalexarian@gmail.com>
* include/kernaux/macro.h: Macro "KERNAUX_STATIC_TEST" has been added
2022-12-10 Alex Kotov <kotovalexarian@gmail.com>
* include/kernaux/macro.h: Macros "KERNAUX_CAST_(VAR|CONST)" have been added

View File

@ -43,8 +43,9 @@ zero). Work-in-progress APIs can change at any time.
* [Macros](/include/kernaux/macro.h) (*non-breaking since* **0.6.0**)
* [Example: packing](/examples/macro_packing.c)
* [Example: CAST\_\*](/examples/macro_cast.c);
* [Example: CONTAINER_OF](/examples/macro_container_of.c)
* [Example: CONTAINER\_OF](/examples/macro_container_of.c)
* [Example: BITS](/examples/macro_bits.c)
* [Example: STATIC\_TEST\*](/examples/macro_static_test.c)
* [Assertions](/include/kernaux/assert.h) (*non-breaking since* **0.4.0**)
* [Example: Assert](/examples/assert.c)
* [Example: Panic](/examples/panic.c)

View File

@ -77,6 +77,14 @@ TESTS += macro_packing
macro_packing_LDADD = $(top_builddir)/libkernaux.la
macro_packing_SOURCES = main.c macro_packing.c
#####################
# macro_static_test #
#####################
TESTS += macro_static_test
macro_static_test_LDADD = $(top_builddir)/libkernaux.la
macro_static_test_SOURCES = main.c macro_static_test.c
##########
# memmap #
##########

View File

@ -0,0 +1,28 @@
#include <kernaux/macro.h>
#include <stdint.h>
KERNAUX_STATIC_TEST(uint8_t_size, sizeof(uint8_t) == 1);
KERNAUX_STATIC_TEST(uint16_t_size, sizeof(uint16_t) == 2);
KERNAUX_STATIC_TEST(uint32_t_size, sizeof(uint32_t) == 4);
KERNAUX_STATIC_TEST(uint64_t_size, sizeof(uint64_t) == 8);
#include <kernaux/macro/packing_start.run>
struct Foo {
uint8_t a;
uint32_t b;
} KERNAUX_PACKED;
KERNAUX_STATIC_TEST_STRUCT_SIZE(Foo, 5);
union Bar {
uint8_t a;
uint16_t b;
} KERNAUX_PACKED;
KERNAUX_STATIC_TEST_UNION_SIZE(Bar, 2);
#include <kernaux/macro/packing_end.run>
void example_main() {}

View File

@ -50,19 +50,16 @@ extern "C" {
* Static assertions *
*********************/
#define KERNAUX_STATIC_TEST(name, cond) \
KERNAUX_UNUSED \
static const int \
_kernaux_static_test_##name[(cond) ? 1 : -1]
#define KERNAUX_STATIC_TEST_STRUCT_SIZE(name, size) \
KERNAUX_UNUSED \
static const int \
_kernaux_static_test_struct_size_##name[ \
sizeof(struct name) == (size) ? 1 : -1 \
]
KERNAUX_STATIC_TEST(struct_size_##name, sizeof(struct name) == (size))
#define KERNAUX_STATIC_TEST_UNION_SIZE(name, size) \
KERNAUX_UNUSED \
static const int \
_kernaux_static_test_union_size_##name[ \
sizeof(union name) == (size) ? 1 : -1 \
]
KERNAUX_STATIC_TEST(union_size_##name, sizeof(union name) == (size))
/*****************
* Simple values *
@ -89,17 +86,9 @@ _kernaux_static_test_union_size_##name[ \
*********************/
#define KERNAUX_CAST_VAR(type, name, value) \
{ \
KERNAUX_UNUSED \
static const int _kernaux_static_test_cast_pos_##name[ \
sizeof(value) <= sizeof(type) ? 1 : -1 \
]; \
KERNAUX_UNUSED \
static const int _kernaux_static_test_cast_neg_##name[ \
sizeof(-(value)) <= sizeof(type) ? 1 : -1 \
]; \
} \
type name = (type)(value); \
KERNAUX_STATIC_TEST(cast_pos_##name, sizeof(value) <= sizeof(type)); \
KERNAUX_STATIC_TEST(cast_neg_##name, sizeof(-(value)) <= sizeof(type)); \
type name = (type)(value); \
do {} while (0)
#define KERNAUX_CAST_CONST(type, name, value) \

View File

@ -21,7 +21,7 @@ CPPCHECK_INC = \
CPPCHECK_SUPPRESS = \
--suppress='constArgument:$(top_srcdir)/examples/macro_cast.c' \
--suppress='unusedStructMember:$(top_srcdir)/examples/multiboot2_header_macro.c' \
--suppress='unusedStructMember:$(top_srcdir)/examples/*.c' \
--suppress='unusedStructMember:$(top_srcdir)/tests/test_multiboot2_info_*.c'
CPPCHECK_PATHS = \