| 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 "core/templates/hash_set.h" |
| 34 |
|
| 35 |
#include "tests/test_macros.h" |
| 36 |
|
| 37 |
namespace TestHashSet { |
| 38 |
|
| 39 |
TEST_CASE("[HashSet] List initialization") { |
| 40 |
HashSet<int> set{ 0, 1, 2, 3, 4 }; |
| 41 |
|
| 42 |
CHECK(set.size() == 5); |
| 43 |
CHECK(set.has(0)); |
| 44 |
CHECK(set.has(1)); |
| 45 |
CHECK(set.has(2)); |
| 46 |
CHECK(set.has(3)); |
| 47 |
CHECK(set.has(4)); |
| 48 |
} |
| 49 |
|
| 50 |
TEST_CASE("[HashSet] List initialization with existing elements") { |
| 51 |
HashSet<int> set{ 0, 0, 0, 0, 0 }; |
| 52 |
|
| 53 |
CHECK(set.size() == 1); |
| 54 |
CHECK(set.has(0)); |
| 55 |
} |
| 56 |
|
| 57 |
TEST_CASE("[HashSet] Insert element") { |
| 58 |
HashSet<int> set; |
| 59 |
HashSet<int>::Iterator e = set.insert(42); |
| 60 |
|
| 61 |
CHECK(e); |
| 62 |
CHECK(*e == 42); |
| 63 |
CHECK(set.has(42)); |
| 64 |
CHECK(set.find(42)); |
| 65 |
set.reset(); |
| 66 |
} |
| 67 |
|
| 68 |
TEST_CASE("[HashSet] Insert existing element") { |
| 69 |
HashSet<int> set; |
| 70 |
set.insert(42); |
| 71 |
set.insert(42); |
| 72 |
|
| 73 |
CHECK(set.has(42)); |
| 74 |
CHECK(set.size() == 1); |
| 75 |
} |
| 76 |
|
| 77 |
TEST_CASE("[HashSet] Insert, iterate and remove many elements") { |
| 78 |
const int elem_max = 12343; |
| 79 |
HashSet<int> set; |
| 80 |
for (int i = 0; i < elem_max; i++) { |
| 81 |
set.insert(i); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
int idx = 0; |
| 86 |
for (const int &K : set) { |
| 87 |
CHECK(idx == K); |
| 88 |
CHECK(set.has(idx)); |
| 89 |
idx++; |
| 90 |
} |
| 91 |
|
| 92 |
Vector<int> elems_still_valid; |
| 93 |
|
| 94 |
for (int i = 0; i < elem_max; i++) { |
| 95 |
if ((i % 5) == 0) { |
| 96 |
set.erase(i); |
| 97 |
} else { |
| 98 |
elems_still_valid.push_back(i); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
CHECK(elems_still_valid.size() == set.size()); |
| 103 |
|
| 104 |
for (int i = 0; i < elems_still_valid.size(); i++) { |
| 105 |
CHECK(set.has(elems_still_valid[i])); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
TEST_CASE("[HashSet] Insert, iterate and remove many strings") { |
| 110 |
|
| 111 |
|
| 112 |
uint64_t pre_mem = Memory::get_mem_usage(); |
| 113 |
const int elem_max = 4018; |
| 114 |
HashSet<String> set; |
| 115 |
for (int i = 0; i < elem_max; i++) { |
| 116 |
set.insert(itos(i)); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
int idx = 0; |
| 121 |
for (const String &K : set) { |
| 122 |
CHECK(itos(idx) == K); |
| 123 |
CHECK(set.has(itos(idx))); |
| 124 |
idx++; |
| 125 |
} |
| 126 |
|
| 127 |
Vector<String> elems_still_valid; |
| 128 |
|
| 129 |
for (int i = 0; i < elem_max; i++) { |
| 130 |
if ((i % 5) == 0) { |
| 131 |
set.erase(itos(i)); |
| 132 |
} else { |
| 133 |
elems_still_valid.push_back(itos(i)); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
CHECK(elems_still_valid.size() == set.size()); |
| 138 |
|
| 139 |
for (int i = 0; i < elems_still_valid.size(); i++) { |
| 140 |
CHECK(set.has(elems_still_valid[i])); |
| 141 |
} |
| 142 |
|
| 143 |
elems_still_valid.clear(); |
| 144 |
set.reset(); |
| 145 |
|
| 146 |
CHECK(Memory::get_mem_usage() == pre_mem); |
| 147 |
} |
| 148 |
|
| 149 |
TEST_CASE("[HashSet] Erase via element") { |
| 150 |
HashSet<int> set; |
| 151 |
HashSet<int>::Iterator e = set.insert(42); |
| 152 |
set.remove(e); |
| 153 |
CHECK(!set.has(42)); |
| 154 |
CHECK(!set.find(42)); |
| 155 |
} |
| 156 |
|
| 157 |
TEST_CASE("[HashSet] Erase via key") { |
| 158 |
HashSet<int> set; |
| 159 |
set.insert(42); |
| 160 |
set.insert(49); |
| 161 |
set.erase(42); |
| 162 |
CHECK(!set.has(42)); |
| 163 |
CHECK(!set.find(42)); |
| 164 |
} |
| 165 |
|
| 166 |
TEST_CASE("[HashSet] Insert and erase half elements") { |
| 167 |
HashSet<int> set; |
| 168 |
set.insert(1); |
| 169 |
set.insert(2); |
| 170 |
set.insert(3); |
| 171 |
set.insert(4); |
| 172 |
set.erase(1); |
| 173 |
set.erase(3); |
| 174 |
|
| 175 |
CHECK(set.size() == 2); |
| 176 |
CHECK(set.has(2)); |
| 177 |
CHECK(set.has(4)); |
| 178 |
} |
| 179 |
|
| 180 |
TEST_CASE("[HashSet] Size") { |
| 181 |
HashSet<int> set; |
| 182 |
set.insert(42); |
| 183 |
set.insert(123); |
| 184 |
set.insert(123); |
| 185 |
set.insert(0); |
| 186 |
set.insert(123485); |
| 187 |
|
| 188 |
CHECK(set.size() == 4); |
| 189 |
} |
| 190 |
|
| 191 |
TEST_CASE("[HashSet] Iteration") { |
| 192 |
HashSet<int> set; |
| 193 |
set.insert(42); |
| 194 |
set.insert(123); |
| 195 |
set.insert(0); |
| 196 |
set.insert(123485); |
| 197 |
|
| 198 |
Vector<int> expected; |
| 199 |
expected.push_back(42); |
| 200 |
expected.push_back(123); |
| 201 |
expected.push_back(0); |
| 202 |
expected.push_back(123485); |
| 203 |
|
| 204 |
int idx = 0; |
| 205 |
for (const int &E : set) { |
| 206 |
CHECK(expected[idx] == E); |
| 207 |
++idx; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
TEST_CASE("[HashSet] Copy") { |
| 212 |
HashSet<int> set; |
| 213 |
set.insert(42); |
| 214 |
set.insert(123); |
| 215 |
set.insert(0); |
| 216 |
set.insert(123485); |
| 217 |
|
| 218 |
Vector<int> expected; |
| 219 |
expected.push_back(42); |
| 220 |
expected.push_back(123); |
| 221 |
expected.push_back(0); |
| 222 |
expected.push_back(123485); |
| 223 |
|
| 224 |
HashSet<int> copy_assign = set; |
| 225 |
|
| 226 |
int idx = 0; |
| 227 |
for (const int &E : copy_assign) { |
| 228 |
CHECK(expected[idx] == E); |
| 229 |
++idx; |
| 230 |
} |
| 231 |
|
| 232 |
HashSet<int> copy_construct(set); |
| 233 |
|
| 234 |
idx = 0; |
| 235 |
for (const int &E : copy_construct) { |
| 236 |
CHECK(expected[idx] == E); |
| 237 |
++idx; |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
TEST_CASE("[HashSet] Equality") { |
| 242 |
|
| 243 |
CHECK(HashSet<int>{} == HashSet<int>{}); |
| 244 |
CHECK(HashSet<int>{} != HashSet<int>{ 1, 2, 3 }); |
| 245 |
CHECK(HashSet<int>{ 1, 2, 3 } != HashSet<int>{}); |
| 246 |
|
| 247 |
|
| 248 |
CHECK(HashSet<int>{ 1, 2, 3 } != HashSet<int>{ 1, 2, 3, 4 }); |
| 249 |
CHECK(HashSet<int>{ 1, 2, 3, 4 } != HashSet<int>{ 4, 3, 2 }); |
| 250 |
|
| 251 |
|
| 252 |
CHECK(HashSet<int>{ 1, 2, 3 } == HashSet<int>{ 1, 2, 3 }); |
| 253 |
CHECK(HashSet<int>{ 1, 2, 3 } == HashSet<int>{ 3, 2, 1 }); |
| 254 |
CHECK(HashSet<int>{ 1, 2, 3 } != HashSet<int>{ 1, 2, 8 }); |
| 255 |
} |
| 256 |
|
| 257 |
} |