Debian Codegraph @

tests/servers/test_nav_heap.h

raw symbols SCIP
1 /**************************************************************************/
2 /* test_nav_heap.h */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #pragma once
32
33 #include "servers/nav_heap.h"
34
35 #include "tests/test_macros.h"
36
37 namespace TestHeap {
38 struct GreaterThan {
39 bool operator()(int p_a, int p_b) const { return p_a > p_b; }
40 };
41
42 struct CompareArrayValues {
43 const int *array;
44
45 CompareArrayValues(const int *p_array) :
46 array(p_array) {}
47
48 bool operator()(uint32_t p_index_a, uint32_t p_index_b) const {
49 return array[p_index_a] < array[p_index_b];
50 }
51 };
52
53 struct RegisterHeapIndexes {
54 uint32_t *indexes;
55
56 RegisterHeapIndexes(uint32_t *p_indexes) :
57 indexes(p_indexes) {}
58
59 void operator()(uint32_t p_vector_index, uint32_t p_heap_index) {
60 indexes[p_vector_index] = p_heap_index;
61 }
62 };
63
64 TEST_CASE("[Heap] size") {
65 Heap<int> heap;
66
67 CHECK(heap.size() == 0);
68
69 heap.push(0);
70 CHECK(heap.size() == 1);
71
72 heap.push(1);
73 CHECK(heap.size() == 2);
74
75 heap.pop();
76 CHECK(heap.size() == 1);
77
78 heap.pop();
79 CHECK(heap.size() == 0);
80 }
81
82 TEST_CASE("[Heap] is_empty") {
83 Heap<int> heap;
84
85 CHECK(heap.is_empty() == true);
86
87 heap.push(0);
88 CHECK(heap.is_empty() == false);
89
90 heap.pop();
91 CHECK(heap.is_empty() == true);
92 }
93
94 TEST_CASE("[Heap] push/pop") {
95 SUBCASE("Default comparator") {
96 Heap<int> heap;
97
98 heap.push(2);
99 heap.push(7);
100 heap.push(5);
101 heap.push(3);
102 heap.push(4);
103
104 CHECK(heap.pop() == 7);
105 CHECK(heap.pop() == 5);
106 CHECK(heap.pop() == 4);
107 CHECK(heap.pop() == 3);
108 CHECK(heap.pop() == 2);
109 }
110
111 SUBCASE("Custom comparator") {
112 GreaterThan greaterThan;
113 Heap<int, GreaterThan> heap(greaterThan);
114
115 heap.push(2);
116 heap.push(7);
117 heap.push(5);
118 heap.push(3);
119 heap.push(4);
120
121 CHECK(heap.pop() == 2);
122 CHECK(heap.pop() == 3);
123 CHECK(heap.pop() == 4);
124 CHECK(heap.pop() == 5);
125 CHECK(heap.pop() == 7);
126 }
127
128 SUBCASE("Intermediate pops") {
129 Heap<int> heap;
130
131 heap.push(0);
132 heap.push(3);
133 heap.pop();
134 heap.push(1);
135 heap.push(2);
136
137 CHECK(heap.pop() == 2);
138 CHECK(heap.pop() == 1);
139 CHECK(heap.pop() == 0);
140 }
141 }
142
143 TEST_CASE("[Heap] shift") {
144 int values[] = { 5, 3, 6, 7, 1 };
145 uint32_t heap_indexes[] = { 0, 0, 0, 0, 0 };
146 CompareArrayValues comparator(values);
147 RegisterHeapIndexes indexer(heap_indexes);
148 Heap<uint32_t, CompareArrayValues, RegisterHeapIndexes> heap(comparator, indexer);
149
150 heap.push(0);
151 heap.push(1);
152 heap.push(2);
153 heap.push(3);
154 heap.push(4);
155
156 // Shift down: 6 -> 2
157 values[2] = 2;
158 heap.shift(heap_indexes[2]);
159
160 // Shift up: 5 -> 8
161 values[0] = 8;
162 heap.shift(heap_indexes[0]);
163
164 CHECK(heap.pop() == 0);
165 CHECK(heap.pop() == 3);
166 CHECK(heap.pop() == 1);
167 CHECK(heap.pop() == 2);
168 CHECK(heap.pop() == 4);
169
170 CHECK(heap_indexes[0] == Heap<uint32_t, CompareArrayValues, RegisterHeapIndexes>::INVALID_INDEX);
171 CHECK(heap_indexes[1] == Heap<uint32_t, CompareArrayValues, RegisterHeapIndexes>::INVALID_INDEX);
172 CHECK(heap_indexes[2] == Heap<uint32_t, CompareArrayValues, RegisterHeapIndexes>::INVALID_INDEX);
173 CHECK(heap_indexes[3] == Heap<uint32_t, CompareArrayValues, RegisterHeapIndexes>::INVALID_INDEX);
174 CHECK(heap_indexes[4] == Heap<uint32_t, CompareArrayValues, RegisterHeapIndexes>::INVALID_INDEX);
175 }
176
177 TEST_CASE("[Heap] clear") {
178 uint32_t heap_indexes[] = { 0, 0, 0, 0 };
179 RegisterHeapIndexes indexer(heap_indexes);
180 Heap<uint32_t, Comparator<uint32_t>, RegisterHeapIndexes> heap(indexer);
181
182 heap.push(0);
183 heap.push(2);
184 heap.push(1);
185 heap.push(3);
186
187 heap.clear();
188
189 CHECK(heap.size() == 0);
190
191 CHECK(heap_indexes[0] == Heap<uint32_t, Comparator<uint32_t>, RegisterHeapIndexes>::INVALID_INDEX);
192 CHECK(heap_indexes[1] == Heap<uint32_t, Comparator<uint32_t>, RegisterHeapIndexes>::INVALID_INDEX);
193 CHECK(heap_indexes[2] == Heap<uint32_t, Comparator<uint32_t>, RegisterHeapIndexes>::INVALID_INDEX);
194 CHECK(heap_indexes[3] == Heap<uint32_t, Comparator<uint32_t>, RegisterHeapIndexes>::INVALID_INDEX);
195 }
196 } //namespace TestHeap