Fri, 23 May 2025 12:44:24 +0200
make test-compile depend on both static and shared
the shared lib is not needed for the tests,
but when run with coverage, gcov will be confused
when outdated line information is available from
a previous shared build
/* * 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 = cxMempoolCreateSimple(16); CX_TEST_DO { CX_TEST_ASSERT(pool->destr == NULL); CX_TEST_ASSERT(pool->destr2 == NULL); CX_TEST_ASSERT(pool->destr2_data == 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); } cxMempoolFree(pool); } CX_TEST(test_mempool_malloc) { CxMempool *pool = cxMempoolCreateSimple(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); } cxMempoolFree(pool); } CX_TEST(test_mempool_calloc) { CxMempool *pool = cxMempoolCreateSimple(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); } cxMempoolFree(pool); } static unsigned test_mempool_destructor_called; static void test_mempool_destructor(cx_attr_unused void *mem) { test_mempool_destructor_called++; } CX_TEST(test_mempool_realloc) { CxMempool *pool = cxMempoolCreateSimple(4); cxMempoolGlobalDestructor(pool, test_mempool_destructor); CX_TEST_DO { CX_TEST_ASSERT(pool->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); } cxMempoolFree(pool); } CX_TEST(test_mempool_free) { CxMempool *pool = cxMempoolCreateSimple(4); void *mem1, *mem2; CX_TEST_DO { mem1 = cxMalloc(pool->allocator, 16); cxFree(pool->allocator, mem1); CX_TEST_ASSERT(pool->size == 0); mem1 = cxMalloc(pool->allocator, 16); mem1 = cxMalloc(pool->allocator, 16); mem1 = cxMalloc(pool->allocator, 16); mem2 = 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); } cxMempoolFree(pool); } CX_TEST(test_mempool_destroy) { CxMempool *pool = cxMempoolCreateSimple(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); cxMempoolFree(pool); CX_TEST_ASSERT(test_mempool_destructor_called == 2); } } CX_TEST(test_mempool_register) { CxMempool *pool = cxMempoolCreateSimple(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); cxMempoolFree(pool); CX_TEST_ASSERT(test_mempool_destructor_called == 2); } } CX_TEST(test_mempool_transfer) { CxMempool *src = cxMempoolCreateSimple(4); CxMempool *dest = cxMempoolCreateSimple(4); CX_TEST_DO { // allocate the first object int *c = cxMalloc(src->allocator, sizeof(int)); // allocate the second object c = cxMalloc(src->allocator, sizeof(int)); // check that the destructor functions are also transferred cxMempoolSetDestructor(c, test_mempool_destructor); // register foreign object c = malloc(sizeof(int)); cxMempoolRegister(src, c, test_mempool_destructor); // check source pool CX_TEST_ASSERT(src->size == 2); CX_TEST_ASSERT(src->registered_size == 1); const CxAllocator *old_allocator = src->allocator; CX_TEST_ASSERT(old_allocator->data == src); // perform transfer int result = cxMempoolTransfer(src, dest); CX_TEST_ASSERT(result == 0); // check transfer CX_TEST_ASSERT(src->size == 0); CX_TEST_ASSERT(dest->size == 2); CX_TEST_ASSERT(src->registered_size == 0); CX_TEST_ASSERT(dest->registered_size == 2); // 1 object + old allocator CX_TEST_ASSERT(src->allocator != old_allocator); CX_TEST_ASSERT(old_allocator->data == dest); // verify that destroying old pool does nothing test_mempool_destructor_called = 0; cxMempoolFree(src); CX_TEST_ASSERT(test_mempool_destructor_called == 0); // cover illegal arguments result = cxMempoolTransfer(dest, dest); CX_TEST_ASSERT(result != 0); // verify that destroying new pool calls the destructors // but only two times (the old allocator has a different destructor) cxMempoolFree(dest); CX_TEST_ASSERT(test_mempool_destructor_called == 2); // free the foreign object free(c); } } CX_TEST(test_mempool_transfer_object) { CxMempool *src = cxMempoolCreateSimple(4); CxMempool *dest = cxMempoolCreateSimple(4); CX_TEST_DO { int *b = cxMalloc(src->allocator, sizeof(int)); b = cxMalloc(src->allocator, sizeof(int)); int *c = malloc(sizeof(int)); cxMempoolRegister(src, c, free); CX_TEST_ASSERT(src->size == 2); CX_TEST_ASSERT(src->registered_size == 1); int result = cxMempoolTransferObject(src, dest, b); CX_TEST_ASSERT(result == 0); CX_TEST_ASSERT(src->size == 1); CX_TEST_ASSERT(dest->size == 1); result = cxMempoolTransferObject(src, dest, b); CX_TEST_ASSERT(result != 0); CX_TEST_ASSERT(src->size == 1); CX_TEST_ASSERT(dest->size == 1); // can also transfer foreign memory this way CX_TEST_ASSERT(src->registered_size == 1); CX_TEST_ASSERT(dest->registered_size == 0); result = cxMempoolTransferObject(src, dest, c); CX_TEST_ASSERT(result == 0); CX_TEST_ASSERT(src->registered_size == 0); CX_TEST_ASSERT(dest->registered_size == 1); result = cxMempoolTransferObject(dest, dest, b); CX_TEST_ASSERT(result != 0); CX_TEST_ASSERT(src->size == 1); CX_TEST_ASSERT(dest->size == 1); } cxMempoolFree(src); cxMempoolFree(dest); // let valgrind check that everything worked } 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); cx_test_register(suite, test_mempool_transfer); cx_test_register(suite, test_mempool_transfer_object); return suite; }