tests/util_allocator.c

Sun, 20 Oct 2024 11:39:54 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 20 Oct 2024 11:39:54 +0200
changeset 939
0bb7258366a0
parent 890
54565fd74e74
permissions
-rw-r--r--

add test for parsing json with multiple incomplete tokens

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++;
814
5f9e07d3dd6c fix using warning options with compilers which do not support them - fixes #363
Mike Becker <universe@uap-core.de>
parents: 775
diff changeset
86 #if !defined(__clang__) && __GNUC__ > 11
775
d3f451440eef ignore the intentional use-after-free
Mike Becker <universe@uap-core.de>
parents: 770
diff changeset
87 #pragma GCC diagnostic push
d3f451440eef ignore the intentional use-after-free
Mike Becker <universe@uap-core.de>
parents: 770
diff changeset
88 #pragma GCC diagnostic ignored "-Wuse-after-free"
814
5f9e07d3dd6c fix using warning options with compilers which do not support them - fixes #363
Mike Becker <universe@uap-core.de>
parents: 775
diff changeset
89 #endif
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
90 if (!cx_testing_allocator_untrack(data, mem)) {
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
91 data->free_failed++;
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
92 }
814
5f9e07d3dd6c fix using warning options with compilers which do not support them - fixes #363
Mike Becker <universe@uap-core.de>
parents: 775
diff changeset
93 #if !defined(__clang__) && __GNUC__ > 11
775
d3f451440eef ignore the intentional use-after-free
Mike Becker <universe@uap-core.de>
parents: 770
diff changeset
94 #pragma GCC diagnostic pop
814
5f9e07d3dd6c fix using warning options with compilers which do not support them - fixes #363
Mike Becker <universe@uap-core.de>
parents: 775
diff changeset
95 #endif
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
96 cx_testing_allocator_track(data, ptr);
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 }
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
98 return ptr;
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
99 }
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
100 }
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
102 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
103 CxTestingAllocator *data = d;
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 void *ptr = calloc(nelem, n);
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 data->alloc_total++;
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
106 if (ptr == NULL) {
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
107 data->alloc_failed++;
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
108 } else {
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
109 cx_testing_allocator_track(data, ptr);
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 }
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111 return ptr;
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112 }
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
114 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
115 CxTestingAllocator *data = d;
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 data->free_total++;
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
117 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
118 free(mem);
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
119 } else {
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 data->free_failed++;
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
121 // 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
122 }
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
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 cx_allocator_class cx_testing_allocator_class = {
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 cx_malloc_testing,
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127 cx_realloc_testing,
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 cx_calloc_testing,
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129 cx_free_testing
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130 };
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
132
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
133 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
134 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
135 alloc->base.data = alloc;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
136 alloc->alloc_failed = 0;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
137 alloc->alloc_total = 0;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
138 alloc->free_failed = 0;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
139 alloc->free_total = 0;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
140 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
141 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
142 alloc->tracked_count = 0;
861
bab51b32fcb1 fix mixed up arguments for calloc()
Mike Becker <universe@uap-core.de>
parents: 814
diff changeset
143 alloc->tracked = calloc(initial_capa, sizeof(void *));
518
74d0372f5c6f improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
144 }
74d0372f5c6f improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents: 500
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 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
147 free(alloc->tracked);
571
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents: 518
diff changeset
148 }
f83583a0bbac #201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents: 518
diff changeset
149
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 861
diff changeset
150 bool cx_testing_allocator_used(const CxTestingAllocator *alloc) {
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
151 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
152 }
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
153
890
54565fd74e74 move all const keywords to the west - fixes #426
Mike Becker <universe@uap-core.de>
parents: 861
diff changeset
154 bool cx_testing_allocator_verify(const CxTestingAllocator *alloc) {
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
155 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
156 && 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
157 }
74d0372f5c6f improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
158
74d0372f5c6f improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
159 // SELF-TEST
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
161 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
162 CxTestingAllocator talloc;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
163 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
164 CxAllocator *alloc = &talloc.base;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
165 CX_TEST_DO {
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
166 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
167 "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
168 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
169 "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
170 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
171 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
172 "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
173 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
174 CX_TEST_ASSERT(ptr != NULL);
422
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 cxFree(alloc, ptr);
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
177 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
178 "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
179 }
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
180 cx_testing_allocator_destroy(&talloc);
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
181 }
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
182
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
183 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
184 CxTestingAllocator talloc;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
185 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
186 CxAllocator *alloc = &talloc.base;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
187 CX_TEST_DO {
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 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
190 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
191 cxFree(alloc, ptr);
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
192 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
193 cxFree(alloc, ptr);
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
194 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
195 "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
196 }
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
197 cx_testing_allocator_destroy(&talloc);
518
74d0372f5c6f improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
198 }
74d0372f5c6f improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
199
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
200 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
201 CxTestingAllocator talloc;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
202 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
203 CxAllocator *alloc = &talloc.base;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
204 void *ptr = malloc(16);
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
205 CX_TEST_DO {
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
206 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
207 cxFree(alloc, ptr);
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
208 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
209 "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
210 }
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
211 free(ptr);
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
212 cx_testing_allocator_destroy(&talloc);
422
afd87df80b13 add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff changeset
213 }
518
74d0372f5c6f improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
214
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
215 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
216 CxTestingAllocator talloc;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
217 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
218 CxAllocator *alloc = &talloc.base;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
219 CX_TEST_DO {
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
220 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
221 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
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 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
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 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
228 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
229 cxFree(alloc, ptr);
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
230 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
231 }
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
232 cx_testing_allocator_destroy(&talloc);
518
74d0372f5c6f improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
233 }
74d0372f5c6f improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
234
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
235 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
236 CxTestingAllocator talloc;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
237 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
238 CxAllocator *alloc = &talloc.base;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
239 CX_TEST_DO {
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
240 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
241 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
242 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
243 cxFree(alloc, ptr);
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
244 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
245 }
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
246 cx_testing_allocator_destroy(&talloc);
518
74d0372f5c6f improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents: 500
diff changeset
247 }
770
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
248
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
249 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
250 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
251
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_expect_free);
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
253 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
254 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
255 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
256 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
257
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
258 return suite;
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
259 }
ed710122af44 migrates self-test for testing allocator - relates to #342
Mike Becker <universe@uap-core.de>
parents: 653
diff changeset
260

mercurial