migrate allocator tests - relates to #342

Sat, 30 Dec 2023 14:32:42 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 30 Dec 2023 14:32:42 +0100
changeset 782
74d777455e96
parent 781
a786b0a89b37
child 783
7ca8cf155489

migrate allocator tests - relates to #342

tests/Makefile file | annotate | diff | comparison | revisions
tests/test_allocator.c file | annotate | diff | comparison | revisions
tests/test_allocator.cpp file | annotate | diff | comparison | revisions
tests/ucxtest.c file | annotate | diff | comparison | revisions
--- a/tests/Makefile	Sat Dec 30 14:11:20 2023 +0100
+++ b/tests/Makefile	Sat Dec 30 14:32:42 2023 +0100
@@ -27,8 +27,8 @@
 
 TEST_DIR=$(build_dir)/tests
 
-SRC = util_allocator.c test_utils.c test_hash_key.c test_string.c \
-	test_printf.c test_mempool.c ucxtest.c
+SRC = util_allocator.c test_utils.c test_hash_key.c test_allocator.c \
+	test_string.c test_printf.c test_mempool.c ucxtest.c
 
 OBJ_EXT=.o
 OBJ=$(SRC:%.c=$(TEST_DIR)/%$(OBJ_EXT))
@@ -46,6 +46,11 @@
 
 FORCE:
 
+$(TEST_DIR)/test_allocator$(OBJ_EXT): test_allocator.c ../src/cx/test.h \
+ ../src/cx/allocator.h ../src/cx/common.h
+	@echo "Compiling $<"
+	$(CC) -o $@ $(CFLAGS) -c $<
+
 $(TEST_DIR)/test_hash_key$(OBJ_EXT): test_hash_key.c ../src/cx/test.h \
  ../src/cx/hash_key.h ../src/cx/common.h ../src/cx/string.h \
  ../src/cx/allocator.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_allocator.c	Sat Dec 30 14:32:42 2023 +0100
