diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 17:36:47 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 17:36:47 +0000 |
commit | 0441d265f2bb9da249c7abf333f0f771fadb4ab5 (patch) | |
tree | 3f3789daa2f6db22da6e55e92bee0062a7d613fe /src/lib/test-macros.c | |
parent | Initial commit. (diff) | |
download | dovecot-0441d265f2bb9da249c7abf333f0f771fadb4ab5.tar.xz dovecot-0441d265f2bb9da249c7abf333f0f771fadb4ab5.zip |
Adding upstream version 1:2.3.21+dfsg1.upstream/1%2.3.21+dfsg1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/lib/test-macros.c')
-rw-r--r-- | src/lib/test-macros.c | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/lib/test-macros.c b/src/lib/test-macros.c new file mode 100644 index 0000000..4b9e1b1 --- /dev/null +++ b/src/lib/test-macros.c @@ -0,0 +1,63 @@ +/* Copyright (c) 2021 Dovecot authors, see the included COPYING file */ + +#include "test-lib.h" + +struct parent { + unsigned int a; +}; +struct child { + unsigned int b; + struct parent p; +}; + +static void test_container_of(void) +{ + struct child child; + struct parent *parent = &child.p; + + test_begin("container_of()"); + struct child *ptr_child = container_of(parent, struct child, p); + test_assert(ptr_child == &child); + test_end(); +} + +static void test_pointer_cast(void) +{ +#define TEST_POINTER_CAST(type, prefix, value) \ + type prefix ## _num = value; \ + void *prefix ## _ptr = POINTER_CAST(prefix ## _num); \ + test_assert(POINTER_CAST_TO(prefix ## _ptr, type) == prefix ## _num); + test_begin("POINTER_CAST"); + + TEST_POINTER_CAST(unsigned int, uint, 0x87654321); + TEST_POINTER_CAST(uint32_t, uint32, 0xf00dabcd); + TEST_POINTER_CAST(uint16_t, uint16, 0x9876); + TEST_POINTER_CAST(uint8_t, uint8, 0xf8); +#if SIZEOF_VOID_P == 8 + TEST_POINTER_CAST(unsigned long, ulong, 0xfedcba9876543210); + TEST_POINTER_CAST(size_t, size, 0xfedcba9876543210); +#else + TEST_POINTER_CAST(unsigned long, ulong, 0xfedcba98); + TEST_POINTER_CAST(size_t, size, 0xfedcba98); +#endif + + test_end(); +} + +static void test_ptr_offset(void) +{ + uint32_t foo[] = { 1, 2, 3 }; + const uint32_t foo2[] = { 1, 2, 3 }; + + test_begin("PTR_OFFSET"); + test_assert(PTR_OFFSET(foo, 4) == &foo[1]); + test_assert(CONST_PTR_OFFSET(foo2, 8) == &foo2[2]); + test_end(); +} + +void test_macros(void) +{ + test_container_of(); + test_pointer_cast(); + test_ptr_offset(); +} |