mirror of
https://github.com/tailix/libkernaux.git
synced 2024-11-27 11:14:42 -05:00
Add macro KERNAUX_STATIC_TEST
(#138)
This commit is contained in:
parent
6d91acc75f
commit
c19193c390
7 changed files with 54 additions and 23 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 #
|
||||
##########
|
||||
|
|
28
examples/macro_static_test.c
Normal file
28
examples/macro_static_test.c
Normal 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() {}
|
|
@ -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) \
|
||||
|
|
|
@ -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 = \
|
||||
|
|
Loading…
Reference in a new issue