@@ -0,0 +1,145 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "cx/test.h"
+
+#include "cx/allocator.h"
+
+CX_TEST(test_allocator_default) {
+    CX_TEST_DO {
+        CX_TEST_ASSERT(cxDefaultAllocator->cl != NULL);
+    }
+}
+
+CX_TEST(test_allocator_default_malloc) {
+    void *test = cxMalloc(cxDefaultAllocator, 16);
+    CX_TEST_DO {
+        CX_TEST_ASSERT(test != NULL);
+        // we cannot assert sth. but valgrind will detect an error
+        memcpy(test, "0123456789ABCDEF", 16);
+    }
+    free(test);
+}
+
+CX_TEST(test_allocator_default_calloc) {
+    char *test = cxCalloc(cxDefaultAllocator, 8, 2);
+    CX_TEST_DO {
+        CX_TEST_ASSERT(test != NULL);
+        for (int i = 0; i < 16; i++) {
+            CX_TEST_ASSERT(test[i] == 0);
+        }
+    }
+    free(test);
+}
+
+CX_TEST(test_allocator_default_realloc) {
+    char *test = calloc(8, 1);
+    memcpy(test, "Test", 5);
+    CX_TEST_DO {
+        test = cxRealloc(cxDefaultAllocator, test, 16);
+        CX_TEST_ASSERT(test != NULL);
+        CX_TEST_ASSERT(0 == strcmp(test, "Test"));
+    }
+    free(test);
+}
+
+CX_TEST(test_allocator_default_free) {
+    void *test = malloc(16);
+    CX_TEST_DO {
+        // we cannot assert sth. but valgrind will detect an error
+        cxFree(cxDefaultAllocator, test);
+        CX_TEST_ASSERT(true);
+    }
+}
+
+CX_TEST(test_allocator_reallocate) {
+    void *test = calloc(8, 1);
+    memcpy(test, "Test", 5);
+    CX_TEST_DO {
+        int ret = cxReallocate(cxDefaultAllocator, &test, 16);
+        CX_TEST_ASSERT(ret == 0);
+        CX_TEST_ASSERT(test != NULL);
+        CX_TEST_ASSERT(0 == strcmp(test, "Test"));
+    }
+    free(test);
+}
+
+CX_TEST(test_allocator_reallocate_low_level) {
+    void *test = calloc(8, 1);
+    memcpy(test, "Test", 5);
+    CX_TEST_DO {
+        int ret = cx_reallocate(&test, 16);
+        CX_TEST_ASSERT(ret == 0);
+        CX_TEST_ASSERT(test != NULL);
+        CX_TEST_ASSERT(0 == strcmp(test, "Test"));
+    }
+    free(test);
+}
+
+static void *test_allocator_mock_failing_realloc(
+        __attribute__((__unused__))void *p,
+        __attribute__((__unused__))void *d,
+        __attribute__((__unused__))size_t n
+) {
+    return NULL;
+}
+
+CX_TEST(test_allocator_reallocate_fails) {
+    // Mock an allocator that always returns NULL on realloc
+    cx_allocator_class mock_cl;
+    mock_cl.realloc = test_allocator_mock_failing_realloc;
+    CxAllocator mock = {&mock_cl, NULL};
+
+    void *test = calloc(8, 1);
+    memcpy(test, "Test", 5);
+    void *original = test;
+    CX_TEST_DO {
+        int ret = cxReallocate(&mock, &test, 16);
+        // non-zero return code because of the failure
+        CX_TEST_ASSERT(ret != 0);
+        // the test pointer was not changed and still points to the same memory
+        CX_TEST_ASSERT(test == original);
+        CX_TEST_ASSERT(0 == strcmp(test, "Test"));
+    }
+    free(test);
+}
+
+CxTestSuite *cx_test_suite_allocator(void) {
+    CxTestSuite *suite = cx_test_suite_new("allocator");
+
+    cx_test_register(suite, test_allocator_default);
+    cx_test_register(suite, test_allocator_default_malloc);
+    cx_test_register(suite, test_allocator_default_calloc);
+    cx_test_register(suite, test_allocator_default_realloc);
+    cx_test_register(suite, test_allocator_default_free);
+    cx_test_register(suite, test_allocator_reallocate);
+    cx_test_register(suite, test_allocator_reallocate_fails);
+    cx_test_register(suite, test_allocator_reallocate_low_level);
+
+    return suite;
+}
--- a/tests/test_allocator.cpp	Sat Dec 30 14:11:20 2023 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,96 +0,0 @@
-/*
- * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
- *
- * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- *   1. Redistributions of source code must retain the above copyright
- *      notice, this list of conditions and the following disclaimer.
- *
- *   2. Redistributions in binary form must reproduce the above copyright
- *      notice, this list of conditions and the following disclaimer in the
- *      documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
- * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
- * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
- * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
- * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
- * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
- * POSSIBILITY OF SUCH DAMAGE.
- */
-
-#include "cx/allocator.h"
-#include <gtest/gtest.h>
-
-TEST(Allocator, DefaultAllocator) {
-    cx_allocator_class *clazz = cxDefaultAllocator->cl;
-    ASSERT_NE(clazz, nullptr);
-}
-
-TEST(Allocator, DefaultMalloc) {
-    void *test = cxMalloc(cxDefaultAllocator, 16);
-    ASSERT_NE(test, nullptr);
-    free(test);
-}
-
-TEST(Allocator, DefaultRealloc) {
-    void *test = calloc(8, 1);
-    memcpy(test, "Test", 5);
-    test = cxRealloc(cxDefaultAllocator, test, 16);
-    ASSERT_NE(test, nullptr);
-    EXPECT_STREQ(reinterpret_cast<char *>(test), "Test");
-    free(test);
-}
-
-TEST(Allocator, Reallocate) {
-    void *test = calloc(8, 1);
-    memcpy(test, "Test", 5);
-    int ret = cxReallocate(cxDefaultAllocator, &test, 16);
-    EXPECT_EQ(ret, 0);
-    ASSERT_NE(test, nullptr);
-    EXPECT_STREQ(reinterpret_cast<char *>(test), "Test");
-    free(test);
-}
-
-TEST(Allocator, DefaultCalloc) {
-    char *test = reinterpret_cast<char *>(cxCalloc(cxDefaultAllocator, 8, 2));
-    ASSERT_NE(test, nullptr);
-    for (int i = 0; i < 16; i++) ASSERT_EQ(test[i], 0);
-    free(test);
-}
-
-TEST(Allocator, DefaultFree) {
-    void *test = malloc(16);
-    EXPECT_NO_FATAL_FAILURE(
-            cxFree(cxDefaultAllocator, test);
-    );
-}
-
-TEST(Allocator, FailingReallocate) {
-    // Mock an allocator that always returns nullptr on realloc
-    cx_allocator_class mock_cl;
-    mock_cl.realloc = [](
-            [[maybe_unused]]void *p,
-            [[maybe_unused]]void *d,
-            [[maybe_unused]]size_t n
-    ) -> void * { return nullptr; };
-    cx_allocator_s mock{&mock_cl, nullptr};
-
-    void *test = calloc(8, 1);
-    memcpy(test, "Test", 5);
-    void *original = test;
-    int ret = cxReallocate(&mock, &test, 16);
-    // non-zero return code because of the failure
-    EXPECT_NE(ret, 0);
-    // the test pointer was not changed and still points to the same memory
-    EXPECT_EQ(test, original);
-    EXPECT_STREQ(reinterpret_cast<char *>(test), "Test");
-    free(test);
-}
--- a/tests/ucxtest.c	Sat Dec 30 14:11:20 2023 +0100
+++ b/tests/ucxtest.c	Sat Dec 30 14:32:42 2023 +0100
@@ -31,6 +31,7 @@
 CxTestSuite *cx_test_suite_testing_allocator(void);
 CxTestSuite *cx_test_suite_utils(void);
 CxTestSuite *cx_test_suite_hash_key(void);
+CxTestSuite *cx_test_suite_allocator(void);
 CxTestSuite *cx_test_suite_string(void);
 CxTestSuite *cx_test_suite_printf(void);
 CxTestSuite *cx_test_suite_mempool(void);
@@ -47,6 +48,7 @@
             cx_test_suite_testing_allocator(),
             cx_test_suite_utils(),
             cx_test_suite_hash_key(),
+            cx_test_suite_allocator(),
             cx_test_suite_string(),
             cx_test_suite_printf(),
             cx_test_suite_mempool()

mercurial