Debian Codegraph @

debian/patches/0023-Some-unit-tests-for-heightfield-filtering-functions-.patch

raw
1 From fc18ed81940ea85af94b319c314e8220e467fe75 Mon Sep 17 00:00:00 2001
2 From: Graham Pentheny <[email protected]>
3 Date: Sun, 31 Dec 2023 14:55:41 -0500
4 Subject: [PATCH 23/36] Some unit tests for heightfield filtering functions
5 (#682)
6
7 This adds some unit tests for the functions in RecastFilter.cpp, and updates docs around these functions. This also splits up the Tests_Recast.cpp file into a few smaller, more focused files.
8 ---
9 Recast/Include/Recast.h | 22 +-
10 Recast/Source/RecastFilter.cpp | 32 +-
11 Tests/Recast/Bench_rcVector.cpp | 168 ++++++++++
12 Tests/Recast/Tests_Alloc.cpp | 260 ++++++++++++++++
13 Tests/Recast/Tests_Recast.cpp | 468 ++--------------------------
14 Tests/Recast/Tests_RecastFilter.cpp | 389 +++++++++++++++++++++++
15 6 files changed, 872 insertions(+), 467 deletions(-)
16 create mode 100644 Tests/Recast/Bench_rcVector.cpp
17 create mode 100644 Tests/Recast/Tests_Alloc.cpp
18 create mode 100644 Tests/Recast/Tests_RecastFilter.cpp
19
20 diff --git a/Recast/Include/Recast.h b/Recast/Include/Recast.h
21 index 2a5e075..2104aa5 100644
22 --- a/Recast/Include/Recast.h
23 +++ b/Recast/Include/Recast.h
24 @@ -321,6 +321,8 @@ struct rcHeightfield
25 float cs; ///< The size of each cell. (On the xz-plane.)
26 float ch; ///< The height of each cell. (The minimum increment along the y-axis.)
27 rcSpan** spans; ///< Heightfield of spans (width*height).
28 +
29 + // memory pool for rcSpan instances.
30 rcSpanPool* pools; ///< Linked list of span pools.
31 rcSpan* freelist; ///< The next free span.
32
33 @@ -998,23 +1000,21 @@ bool rcRasterizeTriangles(rcContext* context,
34 const float* verts, const unsigned char* triAreaIDs, int numTris,
35 rcHeightfield& heightfield, int flagMergeThreshold = 1);
36
37 -/// Marks non-walkable spans as walkable if their maximum is within @p walkableClimb of a walkable neighbor.
38 +/// Marks non-walkable spans as walkable if their maximum is within @p walkableClimb of the span below them.
39 ///
40 -/// Allows the formation of walkable regions that will flow over low lying
41 -/// objects such as curbs, and up structures such as stairways.
42 +/// This removes small obstacles that the agent would be able to walk over such as curbs, and also allows agents to move up structures such as stairs.
43 ///
44 -/// Two neighboring spans are walkable if: <tt>rcAbs(currentSpan.smax - neighborSpan.smax) < walkableClimb</tt>
45 +/// Obstacle spans are marked walkable if: <tt>obstacleSpan.smax - walkableSpan.smax < walkableClimb</tt>
46 ///
47 -/// @warning Will override the effect of #rcFilterLedgeSpans. So if both filters are used, call
48 -/// #rcFilterLedgeSpans after calling this filter.
49 +/// @warning Will override the effect of #rcFilterLedgeSpans. If both filters are used, call #rcFilterLedgeSpans only after applying this filter.
50 ///
51 /// @see rcHeightfield, rcConfig
52 ///
53 /// @ingroup recast
54 -/// @param[in,out] context The build context to use during the operation.
55 +/// @param[in,out] context The build context to use during the operation.
56 /// @param[in] walkableClimb Maximum ledge height that is considered to still be traversable.
57 /// [Limit: >=0] [Units: vx]
58 -/// @param[in,out] heightfield A fully built heightfield. (All spans have been added.)
59 +/// @param[in,out] heightfield A fully built heightfield. (All spans have been added.)
60 void rcFilterLowHangingWalkableObstacles(rcContext* context, int walkableClimb, rcHeightfield& heightfield);
61
62 /// Marks spans that are ledges as not-walkable.
63 @@ -1037,10 +1037,12 @@ void rcFilterLowHangingWalkableObstacles(rcContext* context, int walkableClimb,
64 /// @param[in,out] heightfield A fully built heightfield. (All spans have been added.)
65 void rcFilterLedgeSpans(rcContext* context, int walkableHeight, int walkableClimb, rcHeightfield& heightfield);
66
67 -/// Marks walkable spans as not walkable if the clearance above the span is less than the specified height.
68 +/// Marks walkable spans as not walkable if the clearance above the span is less than the specified walkableHeight.
69 ///
70 /// For this filter, the clearance above the span is the distance from the span's
71 -/// maximum to the next higher span's minimum. (Same grid column.)
72 +/// maximum to the minimum of the next higher span in the same column.
73 +/// If there is no higher span in the column, the clearance is computed as the
74 +/// distance from the top of the span to the maximum heightfield height.
75 ///
76 /// @see rcHeightfield, rcConfig
77 /// @ingroup recast
78 diff --git a/Recast/Source/RecastFilter.cpp b/Recast/Source/RecastFilter.cpp
79 index 8f3414b..1ecf858 100644
80 --- a/Recast/Source/RecastFilter.cpp
81 +++ b/Recast/Source/RecastFilter.cpp
82 @@ -21,6 +21,11 @@
83
84 #include <stdlib.h>
85
86 +namespace
87 +{
88 + const int MAX_HEIGHTFIELD_HEIGHT = 0xffff; // TODO (graham): Move this to a more visible constant and update usages.
89 +}
90 +
91 void rcFilterLowHangingWalkableObstacles(rcContext* context, const int walkableClimb, rcHeightfield& heightfield)
92 {
93 rcAssert(context);
94 @@ -59,8 +64,7 @@ void rcFilterLowHangingWalkableObstacles(rcContext* context, const int walkableC
95 }
96 }
97
98 -void rcFilterLedgeSpans(rcContext* context, const int walkableHeight, const int walkableClimb,
99 - rcHeightfield& heightfield)
100 +void rcFilterLedgeSpans(rcContext* context, const int walkableHeight, const int walkableClimb, rcHeightfield& heightfield)
101 {
102 rcAssert(context);
103
104 @@ -68,7 +72,6 @@ void rcFilterLedgeSpans(rcContext* context, const int walkableHeight, const int
105
106 const int xSize = heightfield.width;
107 const int zSize = heightfield.height;
108 - const int MAX_HEIGHT = 0xffff; // TODO (graham): Move this to a more visible constant and update usages.
109
110 // Mark border spans.
111 for (int z = 0; z < zSize; ++z)
112 @@ -84,10 +87,10 @@ void rcFilterLedgeSpans(rcContext* context, const int walkableHeight, const int
113 }
114
115 const int bot = (int)(span->smax);
116 - const int top = span->next ? (int)(span->next->smin) : MAX_HEIGHT;
117 + const int top = span->next ? (int)(span->next->smin) : MAX_HEIGHTFIELD_HEIGHT;
118
119 // Find neighbours minimum height.
120 - int minNeighborHeight = MAX_HEIGHT;
121 + int minNeighborHeight = MAX_HEIGHTFIELD_HEIGHT;
122
123 // Min and max height of accessible neighbours.
124 int accessibleNeighborMinHeight = span->smax;
125 @@ -106,7 +109,7 @@ void rcFilterLedgeSpans(rcContext* context, const int walkableHeight, const int
126
127 // From minus infinity to the first span.
128 const rcSpan* neighborSpan = heightfield.spans[dx + dz * xSize];
129 - int neighborTop = neighborSpan ? (int)neighborSpan->smin : MAX_HEIGHT;
130 + int neighborTop = neighborSpan ? (int)neighborSpan->smin : MAX_HEIGHTFIELD_HEIGHT;
131
132 // Skip neighbour if the gap between the spans is too small.
133 if (rcMin(top, neighborTop) - bot >= walkableHeight)
134 @@ -119,7 +122,7 @@ void rcFilterLedgeSpans(rcContext* context, const int walkableHeight, const int
135 for (neighborSpan = heightfield.spans[dx + dz * xSize]; neighborSpan; neighborSpan = neighborSpan->next)
136 {
137 int neighborBot = (int)neighborSpan->smax;
138 - neighborTop = neighborSpan->next ? (int)neighborSpan->next->smin : MAX_HEIGHT;
139 + neighborTop = neighborSpan->next ? (int)neighborSpan->next->smin : MAX_HEIGHTFIELD_HEIGHT;
140
141 // Skip neighbour if the gap between the spans is too small.
142 if (rcMin(top, neighborTop) - rcMax(bot, neighborBot) >= walkableHeight)
143 @@ -137,19 +140,16 @@ void rcFilterLedgeSpans(rcContext* context, const int walkableHeight, const int
144 {
145 break;
146 }
147 -
148 }
149 }
150 }
151
152 - // The current span is close to a ledge if the drop to any
153 - // neighbour span is less than the walkableClimb.
154 + // The current span is close to a ledge if the drop to any neighbour span is less than the walkableClimb.
155 if (minNeighborHeight < -walkableClimb)
156 {
157 span->area = RC_NULL_AREA;
158 }
159 - // If the difference between all neighbours is too large,
160 - // we are at steep slope, mark the span as ledge.
161 + // If the difference between all neighbours is too large, we are at steep slope, mark the span as ledge.
162 else if ((accessibleNeighborMaxHeight - accessibleNeighborMinHeight) > walkableClimb)
163 {
164 span->area = RC_NULL_AREA;
165 @@ -162,13 +162,11 @@ void rcFilterLedgeSpans(rcContext* context, const int walkableHeight, const int
166 void rcFilterWalkableLowHeightSpans(rcContext* context, const int walkableHeight, rcHeightfield& heightfield)
167 {
168 rcAssert(context);
169 -
170 rcScopedTimer timer(context, RC_TIMER_FILTER_WALKABLE);
171 -
172 +
173 const int xSize = heightfield.width;
174 const int zSize = heightfield.height;
175 - const int MAX_HEIGHT = 0xffff;
176 -
177 +
178 // Remove walkable flag from spans which do not have enough
179 // space above them for the agent to stand there.
180 for (int z = 0; z < zSize; ++z)
181 @@ -178,7 +176,7 @@ void rcFilterWalkableLowHeightSpans(rcContext* context, const int walkableHeight
182 for (rcSpan* span = heightfield.spans[x + z*xSize]; span; span = span->next)
183 {
184 const int bot = (int)(span->smax);
185 - const int top = span->next ? (int)(span->next->smin) : MAX_HEIGHT;
186 + const int top = span->next ? (int)(span->next->smin) : MAX_HEIGHTFIELD_HEIGHT;
187 if ((top - bot) < walkableHeight)
188 {
189 span->area = RC_NULL_AREA;
190 diff --git a/Tests/Recast/Bench_rcVector.cpp b/Tests/Recast/Bench_rcVector.cpp
191 new file mode 100644
192 index 0000000..46c1cee
193 --- /dev/null
194 +++ b/Tests/Recast/Bench_rcVector.cpp
195 @@ -0,0 +1,168 @@
196 +#include <stdio.h>
197 +#include <string.h>
198 +
199 +#include "catch2/catch_all.hpp"
200 +
201 +#include "Recast.h"
202 +#include "RecastAlloc.h"
203 +#include "RecastAssert.h"
204 +#include <vector>
205 +
206 +// TODO: Implement benchmarking for platforms other than posix.
207 +#ifdef __unix__
208 +#include <unistd.h>
209 +#ifdef _POSIX_TIMERS
210 +#include <time.h>
211 +#include <stdint.h>
212 +
213 +int64_t NowNanos() {
214 + struct timespec tp;
215 + clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp);
216 + return tp.tv_nsec + 1000000000LL * tp.tv_sec;
217 +}
218 +
219 +#define BM(name, iterations) \
220 + struct BM_ ## name { \
221 + static void Run() { \
222 + int64_t begin_time = NowNanos(); \
223 + for (int i = 0 ; i < iterations; i++) { \
224 + Body(); \
225 + } \
226 + int64_t nanos = NowNanos() - begin_time; \
227 + printf("BM_%-35s %ld iterations in %10ld nanos: %10.2f nanos/it\n", #name ":", (int64_t)iterations, nanos, double(nanos) / iterations); \
228 + } \
229 + static void Body(); \
230 + }; \
231 + TEST_CASE(#name) { \
232 + BM_ ## name::Run(); \
233 + } \
234 + void BM_ ## name::Body()
235 +
236 +const int64_t kNumLoops = 100;
237 +const int64_t kNumInserts = 100000;
238 +
239 +// Prevent compiler from eliding a calculation.
240 +// TODO: Implement for MSVC.
241 +template <typename T>
242 +void DoNotOptimize(T* v) {
243 + asm volatile ("" : "+r" (v));
244 +}
245 +
246 +BM(FlatArray_Push, kNumLoops)
247 +{
248 + int cap = 64;
249 + int* v = (int*)rcAlloc(cap * sizeof(int), RC_ALLOC_TEMP);
250 + for (int j = 0; j < kNumInserts; j++) {
251 + if (j == cap) {
252 + cap *= 2;
253 + int* tmp = (int*)rcAlloc(sizeof(int) * cap, RC_ALLOC_TEMP);
254 + memcpy(tmp, v, j * sizeof(int));
255 + rcFree(v);
256 + v = tmp;
257 + }
258 + v[j] = 2;
259 + }
260 +
261 + DoNotOptimize(v);
262 + rcFree(v);
263 +}
264 +BM(FlatArray_Fill, kNumLoops)
265 +{
266 + int* v = (int*)rcAlloc(sizeof(int) * kNumInserts, RC_ALLOC_TEMP);
267 + for (int j = 0; j < kNumInserts; j++) {
268 + v[j] = 2;
269 + }
270 +
271 + DoNotOptimize(v);
272 + rcFree(v);
273 +}
274 +BM(FlatArray_Memset, kNumLoops)
275 +{
276 + int* v = (int*)rcAlloc(sizeof(int) * kNumInserts, RC_ALLOC_TEMP);
277 + memset(v, 0, kNumInserts * sizeof(int));
278 +
279 + DoNotOptimize(v);
280 + rcFree(v);
281 +}
282 +
283 +BM(rcVector_Push, kNumLoops)
284 +{
285 + rcTempVector<int> v;
286 + for (int j = 0; j < kNumInserts; j++) {
287 + v.push_back(2);
288 + }
289 + DoNotOptimize(v.data());
290 +}
291 +BM(rcVector_PushPreallocated, kNumLoops)
292 +{
293 + rcTempVector<int> v;
294 + v.reserve(kNumInserts);
295 + for (int j = 0; j < kNumInserts; j++) {
296 + v.push_back(2);
297 + }
298 + DoNotOptimize(v.data());
299 +}
300 +BM(rcVector_Assign, kNumLoops)
301 +{
302 + rcTempVector<int> v;
303 + v.assign(kNumInserts, 2);
304 + DoNotOptimize(v.data());
305 +}
306 +BM(rcVector_AssignIndices, kNumLoops)
307 +{
308 + rcTempVector<int> v;
309 + v.resize(kNumInserts);
310 + for (int j = 0; j < kNumInserts; j++) {
311 + v[j] = 2;
312 + }
313 + DoNotOptimize(v.data());
314 +}
315 +BM(rcVector_Resize, kNumLoops)
316 +{
317 + rcTempVector<int> v;
318 + v.resize(kNumInserts, 2);
319 + DoNotOptimize(v.data());
320 +}
321 +
322 +BM(stdvector_Push, kNumLoops)
323 +{
324 + std::vector<int> v;
325 + for (int j = 0; j < kNumInserts; j++) {
326 + v.push_back(2);
327 + }
328 + DoNotOptimize(v.data());
329 +}
330 +BM(stdvector_PushPreallocated, kNumLoops)
331 +{
332 + std::vector<int> v;
333 + v.reserve(kNumInserts);
334 + for (int j = 0; j < kNumInserts; j++) {
335 + v.push_back(2);
336 + }
337 + DoNotOptimize(v.data());
338 +}
339 +BM(stdvector_Assign, kNumLoops)
340 +{
341 + std::vector<int> v;
342 + v.assign(kNumInserts, 2);
343 + DoNotOptimize(v.data());
344 +}
345 +BM(stdvector_AssignIndices, kNumLoops)
346 +{
347 + std::vector<int> v;
348 + v.resize(kNumInserts);
349 + for (int j = 0; j < kNumInserts; j++) {
350 + v[j] = 2;
351 + }
352 + DoNotOptimize(v.data());
353 +}
354 +BM(stdvector_Resize, kNumLoops)
355 +{
356 + std::vector<int> v;
357 + v.resize(kNumInserts, 2);
358 + DoNotOptimize(v.data());
359 +}
360 +
361 +#undef BM
362 +#endif // _POSIX_TIMERS
363 +#endif // __unix__
364 diff --git a/Tests/Recast/Tests_Alloc.cpp b/Tests/Recast/Tests_Alloc.cpp
365 new file mode 100644
366 index 0000000..2e7b2eb
367 --- /dev/null
368 +++ b/Tests/Recast/Tests_Alloc.cpp
369 @@ -0,0 +1,260 @@
370 +#include <stdio.h>
371 +#include <string.h>
372 +
373 +#include "catch2/catch_all.hpp"
374 +
375 +#include "RecastAlloc.h"
376 +#include "RecastAssert.h"
377 +
378 +/// Used to verify that rcVector constructs/destroys objects correctly.
379 +struct Incrementor {
380 + static int constructions;
381 + static int destructions;
382 + static int copies;
383 + Incrementor() { constructions++; }
384 + ~Incrementor() { destructions++; }
385 + Incrementor(const Incrementor&) { copies++; }
386 + Incrementor& operator=(const Incrementor&); // Deleted assignment.
387 +
388 + static void Reset() {
389 + constructions = 0;
390 + destructions = 0;
391 + copies = 0;
392 + }
393 +};
394 +
395 +int Incrementor::constructions = 0;
396 +int Incrementor::destructions = 0;
397 +int Incrementor::copies = 0;
398 +
399 +const int kMaxAllocSize = 1024;
400 +const unsigned char kClearValue = 0xff;
401 +
402 +/// Simple alloc/free that clears the memory on free..
403 +void* AllocAndInit(size_t size, rcAllocHint) {
404 + rcAssert(kMaxAllocSize >= size);
405 + return memset(malloc(kMaxAllocSize), 0, kMaxAllocSize);
406 +}
407 +
408 +void FreeAndClear(void* mem) {
409 + if (mem) {
410 + memset(mem, kClearValue, kMaxAllocSize);
411 + }
412 + free(mem);
413 +}
414 +
415 +// Verifies that memory has been initialized by AllocAndInit, and not cleared by FreeAndClear.
416 +struct Copier {
417 + const static int kAlive;
418 + const static int kDead;
419 + Copier() : value(kAlive) {}
420 +
421 + // checks that the source of the copy is valid.
422 + Copier(const Copier& other) : value(kAlive) {
423 + other.Verify();
424 + }
425 + Copier& operator=(const Copier&);
426 +
427 + // Marks the value as dead.
428 + ~Copier() { value = kDead; }
429 + void Verify() const {
430 + REQUIRE(value == kAlive);
431 + }
432 + volatile int value;
433 +};
434 +
435 +const int Copier::kAlive = 0x1f;
436 +const int Copier::kDead = 0xde;
437 +
438 +struct NotDefaultConstructible {
439 + NotDefaultConstructible(int) {}
440 +};
441 +
442 +TEST_CASE("rcVector", "[recast, alloc]")
443 +{
444 + SECTION("Vector basics.")
445 + {
446 + rcTempVector<int> vec;
447 + REQUIRE(vec.size() == 0);
448 + vec.push_back(10);
449 + vec.push_back(12);
450 + REQUIRE(vec.size() == 2);
451 + REQUIRE(vec.capacity() >= 2);
452 + REQUIRE(vec[0] == 10);
453 + REQUIRE(vec[1] == 12);
454 + vec.pop_back();
455 + REQUIRE(vec.size() == 1);
456 + REQUIRE(vec[0] == 10);
457 + vec.pop_back();
458 + REQUIRE(vec.size() == 0);
459 + vec.resize(100, 5);
460 + REQUIRE(vec.size() == 100);
461 + for (int i = 0; i < 100; i++) {
462 + REQUIRE(vec[i] == 5);
463 + vec[i] = i;
464 + }
465 + for (int i = 0; i < 100; i++) {
466 + REQUIRE(vec[i] == i);
467 + }
468 + }
469 +
470 + SECTION("Constructors/Destructors")
471 + {
472 + Incrementor::Reset();
473 + rcTempVector<Incrementor> vec;
474 + REQUIRE(Incrementor::constructions == 0);
475 + REQUIRE(Incrementor::destructions == 0);
476 + REQUIRE(Incrementor::copies == 0);
477 + vec.push_back(Incrementor());
478 + // push_back() may create and copy objects internally.
479 + REQUIRE(Incrementor::constructions == 1);
480 + REQUIRE(Incrementor::destructions >= 1);
481 + // REQUIRE(Incrementor::copies >= 2);
482 +
483 + vec.clear();
484 + Incrementor::Reset();
485 + vec.resize(100);
486 + // Initialized with default instance. Temporaries may be constructed, then destroyed.
487 + REQUIRE(Incrementor::constructions == 100);
488 + REQUIRE(Incrementor::destructions == 0);
489 + REQUIRE(Incrementor::copies == 0);
490 +
491 + Incrementor::Reset();
492 + for (int i = 0; i < 100; i++) {
493 + REQUIRE(Incrementor::destructions == i);
494 + vec.pop_back();
495 + }
496 + REQUIRE(Incrementor::constructions == 0);
497 + REQUIRE(Incrementor::destructions == 100);
498 + REQUIRE(Incrementor::copies == 0);
499 +
500 + vec.resize(100);
501 + Incrementor::Reset();
502 + vec.clear();
503 + // One temp object is constructed for the default argument of resize().
504 + REQUIRE(Incrementor::constructions == 0);
505 + REQUIRE(Incrementor::destructions == 100);
506 + REQUIRE(Incrementor::copies == 0);
507 +
508 + Incrementor::Reset();
509 + vec.resize(100, Incrementor());
510 + REQUIRE(Incrementor::constructions == 1);
511 + REQUIRE(Incrementor::destructions == 1);
512 + REQUIRE(Incrementor::copies == 100);
513 + }
514 +
515 + SECTION("Copying Contents")
516 + {
517 +
518 + // veriyf event counts after doubling size -- should require a lot of copying and destroying.
519 + rcTempVector<Incrementor> vec;
520 + Incrementor::Reset();
521 + vec.resize(100);
522 + REQUIRE(Incrementor::constructions == 100);
523 + REQUIRE(Incrementor::destructions == 0);
524 + REQUIRE(Incrementor::copies == 0);
525 + Incrementor::Reset();
526 + vec.resize(200);
527 + REQUIRE(vec.size() == vec.capacity());
528 + REQUIRE(Incrementor::constructions == 100); // Construc new elements.
529 + REQUIRE(Incrementor::destructions == 100); // Destroy old contents.
530 + REQUIRE(Incrementor::copies == 100); // Copy old elements into new array.
531 + }
532 +
533 + SECTION("Swap")
534 + {
535 + rcTempVector<int> a(10, 0xa);
536 + rcTempVector<int> b;
537 +
538 + int* a_data = a.data();
539 + int* b_data = b.data();
540 +
541 + a.swap(b);
542 + REQUIRE(a.size() == 0);
543 + REQUIRE(b.size() == 10);
544 + REQUIRE(b[0] == 0xa);
545 + REQUIRE(b[9] == 0xa);
546 + REQUIRE(a.data() == b_data);
547 + REQUIRE(b.data() == a_data);
548 + }
549 +
550 + SECTION("Overlapping init")
551 + {
552 + rcAllocSetCustom(&AllocAndInit, &FreeAndClear);
553 + rcTempVector<Copier> vec;
554 + // Force a realloc during push_back().
555 + vec.resize(64);
556 + REQUIRE(vec.capacity() == vec.size());
557 + REQUIRE(vec.capacity() > 0);
558 + REQUIRE(vec.size() == vec.capacity());
559 +
560 + // Don't crash.
561 + vec.push_back(vec[0]);
562 + rcAllocSetCustom(NULL, NULL);
563 + }
564 +
565 + SECTION("Vector Destructor")
566 + {
567 + {
568 + rcTempVector<Incrementor> vec;
569 + vec.resize(10);
570 + Incrementor::Reset();
571 + }
572 + REQUIRE(Incrementor::destructions == 10);
573 + }
574 +
575 + SECTION("Assign")
576 + {
577 + rcTempVector<int> a(10, 0xa);
578 + a.assign(5, 0xb);
579 + REQUIRE(a.size() == 5);
580 + REQUIRE(a[0] == 0xb);
581 + REQUIRE(a[4] == 0xb);
582 + a.assign(15, 0xc);
583 + REQUIRE(a.size() == 15);
584 + REQUIRE(a[0] == 0xc);
585 + REQUIRE(a[14] == 0xc);
586 +
587 + rcTempVector<int> b;
588 + b.assign(a.data(), a.data() + a.size());
589 + REQUIRE(b.size() == a.size());
590 + REQUIRE(b[0] == a[0]);
591 + }
592 +
593 + SECTION("Copy")
594 + {
595 + rcTempVector<int> a(10, 0xa);
596 + rcTempVector<int> b(a);
597 + REQUIRE(a.size() == 10);
598 + REQUIRE(a.size() == b.size());
599 + REQUIRE(a[0] == b[0]);
600 + REQUIRE(a.data() != b.data());
601 + rcTempVector<int> c(a.data(), a.data() + a.size());
602 + REQUIRE(c.size() == a.size());
603 + REQUIRE(c[0] == a[0]);
604 +
605 + rcTempVector<Incrementor> d(10);
606 + Incrementor::Reset();
607 + rcTempVector<Incrementor> e(d);
608 + REQUIRE(Incrementor::constructions == 0);
609 + REQUIRE(Incrementor::destructions == 0);
610 + REQUIRE(Incrementor::copies == 10);
611 +
612 + Incrementor::Reset();
613 + rcTempVector<Incrementor> f(d.data(), d.data() + d.size());
614 + REQUIRE(Incrementor::constructions == 0);
615 + REQUIRE(Incrementor::destructions == 0);
616 + REQUIRE(Incrementor::copies == 10);
617 + }
618 +
619 + SECTION("Type Requirements")
620 + {
621 + // This section verifies that we don't enforce unnecessary
622 + // requirements on the types we hold.
623 +
624 + // Implementing clear as resize(0) will cause this to fail
625 + // as resize(0) requires T to be default constructible.
626 + rcTempVector<NotDefaultConstructible> v;
627 + v.clear();
628 + }
629 +}
630 diff --git a/Tests/Recast/Tests_Recast.cpp b/Tests/Recast/Tests_Recast.cpp
631 index 34c61d0..62d1507 100644
632 --- a/Tests/Recast/Tests_Recast.cpp
633 +++ b/Tests/Recast/Tests_Recast.cpp
634 @@ -4,13 +4,8 @@
635 #include "catch2/catch_all.hpp"
636
637 #include "Recast.h"
638 -#include "RecastAlloc.h"
639 -#include "RecastAssert.h"
640
641 -// For comparing to rcVector in benchmarks.
642 -#include <vector>
643 -
644 -TEST_CASE("rcSwap")
645 +TEST_CASE("rcSwap", "[recast]")
646 {
647 SECTION("Swap two values")
648 {
649 @@ -22,7 +17,7 @@ TEST_CASE("rcSwap")
650 }
651 }
652
653 -TEST_CASE("rcMin")
654 +TEST_CASE("rcMin", "[recast]")
655 {
656 SECTION("Min returns the lowest value.")
657 {
658 @@ -36,7 +31,7 @@ TEST_CASE("rcMin")
659 }
660 }
661
662 -TEST_CASE("rcMax")
663 +TEST_CASE("rcMax", "[recast]")
664 {
665 SECTION("Max returns the greatest value.")
666 {
667 @@ -50,7 +45,7 @@ TEST_CASE("rcMax")
668 }
669 }
670
671 -TEST_CASE("rcAbs")
672 +TEST_CASE("rcAbs", "[recast]")
673 {
674 SECTION("Abs returns the absolute value.")
675 {
676 @@ -60,7 +55,7 @@ TEST_CASE("rcAbs")
677 }
678 }
679
680 -TEST_CASE("rcSqr")
681 +TEST_CASE("rcSqr", "[recast]")
682 {
683 SECTION("Sqr squares a number")
684 {
685 @@ -70,7 +65,7 @@ TEST_CASE("rcSqr")
686 }
687 }
688
689 -TEST_CASE("rcClamp")
690 +TEST_CASE("rcClamp", "[recast]")
691 {
692 SECTION("Higher than range")
693 {
694 @@ -88,7 +83,7 @@ TEST_CASE("rcClamp")
695 }
696 }
697
698 -TEST_CASE("rcSqrt")
699 +TEST_CASE("rcSqrt", "[recast]")
700 {
701 SECTION("Sqrt gets the sqrt of a number")
702 {
703 @@ -97,7 +92,7 @@ TEST_CASE("rcSqrt")
704 }
705 }
706
707 -TEST_CASE("rcVcross")
708 +TEST_CASE("rcVcross", "[recast]")
709 {
710 SECTION("Computes cross product")
711 {
712 @@ -121,7 +116,7 @@ TEST_CASE("rcVcross")
713 }
714 }
715
716 -TEST_CASE("rcVdot")
717 +TEST_CASE("rcVdot", "[recast]")
718 {
719 SECTION("Dot normalized vector with itself")
720 {
721 @@ -140,7 +135,7 @@ TEST_CASE("rcVdot")
722 }
723 }
724
725 -TEST_CASE("rcVmad")
726 +TEST_CASE("rcVmad", "[recast]")
727 {
728 SECTION("scaled add two vectors")
729 {
730 @@ -165,7 +160,7 @@ TEST_CASE("rcVmad")
731 }
732 }
733
734 -TEST_CASE("rcVadd")
735 +TEST_CASE("rcVadd", "[recast]")
736 {
737 SECTION("add two vectors")
738 {
739 @@ -179,7 +174,7 @@ TEST_CASE("rcVadd")
740 }
741 }
742
743 -TEST_CASE("rcVsub")
744 +TEST_CASE("rcVsub", "[recast]")
745 {
746 SECTION("subtract two vectors")
747 {
748 @@ -193,7 +188,7 @@ TEST_CASE("rcVsub")
749 }
750 }
751
752 -TEST_CASE("rcVmin")
753 +TEST_CASE("rcVmin", "[recast]")
754 {
755 SECTION("selects the min component from the vectors")
756 {
757 @@ -226,7 +221,7 @@ TEST_CASE("rcVmin")
758 }
759 }
760
761 -TEST_CASE("rcVmax")
762 +TEST_CASE("rcVmax", "[recast]")
763 {
764 SECTION("selects the max component from the vectors")
765 {
766 @@ -259,7 +254,7 @@ TEST_CASE("rcVmax")
767 }
768 }
769
770 -TEST_CASE("rcVcopy")
771 +TEST_CASE("rcVcopy", "[recast]")
772 {
773 SECTION("copies a vector into another vector")
774 {
775 @@ -275,7 +270,7 @@ TEST_CASE("rcVcopy")
776 }
777 }
778
779 -TEST_CASE("rcVdist")
780 +TEST_CASE("rcVdist", "[recast]")
781 {
782 SECTION("distance between two vectors")
783 {
784 @@ -296,7 +291,7 @@ TEST_CASE("rcVdist")
785 }
786 }
787
788 -TEST_CASE("rcVdistSqr")
789 +TEST_CASE("rcVdistSqr", "[recast]")
790 {
791 SECTION("squared distance between two vectors")
792 {
793 @@ -317,7 +312,7 @@ TEST_CASE("rcVdistSqr")
794 }
795 }
796
797 -TEST_CASE("rcVnormalize")
798 +TEST_CASE("rcVnormalize", "[recast]")
799 {
800 SECTION("normalizing reduces magnitude to 1")
801 {
802 @@ -331,7 +326,7 @@ TEST_CASE("rcVnormalize")
803 }
804 }
805
806 -TEST_CASE("rcCalcBounds")
807 +TEST_CASE("rcCalcBounds", "[recast]")
808 {
809 SECTION("bounds of one vector")
810 {
811 @@ -369,7 +364,7 @@ TEST_CASE("rcCalcBounds")
812 }
813 }
814
815 -TEST_CASE("rcCalcGridSize")
816 +TEST_CASE("rcCalcGridSize", "[recast]")
817 {
818 SECTION("computes the size of an x & z axis grid")
819 {
820 @@ -393,7 +388,7 @@ TEST_CASE("rcCalcGridSize")
821 }
822 }
823
824 -TEST_CASE("rcCreateHeightfield")
825 +TEST_CASE("rcCreateHeightfield", "[recast]")
826 {
827 SECTION("create a heightfield")
828 {
829 @@ -439,7 +434,7 @@ TEST_CASE("rcCreateHeightfield")
830 }
831 }
832
833 -TEST_CASE("rcMarkWalkableTriangles")
834 +TEST_CASE("rcMarkWalkableTriangles", "[recast]")
835 {
836 rcContext* ctx = 0;
837 float walkableSlopeAngle = 45;
838 @@ -481,7 +476,7 @@ TEST_CASE("rcMarkWalkableTriangles")
839 }
840 }
841
842 -TEST_CASE("rcClearUnwalkableTriangles")
843 +TEST_CASE("rcClearUnwalkableTriangles", "[recast]")
844 {
845 rcContext* ctx = 0;
846 float walkableSlopeAngle = 45;
847 @@ -516,7 +511,7 @@ TEST_CASE("rcClearUnwalkableTriangles")
848 }
849 }
850
851 -TEST_CASE("rcAddSpan")
852 +TEST_CASE("rcAddSpan", "[recast]")
853 {
854 rcContext ctx(false);
855
856 @@ -605,7 +600,7 @@ TEST_CASE("rcAddSpan")
857 }
858 }
859
860 -TEST_CASE("rcRasterizeTriangle")
861 +TEST_CASE("rcRasterizeTriangle", "[recast]")
862 {
863 rcContext ctx;
864 float verts[] = {
865 @@ -657,7 +652,7 @@ TEST_CASE("rcRasterizeTriangle")
866 }
867 }
868
869 -TEST_CASE("rcRasterizeTriangle overlapping bb but non-overlapping triangle")
870 +TEST_CASE("rcRasterizeTriangle overlapping bb but non-overlapping triangle", "[recast]")
871 {
872 // This is a minimal repro case for the issue fixed in PR #476 (https://github.com/recastnavigation/recastnavigation/pull/476)
873 rcContext ctx;
874 @@ -694,7 +689,7 @@ TEST_CASE("rcRasterizeTriangle overlapping bb but non-overlapping triangle")
875 }
876 }
877
878 -TEST_CASE("rcRasterizeTriangle smaller than half a voxel size in x")
879 +TEST_CASE("rcRasterizeTriangle smaller than half a voxel size in x", "[recast]")
880 {
881 SECTION("Skinny triangle along x axis")
882 {
883 @@ -761,7 +756,7 @@ TEST_CASE("rcRasterizeTriangle smaller than half a voxel size in x")
884 }
885 }
886
887 -TEST_CASE("rcRasterizeTriangles")
888 +TEST_CASE("rcRasterizeTriangles", "[recast]")
889 {
890 rcContext ctx;
891 float verts[] = {
892 @@ -940,410 +935,3 @@ TEST_CASE("rcRasterizeTriangles")
893 REQUIRE(!solid.spans[1 + 2 * width]->next);
894 }
895 }
896 -
897 -// Used to verify that rcVector constructs/destroys objects correctly.
898 -struct Incrementor {
899 - static int constructions;
900 - static int destructions;
901 - static int copies;
902 - Incrementor() { constructions++; }
903 - ~Incrementor() { destructions++; }
904 - Incrementor(const Incrementor&) { copies++; }
905 - Incrementor& operator=(const Incrementor&); // Deleted assignment.
906 -
907 - static void Reset() {
908 - constructions = 0;
909 - destructions = 0;
910 - copies = 0;
911 - }
912 -};
913 -int Incrementor::constructions = 0;
914 -int Incrementor::destructions = 0;
915 -int Incrementor::copies = 0;
916 -
917 -const int kMaxAllocSize = 1024;
918 -const unsigned char kClearValue = 0xff;
919 -// Simple alloc/free that clears the memory on free..
920 -void* AllocAndInit(size_t size, rcAllocHint) {
921 - rcAssert(kMaxAllocSize >= size);
922 - return memset(malloc(kMaxAllocSize), 0, kMaxAllocSize);
923 -}
924 -void FreeAndClear(void* mem) {
925 - if (mem) {
926 - memset(mem, kClearValue, kMaxAllocSize);
927 - }
928 - free(mem);
929 -}
930 -// Verifies that memory has been initialized by AllocAndInit, and not cleared by FreeAndClear.
931 -struct Copier {
932 - const static int kAlive;
933 - const static int kDead;
934 - Copier() : value(kAlive) {}
935 -
936 - // checks that the source of the copy is valid.
937 - Copier(const Copier& other) : value(kAlive) {
938 - other.Verify();
939 - }
940 - Copier& operator=(const Copier&);
941 -
942 - // Marks the value as dead.
943 - ~Copier() { value = kDead; }
944 - void Verify() const {
945 - REQUIRE(value == kAlive);
946 - }
947 - volatile int value;
948 -};
949 -const int Copier::kAlive = 0x1f;
950 -const int Copier::kDead = 0xde;
951 -
952 -struct NotDefaultConstructible {
953 - NotDefaultConstructible(int) {}
954 -};
955 -
956 -TEST_CASE("rcVector")
957 -{
958 - SECTION("Vector basics.")
959 - {
960 - rcTempVector<int> vec;
961 - REQUIRE(vec.size() == 0);
962 - vec.push_back(10);
963 - vec.push_back(12);
964 - REQUIRE(vec.size() == 2);
965 - REQUIRE(vec.capacity() >= 2);
966 - REQUIRE(vec[0] == 10);
967 - REQUIRE(vec[1] == 12);
968 - vec.pop_back();
969 - REQUIRE(vec.size() == 1);
970 - REQUIRE(vec[0] == 10);
971 - vec.pop_back();
972 - REQUIRE(vec.size() == 0);
973 - vec.resize(100, 5);
974 - REQUIRE(vec.size() == 100);
975 - for (int i = 0; i < 100; i++) {
976 - REQUIRE(vec[i] == 5);
977 - vec[i] = i;
978 - }
979 - for (int i = 0; i < 100; i++) {
980 - REQUIRE(vec[i] == i);
981 - }
982 - }
983 -
984 - SECTION("Constructors/Destructors")
985 - {
986 - Incrementor::Reset();
987 - rcTempVector<Incrementor> vec;
988 - REQUIRE(Incrementor::constructions == 0);
989 - REQUIRE(Incrementor::destructions == 0);
990 - REQUIRE(Incrementor::copies == 0);
991 - vec.push_back(Incrementor());
992 - // push_back() may create and copy objects internally.
993 - REQUIRE(Incrementor::constructions == 1);
994 - REQUIRE(Incrementor::destructions >= 1);
995 - // REQUIRE(Incrementor::copies >= 2);
996 -
997 - vec.clear();
998 - Incrementor::Reset();
999 - vec.resize(100);
1000 - // Initialized with default instance. Temporaries may be constructed, then destroyed.
1001 - REQUIRE(Incrementor::constructions == 100);
1002 - REQUIRE(Incrementor::destructions == 0);
1003 - REQUIRE(Incrementor::copies == 0);
1004 -
1005 - Incrementor::Reset();
1006 - for (int i = 0; i < 100; i++) {
1007 - REQUIRE(Incrementor::destructions == i);
1008 - vec.pop_back();
1009 - }
1010 - REQUIRE(Incrementor::constructions == 0);
1011 - REQUIRE(Incrementor::destructions == 100);
1012 - REQUIRE(Incrementor::copies == 0);
1013 -
1014 - vec.resize(100);
1015 - Incrementor::Reset();
1016 - vec.clear();
1017 - // One temp object is constructed for the default argument of resize().
1018 - REQUIRE(Incrementor::constructions == 0);
1019 - REQUIRE(Incrementor::destructions == 100);
1020 - REQUIRE(Incrementor::copies == 0);
1021 -
1022 - Incrementor::Reset();
1023 - vec.resize(100, Incrementor());
1024 - REQUIRE(Incrementor::constructions == 1);
1025 - REQUIRE(Incrementor::destructions == 1);
1026 - REQUIRE(Incrementor::copies == 100);
1027 - }
1028 -
1029 - SECTION("Copying Contents")
1030 - {
1031 -
1032 - // veriyf event counts after doubling size -- should require a lot of copying and destroying.
1033 - rcTempVector<Incrementor> vec;
1034 - Incrementor::Reset();
1035 - vec.resize(100);
1036 - REQUIRE(Incrementor::constructions == 100);
1037 - REQUIRE(Incrementor::destructions == 0);
1038 - REQUIRE(Incrementor::copies == 0);
1039 - Incrementor::Reset();
1040 - vec.resize(200);
1041 - REQUIRE(vec.size() == vec.capacity());
1042 - REQUIRE(Incrementor::constructions == 100); // Construc new elements.
1043 - REQUIRE(Incrementor::destructions == 100); // Destroy old contents.
1044 - REQUIRE(Incrementor::copies == 100); // Copy old elements into new array.
1045 - }
1046 -
1047 - SECTION("Swap")
1048 - {
1049 - rcTempVector<int> a(10, 0xa);
1050 - rcTempVector<int> b;
1051 -
1052 - int* a_data = a.data();
1053 - int* b_data = b.data();
1054 -
1055 - a.swap(b);
1056 - REQUIRE(a.size() == 0);
1057 - REQUIRE(b.size() == 10);
1058 - REQUIRE(b[0] == 0xa);
1059 - REQUIRE(b[9] == 0xa);
1060 - REQUIRE(a.data() == b_data);
1061 - REQUIRE(b.data() == a_data);
1062 - }
1063 -
1064 - SECTION("Overlapping init")
1065 - {
1066 - rcAllocSetCustom(&AllocAndInit, &FreeAndClear);
1067 - rcTempVector<Copier> vec;
1068 - // Force a realloc during push_back().
1069 - vec.resize(64);
1070 - REQUIRE(vec.capacity() == vec.size());
1071 - REQUIRE(vec.capacity() > 0);
1072 - REQUIRE(vec.size() == vec.capacity());
1073 -
1074 - // Don't crash.
1075 - vec.push_back(vec[0]);
1076 - rcAllocSetCustom(NULL, NULL);
1077 - }
1078 -
1079 - SECTION("Vector Destructor")
1080 - {
1081 - {
1082 - rcTempVector<Incrementor> vec;
1083 - vec.resize(10);
1084 - Incrementor::Reset();
1085 - }
1086 - REQUIRE(Incrementor::destructions == 10);
1087 - }
1088 -
1089 - SECTION("Assign")
1090 - {
1091 - rcTempVector<int> a(10, 0xa);
1092 - a.assign(5, 0xb);
1093 - REQUIRE(a.size() == 5);
1094 - REQUIRE(a[0] == 0xb);
1095 - REQUIRE(a[4] == 0xb);
1096 - a.assign(15, 0xc);
1097 - REQUIRE(a.size() == 15);
1098 - REQUIRE(a[0] == 0xc);
1099 - REQUIRE(a[14] == 0xc);
1100 -
1101 - rcTempVector<int> b;
1102 - b.assign(a.data(), a.data() + a.size());
1103 - REQUIRE(b.size() == a.size());
1104 - REQUIRE(b[0] == a[0]);
1105 - }
1106 -
1107 - SECTION("Copy")
1108 - {
1109 - rcTempVector<int> a(10, 0xa);
1110 - rcTempVector<int> b(a);
1111 - REQUIRE(a.size() == 10);
1112 - REQUIRE(a.size() == b.size());
1113 - REQUIRE(a[0] == b[0]);
1114 - REQUIRE(a.data() != b.data());
1115 - rcTempVector<int> c(a.data(), a.data() + a.size());
1116 - REQUIRE(c.size() == a.size());
1117 - REQUIRE(c[0] == a[0]);
1118 -
1119 - rcTempVector<Incrementor> d(10);
1120 - Incrementor::Reset();
1121 - rcTempVector<Incrementor> e(d);
1122 - REQUIRE(Incrementor::constructions == 0);
1123 - REQUIRE(Incrementor::destructions == 0);
1124 - REQUIRE(Incrementor::copies == 10);
1125 -
1126 - Incrementor::Reset();
1127 - rcTempVector<Incrementor> f(d.data(), d.data() + d.size());
1128 - REQUIRE(Incrementor::constructions == 0);
1129 - REQUIRE(Incrementor::destructions == 0);
1130 - REQUIRE(Incrementor::copies == 10);
1131 - }
1132 -
1133 - SECTION("Type Requirements")
1134 - {
1135 - // This section verifies that we don't enforce unnecessary
1136 - // requirements on the types we hold.
1137 -
1138 - // Implementing clear as resize(0) will cause this to fail
1139 - // as resize(0) requires T to be default constructible.
1140 - rcTempVector<NotDefaultConstructible> v;
1141 - v.clear();
1142 - }
1143 -}
1144 -
1145 -// TODO: Implement benchmarking for platforms other than posix.
1146 -#ifdef __unix__
1147 -#include <unistd.h>
1148 -#ifdef _POSIX_TIMERS
1149 -#include <time.h>
1150 -#include <stdint.h>
1151 -
1152 -int64_t NowNanos() {
1153 - struct timespec tp;
1154 - clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp);
1155 - return tp.tv_nsec + 1000000000LL * tp.tv_sec;
1156 -}
1157 -
1158 -#define BM(name, iterations) \
1159 - struct BM_ ## name { \
1160 - static void Run() { \
1161 - int64_t begin_time = NowNanos(); \
1162 - for (int i = 0 ; i < iterations; i++) { \
1163 - Body(); \
1164 - } \
1165 - int64_t nanos = NowNanos() - begin_time; \
1166 - printf("BM_%-35s %ld iterations in %10ld nanos: %10.2f nanos/it\n", #name ":", (int64_t)iterations, nanos, double(nanos) / iterations); \
1167 - } \
1168 - static void Body(); \
1169 - }; \
1170 - TEST_CASE(#name) { \
1171 - BM_ ## name::Run(); \
1172 - } \
1173 - void BM_ ## name::Body()
1174 -
1175 -const int64_t kNumLoops = 100;
1176 -const int64_t kNumInserts = 100000;
1177 -
1178 -// Prevent compiler from eliding a calculation.
1179 -// TODO: Implement for MSVC.
1180 -template <typename T>
1181 -void DoNotOptimize(T* v) {
1182 - asm volatile ("" : "+r" (v));
1183 -}
1184 -
1185 -BM(FlatArray_Push, kNumLoops)
1186 -{
1187 - int cap = 64;
1188 - int* v = (int*)rcAlloc(cap * sizeof(int), RC_ALLOC_TEMP);
1189 - for (int j = 0; j < kNumInserts; j++) {
1190 - if (j == cap) {
1191 - cap *= 2;
1192 - int* tmp = (int*)rcAlloc(sizeof(int) * cap, RC_ALLOC_TEMP);
1193 - memcpy(tmp, v, j * sizeof(int));
1194 - rcFree(v);
1195 - v = tmp;
1196 - }
1197 - v[j] = 2;
1198 - }
1199 -
1200 - DoNotOptimize(v);
1201 - rcFree(v);
1202 -}
1203 -BM(FlatArray_Fill, kNumLoops)
1204 -{
1205 - int* v = (int*)rcAlloc(sizeof(int) * kNumInserts, RC_ALLOC_TEMP);
1206 - for (int j = 0; j < kNumInserts; j++) {
1207 - v[j] = 2;
1208 - }
1209 -
1210 - DoNotOptimize(v);
1211 - rcFree(v);
1212 -}
1213 -BM(FlatArray_Memset, kNumLoops)
1214 -{
1215 - int* v = (int*)rcAlloc(sizeof(int) * kNumInserts, RC_ALLOC_TEMP);
1216 - memset(v, 0, kNumInserts * sizeof(int));
1217 -
1218 - DoNotOptimize(v);
1219 - rcFree(v);
1220 -}
1221 -
1222 -BM(rcVector_Push, kNumLoops)
1223 -{
1224 - rcTempVector<int> v;
1225 - for (int j = 0; j < kNumInserts; j++) {
1226 - v.push_back(2);
1227 - }
1228 - DoNotOptimize(v.data());
1229 -}
1230 -BM(rcVector_PushPreallocated, kNumLoops)
1231 -{
1232 - rcTempVector<int> v;
1233 - v.reserve(kNumInserts);
1234 - for (int j = 0; j < kNumInserts; j++) {
1235 - v.push_back(2);
1236 - }
1237 - DoNotOptimize(v.data());
1238 -}
1239 -BM(rcVector_Assign, kNumLoops)
1240 -{
1241 - rcTempVector<int> v;
1242 - v.assign(kNumInserts, 2);
1243 - DoNotOptimize(v.data());
1244 -}
1245 -BM(rcVector_AssignIndices, kNumLoops)
1246 -{
1247 - rcTempVector<int> v;
1248 - v.resize(kNumInserts);
1249 - for (int j = 0; j < kNumInserts; j++) {
1250 - v[j] = 2;
1251 - }
1252 - DoNotOptimize(v.data());
1253 -}
1254 -BM(rcVector_Resize, kNumLoops)
1255 -{
1256 - rcTempVector<int> v;
1257 - v.resize(kNumInserts, 2);
1258 - DoNotOptimize(v.data());
1259 -}
1260 -
1261 -BM(stdvector_Push, kNumLoops)
1262 -{
1263 - std::vector<int> v;
1264 - for (int j = 0; j < kNumInserts; j++) {
1265 - v.push_back(2);
1266 - }
1267 - DoNotOptimize(v.data());
1268 -}
1269 -BM(stdvector_PushPreallocated, kNumLoops)
1270 -{
1271 - std::vector<int> v;
1272 - v.reserve(kNumInserts);
1273 - for (int j = 0; j < kNumInserts; j++) {
1274 - v.push_back(2);
1275 - }
1276 - DoNotOptimize(v.data());
1277 -}
1278 -BM(stdvector_Assign, kNumLoops)
1279 -{
1280 - std::vector<int> v;
1281 - v.assign(kNumInserts, 2);
1282 - DoNotOptimize(v.data());
1283 -}
1284 -BM(stdvector_AssignIndices, kNumLoops)
1285 -{
1286 - std::vector<int> v;
1287 - v.resize(kNumInserts);
1288 - for (int j = 0; j < kNumInserts; j++) {
1289 - v[j] = 2;
1290 - }
1291 - DoNotOptimize(v.data());
1292 -}
1293 -BM(stdvector_Resize, kNumLoops)
1294 -{
1295 - std::vector<int> v;
1296 - v.resize(kNumInserts, 2);
1297 - DoNotOptimize(v.data());
1298 -}
1299 -
1300 -#undef BM
1301 -#endif // _POSIX_TIMERS
1302 -#endif // __unix__
1303 diff --git a/Tests/Recast/Tests_RecastFilter.cpp b/Tests/Recast/Tests_RecastFilter.cpp
1304 new file mode 100644
1305 index 0000000..1882adb
1306 --- /dev/null
1307 +++ b/Tests/Recast/Tests_RecastFilter.cpp
1308 @@ -0,0 +1,389 @@
1309 +#include <stdio.h>
1310 +#include <string.h>
1311 +#include <vector>
1312 +
1313 +#include "catch2/catch_all.hpp"
1314 +
1315 +#include "Recast.h"
1316 +#include "RecastAlloc.h"
1317 +
1318 +TEST_CASE("rcFilterLowHangingWalkableObstacles", "[recast, filtering]")
1319 +{
1320 + rcContext context;
1321 + int walkableHeight = 5;
1322 +
1323 + rcHeightfield heightfield;
1324 + heightfield.width = 1;
1325 + heightfield.height = 1;
1326 + heightfield.bmin[0] = 0;
1327 + heightfield.bmin[1] = 0;
1328 + heightfield.bmin[2] = 0;
1329 + heightfield.bmax[0] = 1;
1330 + heightfield.bmax[1] = 1;
1331 + heightfield.bmax[2] = 1;
1332 + heightfield.cs = 1;
1333 + heightfield.ch = 1;
1334 + heightfield.spans = (rcSpan**)rcAlloc(heightfield.width * heightfield.height * sizeof(rcSpan*), RC_ALLOC_PERM);
1335 + heightfield.pools = NULL;
1336 + heightfield.freelist = NULL;
1337 +
1338 + SECTION("Span with no spans above it is unchanged")
1339 + {
1340 + rcSpan* span = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
1341 + span->area = 1;
1342 + span->next = NULL;
1343 + span->smin = 0;
1344 + span->smax = 1;
1345 + heightfield.spans[0] = span;
1346 +
1347 + rcFilterLowHangingWalkableObstacles(&context, walkableHeight, heightfield);
1348 +
1349 + REQUIRE(heightfield.spans[0]->area == 1);
1350 +
1351 + rcFree(span);
1352 + }
1353 +
1354 + SECTION("Span with span above that is higher than walkableHeight is unchanged")
1355 + {
1356 + // Put the second span just above the first one.
1357 + rcSpan* secondSpan = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
1358 + secondSpan->area = 1;
1359 + secondSpan->next = NULL;
1360 + secondSpan->smin = 1 + walkableHeight;
1361 + secondSpan->smax = secondSpan->smin + 1;
1362 +
1363 + rcSpan* span = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
1364 + span->area = 1;
1365 + span->next = secondSpan;
1366 + span->smin = 0;
1367 + span->smax = 1;
1368 +
1369 + heightfield.spans[0] = span;
1370 +
1371 + rcFilterLowHangingWalkableObstacles(&context, walkableHeight, heightfield);
1372 +
1373 + // Check that nothing has changed.
1374 + REQUIRE(heightfield.spans[0]->area == 1);
1375 + REQUIRE(heightfield.spans[0]->next->area == 1);
1376 +
1377 + // Check again but with a more clearance
1378 + secondSpan->smin += 10;
1379 + secondSpan->smax += 10;
1380 +
1381 + rcFilterLowHangingWalkableObstacles(&context, walkableHeight, heightfield);
1382 +
1383 + // Check that nothing has changed.
1384 + REQUIRE(heightfield.spans[0]->area == 1);
1385 + REQUIRE(heightfield.spans[0]->next->area == 1);
1386 +
1387 + rcFree(span);
1388 + rcFree(secondSpan);
1389 + }
1390 +
1391 + SECTION("Marks low obstacles walkable if they're below the walkableClimb")
1392 + {
1393 + // Put the second span just above the first one.
1394 + rcSpan* secondSpan = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
1395 + secondSpan->area = RC_NULL_AREA;
1396 + secondSpan->next = NULL;
1397 + secondSpan->smin = 1 + (walkableHeight - 1);
1398 + secondSpan->smax = secondSpan->smin + 1;
1399 +
1400 + rcSpan* span = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
1401 + span->area = 1;
1402 + span->next = secondSpan;
1403 + span->smin = 0;
1404 + span->smax = 1;
1405 +
1406 + heightfield.spans[0] = span;
1407 +
1408 + rcFilterLowHangingWalkableObstacles(&context, walkableHeight, heightfield);
1409 +
1410 + // Check that the second span was changed to walkable.
1411 + REQUIRE(heightfield.spans[0]->area == 1);
1412 + REQUIRE(heightfield.spans[0]->next->area == 1);
1413 +
1414 + rcFree(span);
1415 + rcFree(secondSpan);
1416 + }
1417 +
1418 + SECTION("Low obstacle that overlaps the walkableClimb distance is not changed")
1419 + {
1420 + // Put the second span just above the first one.
1421 + rcSpan* secondSpan = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
1422 + secondSpan->area = RC_NULL_AREA;
1423 + secondSpan->next = NULL;
1424 + secondSpan->smin = 2 + (walkableHeight - 1);
1425 + secondSpan->smax = secondSpan->smin + 1;
1426 +
1427 + rcSpan* span = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
1428 + span->area = 1;
1429 + span->next = secondSpan;
1430 + span->smin = 0;
1431 + span->smax = 1;
1432 +
1433 + heightfield.spans[0] = span;
1434 +
1435 + rcFilterLowHangingWalkableObstacles(&context, walkableHeight, heightfield);
1436 +
1437 + // Check that the second span was changed to walkable.
1438 + REQUIRE(heightfield.spans[0]->area == 1);
1439 + REQUIRE(heightfield.spans[0]->next->area == RC_NULL_AREA);
1440 +
1441 + rcFree(span);
1442 + rcFree(secondSpan);
1443 + }
1444 +
1445 + SECTION("Only the first of multiple, low obstacles are marked walkable")
1446 + {
1447 + rcSpan* span = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
1448 + span->area = 1;
1449 + span->next = NULL;
1450 + span->smin = 0;
1451 + span->smax = 1;
1452 + heightfield.spans[0] = span;
1453 +
1454 + rcSpan* previousSpan = span;
1455 + for (int i = 0; i < 9; ++i)
1456 + {
1457 + rcSpan* nextSpan = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
1458 + nextSpan->area = RC_NULL_AREA;
1459 + nextSpan->next = NULL;
1460 + nextSpan->smin = previousSpan->smax + (walkableHeight - 1);
1461 + nextSpan->smax = nextSpan->smin + 1;
1462 + previousSpan->next = nextSpan;
1463 + previousSpan = nextSpan;
1464 + }
1465 +
1466 + rcFilterLowHangingWalkableObstacles(&context, walkableHeight, heightfield);
1467 +
1468 + rcSpan* currentSpan = heightfield.spans[0];
1469 + for (int i = 0; i < 10; ++i)
1470 + {
1471 + REQUIRE(currentSpan != NULL);
1472 + // only the first and second spans should be marked as walkabl
1473 + REQUIRE(currentSpan->area == (i <= 1 ? 1 : RC_NULL_AREA));
1474 + currentSpan = currentSpan->next;
1475 + }
1476 +
1477 + std::vector<rcSpan*> toFree;
1478 + span = heightfield.spans[0];
1479 + for (int i = 0; i < 10; ++i)
1480 + {
1481 + toFree.push_back(span);
1482 + span = span->next;
1483 + }
1484 +
1485 + for (int i = 0; i < 10; ++i)
1486 + {
1487 + rcFree(toFree[i]);
1488 + }
1489 + }
1490 +}
1491 +
1492 +TEST_CASE("rcFilterLedgeSpans", "[recast, filtering]")
1493 +{
1494 + rcContext context;
1495 + int walkableClimb = 5;
1496 + int walkableHeight = 10;
1497 +
1498 + rcHeightfield heightfield;
1499 + heightfield.width = 10;
1500 + heightfield.height = 10;
1501 + heightfield.bmin[0] = 0;
1502 + heightfield.bmin[1] = 0;
1503 + heightfield.bmin[2] = 0;
1504 + heightfield.bmax[0] = 10;
1505 + heightfield.bmax[1] = 1;
1506 + heightfield.bmax[2] = 10;
1507 + heightfield.cs = 1;
1508 + heightfield.ch = 1;
1509 + heightfield.spans = (rcSpan**)rcAlloc(heightfield.width * heightfield.height * sizeof(rcSpan*), RC_ALLOC_PERM);
1510 + heightfield.pools = NULL;
1511 + heightfield.freelist = NULL;
1512 +
1513 + SECTION("Edge spans are marked unwalkable")
1514 + {
1515 + // Create a flat plane.
1516 + for (int x = 0; x < heightfield.width; ++x)
1517 + {
1518 + for (int z = 0; z < heightfield.height; ++z)
1519 + {
1520 + rcSpan* span = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
1521 + span->area = 1;
1522 + span->next = NULL;
1523 + span->smin = 0;
1524 + span->smax = 1;
1525 + heightfield.spans[x + z * heightfield.width] = span;
1526 + }
1527 + }
1528 +
1529 + rcFilterLedgeSpans(&context, walkableHeight, walkableClimb, heightfield);
1530 +
1531 + for (int x = 0; x < heightfield.width; ++x)
1532 + {
1533 + for (int z = 0; z < heightfield.height; ++z)
1534 + {
1535 + rcSpan* span = heightfield.spans[x + z * heightfield.width];
1536 + REQUIRE(span != NULL);
1537 +
1538 + if (x == 0 || z == 0 || x == 9 || z == 9)
1539 + {
1540 + REQUIRE(span->area == RC_NULL_AREA);
1541 + }
1542 + else
1543 + {
1544 + REQUIRE(span->area == 1);
1545 + }
1546 +
1547 + REQUIRE(span->next == NULL);
1548 + REQUIRE(span->smin == 0);
1549 + REQUIRE(span->smax == 1);
1550 + }
1551 + }
1552 +
1553 + // Free all the heightfield spans
1554 + for (int x = 0; x < heightfield.width; ++x)
1555 + {
1556 + for (int z = 0; z < heightfield.height; ++z)
1557 + {
1558 + rcFree(heightfield.spans[x + z * heightfield.width]);
1559 + }
1560 + }
1561 + }
1562 +
1563 + SECTION("Edge spans are marked unwalkable")
1564 + {
1565 + // Create a flat plane.
1566 + for (int x = 0; x < heightfield.width; ++x)
1567 + {
1568 + for (int z = 0; z < heightfield.height; ++z)
1569 + {
1570 + rcSpan* span = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
1571 + span->area = 1;
1572 + span->next = NULL;
1573 + span->smin = 0;
1574 + span->smax = 1;
1575 + heightfield.spans[x + z * heightfield.width] = span;
1576 + }
1577 + }
1578 +
1579 + rcFilterLedgeSpans(&context, walkableHeight, walkableClimb, heightfield);
1580 +
1581 + for (int x = 0; x < heightfield.width; ++x)
1582 + {
1583 + for (int z = 0; z < heightfield.height; ++z)
1584 + {
1585 + rcSpan* span = heightfield.spans[x + z * heightfield.width];
1586 + REQUIRE(span != NULL);
1587 +
1588 + if (x == 0 || z == 0 || x == 9 || z == 9)
1589 + {
1590 + REQUIRE(span->area == RC_NULL_AREA);
1591 + }
1592 + else
1593 + {
1594 + REQUIRE(span->area == 1);
1595 + }
1596 +
1597 + REQUIRE(span->next == NULL);
1598 + REQUIRE(span->smin == 0);
1599 + REQUIRE(span->smax == 1);
1600 + }
1601 + }
1602 +
1603 + // Free all the heightfield spans
1604 + for (int x = 0; x < heightfield.width; ++x)
1605 + {
1606 + for (int z = 0; z < heightfield.height; ++z)
1607 + {
1608 + rcFree(heightfield.spans[x + z * heightfield.width]);
1609 + }
1610 + }
1611 + }
1612 +}
1613 +
1614 +TEST_CASE("rcFilterWalkableLowHeightSpans", "[recast, filtering]")
1615 +{
1616 + rcContext context;
1617 + int walkableHeight = 5;
1618 +
1619 + rcHeightfield heightfield;
1620 + heightfield.width = 1;
1621 + heightfield.height = 1;
1622 + heightfield.bmin[0] = 0;
1623 + heightfield.bmin[1] = 0;
1624 + heightfield.bmin[2] = 0;
1625 + heightfield.bmax[0] = 1;
1626 + heightfield.bmax[1] = 1;
1627 + heightfield.bmax[2] = 1;
1628 + heightfield.cs = 1;
1629 + heightfield.ch = 1;
1630 + heightfield.spans = (rcSpan**)rcAlloc(heightfield.width * heightfield.height * sizeof(rcSpan*), RC_ALLOC_PERM);
1631 + heightfield.pools = NULL;
1632 + heightfield.freelist = NULL;
1633 +
1634 + SECTION("span nothing above is unchanged")
1635 + {
1636 + rcSpan* span = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
1637 + span->area = 1;
1638 + span->next = NULL;
1639 + span->smin = 0;
1640 + span->smax = 1;
1641 + heightfield.spans[0] = span;
1642 +
1643 + rcFilterWalkableLowHeightSpans(&context, walkableHeight, heightfield);
1644 +
1645 + REQUIRE(heightfield.spans[0]->area == 1);
1646 +
1647 + rcFree(span);
1648 + }
1649 +
1650 + SECTION("span with lots of room above is unchanged")
1651 + {
1652 + rcSpan* overheadSpan = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
1653 + overheadSpan->area = RC_NULL_AREA;
1654 + overheadSpan->next = NULL;
1655 + overheadSpan->smin = 10;
1656 + overheadSpan->smax = 11;
1657 +
1658 + rcSpan* span = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
1659 + span->area = 1;
1660 + span->next = overheadSpan;
1661 + span->smin = 0;
1662 + span->smax = 1;
1663 + heightfield.spans[0] = span;
1664 +
1665 + rcFilterWalkableLowHeightSpans(&context, walkableHeight, heightfield);
1666 +
1667 + REQUIRE(heightfield.spans[0]->area == 1);
1668 + REQUIRE(heightfield.spans[0]->next->area == RC_NULL_AREA);
1669 +
1670 + rcFree(overheadSpan);
1671 + rcFree(span);
1672 + }
1673 +
1674 + SECTION("Span with low hanging obstacle is marked as unwalkable")
1675 + {
1676 + rcSpan* overheadSpan = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
1677 + overheadSpan->area = RC_NULL_AREA;
1678 + overheadSpan->next = NULL;
1679 + overheadSpan->smin = 3;
1680 + overheadSpan->smax = 4;
1681 +
1682 + rcSpan* span = (rcSpan*)rcAlloc(sizeof(rcSpan), RC_ALLOC_PERM);
1683 + span->area = 1;
1684 + span->next = overheadSpan;
1685 + span->smin = 0;
1686 + span->smax = 1;
1687 + heightfield.spans[0] = span;
1688 +
1689 + rcFilterWalkableLowHeightSpans(&context, walkableHeight, heightfield);
1690 +
1691 + REQUIRE(heightfield.spans[0]->area == RC_NULL_AREA);
1692 + REQUIRE(heightfield.spans[0]->next->area == RC_NULL_AREA);
1693 +
1694 + rcFree(overheadSpan);
1695 + rcFree(span);
1696 + }
1697 +}
1698 \ No newline at end of file
1699 --
1700 2.43.0
1701