tests/test_mempool.c

Sat, 30 Dec 2023 18:48:25 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 30 Dec 2023 18:48:25 +0100
changeset 785
bb18daa62d5f
parent 781
a786b0a89b37
permissions
-rw-r--r--

migrate map tests - relates to #342

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2023 Mike Becker, Olaf Wintermann All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  */
    29 #include "cx/test.h"
    30 #include "util_allocator.h"
    32 #include "cx/mempool.h"
    34 CX_TEST(test_mempool_create) {
    35     CxMempool *pool = cxBasicMempoolCreate(16);
    36     CX_TEST_DO {
    37         CX_TEST_ASSERT(pool->auto_destr == NULL);
    38         CX_TEST_ASSERT(pool->allocator != NULL);
    39         CX_TEST_ASSERT(pool->allocator->cl != NULL);
    40         CX_TEST_ASSERT(pool->allocator->data == pool);
    41         CX_TEST_ASSERT(pool->allocator->cl->malloc != NULL);
    42         CX_TEST_ASSERT(pool->allocator->cl->calloc != NULL);
    43         CX_TEST_ASSERT(pool->allocator->cl->realloc != NULL);
    44         CX_TEST_ASSERT(pool->allocator->cl->free != NULL);
    45         CX_TEST_ASSERT(pool->capacity == 16);
    46         CX_TEST_ASSERT(pool->size == 0);
    47         CX_TEST_ASSERT(pool->data != NULL);
    48     }
    49     cxMempoolDestroy(pool);
    50 }
    52 CX_TEST(test_mempool_malloc) {
    53     CxMempool *pool = cxBasicMempoolCreate(4);
    54     CX_TEST_DO {
    55         CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL);
    56         CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL);
    57         CX_TEST_ASSERT(pool->size == 2);
    58         CX_TEST_ASSERT(pool->capacity == 4);
    59         CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL);
    60         CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL);
    61         CX_TEST_ASSERT(pool->size == 4);
    62         CX_TEST_ASSERT(pool->capacity == 4);
    63         CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL);
    64         CX_TEST_ASSERT(cxMalloc(pool->allocator, sizeof(int)) != NULL);
    65         CX_TEST_ASSERT(pool->size == 6);
    66         CX_TEST_ASSERT(pool->capacity >= 6);
    67     }
    68     cxMempoolDestroy(pool);
    69 }
    71 CX_TEST(test_mempool_calloc) {
    72     CxMempool *pool = cxBasicMempoolCreate(4);
    73     CX_TEST_DO {
    74         int *test = cxCalloc(pool->allocator, 2, sizeof(int));
    75         CX_TEST_ASSERT(test != NULL);
    76         CX_TEST_ASSERT(test[0] == 0);
    77         CX_TEST_ASSERT(test[1] == 0);
    78     }
    79     cxMempoolDestroy(pool);
    80 }
    82 static unsigned test_mempool_destructor_called;
    84 static void test_mempool_destructor(__attribute__((__unused__)) void *mem) {
    85     test_mempool_destructor_called++;
    86 }
    88 CX_TEST(test_mempool_realloc) {
    89     CxMempool *pool = cxMempoolCreate(4, test_mempool_destructor);
    90     CX_TEST_DO {
    91         CX_TEST_ASSERT(pool->auto_destr == test_mempool_destructor);
    92         int *data = cxMalloc(pool->allocator, sizeof(int));
    93         *data = 13;
    95         int *rdata = data;
    96         unsigned n = 1;
    97         while (rdata == data) {
    98             n <<= 1;
    99             // eventually the memory should be moved elsewhere
   100             CX_TEST_ASSERTM(n < 65536, "Reallocation attempt failed - test not executable");
   101             rdata = cxRealloc(pool->allocator, data, n * sizeof(intptr_t));
   102         }
   104         CX_TEST_ASSERT(*rdata == 13);
   105         // test if destructor is still intact
   106         test_mempool_destructor_called = 0;
   107         cxFree(pool->allocator, rdata);
   108         CX_TEST_ASSERT(test_mempool_destructor_called == 1);
   109     }
   110     cxMempoolDestroy(pool);
   111 }
   114 CX_TEST(test_mempool_free) {
   115     CxMempool *pool = cxBasicMempoolCreate(4);
   116     void *mem1, *mem2;
   117     CX_TEST_DO {
   118         mem1 = cxMalloc(pool->allocator, 16);
   119         cxFree(pool->allocator, mem1);
   120         CX_TEST_ASSERT(pool->size == 0);
   122         cxMalloc(pool->allocator, 16);
   123         cxMalloc(pool->allocator, 16);
   124         mem1 = cxMalloc(pool->allocator, 16);
   125         cxMalloc(pool->allocator, 16);
   126         mem2 = cxMalloc(pool->allocator, 16);
   128         CX_TEST_ASSERT(pool->size == 5);
   129         cxFree(pool->allocator, mem1);
   130         CX_TEST_ASSERT(pool->size == 4);
   131         cxFree(pool->allocator, mem2);
   132         CX_TEST_ASSERT(pool->size == 3);
   133     }
   134     cxMempoolDestroy(pool);
   135 }
   137 CX_TEST(test_mempool_destroy) {
   138     CxMempool *pool = cxBasicMempoolCreate(4);
   139     CX_TEST_DO {
   140         int *data = cxMalloc(pool->allocator, sizeof(int));
   141         *data = 13;
   142         cxMempoolSetDestructor(data, test_mempool_destructor);
   143         CX_TEST_ASSERT(*data == 13);
   144         test_mempool_destructor_called = 0;
   145         cxFree(pool->allocator, data);
   146         CX_TEST_ASSERT(test_mempool_destructor_called == 1);
   147         data = cxMalloc(pool->allocator, sizeof(int));
   148         cxMempoolSetDestructor(data, test_mempool_destructor);
   149         cxMempoolDestroy(pool);
   150         CX_TEST_ASSERT(test_mempool_destructor_called == 2);
   151     }
   152 }
   154 CX_TEST(test_mempool_register) {
   155     CxMempool *pool = cxBasicMempoolCreate(4);
   156     CX_TEST_DO {
   157         int *data = cxMalloc(pool->allocator, sizeof(int));
   158         test_mempool_destructor_called = 0;
   159         cxMempoolSetDestructor(data, test_mempool_destructor);
   160         int donotfree = 0;
   161         cxMempoolRegister(pool, &donotfree, test_mempool_destructor);
   162         cxMempoolDestroy(pool);
   163         CX_TEST_ASSERT(test_mempool_destructor_called == 2);
   164     }
   165 }
   168 CxTestSuite *cx_test_suite_mempool(void) {
   169     CxTestSuite *suite = cx_test_suite_new("mempool");
   171     cx_test_register(suite, test_mempool_create);
   172     cx_test_register(suite, test_mempool_malloc);
   173     cx_test_register(suite, test_mempool_calloc);
   174     cx_test_register(suite, test_mempool_realloc);
   175     cx_test_register(suite, test_mempool_free);
   176     cx_test_register(suite, test_mempool_destroy);
   177     cx_test_register(suite, test_mempool_register);
   179     return suite;
   180 }

mercurial