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 |
--- a/tests/Makefile Fri Dec 29 17:27:14 2023 +0100 +++ b/tests/Makefile Sat Dec 30 14:11:20 2023 +0100 @@ -28,7 +28,7 @@ TEST_DIR=$(build_dir)/tests SRC = util_allocator.c test_utils.c test_hash_key.c test_string.c \ - test_printf.c ucxtest.c + test_printf.c test_mempool.c ucxtest.c OBJ_EXT=.o OBJ=$(SRC:%.c=$(TEST_DIR)/%$(OBJ_EXT)) @@ -60,6 +60,12 @@ @echo "Compiling $<" $(CC) -o $@ $(CFLAGS) -c $< +$(TEST_DIR)/test_mempool$(OBJ_EXT): test_mempool.c ../src/cx/test.h \ + util_allocator.h ../src/cx/allocator.h ../src/cx/common.h \ + ../src/cx/mempool.h ../src/cx/allocator.h + @echo "Compiling $<" + $(CC) -o $@ $(CFLAGS) -c $< + $(TEST_DIR)/test_printf$(OBJ_EXT): test_printf.c ../src/cx/test.h \ util_allocator.h ../src/cx/allocator.h ../src/cx/common.h \ ../src/cx/printf.h ../src/cx/string.h ../src/cx/allocator.h \
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_mempool.c Sat Dec 30 14:11:20 2023 +0100 @@ -0,0 +1,180 @@ +/* + * 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 "util_allocator.h" + +#include "cx/mempool.h" + +CX_TEST(test_mempool_create) { + CxMempool *pool = cxBasicMempoolCreate(16); + CX_TEST_DO { + CX_TEST_ASSERT(pool->auto_destr == NULL); + CX_TEST_ASSERT(pool->allocator != NULL); + CX_TEST_ASSERT(pool->allocator->cl != NULL); + CX_TEST_ASSERT(pool->allocator->data == pool); + CX_TEST_ASSERT(pool->allocator->cl->malloc != NULL); + CX_TEST_ASSERT(pool->allocator->cl->calloc != NULL); + CX_TEST_ASSERT(pool->allocator->cl->realloc != NULL); + CX_TEST_ASSERT(pool->allocator->cl->free != NULL); + CX_TEST_ASSERT(pool->capacity == 16); + CX_TEST_ASSERT(pool->size == 0); + CX_TEST_ASSERT(pool->data != NULL); + } + cxMempoolDestroy(pool); +} + +CX_TEST(test_mempool_malloc) { + CxMempool *pool = cxBasicMempoolCreate(4); + CX_TEST_DO { + CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL); + CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL); + CX_TEST_ASSERT(pool->size == 2); + CX_TEST_ASSERT(pool->capacity == 4); + CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL); + CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL); + CX_TEST_ASSERT(pool->size == 4); + CX_TEST_ASSERT(pool->capacity == 4); + CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL); + CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL); + CX_TEST_ASSERT(pool->size == 6); + CX_TEST_ASSERT(pool->capacity >= 6); + } + cxMempoolDestroy(pool); +} + +CX_TEST(test_mempool_calloc) { + CxMempool *pool = cxBasicMempoolCreate(4); + CX_TEST_DO { + int *test = cxCalloc(pool->allocator, 2, sizeof(int)); + CX_TEST_ASSERT(test != NULL); + CX_TEST_ASSERT(test[0] == 0); + CX_TEST_ASSERT(test[1] == 0); + } + cxMempoolDestroy(pool); +} + +static unsigned test_mempool_destructor_called; + +static void test_mempool_destructor(__attribute__((__unused__)) void *mem) { + test_mempool_destructor_called++; +} + +CX_TEST(test_mempool_realloc) { + CxMempool *pool = cxMempoolCreate(4, test_mempool_destructor); + CX_TEST_DO { + CX_TEST_ASSERT(pool->auto_destr == test_mempool_destructor); + int *data = cxMalloc(pool->allocator, sizeof(int)); + *data = 13; + + int *rdata = data; + unsigned n = 1; + while (rdata == data) { + n <<= 1; + // eventually the memory should be moved elsewhere + CX_TEST_ASSERTM(n < 65536, "Reallocation attempt failed - test not executable."); + rdata = cxRealloc(pool->allocator, data, n * sizeof(intptr_t)); + } + + CX_TEST_ASSERT(*rdata == 13); + // test if destructor is still intact + test_mempool_destructor_called = 0; + cxFree(pool->allocator, rdata); + CX_TEST_ASSERT(test_mempool_destructor_called == 1); + } + cxMempoolDestroy(pool); +} + + +CX_TEST(test_mempool_free) { + CxMempool *pool = cxBasicMempoolCreate(4); + void *mem1, *mem2; + CX_TEST_DO { + mem1 = cxMalloc(pool->allocator, 16); + cxFree(pool->allocator, mem1); + CX_TEST_ASSERT(pool->size == 0); + + cxMalloc(pool->allocator, 16); + cxMalloc(pool->allocator, 16); + mem1 = cxMalloc(pool->allocator, 16); + cxMalloc(pool->allocator, 16); + mem2 = cxMalloc(pool->allocator, 16); + + CX_TEST_ASSERT(pool->size == 5); + cxFree(pool->allocator, mem1); + CX_TEST_ASSERT(pool->size == 4); + cxFree(pool->allocator, mem2); + CX_TEST_ASSERT(pool->size == 3); + } + cxMempoolDestroy(pool); +} + +CX_TEST(test_mempool_destroy) { + CxMempool *pool = cxBasicMempoolCreate(4); + CX_TEST_DO { + int *data = cxMalloc(pool->allocator, sizeof(int)); + *data = 13; + cxMempoolSetDestructor(data, test_mempool_destructor); + CX_TEST_ASSERT(*data == 13); + test_mempool_destructor_called = 0; + cxFree(pool->allocator, data); + CX_TEST_ASSERT(test_mempool_destructor_called == 1); + data = cxMalloc(pool->allocator, sizeof(int)); + cxMempoolSetDestructor(data, test_mempool_destructor); + cxMempoolDestroy(pool); + CX_TEST_ASSERT(test_mempool_destructor_called == 2); + } +} + +CX_TEST(test_mempool_register) { + CxMempool *pool = cxBasicMempoolCreate(4); + CX_TEST_DO { + int *data = cxMalloc(pool->allocator, sizeof(int)); + test_mempool_destructor_called = 0; + cxMempoolSetDestructor(data, test_mempool_destructor); + int donotfree = 0; + cxMempoolRegister(pool, &donotfree, test_mempool_destructor); + cxMempoolDestroy(pool); + CX_TEST_ASSERT(test_mempool_destructor_called == 2); + } +} + + +CxTestSuite *cx_test_suite_mempool(void) { + CxTestSuite *suite = cx_test_suite_new("mempool"); + + cx_test_register(suite, test_mempool_create); + cx_test_register(suite, test_mempool_malloc); + cx_test_register(suite, test_mempool_calloc); + cx_test_register(suite, test_mempool_realloc); + cx_test_register(suite, test_mempool_free); + cx_test_register(suite, test_mempool_destroy); + cx_test_register(suite, test_mempool_register); + + return suite; +}
--- a/tests/test_mempool.cpp Fri Dec 29 17:27:14 2023 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,153 +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/mempool.h" -#include "util_allocator.h" -#include <gtest/gtest.h> - -TEST(Mempool, Create) { - auto pool = cxBasicMempoolCreate(16); - ASSERT_EQ(pool->auto_destr, nullptr); - ASSERT_NE(pool->allocator, nullptr); - ASSERT_NE(pool->allocator->cl, nullptr); - EXPECT_EQ(pool->allocator->data, pool); - EXPECT_NE(pool->allocator->cl->malloc, nullptr); - EXPECT_NE(pool->allocator->cl->calloc, nullptr); - EXPECT_NE(pool->allocator->cl->realloc, nullptr); - EXPECT_NE(pool->allocator->cl->free, nullptr); - EXPECT_EQ(pool->capacity, 16); - EXPECT_EQ(pool->size, 0); - EXPECT_NE(pool->data, nullptr); - cxMempoolDestroy(pool); -} - -TEST(Mempool, malloc) { - auto pool = cxBasicMempoolCreate(4); - EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); - EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); - EXPECT_EQ(pool->size, 2); - EXPECT_EQ(pool->capacity, 4); - EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); - EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); - EXPECT_EQ(pool->size, 4); - EXPECT_EQ(pool->capacity, 4); - EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); - EXPECT_NE(cxMalloc(pool->allocator, sizeof(int)), nullptr); - EXPECT_EQ(pool->size, 6); - EXPECT_GE(pool->capacity, 6); - cxMempoolDestroy(pool); -} - -TEST(Mempool, calloc) { - auto pool = cxBasicMempoolCreate(4); - - auto test = (int *) cxCalloc(pool->allocator, 2, sizeof(int)); - ASSERT_NE(test, nullptr); - EXPECT_EQ(test[0], 0); - EXPECT_EQ(test[1], 0); - cxMempoolDestroy(pool); -} - -static unsigned test_destructor_called; - -static void test_destructor([[maybe_unused]] void *mem) { - test_destructor_called++; -} - -TEST(Mempool, realloc) { - auto pool = cxMempoolCreate(4, test_destructor); - ASSERT_EQ(pool->auto_destr, test_destructor); - auto data = cxMalloc(pool->allocator, sizeof(int)); - *((int *) data) = 13; - - void *rdata = data; - unsigned n = 1; - while (rdata == data) { - n <<= 1; - ASSERT_LT(n, 65536); // eventually the memory should be moved elsewhere - rdata = cxRealloc(pool->allocator, data, n * sizeof(intptr_t)); - } - - EXPECT_EQ(*((int *) rdata), 13); - // test if destructor is still intact - test_destructor_called = 0; - cxFree(pool->allocator, rdata); - EXPECT_EQ(test_destructor_called, 1); - cxMempoolDestroy(pool); -} - - -TEST(Mempool, free) { - auto pool = cxBasicMempoolCreate(4); - - void *mem1; - void *mem2; - - mem1 = cxMalloc(pool->allocator, 16); - cxFree(pool->allocator, mem1); - EXPECT_EQ(pool->size, 0); - - cxMalloc(pool->allocator, 16); - cxMalloc(pool->allocator, 16); - mem1 = cxMalloc(pool->allocator, 16); - cxMalloc(pool->allocator, 16); - mem2 = cxMalloc(pool->allocator, 16); - - EXPECT_EQ(pool->size, 5); - cxFree(pool->allocator, mem1); - EXPECT_EQ(pool->size, 4); - cxFree(pool->allocator, mem2); - EXPECT_EQ(pool->size, 3); - cxMempoolDestroy(pool); -} - -TEST(Mempool, Destroy) { - auto pool = cxBasicMempoolCreate(4); - auto data = cxMalloc(pool->allocator, sizeof(int)); - *((int *) data) = 13; - cxMempoolSetDestructor(data, test_destructor); - EXPECT_EQ(*((int *) data), 13); - test_destructor_called = 0; - cxFree(pool->allocator, data); - EXPECT_EQ(test_destructor_called, 1); - data = cxMalloc(pool->allocator, sizeof(int)); - cxMempoolSetDestructor(data, test_destructor); - cxMempoolDestroy(pool); - EXPECT_EQ(test_destructor_called, 2); -} - -TEST(Mempool, Register) { - auto pool = cxBasicMempoolCreate(4); - auto data = cxMalloc(pool->allocator, sizeof(int)); - test_destructor_called = 0; - cxMempoolSetDestructor(data, test_destructor); - int donotfree = 0; - cxMempoolRegister(pool, &donotfree, test_destructor); - cxMempoolDestroy(pool); - EXPECT_EQ(test_destructor_called, 2); -}
--- a/tests/ucxtest.c Fri Dec 29 17:27:14 2023 +0100 +++ b/tests/ucxtest.c Sat Dec 30 14:11:20 2023 +0100 @@ -32,8 +32,8 @@ CxTestSuite *cx_test_suite_utils(void); CxTestSuite *cx_test_suite_hash_key(void); CxTestSuite *cx_test_suite_string(void); - CxTestSuite *cx_test_suite_printf(void); +CxTestSuite *cx_test_suite_mempool(void); #define run_tests(suite) cx_test_run_stdout(suite); success += (suite)->success; failure += (suite)->failure #define execute_test_suites(...) unsigned success = 0, failure = 0; CxTestSuite* test_suites[] = {__VA_ARGS__}; \ @@ -48,7 +48,8 @@ cx_test_suite_utils(), cx_test_suite_hash_key(), cx_test_suite_string(), - cx_test_suite_printf() + cx_test_suite_printf(), + cx_test_suite_mempool() ); printf("=== OVERALL RESULT ===\n"); printf(" Total: %u\n Success: %u\n Failure: %u\n",