tests/util_allocator.c

Wed, 27 Dec 2023 16:18:10 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 27 Dec 2023 16:18:10 +0100
changeset 773
bbf2f18b3818
parent 770
ed710122af44
child 775
d3f451440eef
permissions
-rw-r--r--

fix missing LDFLAGS when linking ucxtest

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++;
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
86 if (!cx_testing_allocator_untrack(data, mem)) {
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 data->free_failed++;
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 }
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
89 cx_testing_allocator_track(data, ptr);
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 }
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 return ptr;
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 }
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
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
95 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
96 CxTestingAllocator *data = d;
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 void *ptr = calloc(nelem, n);
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 data->alloc_total++;
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
99 if (ptr == NULL) {
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 data->alloc_failed++;
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 } else {
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
102 cx_testing_allocator_track(data, ptr);
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 }
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 return ptr;
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 }
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
107 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
108 CxTestingAllocator *data = d;
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 data->free_total++;
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
110 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
111 free(mem);
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
112 } else {
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 data->free_failed++;
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 // 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
115 }
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 }
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 cx_allocator_class cx_testing_allocator_class = {
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119 cx_malloc_testing,
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 cx_realloc_testing,
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 cx_calloc_testing,
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
122 cx_free_testing
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 };
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
125
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
126 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
127 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
128 alloc->base.data = alloc;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
129 alloc->alloc_failed = 0;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
130 alloc->alloc_total = 0;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
131 alloc->free_failed = 0;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
132 alloc->free_total = 0;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
133 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
134 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
135 alloc->tracked_count = 0;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
136 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
137 }
74d0372f5c6f improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
138
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
139 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
140 free(alloc->tracked);
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents: 518
diff changeset
141 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents: 518
diff changeset
142
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
143 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
144 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
145 }
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
146
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
147 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
148 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
149 && 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
150 }
74d0372f5c6f improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
151
74d0372f5c6f improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
152 // SELF-TEST
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
153
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
154 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
155 CxTestingAllocator talloc;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
156 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
157 CxAllocator *alloc = &talloc.base;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
158 CX_TEST_DO {
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
159 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
160 "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
161 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
162 "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
163 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
164 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
165 "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
166 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
167 CX_TEST_ASSERT(ptr != NULL);
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
168
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
169 cxFree(alloc, ptr);
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
170 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
171 "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
172 }
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
173 cx_testing_allocator_destroy(&talloc);
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
174 }
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
175
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
176 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
177 CxTestingAllocator talloc;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
178 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
179 CxAllocator *alloc = &talloc.base;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
180 CX_TEST_DO {
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
181 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
182 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
183 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
184 cxFree(alloc, ptr);
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
185 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
186 cxFree(alloc, ptr);
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
187 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
188 "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
189 }
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
190 cx_testing_allocator_destroy(&talloc);
518
74d0372f5c6f improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
191 }
74d0372f5c6f improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
192
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
193 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
194 CxTestingAllocator talloc;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
195 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
196 CxAllocator *alloc = &talloc.base;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
197 void *ptr = malloc(16);
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
198 CX_TEST_DO {
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
199 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
200 cxFree(alloc, ptr);
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
201 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
202 "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
203 }
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
204 free(ptr);
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
205 cx_testing_allocator_destroy(&talloc);
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
206 }
518
74d0372f5c6f improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
207
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
208 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
209 CxTestingAllocator talloc;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
210 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
211 CxAllocator *alloc = &talloc.base;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
212 CX_TEST_DO {
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
213 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
214 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
215 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
216 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
217 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
218 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
219 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
220 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
221 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
222 cxFree(alloc, ptr);
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
223 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
224 }
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
225 cx_testing_allocator_destroy(&talloc);
518
74d0372f5c6f improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
226 }
74d0372f5c6f improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
227
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
228 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
229 CxTestingAllocator talloc;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
230 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
231 CxAllocator *alloc = &talloc.base;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
232 CX_TEST_DO {
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
233 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
234 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
235 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
236 cxFree(alloc, ptr);
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
237 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
238 }
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
239 cx_testing_allocator_destroy(&talloc);
518
74d0372f5c6f improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
240 }
770
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 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
243 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
244
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
245 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
246 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
247 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
248 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
249 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
250
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
251 return suite;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
252 }
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
253

mercurial