Sat, 30 Dec 2023 14:11:20 +0100
migrate mempool tests - relates to #342
tests/Makefile | file | annotate | diff | comparison | revisions | |
tests/test_mempool.c | file | annotate | diff | comparison | revisions | |
tests/test_mempool.cpp | file | annotate | diff | comparison | revisions | |
tests/ucxtest.c | file | annotate | diff | comparison | revisions |
1.1 --- a/tests/Makefile Fri Dec 29 17:27:14 2023 +0100 1.2 +++ b/tests/Makefile Sat Dec 30 14:11:20 2023 +0100 1.3 @@ -28,7 +28,7 @@ 1.4 TEST_DIR=$(build_dir)/tests 1.5 1.6 SRC = util_allocator.c test_utils.c test_hash_key.c test_string.c \ 1.7 - test_printf.c ucxtest.c 1.8 + test_printf.c test_mempool.c ucxtest.c 1.9 1.10 OBJ_EXT=.o 1.11 OBJ=$(SRC:%.c=$(TEST_DIR)/%$(OBJ_EXT)) 1.12 @@ -60,6 +60,12 @@ 1.13 @echo "Compiling $<" 1.14 $(CC) -o $@ $(CFLAGS) -c $< 1.15 1.16 +$(TEST_DIR)/test_mempool$(OBJ_EXT): test_mempool.c ../src/cx/test.h \ 1.17 + util_allocator.h ../src/cx/allocator.h ../src/cx/common.h \ 1.18 + ../src/cx/mempool.h ../src/cx/allocator.h 1.19 + @echo "Compiling $<" 1.20 + $(CC) -o $@ $(CFLAGS) -c $< 1.21 + 1.22 $(TEST_DIR)/test_printf$(OBJ_EXT): test_printf.c ../src/cx/test.h \ 1.23 util_allocator.h ../src/cx/allocator.h ../src/cx/common.h \ 1.24 ../src/cx/printf.h ../src/cx/string.h ../src/cx/allocator.h \
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/tests/test_mempool.c Sat Dec 30 14:11:20 2023 +0100 2.3 @@ -0,0 +1,180 @@ 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 +#include "util_allocator.h" 2.34 + 2.35 +#include "cx/mempool.h" 2.36 + 2.37 +CX_TEST(test_mempool_create) { 2.38 + CxMempool *pool = cxBasicMempoolCreate(16); 2.39 + CX_TEST_DO { 2.40 + CX_TEST_ASSERT(pool->auto_destr == NULL); 2.41 + CX_TEST_ASSERT(pool->allocator != NULL); 2.42 + CX_TEST_ASSERT(pool->allocator->cl != NULL); 2.43 + CX_TEST_ASSERT(pool->allocator->data == pool); 2.44 + CX_TEST_ASSERT(pool->allocator->cl->malloc != NULL); 2.45 + CX_TEST_ASSERT(pool->allocator->cl->calloc != NULL); 2.46 + CX_TEST_ASSERT(pool->allocator->cl->realloc != NULL); 2.47 + CX_TEST_ASSERT(pool->allocator->cl->free != NULL); 2.48 + CX_TEST_ASSERT(pool->capacity == 16); 2.49 + CX_TEST_ASSERT(pool->size == 0); 2.50 + CX_TEST_ASSERT(pool->data != NULL); 2.51 + } 2.52 + cxMempoolDestroy(pool); 2.53 +} 2.54 + 2.55 +CX_TEST(test_mempool_malloc) { 2.56 + CxMempool *pool = cxBasicMempoolCreate(4); 2.57 + CX_TEST_DO { 2.58 + CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL); 2.59 + CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL); 2.60 + CX_TEST_ASSERT(pool->size == 2); 2.61 + CX_TEST_ASSERT(pool->capacity == 4); 2.62 + CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL); 2.63 + CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL); 2.64 + CX_TEST_ASSERT(pool->size == 4); 2.65 + CX_TEST_ASSERT(pool->capacity == 4); 2.66 + CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL); 2.67 + CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL); 2.68 + CX_TEST_ASSERT(pool->size == 6); 2.69 + CX_TEST_ASSERT(pool->capacity >= 6); 2.70 + } 2.71 + cxMempoolDestroy(pool); 2.72 +} 2.73 + 2.74 +CX_TEST(test_mempool_calloc) { 2.75 + CxMempool *pool = cxBasicMempoolCreate(4); 2.76 + CX_TEST_DO { 2.77 + int *test = cxCalloc(pool->allocator, 2, sizeof(int)); 2.78 + CX_TEST_ASSERT(test != NULL); 2.79 + CX_TEST_ASSERT(test[0] == 0); 2.80 + CX_TEST_ASSERT(test[1] == 0); 2.81 + } 2.82 + cxMempoolDestroy(pool); 2.83 +} 2.84 + 2.85 +static unsigned test_mempool_destructor_called; 2.86 + 2.87 +static void test_mempool_destructor(__attribute__((__unused__)) void *mem) { 2.88 + test_mempool_destructor_called++; 2.89 +} 2.90 + 2.91 +CX_TEST(test_mempool_realloc) { 2.92 + CxMempool *pool = cxMempoolCreate(4, test_mempool_destructor); 2.93 + CX_TEST_DO { 2.94 + CX_TEST_ASSERT(pool->auto_destr == test_mempool_destructor); 2.95 + int *data = cxMalloc(pool->allocator, sizeof(int)); 2.96 + *data = 13; 2.97 + 2.98 + int *rdata = data; 2.99 + unsigned n = 1; 2.100 + while (rdata == data) { 2.101 + n <<= 1; 2.102 + // eventually the memory should be moved elsewhere 2.103 + CX_TEST_ASSERTM(n < 65536, "Reallocation attempt failed - test not executable."); 2.104 + rdata = cxRealloc(pool->allocator, data, n * sizeof(intptr_t)); 2.105 + } 2.106 + 2.107 + CX_TEST_ASSERT(*rdata == 13); 2.108 + // test if destructor is still intact 2.109 + test_mempool_destructor_called = 0; 2.110 + cxFree(pool->allocator, rdata); 2.111 + CX_TEST_ASSERT(test_mempool_destructor_called == 1); 2.112 + } 2.113 + cxMempoolDestroy(pool); 2.114 +} 2.115 + 2.116 + 2.117 +CX_TEST(test_mempool_free) { 2.118 + CxMempool *pool = cxBasicMempoolCreate(4); 2.119 + void *mem1, *mem2; 2.120 + CX_TEST_DO { 2.121 + mem1 = cxMalloc(pool->allocator, 16); 2.122 + cxFree(pool->allocator, mem1); 2.123 + CX_TEST_ASSERT(pool->size == 0); 2.124 + 2.125 + cxMalloc(pool->allocator, 16); 2.126 + cxMalloc(pool->allocator, 16); 2.127 + mem1 = cxMalloc(pool->allocator, 16); 2.128 + cxMalloc(pool->allocator, 16); 2.129 + mem2 = cxMalloc(pool->allocator, 16); 2.130 + 2.131 + CX_TEST_ASSERT(pool->size == 5); 2.132 + cxFree(pool->allocator, mem1); 2.133 + CX_TEST_ASSERT(pool->size == 4); 2.134 + cxFree(pool->allocator, mem2); 2.135 + CX_TEST_ASSERT(pool->size == 3); 2.136 + } 2.137 + cxMempoolDestroy(pool); 2.138 +} 2.139 + 2.140 +CX_TEST(test_mempool_destroy) { 2.141 + CxMempool *pool = cxBasicMempoolCreate(4); 2.142 + CX_TEST_DO { 2.143 + int *data = cxMalloc(pool->allocator, sizeof(int)); 2.144 + *data = 13; 2.145 + cxMempoolSetDestructor(data, test_mempool_destructor); 2.146 + CX_TEST_ASSERT(*data == 13); 2.147 + test_mempool_destructor_called = 0; 2.148 + cxFree(pool->allocator, data); 2.149 + CX_TEST_ASSERT(test_mempool_destructor_called == 1); 2.150 + data = cxMalloc(pool->allocator, sizeof(int)); 2.151 + cxMempoolSetDestructor(data, test_mempool_destructor); 2.152 + cxMempoolDestroy(pool); 2.153 + CX_TEST_ASSERT(test_mempool_destructor_called == 2); 2.154 + } 2.155 +} 2.156 + 2.157 +CX_TEST(test_mempool_register) { 2.158 + CxMempool *pool = cxBasicMempoolCreate(4); 2.159 + CX_TEST_DO { 2.160 + int *data = cxMalloc(pool->allocator, sizeof(int)); 2.161 + test_mempool_destructor_called = 0; 2.162 + cxMempoolSetDestructor(data, test_mempool_destructor); 2.163 + int donotfree = 0; 2.164 + cxMempoolRegister(pool, &donotfree, test_mempool_destructor); 2.165 + cxMempoolDestroy(pool); 2.166 + CX_TEST_ASSERT(test_mempool_destructor_called == 2); 2.167 + } 2.168 +} 2.169 + 2.170 + 2.171 +CxTestSuite *cx_test_suite_mempool(void) { 2.172 + CxTestSuite *suite = cx_test_suite_new("mempool"); 2.173 + 2.174 + cx_test_register(suite, test_mempool_create); 2.175 + cx_test_register(suite, test_mempool_malloc); 2.176 + cx_test_register(suite, test_mempool_calloc); 2.177 + cx_test_register(suite, test_mempool_realloc); 2.178 + cx_test_register(suite, test_mempool_free); 2.179 + cx_test_register(suite, test_mempool_destroy); 2.180 + cx_test_register(suite, test_mempool_register); 2.181 + 2.182 + return suite; 2.183 +}
3.1 --- a/tests/test_mempool.cpp Fri Dec 29 17:27:14 2023 +0100 3.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 3.3 @@ -1,153 +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/mempool.h" 3.33 -#include "util_allocator.h" 3.34 -#include <gtest/gtest.h> 3.35 - 3.36 -TEST(Mempool, Create) { 3.37 - auto pool = cxBasicMempoolCreate(16); 3.38 - ASSERT_EQ(pool->auto_destr, nullptr); 3.39 - ASSERT_NE(pool->allocator, nullptr); 3.40 - ASSERT_NE(pool->allocator->cl, nullptr); 3.41 - EXPECT_EQ(pool->allocator->data, pool); 3.42 - EXPECT_NE(pool->allocator->cl->malloc, nullptr); 3.43 - EXPECT_NE(pool->allocator->cl->calloc, nullptr); 3.44 - EXPECT_NE(pool->allocator->cl->realloc, nullptr); 3.45 - EXPECT_NE(pool->allocator->cl->free, nullptr); 3.46 - EXPECT_EQ(pool->capacity, 16); 3.47 - EXPECT_EQ(pool->size, 0); 3.48 - EXPECT_NE(pool->data, nullptr); 3.49 - cxMempoolDestroy(pool); 3.50 -} 3.51 - 3.52 -TEST(Mempool, malloc) { 3.53 - auto pool = cxBasicMempoolCreate(4); 3.54 - EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); 3.55 - EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); 3.56 - EXPECT_EQ(pool->size, 2); 3.57 - EXPECT_EQ(pool->capacity, 4); 3.58 - EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); 3.59 - EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); 3.60 - EXPECT_EQ(pool->size, 4); 3.61 - EXPECT_EQ(pool->capacity, 4); 3.62 - EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); 3.63 - EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); 3.64 - EXPECT_EQ(pool->size, 6); 3.65 - EXPECT_GE(pool->capacity, 6); 3.66 - cxMempoolDestroy(pool); 3.67 -} 3.68 - 3.69 -TEST(Mempool, calloc) { 3.70 - auto pool = cxBasicMempoolCreate(4); 3.71 - 3.72 - auto test = (int *) cxCalloc(pool->allocator, 2, sizeof(int)); 3.73 - ASSERT_NE(test, nullptr); 3.74 - EXPECT_EQ(test[0], 0); 3.75 - EXPECT_EQ(test[1], 0); 3.76 - cxMempoolDestroy(pool); 3.77 -} 3.78 - 3.79 -static unsigned test_destructor_called; 3.80 - 3.81 -static void test_destructor([[maybe_unused]] void *mem) { 3.82 - test_destructor_called++; 3.83 -} 3.84 - 3.85 -TEST(Mempool, realloc) { 3.86 - auto pool = cxMempoolCreate(4, test_destructor); 3.87 - ASSERT_EQ(pool->auto_destr, test_destructor); 3.88 - auto data = cxMalloc(pool->allocator, sizeof(int)); 3.89 - *((int *) data) = 13; 3.90 - 3.91 - void *rdata = data; 3.92 - unsigned n = 1; 3.93 - while (rdata == data) { 3.94 - n <<= 1; 3.95 - ASSERT_LT(n, 65536); // eventually the memory should be moved elsewhere 3.96 - rdata = cxRealloc(pool->allocator, data, n * sizeof(intptr_t)); 3.97 - } 3.98 - 3.99 - EXPECT_EQ(*((int *) rdata), 13); 3.100 - // test if destructor is still intact 3.101 - test_destructor_called = 0; 3.102 - cxFree(pool->allocator, rdata); 3.103 - EXPECT_EQ(test_destructor_called, 1); 3.104 - cxMempoolDestroy(pool); 3.105 -} 3.106 - 3.107 - 3.108 -TEST(Mempool, free) { 3.109 - auto pool = cxBasicMempoolCreate(4); 3.110 - 3.111 - void *mem1; 3.112 - void *mem2; 3.113 - 3.114 - mem1 = cxMalloc(pool->allocator, 16); 3.115 - cxFree(pool->allocator, mem1); 3.116 - EXPECT_EQ(pool->size, 0); 3.117 - 3.118 - cxMalloc(pool->allocator, 16); 3.119 - cxMalloc(pool->allocator, 16); 3.120 - mem1 = cxMalloc(pool->allocator, 16); 3.121 - cxMalloc(pool->allocator, 16); 3.122 - mem2 = cxMalloc(pool->allocator, 16); 3.123 - 3.124 - EXPECT_EQ(pool->size, 5); 3.125 - cxFree(pool->allocator, mem1); 3.126 - EXPECT_EQ(pool->size, 4); 3.127 - cxFree(pool->allocator, mem2); 3.128 - EXPECT_EQ(pool->size, 3); 3.129 - cxMempoolDestroy(pool); 3.130 -} 3.131 - 3.132 -TEST(Mempool, Destroy) { 3.133 - auto pool = cxBasicMempoolCreate(4); 3.134 - auto data = cxMalloc(pool->allocator, sizeof(int)); 3.135 - *((int *) data) = 13; 3.136 - cxMempoolSetDestructor(data, test_destructor); 3.137 - EXPECT_EQ(*((int *) data), 13); 3.138 - test_destructor_called = 0; 3.139 - cxFree(pool->allocator, data); 3.140 - EXPECT_EQ(test_destructor_called, 1); 3.141 - data = cxMalloc(pool->allocator, sizeof(int)); 3.142 - cxMempoolSetDestructor(data, test_destructor); 3.143 - cxMempoolDestroy(pool); 3.144 - EXPECT_EQ(test_destructor_called, 2); 3.145 -} 3.146 - 3.147 -TEST(Mempool, Register) { 3.148 - auto pool = cxBasicMempoolCreate(4); 3.149 - auto data = cxMalloc(pool->allocator, sizeof(int)); 3.150 - test_destructor_called = 0; 3.151 - cxMempoolSetDestructor(data, test_destructor); 3.152 - int donotfree = 0; 3.153 - cxMempoolRegister(pool, &donotfree, test_destructor); 3.154 - cxMempoolDestroy(pool); 3.155 - EXPECT_EQ(test_destructor_called, 2); 3.156 -}
4.1 --- a/tests/ucxtest.c Fri Dec 29 17:27:14 2023 +0100 4.2 +++ b/tests/ucxtest.c Sat Dec 30 14:11:20 2023 +0100 4.3 @@ -32,8 +32,8 @@ 4.4 CxTestSuite *cx_test_suite_utils(void); 4.5 CxTestSuite *cx_test_suite_hash_key(void); 4.6 CxTestSuite *cx_test_suite_string(void); 4.7 - 4.8 CxTestSuite *cx_test_suite_printf(void); 4.9 +CxTestSuite *cx_test_suite_mempool(void); 4.10 4.11 #define run_tests(suite) cx_test_run_stdout(suite); success += (suite)->success; failure += (suite)->failure 4.12 #define execute_test_suites(...) unsigned success = 0, failure = 0; CxTestSuite* test_suites[] = {__VA_ARGS__}; \ 4.13 @@ -48,7 +48,8 @@ 4.14 cx_test_suite_utils(), 4.15 cx_test_suite_hash_key(), 4.16 cx_test_suite_string(), 4.17 - cx_test_suite_printf() 4.18 + cx_test_suite_printf(), 4.19 + cx_test_suite_mempool() 4.20 ); 4.21 printf("=== OVERALL RESULT ===\n"); 4.22 printf(" Total: %u\n Success: %u\n Failure: %u\n",