Sun, 23 Oct 2022 13:32:46 +0200
explicitly cast int to char
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" |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
30 | |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
31 | void *cx_malloc_testing(void *d, size_t n) { |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
32 | auto data = reinterpret_cast<CxTestingAllocator *>(d); |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
33 | void *ptr = malloc(n); |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
34 | data->alloc_total++; |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
35 | if (ptr == nullptr) { |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
36 | data->alloc_failed++; |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
37 | } else { |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
38 | data->tracked.insert(ptr); |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
39 | } |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
40 | return ptr; |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
41 | } |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
42 | |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
43 | void *cx_realloc_testing(void *d, void *mem, size_t n) { |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
44 | auto data = reinterpret_cast<CxTestingAllocator *>(d); |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
45 | void *ptr = realloc(mem, n); |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
46 | if (ptr == mem) { |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
47 | return ptr; |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
48 | } else { |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
49 | data->alloc_total++; |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
50 | if (ptr == nullptr) { |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
51 | data->alloc_failed++; |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
52 | } else { |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
53 | data->free_total++; |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
54 | if (data->tracked.erase(mem) == 0) { |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
55 | data->free_failed++; |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
56 | } |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
57 | data->tracked.insert(ptr); |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
58 | } |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
59 | return ptr; |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
60 | } |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
61 | } |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
62 | |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
63 | void *cx_calloc_testing(void *d, size_t nelem, size_t n) { |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
64 | auto data = reinterpret_cast<CxTestingAllocator *>(d); |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
65 | void *ptr = calloc(nelem, n); |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
66 | data->alloc_total++; |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
67 | if (ptr == nullptr) { |
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 { |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
70 | data->tracked.insert(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 | |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
75 | void cx_free_testing(void *d, void *mem) { |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
76 | auto data = reinterpret_cast<CxTestingAllocator *>(d); |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
77 | data->free_total++; |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
78 | if (data->tracked.erase(mem) == 0) { |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
79 | data->free_failed++; |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
80 | // 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
|
81 | } else { |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
82 | free(mem); |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
83 | } |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
84 | } |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
85 | |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
86 | cx_allocator_class cx_testing_allocator_class = { |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
87 | cx_malloc_testing, |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
88 | cx_realloc_testing, |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
89 | cx_calloc_testing, |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
90 | cx_free_testing |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
91 | }; |
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
92 | |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
93 | CxTestingAllocator::CxTestingAllocator() : CxAllocator() { |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
94 | cl = &cx_testing_allocator_class; |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
95 | data = this; |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
96 | } |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
97 | |
571
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
518
diff
changeset
|
98 | bool CxTestingAllocator::used() const { |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
518
diff
changeset
|
99 | return alloc_total > 0; |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
518
diff
changeset
|
100 | } |
f83583a0bbac
#201 - add mempool implementation
Mike Becker <universe@uap-core.de>
parents:
518
diff
changeset
|
101 | |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
102 | bool CxTestingAllocator::verify() const { |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
103 | return tracked.empty() && alloc_failed == 0 && free_failed == 0 && alloc_total == free_total; |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
104 | } |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
105 | |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
106 | // SELF-TEST |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
107 | |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
108 | #include <gtest/gtest.h> |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
109 | |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
110 | TEST(TestingAllocator, ExpectFree) { |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
111 | CxTestingAllocator allocator; |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
112 | |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
113 | ASSERT_TRUE(allocator.verify()); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
114 | auto ptr = cxMalloc(&allocator, 16); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
115 | ASSERT_NE(ptr, nullptr); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
116 | EXPECT_FALSE(allocator.verify()); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
117 | |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
118 | cxFree(&allocator, ptr); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
119 | EXPECT_TRUE(allocator.verify()); |
422
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 | |
518
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
122 | TEST(TestingAllocator, DetectDoubleFree) { |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
123 | CxTestingAllocator allocator; |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
124 | |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
125 | ASSERT_TRUE(allocator.verify()); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
126 | auto ptr = cxMalloc(&allocator, 16); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
127 | ASSERT_NE(ptr, nullptr); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
128 | |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
129 | cxFree(&allocator, ptr); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
130 | EXPECT_TRUE(allocator.verify()); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
131 | ASSERT_NO_FATAL_FAILURE(cxFree(&allocator, ptr)); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
132 | EXPECT_FALSE(allocator.verify()); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
133 | } |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
134 | |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
135 | TEST(TestingAllocator, FreeUntracked) { |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
136 | CxTestingAllocator allocator; |
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 | auto ptr = malloc(16); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
139 | ASSERT_TRUE(allocator.verify()); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
140 | ASSERT_NO_FATAL_FAILURE(cxFree(&allocator, ptr)); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
141 | EXPECT_FALSE(allocator.verify()); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
142 | ASSERT_NO_FATAL_FAILURE(free(ptr)); |
422
afd87df80b13
add utility to verify allocations
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
143 | } |
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 | TEST(TestingAllocator, FullLifecycleWithRealloc) { |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
146 | CxTestingAllocator allocator; |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
147 | ASSERT_TRUE(allocator.verify()); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
148 | auto ptr = cxMalloc(&allocator, 16); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
149 | ASSERT_NE(ptr, nullptr); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
150 | EXPECT_EQ(allocator.tracked.size(), 1); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
151 | ptr = cxRealloc(&allocator, ptr, 256); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
152 | ASSERT_NE(ptr, nullptr); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
153 | EXPECT_EQ(allocator.tracked.size(), 1); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
154 | cxFree(&allocator, ptr); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
155 | EXPECT_TRUE(allocator.verify()); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
156 | } |
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 | TEST(TestingAllocator, CallocInitializes) { |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
159 | CxTestingAllocator allocator; |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
160 | const char* zeros[16] = {0}; |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
161 | auto ptr = cxCalloc(&allocator, 16, 1); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
162 | EXPECT_EQ(memcmp(ptr, zeros, 16), 0); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
163 | cxFree(&allocator, ptr); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
164 | EXPECT_TRUE(allocator.verify()); |
74d0372f5c6f
improve testing allocator + add tests for it
Mike Becker <universe@uap-core.de>
parents:
500
diff
changeset
|
165 | } |