mirror of https://github.com/tailix/libkernaux.git
Add macro `KERNAUX_STATIC_TEST` (#138)
This commit is contained in:
parent
6d91acc75f
commit
c19193c390
|
@ -86,6 +86,7 @@
|
||||||
/examples/macro_cast
|
/examples/macro_cast
|
||||||
/examples/macro_container_of
|
/examples/macro_container_of
|
||||||
/examples/macro_packing
|
/examples/macro_packing
|
||||||
|
/examples/macro_static_test
|
||||||
/examples/memmap
|
/examples/memmap
|
||||||
/examples/multiboot2_header_macro
|
/examples/multiboot2_header_macro
|
||||||
/examples/ntoa
|
/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>
|
2022-12-10 Alex Kotov <kotovalexarian@gmail.com>
|
||||||
|
|
||||||
* include/kernaux/macro.h: Macros "KERNAUX_CAST_(VAR|CONST)" have been added
|
* 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**)
|
* [Macros](/include/kernaux/macro.h) (*non-breaking since* **0.6.0**)
|
||||||
* [Example: packing](/examples/macro_packing.c)
|
* [Example: packing](/examples/macro_packing.c)
|
||||||
* [Example: CAST\_\*](/examples/macro_cast.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: BITS](/examples/macro_bits.c)
|
||||||
|
* [Example: STATIC\_TEST\*](/examples/macro_static_test.c)
|
||||||
* [Assertions](/include/kernaux/assert.h) (*non-breaking since* **0.4.0**)
|
* [Assertions](/include/kernaux/assert.h) (*non-breaking since* **0.4.0**)
|
||||||
* [Example: Assert](/examples/assert.c)
|
* [Example: Assert](/examples/assert.c)
|
||||||
* [Example: Panic](/examples/panic.c)
|
* [Example: Panic](/examples/panic.c)
|
||||||
|
|
|
@ -77,6 +77,14 @@ TESTS += macro_packing
|
||||||
macro_packing_LDADD = $(top_builddir)/libkernaux.la
|
macro_packing_LDADD = $(top_builddir)/libkernaux.la
|
||||||
macro_packing_SOURCES = main.c macro_packing.c
|
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 #
|
# memmap #
|
||||||
##########
|
##########
|
||||||
|
|
|
@ -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 *
|
* 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) \
|
#define KERNAUX_STATIC_TEST_STRUCT_SIZE(name, size) \
|
||||||
KERNAUX_UNUSED \
|
KERNAUX_STATIC_TEST(struct_size_##name, sizeof(struct name) == (size))
|
||||||
static const int \
|
|
||||||
_kernaux_static_test_struct_size_##name[ \
|
|
||||||
sizeof(struct name) == (size) ? 1 : -1 \
|
|
||||||
]
|
|
||||||
|
|
||||||
#define KERNAUX_STATIC_TEST_UNION_SIZE(name, size) \
|
#define KERNAUX_STATIC_TEST_UNION_SIZE(name, size) \
|
||||||
KERNAUX_UNUSED \
|
KERNAUX_STATIC_TEST(union_size_##name, sizeof(union name) == (size))
|
||||||
static const int \
|
|
||||||
_kernaux_static_test_union_size_##name[ \
|
|
||||||
sizeof(union name) == (size) ? 1 : -1 \
|
|
||||||
]
|
|
||||||
|
|
||||||
/*****************
|
/*****************
|
||||||
* Simple values *
|
* Simple values *
|
||||||
|
@ -89,16 +86,8 @@ _kernaux_static_test_union_size_##name[ \
|
||||||
*********************/
|
*********************/
|
||||||
|
|
||||||
#define KERNAUX_CAST_VAR(type, name, value) \
|
#define KERNAUX_CAST_VAR(type, name, value) \
|
||||||
{ \
|
KERNAUX_STATIC_TEST(cast_pos_##name, sizeof(value) <= sizeof(type)); \
|
||||||
KERNAUX_UNUSED \
|
KERNAUX_STATIC_TEST(cast_neg_##name, sizeof(-(value)) <= sizeof(type)); \
|
||||||
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); \
|
type name = (type)(value); \
|
||||||
do {} while (0)
|
do {} while (0)
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,7 @@ CPPCHECK_INC = \
|
||||||
|
|
||||||
CPPCHECK_SUPPRESS = \
|
CPPCHECK_SUPPRESS = \
|
||||||
--suppress='constArgument:$(top_srcdir)/examples/macro_cast.c' \
|
--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'
|
--suppress='unusedStructMember:$(top_srcdir)/tests/test_multiboot2_info_*.c'
|
||||||
|
|
||||||
CPPCHECK_PATHS = \
|
CPPCHECK_PATHS = \
|
||||||
|
|
Loading…
Reference in New Issue