tests/test_mempool.c

changeset 781
a786b0a89b37
child 785
bb18daa62d5f
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tests/test_mempool.c	Sat Dec 30 14:11:20 2023 +0100
     1.3 @@ -0,0 +1,180 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved.
     1.8 + *
     1.9 + * Redistribution and use in source and binary forms, with or without
    1.10 + * modification, are permitted provided that the following conditions are met:
    1.11 + *
    1.12 + *   1. Redistributions of source code must retain the above copyright
    1.13 + *      notice, this list of conditions and the following disclaimer.
    1.14 + *
    1.15 + *   2. Redistributions in binary form must reproduce the above copyright
    1.16 + *      notice, this list of conditions and the following disclaimer in the
    1.17 + *      documentation and/or other materials provided with the distribution.
    1.18 + *
    1.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    1.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    1.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    1.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    1.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    1.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    1.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    1.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    1.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    1.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    1.29 + * POSSIBILITY OF SUCH DAMAGE.
    1.30 + */
    1.31 +
    1.32 +#include "cx/test.h"
    1.33 +#include "util_allocator.h"
    1.34 +
    1.35 +#include "cx/mempool.h"
    1.36 +
    1.37 +CX_TEST(test_mempool_create) {
    1.38 +    CxMempool *pool = cxBasicMempoolCreate(16);
    1.39 +    CX_TEST_DO {
    1.40 +        CX_TEST_ASSERT(pool->auto_destr == NULL);
    1.41 +        CX_TEST_ASSERT(pool->allocator != NULL);
    1.42 +        CX_TEST_ASSERT(pool->allocator->cl != NULL);
    1.43 +        CX_TEST_ASSERT(pool->allocator->data == pool);
    1.44 +        CX_TEST_ASSERT(pool->allocator->cl->malloc != NULL);
    1.45 +        CX_TEST_ASSERT(pool->allocator->cl->calloc != NULL);
    1.46 +        CX_TEST_ASSERT(pool->allocator->cl->realloc != NULL);
    1.47 +        CX_TEST_ASSERT(pool->allocator->cl->free != NULL);
    1.48 +        CX_TEST_ASSERT(pool->capacity == 16);
    1.49 +        CX_TEST_ASSERT(pool->size == 0);
    1.50 +        CX_TEST_ASSERT(pool->data != NULL);
    1.51 +    }
    1.52 +    cxMempoolDestroy(pool);
    1.53 +}
    1.54 +
    1.55 +CX_TEST(test_mempool_malloc) {
    1.56 +    CxMempool *pool = cxBasicMempoolCreate(4);
    1.57 +    CX_TEST_DO {
    1.58 +        CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL);
    1.59 +        CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL);
    1.60 +        CX_TEST_ASSERT(pool->size == 2);
    1.61 +        CX_TEST_ASSERT(pool->capacity == 4);
    1.62 +        CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL);
    1.63 +        CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL);
    1.64 +        CX_TEST_ASSERT(pool->size == 4);
    1.65 +        CX_TEST_ASSERT(pool->capacity == 4);
    1.66 +        CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL);
    1.67 +        CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL);
    1.68 +        CX_TEST_ASSERT(pool->size == 6);
    1.69 +        CX_TEST_ASSERT(pool->capacity >= 6);
    1.70 +    }
    1.71 +    cxMempoolDestroy(pool);
    1.72 +}
    1.73 +
    1.74 +CX_TEST(test_mempool_calloc) {
    1.75 +    CxMempool *pool = cxBasicMempoolCreate(4);
    1.76 +    CX_TEST_DO {
    1.77 +        int *test = cxCalloc(pool->allocator, 2, sizeof(int));
    1.78 +        CX_TEST_ASSERT(test != NULL);
    1.79 +        CX_TEST_ASSERT(test[0] == 0);
    1.80 +        CX_TEST_ASSERT(test[1] == 0);
    1.81 +    }
    1.82 +    cxMempoolDestroy(pool);
    1.83 +}
    1.84 +
    1.85 +static unsigned test_mempool_destructor_called;
    1.86 +
    1.87 +static void test_mempool_destructor(__attribute__((__unused__)) void *mem) {
    1.88 +    test_mempool_destructor_called++;
    1.89 +}
    1.90 +
    1.91 +CX_TEST(test_mempool_realloc) {
    1.92 +    CxMempool *pool = cxMempoolCreate(4, test_mempool_destructor);
    1.93 +    CX_TEST_DO {
    1.94 +        CX_TEST_ASSERT(pool->auto_destr == test_mempool_destructor);
    1.95 +        int *data = cxMalloc(pool->allocator, sizeof(int));
    1.96 +        *data = 13;
    1.97 +
    1.98 +        int *rdata = data;
    1.99 +        unsigned n = 1;
   1.100 +        while (rdata == data) {
   1.101 +            n <<= 1;
   1.102 +            // eventually the memory should be moved elsewhere
   1.103 +            CX_TEST_ASSERTM(n < 65536, "Reallocation attempt failed - test not executable.");
   1.104 +            rdata = cxRealloc(pool->allocator, data, n * sizeof(intptr_t));
   1.105 +        }
   1.106 +
   1.107 +        CX_TEST_ASSERT(*rdata == 13);
   1.108 +        // test if destructor is still intact
   1.109 +        test_mempool_destructor_called = 0;
   1.110 +        cxFree(pool->allocator, rdata);
   1.111 +        CX_TEST_ASSERT(test_mempool_destructor_called == 1);
   1.112 +    }
   1.113 +    cxMempoolDestroy(pool);
   1.114 +}
   1.115 +
   1.116 +
   1.117 +CX_TEST(test_mempool_free) {
   1.118 +    CxMempool *pool = cxBasicMempoolCreate(4);
   1.119 +    void *mem1, *mem2;
   1.120 +    CX_TEST_DO {
   1.121 +        mem1 = cxMalloc(pool->allocator, 16);
   1.122 +        cxFree(pool->allocator, mem1);
   1.123 +        CX_TEST_ASSERT(pool->size == 0);
   1.124 +
   1.125 +        cxMalloc(pool->allocator, 16);
   1.126 +        cxMalloc(pool->allocator, 16);
   1.127 +        mem1 = cxMalloc(pool->allocator, 16);
   1.128 +        cxMalloc(pool->allocator, 16);
   1.129 +        mem2 = cxMalloc(pool->allocator, 16);
   1.130 +
   1.131 +        CX_TEST_ASSERT(pool->size == 5);
   1.132 +        cxFree(pool->allocator, mem1);
   1.133 +        CX_TEST_ASSERT(pool->size == 4);
   1.134 +        cxFree(pool->allocator, mem2);
   1.135 +        CX_TEST_ASSERT(pool->size == 3);
   1.136 +    }
   1.137 +    cxMempoolDestroy(pool);
   1.138 +}
   1.139 +
   1.140 +CX_TEST(test_mempool_destroy) {
   1.141 +    CxMempool *pool = cxBasicMempoolCreate(4);
   1.142 +    CX_TEST_DO {
   1.143 +        int *data = cxMalloc(pool->allocator, sizeof(int));
   1.144 +        *data = 13;
   1.145 +        cxMempoolSetDestructor(data, test_mempool_destructor);
   1.146 +        CX_TEST_ASSERT(*data == 13);
   1.147 +        test_mempool_destructor_called = 0;
   1.148 +        cxFree(pool->allocator, data);
   1.149 +        CX_TEST_ASSERT(test_mempool_destructor_called == 1);
   1.150 +        data = cxMalloc(pool->allocator, sizeof(int));
   1.151 +        cxMempoolSetDestructor(data, test_mempool_destructor);
   1.152 +        cxMempoolDestroy(pool);
   1.153 +        CX_TEST_ASSERT(test_mempool_destructor_called == 2);
   1.154 +    }
   1.155 +}
   1.156 +
   1.157 +CX_TEST(test_mempool_register) {
   1.158 +    CxMempool *pool = cxBasicMempoolCreate(4);
   1.159 +    CX_TEST_DO {
   1.160 +        int *data = cxMalloc(pool->allocator, sizeof(int));
   1.161 +        test_mempool_destructor_called = 0;
   1.162 +        cxMempoolSetDestructor(data, test_mempool_destructor);
   1.163 +        int donotfree = 0;
   1.164 +        cxMempoolRegister(pool, &donotfree, test_mempool_destructor);
   1.165 +        cxMempoolDestroy(pool);
   1.166 +        CX_TEST_ASSERT(test_mempool_destructor_called == 2);
   1.167 +    }
   1.168 +}
   1.169 +
   1.170 +
   1.171 +CxTestSuite *cx_test_suite_mempool(void) {
   1.172 +    CxTestSuite *suite = cx_test_suite_new("mempool");
   1.173 +
   1.174 +    cx_test_register(suite, test_mempool_create);
   1.175 +    cx_test_register(suite, test_mempool_malloc);
   1.176 +    cx_test_register(suite, test_mempool_calloc);
   1.177 +    cx_test_register(suite, test_mempool_realloc);
   1.178 +    cx_test_register(suite, test_mempool_free);
   1.179 +    cx_test_register(suite, test_mempool_destroy);
   1.180 +    cx_test_register(suite, test_mempool_register);
   1.181 +
   1.182 +    return suite;
   1.183 +}

mercurial