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