tests/util_allocator.c

Wed, 27 Dec 2023 16:04:38 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 27 Dec 2023 16:04:38 +0100
changeset 770
ed710122af44
parent 653
tests/util_allocator.cpp@e081643aae2a
child 775
d3f451440eef
permissions
-rw-r--r--

migrates self-test for testing allocator - relates to #342

universe@422 1 /*
universe@422 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@422 3 *
universe@422 4 * Copyright 2021 Mike Becker, Olaf Wintermann All rights reserved.
universe@422 5 *
universe@422 6 * Redistribution and use in source and binary forms, with or without
universe@422 7 * modification, are permitted provided that the following conditions are met:
universe@422 8 *
universe@422 9 * 1. Redistributions of source code must retain the above copyright
universe@422 10 * notice, this list of conditions and the following disclaimer.
universe@422 11 *
universe@422 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@422 13 * notice, this list of conditions and the following disclaimer in the
universe@422 14 * documentation and/or other materials provided with the distribution.
universe@422 15 *
universe@422 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@422 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@422 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@422 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@422 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@422 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@422 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@422 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@422 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@422 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@422 26 * POSSIBILITY OF SUCH DAMAGE.
universe@422 27 */
universe@422 28
universe@422 29 #include "util_allocator.h"
universe@770 30 #include "cx/test.h"
universe@422 31
universe@770 32 static void cx_testing_allocator_track(CxTestingAllocator *alloc, void *ptr) {
universe@770 33 for (size_t i = 0; i < alloc->tracked_count; i++) {
universe@770 34 if (alloc->tracked[i] == ptr) return; // is already tracked
universe@770 35 }
universe@770 36
universe@770 37 if (alloc->tracked_count == alloc->tracked_capacity) {
universe@770 38 size_t newcapa = alloc->tracked_capacity + 64;
universe@770 39 void *newarr = realloc(alloc->tracked, newcapa * sizeof(void *));
universe@770 40 if (newarr == NULL) abort();
universe@770 41 alloc->tracked = newarr;
universe@770 42 alloc->tracked_capacity = newcapa;
universe@770 43 }
universe@770 44
universe@770 45 alloc->tracked[alloc->tracked_count] = ptr;
universe@770 46 alloc->tracked_count++;
universe@770 47 }
universe@770 48
universe@770 49 static bool cx_testing_allocator_untrack(CxTestingAllocator *alloc, void *ptr) {
universe@770 50 for (size_t i = 0; i < alloc->tracked_count; i++) {
universe@770 51 if (alloc->tracked[i] == ptr) {
universe@770 52 size_t last = alloc->tracked_count - 1;
universe@770 53 if (i < last) {
universe@770 54 alloc->tracked[i] = alloc->tracked[last];
universe@770 55 }
universe@770 56 alloc->tracked_count--;
universe@770 57 return true;
universe@770 58 }
universe@770 59 }
universe@770 60 return false;
universe@770 61 }
universe@770 62
universe@770 63 static void *cx_malloc_testing(void *d, size_t n) {
universe@770 64 CxTestingAllocator *data = d;
universe@422 65 void *ptr = malloc(n);
universe@422 66 data->alloc_total++;
universe@770 67 if (ptr == NULL) {
universe@422 68 data->alloc_failed++;
universe@422 69 } else {
universe@770 70 cx_testing_allocator_track(data, ptr);
universe@422 71 }
universe@422 72 return ptr;
universe@422 73 }
universe@422 74
universe@770 75 static void *cx_realloc_testing(void *d, void *mem, size_t n) {
universe@770 76 CxTestingAllocator *data = d;
universe@422 77 void *ptr = realloc(mem, n);
universe@422 78 if (ptr == mem) {
universe@422 79 return ptr;
universe@422 80 } else {
universe@422 81 data->alloc_total++;
universe@770 82 if (ptr == NULL) {
universe@422 83 data->alloc_failed++;
universe@422 84 } else {
universe@422 85 data->free_total++;
universe@770 86 if (!cx_testing_allocator_untrack(data, mem)) {
universe@422 87 data->free_failed++;
universe@422 88 }
universe@770 89 cx_testing_allocator_track(data, ptr);
universe@422 90 }
universe@422 91 return ptr;
universe@422 92 }
universe@422 93 }
universe@422 94
universe@770 95 static void *cx_calloc_testing(void *d, size_t nelem, size_t n) {
universe@770 96 CxTestingAllocator *data = d;
universe@422 97 void *ptr = calloc(nelem, n);
universe@422 98 data->alloc_total++;
universe@770 99 if (ptr == NULL) {
universe@422 100 data->alloc_failed++;
universe@422 101 } else {
universe@770 102 cx_testing_allocator_track(data, ptr);
universe@422 103 }
universe@422 104 return ptr;
universe@422 105 }
universe@422 106
universe@770 107 static void cx_free_testing(void *d, void *mem) {
universe@770 108 CxTestingAllocator *data = d;
universe@422 109 data->free_total++;
universe@770 110 if (cx_testing_allocator_untrack(data, mem)) {
universe@770 111 free(mem);
universe@770 112 } else {
universe@422 113 data->free_failed++;
universe@422 114 // do not even attempt to free mem, because it is likely to segfault
universe@422 115 }
universe@422 116 }
universe@422 117
universe@422 118 cx_allocator_class cx_testing_allocator_class = {
universe@422 119 cx_malloc_testing,
universe@422 120 cx_realloc_testing,
universe@422 121 cx_calloc_testing,
universe@422 122 cx_free_testing
universe@422 123 };
universe@422 124
universe@770 125
universe@770 126 void cx_testing_allocator_init(CxTestingAllocator *alloc) {
universe@770 127 alloc->base.cl = &cx_testing_allocator_class;
universe@770 128 alloc->base.data = alloc;
universe@770 129 alloc->alloc_failed = 0;
universe@770 130 alloc->alloc_total = 0;
universe@770 131 alloc->free_failed = 0;
universe@770 132 alloc->free_total = 0;
universe@770 133 size_t initial_capa = 16;
universe@770 134 alloc->tracked_capacity = initial_capa;
universe@770 135 alloc->tracked_count = 0;
universe@770 136 alloc->tracked = calloc(sizeof(void *), initial_capa);
universe@422 137 }
universe@422 138
universe@770 139 void cx_testing_allocator_destroy(CxTestingAllocator *alloc) {
universe@770 140 free(alloc->tracked);
universe@571 141 }
universe@571 142
universe@770 143 bool cx_testing_allocator_used(CxTestingAllocator const *alloc) {
universe@770 144 return alloc->alloc_total > 0;
universe@770 145 }
universe@770 146
universe@770 147 bool cx_testing_allocator_verify(CxTestingAllocator const *alloc) {
universe@770 148 return alloc->tracked_count == 0 && alloc->alloc_failed == 0 && alloc->free_failed == 0
universe@770 149 && alloc->alloc_total == alloc->free_total;
universe@422 150 }
universe@518 151
universe@518 152 // SELF-TEST
universe@518 153
universe@770 154 CX_TEST(test_util_allocator_expect_free) {
universe@770 155 CxTestingAllocator talloc;
universe@770 156 cx_testing_allocator_init(&talloc);
universe@770 157 CxAllocator *alloc = &talloc.base;
universe@770 158 CX_TEST_DO {
universe@770 159 CX_TEST_ASSERTM(cx_testing_allocator_verify(&talloc),
universe@770 160 "Fresh testing allocator fails to verify.");
universe@770 161 CX_TEST_ASSERTM(!cx_testing_allocator_used(&talloc),
universe@770 162 "Fresh testing allocator already used.");
universe@770 163 void *ptr = cxMalloc(alloc, 16);
universe@770 164 CX_TEST_ASSERTM(!cx_testing_allocator_verify(&talloc),
universe@770 165 "Testing allocator verifies with unfreed memory.");
universe@770 166 CX_TEST_ASSERT(cx_testing_allocator_used(&talloc));
universe@770 167 CX_TEST_ASSERT(ptr != NULL);
universe@518 168
universe@770 169 cxFree(alloc, ptr);
universe@770 170 CX_TEST_ASSERTM(cx_testing_allocator_verify(&talloc),
universe@770 171 "Testing allocator fails to verify after everything freed.");
universe@770 172 }
universe@770 173 cx_testing_allocator_destroy(&talloc);
universe@518 174 }
universe@518 175
universe@770 176 CX_TEST(test_util_allocator_detect_double_free) {
universe@770 177 CxTestingAllocator talloc;
universe@770 178 cx_testing_allocator_init(&talloc);
universe@770 179 CxAllocator *alloc = &talloc.base;
universe@770 180 CX_TEST_DO {
universe@770 181 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
universe@770 182 void *ptr = cxMalloc(alloc, 16);
universe@770 183 CX_TEST_ASSERT(ptr != NULL);
universe@770 184 cxFree(alloc, ptr);
universe@770 185 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
universe@770 186 cxFree(alloc, ptr);
universe@770 187 CX_TEST_ASSERTM(!cx_testing_allocator_verify(&talloc),
universe@770 188 "Testing allocator does not detect double-free.");
universe@770 189 }
universe@770 190 cx_testing_allocator_destroy(&talloc);
universe@518 191 }
universe@518 192
universe@770 193 CX_TEST(test_util_allocator_free_untracked) {
universe@770 194 CxTestingAllocator talloc;
universe@770 195 cx_testing_allocator_init(&talloc);
universe@770 196 CxAllocator *alloc = &talloc.base;
universe@770 197 void *ptr = malloc(16);
universe@770 198 CX_TEST_DO {
universe@770 199 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
universe@770 200 cxFree(alloc, ptr);
universe@770 201 CX_TEST_ASSERTM(!cx_testing_allocator_verify(&talloc),
universe@770 202 "Testing allocator does not detect free of untracked memory.");
universe@770 203 }
universe@770 204 free(ptr);
universe@770 205 cx_testing_allocator_destroy(&talloc);
universe@518 206 }
universe@518 207
universe@770 208 CX_TEST(test_util_allocator_full_lifecycle_with_realloc) {
universe@770 209 CxTestingAllocator talloc;
universe@770 210 cx_testing_allocator_init(&talloc);
universe@770 211 CxAllocator *alloc = &talloc.base;
universe@770 212 CX_TEST_DO {
universe@770 213 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
universe@770 214 void *ptr = cxMalloc(alloc, 16);
universe@770 215 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
universe@770 216 CX_TEST_ASSERT(ptr != NULL);
universe@770 217 CX_TEST_ASSERT(talloc.tracked_count == 1);
universe@770 218 ptr = cxRealloc(alloc, ptr, 256);
universe@770 219 CX_TEST_ASSERT(!cx_testing_allocator_verify(&talloc));
universe@770 220 CX_TEST_ASSERT(ptr != NULL);
universe@770 221 CX_TEST_ASSERT(talloc.tracked_count == 1);
universe@770 222 cxFree(alloc, ptr);
universe@770 223 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
universe@770 224 }
universe@770 225 cx_testing_allocator_destroy(&talloc);
universe@518 226 }
universe@518 227
universe@770 228 CX_TEST(test_util_allocator_calloc_initializes) {
universe@770 229 CxTestingAllocator talloc;
universe@770 230 cx_testing_allocator_init(&talloc);
universe@770 231 CxAllocator *alloc = &talloc.base;
universe@770 232 CX_TEST_DO {
universe@770 233 const char zeros[16] = {0};
universe@770 234 void *ptr = cxCalloc(alloc, 16, 1);
universe@770 235 CX_TEST_ASSERT(memcmp(ptr, zeros, 16) == 0);
universe@770 236 cxFree(alloc, ptr);
universe@770 237 CX_TEST_ASSERT(cx_testing_allocator_verify(&talloc));
universe@770 238 }
universe@770 239 cx_testing_allocator_destroy(&talloc);
universe@518 240 }
universe@770 241
universe@770 242 CxTestSuite *cx_test_suite_testing_allocator(void) {
universe@770 243 CxTestSuite *suite = cx_test_suite_new("testing allocator self-test");
universe@770 244
universe@770 245 cx_test_register(suite, test_util_allocator_expect_free);
universe@770 246 cx_test_register(suite, test_util_allocator_detect_double_free);
universe@770 247 cx_test_register(suite, test_util_allocator_free_untracked);
universe@770 248 cx_test_register(suite, test_util_allocator_full_lifecycle_with_realloc);
universe@770 249 cx_test_register(suite, test_util_allocator_calloc_initializes);
universe@770 250
universe@770 251 return suite;
universe@770 252 }
universe@770 253

mercurial