Sat, 30 Dec 2023 14:32:42 +0100
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 |
1.1 --- a/tests/Makefile Sat Dec 30 14:11:20 2023 +0100 1.2 +++ b/tests/Makefile Sat Dec 30 14:32:42 2023 +0100 1.3 @@ -27,8 +27,8 @@ 1.4 1.5 TEST_DIR=$(build_dir)/tests 1.6 1.7 -SRC = util_allocator.c test_utils.c test_hash_key.c test_string.c \ 1.8 - test_printf.c test_mempool.c ucxtest.c 1.9 +SRC = util_allocator.c test_utils.c test_hash_key.c test_allocator.c \ 1.10 + test_string.c test_printf.c test_mempool.c ucxtest.c 1.11 1.12 OBJ_EXT=.o 1.13 OBJ=$(SRC:%.c=$(TEST_DIR)/%$(OBJ_EXT)) 1.14 @@ -46,6 +46,11 @@ 1.15 1.16 FORCE: 1.17 1.18 +$(TEST_DIR)/test_allocator$(OBJ_EXT): test_allocator.c ../src/cx/test.h \ 1.19 + ../src/cx/allocator.h ../src/cx/common.h 1.20 + @echo "Compiling $<" 1.21 + $(CC) -o $@ $(CFLAGS) -c $< 1.22 + 1.23 $(TEST_DIR)/test_hash_key$(OBJ_EXT): test_hash_key.c ../src/cx/test.h \ 1.24 ../src/cx/hash_key.h ../src/cx/common.h ../src/cx/string.h \ 1.25 ../src/cx/allocator.h
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/tests/test_allocator.c Sat Dec 30 14:32:42 2023 +0100 2.3 @@ -0,0 +1,145 @@ 2.4 +/* 2.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 2.6 + * 2.7 + * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved. 2.8 + * 2.9 + * Redistribution and use in source and binary forms, with or without 2.10 + * modification, are permitted provided that the following conditions are met: 2.11 + * 2.12 + * 1. Redistributions of source code must retain the above copyright 2.13 + * notice, this list of conditions and the following disclaimer. 2.14 + * 2.15 + * 2. Redistributions in binary form must reproduce the above copyright 2.16 + * notice, this list of conditions and the following disclaimer in the 2.17 + * documentation and/or other materials provided with the distribution. 2.18 + * 2.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 2.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 2.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 2.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 2.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 2.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 2.29 + * POSSIBILITY OF SUCH DAMAGE. 2.30 + */ 2.31 + 2.32 +#include "cx/test.h" 2.33 + 2.34 +#include "cx/allocator.h" 2.35 + 2.36 +CX_TEST(test_allocator_default) { 2.37 + CX_TEST_DO { 2.38 + CX_TEST_ASSERT(cxDefaultAllocator->cl != NULL); 2.39 + } 2.40 +} 2.41 + 2.42 +CX_TEST(test_allocator_default_malloc) { 2.43 + void *test = cxMalloc(cxDefaultAllocator, 16); 2.44 + CX_TEST_DO { 2.45 + CX_TEST_ASSERT(test != NULL); 2.46 + // we cannot assert sth. but valgrind will detect an error 2.47 + memcpy(test, "0123456789ABCDEF", 16); 2.48 + } 2.49 + free(test); 2.50 +} 2.51 + 2.52 +CX_TEST(test_allocator_default_calloc) { 2.53 + char *test = cxCalloc(cxDefaultAllocator, 8, 2); 2.54 + CX_TEST_DO { 2.55 + CX_TEST_ASSERT(test != NULL); 2.56 + for (int i = 0; i < 16; i++) { 2.57 + CX_TEST_ASSERT(test[i] == 0); 2.58 + } 2.59 + } 2.60 + free(test); 2.61 +} 2.62 + 2.63 +CX_TEST(test_allocator_default_realloc) { 2.64 + char *test = calloc(8, 1); 2.65 + memcpy(test, "Test", 5); 2.66 + CX_TEST_DO { 2.67 + test = cxRealloc(cxDefaultAllocator, test, 16); 2.68 + CX_TEST_ASSERT(test != NULL); 2.69 + CX_TEST_ASSERT(0 == strcmp(test, "Test")); 2.70 + } 2.71 + free(test); 2.72 +} 2.73 + 2.74 +CX_TEST(test_allocator_default_free) { 2.75 + void *test = malloc(16); 2.76 + CX_TEST_DO { 2.77 + // we cannot assert sth. but valgrind will detect an error 2.78 + cxFree(cxDefaultAllocator, test); 2.79 + CX_TEST_ASSERT(true); 2.80 + } 2.81 +} 2.82 + 2.83 +CX_TEST(test_allocator_reallocate) { 2.84 + void *test = calloc(8, 1); 2.85 + memcpy(test, "Test", 5); 2.86 + CX_TEST_DO { 2.87 + int ret = cxReallocate(cxDefaultAllocator, &test, 16); 2.88 + CX_TEST_ASSERT(ret == 0); 2.89 + CX_TEST_ASSERT(test != NULL); 2.90 + CX_TEST_ASSERT(0 == strcmp(test, "Test")); 2.91 + } 2.92 + free(test); 2.93 +} 2.94 + 2.95 +CX_TEST(test_allocator_reallocate_low_level) { 2.96 + void *test = calloc(8, 1); 2.97 + memcpy(test, "Test", 5); 2.98 + CX_TEST_DO { 2.99 + int ret = cx_reallocate(&test, 16); 2.100 + CX_TEST_ASSERT(ret == 0); 2.101 + CX_TEST_ASSERT(test != NULL); 2.102 + CX_TEST_ASSERT(0 == strcmp(test, "Test")); 2.103 + } 2.104 + free(test); 2.105 +} 2.106 + 2.107 +static void *test_allocator_mock_failing_realloc( 2.108 + __attribute__((__unused__))void *p, 2.109 + __attribute__((__unused__))void *d, 2.110 + __attribute__((__unused__))size_t n 2.111 +) { 2.112 + return NULL; 2.113 +} 2.114 + 2.115 +CX_TEST(test_allocator_reallocate_fails) { 2.116 + // Mock an allocator that always returns NULL on realloc 2.117 + cx_allocator_class mock_cl; 2.118 + mock_cl.realloc = test_allocator_mock_failing_realloc; 2.119 + CxAllocator mock = {&mock_cl, NULL}; 2.120 + 2.121 + void *test = calloc(8, 1); 2.122 + memcpy(test, "Test", 5); 2.123 + void *original = test; 2.124 + CX_TEST_DO { 2.125 + int ret = cxReallocate(&mock, &test, 16); 2.126 + // non-zero return code because of the failure 2.127 + CX_TEST_ASSERT(ret != 0); 2.128 + // the test pointer was not changed and still points to the same memory 2.129 + CX_TEST_ASSERT(test == original); 2.130 + CX_TEST_ASSERT(0 == strcmp(test, "Test")); 2.131 + } 2.132 + free(test); 2.133 +} 2.134 + 2.135 +CxTestSuite *cx_test_suite_allocator(void) { 2.136 + CxTestSuite *suite = cx_test_suite_new("allocator"); 2.137 + 2.138 + cx_test_register(suite, test_allocator_default); 2.139 + cx_test_register(suite, test_allocator_default_malloc); 2.140 + cx_test_register(suite, test_allocator_default_calloc); 2.141 + cx_test_register(suite, test_allocator_default_realloc); 2.142 + cx_test_register(suite, test_allocator_default_free); 2.143 + cx_test_register(suite, test_allocator_reallocate); 2.144 + cx_test_register(suite, test_allocator_reallocate_fails); 2.145 + cx_test_register(suite, test_allocator_reallocate_low_level); 2.146 + 2.147 + return suite; 2.148 +}
3.1 --- a/tests/test_allocator.cpp Sat Dec 30 14:11:20 2023 +0100 3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 3.3 @@ -1,96 +0,0 @@ 3.4 -/* 3.5 - * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 3.6 - * 3.7 - * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved. 3.8 - * 3.9 - * Redistribution and use in source and binary forms, with or without 3.10 - * modification, are permitted provided that the following conditions are met: 3.11 - * 3.12 - * 1. Redistributions of source code must retain the above copyright 3.13 - * notice, this list of conditions and the following disclaimer. 3.14 - * 3.15 - * 2. Redistributions in binary form must reproduce the above copyright 3.16 - * notice, this list of conditions and the following disclaimer in the 3.17 - * documentation and/or other materials provided with the distribution. 3.18 - * 3.19 - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 3.20 - * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 3.21 - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 3.22 - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 3.23 - * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 3.24 - * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 3.25 - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 3.26 - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 3.27 - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 3.28 - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 3.29 - * POSSIBILITY OF SUCH DAMAGE. 3.30 - */ 3.31 - 3.32 -#include "cx/allocator.h" 3.33 -#include <gtest/gtest.h> 3.34 - 3.35 -TEST(Allocator, DefaultAllocator) { 3.36 - cx_allocator_class *clazz = cxDefaultAllocator->cl; 3.37 - ASSERT_NE(clazz, nullptr); 3.38 -} 3.39 - 3.40 -TEST(Allocator, DefaultMalloc) { 3.41 - void *test = cxMalloc(cxDefaultAllocator, 16); 3.42 - ASSERT_NE(test, nullptr); 3.43 - free(test); 3.44 -} 3.45 - 3.46 -TEST(Allocator, DefaultRealloc) { 3.47 - void *test = calloc(8, 1); 3.48 - memcpy(test, "Test", 5); 3.49 - test = cxRealloc(cxDefaultAllocator, test, 16); 3.50 - ASSERT_NE(test, nullptr); 3.51 - EXPECT_STREQ(reinterpret_cast<char *>(test), "Test"); 3.52 - free(test); 3.53 -} 3.54 - 3.55 -TEST(Allocator, Reallocate) { 3.56 - void *test = calloc(8, 1); 3.57 - memcpy(test, "Test", 5); 3.58 - int ret = cxReallocate(cxDefaultAllocator, &test, 16); 3.59 - EXPECT_EQ(ret, 0); 3.60 - ASSERT_NE(test, nullptr); 3.61 - EXPECT_STREQ(reinterpret_cast<char *>(test), "Test"); 3.62 - free(test); 3.63 -} 3.64 - 3.65 -TEST(Allocator, DefaultCalloc) { 3.66 - char *test = reinterpret_cast<char *>(cxCalloc(cxDefaultAllocator, 8, 2)); 3.67 - ASSERT_NE(test, nullptr); 3.68 - for (int i = 0; i < 16; i++) ASSERT_EQ(test[i], 0); 3.69 - free(test); 3.70 -} 3.71 - 3.72 -TEST(Allocator, DefaultFree) { 3.73 - void *test = malloc(16); 3.74 - EXPECT_NO_FATAL_FAILURE( 3.75 - cxFree(cxDefaultAllocator, test); 3.76 - ); 3.77 -} 3.78 - 3.79 -TEST(Allocator, FailingReallocate) { 3.80 - // Mock an allocator that always returns nullptr on realloc 3.81 - cx_allocator_class mock_cl; 3.82 - mock_cl.realloc = []( 3.83 - [[maybe_unused]]void *p, 3.84 - [[maybe_unused]]void *d, 3.85 - [[maybe_unused]]size_t n 3.86 - ) -> void * { return nullptr; }; 3.87 - cx_allocator_s mock{&mock_cl, nullptr}; 3.88 - 3.89 - void *test = calloc(8, 1); 3.90 - memcpy(test, "Test", 5); 3.91 - void *original = test; 3.92 - int ret = cxReallocate(&mock, &test, 16); 3.93 - // non-zero return code because of the failure 3.94 - EXPECT_NE(ret, 0); 3.95 - // the test pointer was not changed and still points to the same memory 3.96 - EXPECT_EQ(test, original); 3.97 - EXPECT_STREQ(reinterpret_cast<char *>(test), "Test"); 3.98 - free(test); 3.99 -}
4.1 --- a/tests/ucxtest.c Sat Dec 30 14:11:20 2023 +0100 4.2 +++ b/tests/ucxtest.c Sat Dec 30 14:32:42 2023 +0100 4.3 @@ -31,6 +31,7 @@ 4.4 CxTestSuite *cx_test_suite_testing_allocator(void); 4.5 CxTestSuite *cx_test_suite_utils(void); 4.6 CxTestSuite *cx_test_suite_hash_key(void); 4.7 +CxTestSuite *cx_test_suite_allocator(void); 4.8 CxTestSuite *cx_test_suite_string(void); 4.9 CxTestSuite *cx_test_suite_printf(void); 4.10 CxTestSuite *cx_test_suite_mempool(void); 4.11 @@ -47,6 +48,7 @@ 4.12 cx_test_suite_testing_allocator(), 4.13 cx_test_suite_utils(), 4.14 cx_test_suite_hash_key(), 4.15 + cx_test_suite_allocator(), 4.16 cx_test_suite_string(), 4.17 cx_test_suite_printf(), 4.18 cx_test_suite_mempool()