| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 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 |
|
| 157 |
values[2] = 2; |
| 158 |
heap.shift(heap_indexes[2]); |
| 159 |
|
| 160 |
|
| 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 |
} |