Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/servers/test_nav_heap.cpp
23449 views
1
/**************************************************************************/
2
/* test_nav_heap.cpp */
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
#include "tests/test_macros.h"
32
33
TEST_FORCE_LINK(test_nav_heap)
34
35
#include "servers/nav_heap.h"
36
37
namespace TestNavHeap {
38
39
struct GreaterThan {
40
bool operator()(int p_a, int p_b) const { return p_a > p_b; }
41
};
42
43
struct CompareArrayValues {
44
const int *array;
45
46
CompareArrayValues(const int *p_array) :
47
array(p_array) {}
48
49
bool operator()(uint32_t p_index_a, uint32_t p_index_b) const {
50
return array[p_index_a] < array[p_index_b];
51
}
52
};
53
54
struct RegisterHeapIndexes {
55
uint32_t *indexes;
56
57
RegisterHeapIndexes(uint32_t *p_indexes) :
58
indexes(p_indexes) {}
59
60
void operator()(uint32_t p_vector_index, uint32_t p_heap_index) {
61
indexes[p_vector_index] = p_heap_index;
62
}
63
};
64
65
TEST_CASE("[Heap] size") {
66
Heap<int> heap;
67
68
CHECK(heap.size() == 0);
69
70
heap.push(0);
71
CHECK(heap.size() == 1);
72
73
heap.push(1);
74
CHECK(heap.size() == 2);
75
76
heap.pop();
77
CHECK(heap.size() == 1);
78
79
heap.pop();
80
CHECK(heap.size() == 0);
81
}
82
83
TEST_CASE("[Heap] is_empty") {
84
Heap<int> heap;
85
86
CHECK(heap.is_empty() == true);
87
88
heap.push(0);
89
CHECK(heap.is_empty() == false);
90
91
heap.pop();
92
CHECK(heap.is_empty() == true);
93
}
94
95
TEST_CASE("[Heap] push/pop") {
96
SUBCASE("Default comparator") {
97
Heap<int> heap;
98
99
heap.push(2);
100
heap.push(7);
101
heap.push(5);
102
heap.push(3);
103
heap.push(4);
104
105
CHECK(heap.pop() == 7);
106
CHECK(heap.pop() == 5);
107
CHECK(heap.pop() == 4);
108
CHECK(heap.pop() == 3);
109
CHECK(heap.pop() == 2);
110
}
111
112
SUBCASE("Custom comparator") {
113
GreaterThan greaterThan;
114
Heap<int, GreaterThan> heap(greaterThan);
115
116
heap.push(2);
117
heap.push(7);
118
heap.push(5);
119
heap.push(3);
120
heap.push(4);
121
122
CHECK(heap.pop() == 2);
123
CHECK(heap.pop() == 3);
124
CHECK(heap.pop() == 4);
125
CHECK(heap.pop() == 5);
126
CHECK(heap.pop() == 7);
127
}
128
129
SUBCASE("Intermediate pops") {
130
Heap<int> heap;
131
132
heap.push(0);
133
heap.push(3);
134
heap.pop();
135
heap.push(1);
136
heap.push(2);
137
138
CHECK(heap.pop() == 2);
139
CHECK(heap.pop() == 1);
140
CHECK(heap.pop() == 0);
141
}
142
}
143
144
TEST_CASE("[Heap] shift") {
145
int values[] = { 5, 3, 6, 7, 1 };
146
uint32_t heap_indexes[] = { 0, 0, 0, 0, 0 };
147
CompareArrayValues comparator(values);
148
RegisterHeapIndexes indexer(heap_indexes);
149
Heap<uint32_t, CompareArrayValues, RegisterHeapIndexes> heap(comparator, indexer);
150
151
heap.push(0);
152
heap.push(1);
153
heap.push(2);
154
heap.push(3);
155
heap.push(4);
156
157
// Shift down: 6 -> 2
158
values[2] = 2;
159
heap.shift(heap_indexes[2]);
160
161
// Shift up: 5 -> 8
162
values[0] = 8;
163
heap.shift(heap_indexes[0]);
164
165
CHECK(heap.pop() == 0);
166
CHECK(heap.pop() == 3);
167
CHECK(heap.pop() == 1);
168
CHECK(heap.pop() == 2);
169
CHECK(heap.pop() == 4);
170
171
CHECK(heap_indexes[0] == Heap<uint32_t, CompareArrayValues, RegisterHeapIndexes>::INVALID_INDEX);
172
CHECK(heap_indexes[1] == Heap<uint32_t, CompareArrayValues, RegisterHeapIndexes>::INVALID_INDEX);
173
CHECK(heap_indexes[2] == Heap<uint32_t, CompareArrayValues, RegisterHeapIndexes>::INVALID_INDEX);
174
CHECK(heap_indexes[3] == Heap<uint32_t, CompareArrayValues, RegisterHeapIndexes>::INVALID_INDEX);
175
CHECK(heap_indexes[4] == Heap<uint32_t, CompareArrayValues, RegisterHeapIndexes>::INVALID_INDEX);
176
}
177
178
TEST_CASE("[Heap] clear") {
179
uint32_t heap_indexes[] = { 0, 0, 0, 0 };
180
RegisterHeapIndexes indexer(heap_indexes);
181
Heap<uint32_t, Comparator<uint32_t>, RegisterHeapIndexes> heap(indexer);
182
183
heap.push(0);
184
heap.push(2);
185
heap.push(1);
186
heap.push(3);
187
188
heap.clear();
189
190
CHECK(heap.size() == 0);
191
192
CHECK(heap_indexes[0] == Heap<uint32_t, Comparator<uint32_t>, RegisterHeapIndexes>::INVALID_INDEX);
193
CHECK(heap_indexes[1] == Heap<uint32_t, Comparator<uint32_t>, RegisterHeapIndexes>::INVALID_INDEX);
194
CHECK(heap_indexes[2] == Heap<uint32_t, Comparator<uint32_t>, RegisterHeapIndexes>::INVALID_INDEX);
195
CHECK(heap_indexes[3] == Heap<uint32_t, Comparator<uint32_t>, RegisterHeapIndexes>::INVALID_INDEX);
196
}
197
198
} // namespace TestNavHeap
199
200