tests/util_allocator.c

Fri, 29 Dec 2023 17:17:43 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 29 Dec 2023 17:17:43 +0100
changeset 779
e9611e4d0213
parent 775
d3f451440eef
child 814
5f9e07d3dd6c
permissions
-rw-r--r--

revert the insanely stupid idea to automatically run the test program on default build

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

mercurial