]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: Add test-macros
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Tue, 24 Aug 2021 12:02:44 +0000 (15:02 +0300)
committerTimo Sirainen <timo.sirainen@open-xchange.com>
Tue, 24 Aug 2021 12:02:44 +0000 (15:02 +0300)
src/lib/Makefile.am
src/lib/test-lib.inc
src/lib/test-macros.c [new file with mode: 0644]

index 8884f371276c0fbb26d740c2fa8063dd1d737833..2d2fcdf1ef9b3c885e3b21026918736e59d53345 100644 (file)
@@ -423,6 +423,7 @@ test_lib_SOURCES = \
        test-lib-signals.c \
        test-llist.c \
        test-log-throttle.c \
+       test-macros.c \
        test-malloc-overflow.c \
        test-memarea.c \
        test-mempool.c \
index 07ad5765653cb1f67d99391f3a8110cc8a2b3884..0591f86dc3e453608f61b3ab1fbc4045c12fb954 100644 (file)
@@ -64,6 +64,7 @@ TEST(test_json_tree)
 TEST(test_lib_signals)
 TEST(test_llist)
 TEST(test_log_throttle)
+TEST(test_macros)
 TEST(test_malloc_overflow)
 FATAL(fatal_malloc_overflow)
 TEST(test_memarea)
diff --git a/src/lib/test-macros.c b/src/lib/test-macros.c
new file mode 100644 (file)
index 0000000..4b9e1b1
--- /dev/null
@@ -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();
+}