Debian Codegraph @

Tests/Recast/Tests_Alloc.cpp

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 #include <stdio.h>
2 #include <string.h>
3
4 #include "catch2/catch_all.hpp"
5
6 #include "RecastAlloc.h"
7 #include "RecastAssert.h"
8
9 /// Used to verify that rcVector constructs/destroys objects correctly.
10 struct Incrementor {
11 static int constructions;
12 static int destructions;
13 static int copies;
14 Incrementor() { constructions++; }
15 ~Incrementor() { destructions++; }
16 Incrementor(const Incrementor&) { copies++; }
17 Incrementor& operator=(const Incrementor&); // Deleted assignment.
18
19 static void Reset() {
20 constructions = 0;
21 destructions = 0;
22 copies = 0;
23 }
24 };
25
26 int Incrementor::constructions = 0;
27 int Incrementor::destructions = 0;
28 int Incrementor::copies = 0;
29
30 const int kMaxAllocSize = 1024;
31 const unsigned char kClearValue = 0xff;
32
33 /// Simple alloc/free that clears the memory on free..
34 void* AllocAndInit(size_t size, rcAllocHint) {
35 rcAssert(kMaxAllocSize >= size);
36 return memset(malloc(kMaxAllocSize), 0, kMaxAllocSize);
37 }
38
39 void FreeAndClear(void* mem) {
40 if (mem) {
41 memset(mem, kClearValue, kMaxAllocSize);
42 }
43 free(mem);
44 }
45
46 // Verifies that memory has been initialized by AllocAndInit, and not cleared by FreeAndClear.
47 struct Copier {
48 const static int kAlive;
49 const static int kDead;
50 Copier() : value(kAlive) {}
51
52 // checks that the source of the copy is valid.
53 Copier(const Copier& other) : value(kAlive) {
54 other.Verify();
55 }
56 Copier& operator=(const Copier&);
57
58 // Marks the value as dead.
59 ~Copier() { value = kDead; }
60 void Verify() const {
61 REQUIRE(value == kAlive);
62 }
63 volatile int value;
64 };
65
66 const int Copier::kAlive = 0x1f;
67 const int Copier::kDead = 0xde;
68
69 struct NotDefaultConstructible {
70 NotDefaultConstructible(int) {}
71 };
72
73 TEST_CASE("rcVector", "[recast, alloc]")
74 {
75 SECTION("Vector basics.")
76 {
77 rcTempVector<int> vec;
78 REQUIRE(vec.size() == 0);
79 vec.push_back(10);
80 vec.push_back(12);
81 REQUIRE(vec.size() == 2);
82 REQUIRE(vec.capacity() >= 2);
83 REQUIRE(vec[0] == 10);
84 REQUIRE(vec[1] == 12);
85 vec.pop_back();
86 REQUIRE(vec.size() == 1);
87 REQUIRE(vec[0] == 10);
88 vec.pop_back();
89 REQUIRE(vec.size() == 0);
90 vec.resize(100, 5);
91 REQUIRE(vec.size() == 100);
92 for (int i = 0; i < 100; i++) {
93 REQUIRE(vec[i] == 5);
94 vec[i] = i;
95 }
96 for (int i = 0; i < 100; i++) {
97 REQUIRE(vec[i] == i);
98 }
99 }
100
101 SECTION("Constructors/Destructors")
102 {
103 Incrementor::Reset();
104 rcTempVector<Incrementor> vec;
105 REQUIRE(Incrementor::constructions == 0);
106 REQUIRE(Incrementor::destructions == 0);
107 REQUIRE(Incrementor::copies == 0);
108 vec.push_back(Incrementor());
109 // push_back() may create and copy objects internally.
110 REQUIRE(Incrementor::constructions == 1);
111 REQUIRE(Incrementor::destructions >= 1);
112 // REQUIRE(Incrementor::copies >= 2);
113
114 vec.clear();
115 Incrementor::Reset();
116 vec.resize(100);
117 // Initialized with default instance. Temporaries may be constructed, then destroyed.
118 REQUIRE(Incrementor::constructions == 100);
119 REQUIRE(Incrementor::destructions == 0);
120 REQUIRE(Incrementor::copies == 0);
121
122 Incrementor::Reset();
123 for (int i = 0; i < 100; i++) {
124 REQUIRE(Incrementor::destructions == i);
125 vec.pop_back();
126 }
127 REQUIRE(Incrementor::constructions == 0);
128 REQUIRE(Incrementor::destructions == 100);
129 REQUIRE(Incrementor::copies == 0);
130
131 vec.resize(100);
132 Incrementor::Reset();
133 vec.clear();
134 // One temp object is constructed for the default argument of resize().
135 REQUIRE(Incrementor::constructions == 0);
136 REQUIRE(Incrementor::destructions == 100);
137 REQUIRE(Incrementor::copies == 0);
138
139 Incrementor::Reset();
140 vec.resize(100, Incrementor());
141 REQUIRE(Incrementor::constructions == 1);
142 REQUIRE(Incrementor::destructions == 1);
143 REQUIRE(Incrementor::copies == 100);
144 }
145
146 SECTION("Copying Contents")
147 {
148
149 // veriyf event counts after doubling size -- should require a lot of copying and destroying.
150 rcTempVector<Incrementor> vec;
151 Incrementor::Reset();
152 vec.resize(100);
153 REQUIRE(Incrementor::constructions == 100);
154 REQUIRE(Incrementor::destructions == 0);
155 REQUIRE(Incrementor::copies == 0);
156 Incrementor::Reset();
157 vec.resize(200);
158 REQUIRE(vec.size() == vec.capacity());
159 REQUIRE(Incrementor::constructions == 100); // Construc new elements.
160 REQUIRE(Incrementor::destructions == 100); // Destroy old contents.
161 REQUIRE(Incrementor::copies == 100); // Copy old elements into new array.
162 }
163
164 SECTION("Swap")
165 {
166 rcTempVector<int> a(10, 0xa);
167 rcTempVector<int> b;
168
169 int* a_data = a.data();
170 int* b_data = b.data();
171
172 a.swap(b);
173 REQUIRE(a.size() == 0);
174 REQUIRE(b.size() == 10);
175 REQUIRE(b[0] == 0xa);
176 REQUIRE(b[9] == 0xa);
177 REQUIRE(a.data() == b_data);
178 REQUIRE(b.data() == a_data);
179 }
180
181 SECTION("Overlapping init")
182 {
183 rcAllocSetCustom(&AllocAndInit, &FreeAndClear);
184 rcTempVector<Copier> vec;
185 // Force a realloc during push_back().
186 vec.resize(64);
187 REQUIRE(vec.capacity() == vec.size());
188 REQUIRE(vec.capacity() > 0);
189 REQUIRE(vec.size() == vec.capacity());
190
191 // Don't crash.
192 vec.push_back(vec[0]);
193 rcAllocSetCustom(NULL, NULL);
194 }
195
196 SECTION("Vector Destructor")
197 {
198 {
199 rcTempVector<Incrementor> vec;
200 vec.resize(10);
201 Incrementor::Reset();
202 }
203 REQUIRE(Incrementor::destructions == 10);
204 }
205
206 SECTION("Assign")
207 {
208 rcTempVector<int> a(10, 0xa);
209 a.assign(5, 0xb);
210 REQUIRE(a.size() == 5);
211 REQUIRE(a[0] == 0xb);
212 REQUIRE(a[4] == 0xb);
213 a.assign(15, 0xc);
214 REQUIRE(a.size() == 15);
215 REQUIRE(a[0] == 0xc);
216 REQUIRE(a[14] == 0xc);
217
218 rcTempVector<int> b;
219 b.assign(a.data(), a.data() + a.size());
220 REQUIRE(b.size() == a.size());
221 REQUIRE(b[0] == a[0]);
222 }
223
224 SECTION("Copy")
225 {
226 rcTempVector<int> a(10, 0xa);
227 rcTempVector<int> b(a);
228 REQUIRE(a.size() == 10);
229 REQUIRE(a.size() == b.size());
230 REQUIRE(a[0] == b[0]);
231 REQUIRE(a.data() != b.data());
232 rcTempVector<int> c(a.data(), a.data() + a.size());
233 REQUIRE(c.size() == a.size());
234 REQUIRE(c[0] == a[0]);
235
236 rcTempVector<Incrementor> d(10);
237 Incrementor::Reset();
238 rcTempVector<Incrementor> e(d);
239 REQUIRE(Incrementor::constructions == 0);
240 REQUIRE(Incrementor::destructions == 0);
241 REQUIRE(Incrementor::copies == 10);
242
243 Incrementor::Reset();
244 rcTempVector<Incrementor> f(d.data(), d.data() + d.size());
245 REQUIRE(Incrementor::constructions == 0);
246 REQUIRE(Incrementor::destructions == 0);
247 REQUIRE(Incrementor::copies == 10);
248 }
249
250 SECTION("Type Requirements")
251 {
252 // This section verifies that we don't enforce unnecessary
253 // requirements on the types we hold.
254
255 // Implementing clear as resize(0) will cause this to fail
256 // as resize(0) requires T to be default constructible.
257 rcTempVector<NotDefaultConstructible> v;
258 v.clear();
259 }
260 }