|
@@ -1,5 +1,6 @@
|
|
|
#include "../Tests.hpp"
|
|
|
#include "core/ProbingHashMap.hpp"
|
|
|
+#include "core/Random.hpp"
|
|
|
#include "core/Test.hpp"
|
|
|
|
|
|
template struct Core::ProbingHashMap<int, int>;
|
|
@@ -337,6 +338,55 @@ static void testAddCollisions() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+template<typename T>
|
|
|
+static void testRemove() {
|
|
|
+ T map;
|
|
|
+ map.add(1, 3).add(2, 4).add(3, 5);
|
|
|
+
|
|
|
+ TEST_TRUE(map.remove(2));
|
|
|
+ TEST_FALSE(map.remove(7));
|
|
|
+
|
|
|
+ int* a = map.search(1);
|
|
|
+ int* b = map.search(2);
|
|
|
+ int* c = map.search(3);
|
|
|
+
|
|
|
+ TEST_NULL(b);
|
|
|
+ if(TEST_NOT_NULL(a) && TEST_NOT_NULL(c)) {
|
|
|
+ TEST(3, *a);
|
|
|
+ TEST(5, *c);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+template<typename T>
|
|
|
+static void testRemoveLong() {
|
|
|
+ T map;
|
|
|
+ Core::Random r(5);
|
|
|
+ constexpr size_t LIMIT = 75;
|
|
|
+ Core::Array<i32, LIMIT> a;
|
|
|
+ for(int i = 0; i < 10'000; i++) {
|
|
|
+ i32 r1 = r.nextI32(0, LIMIT);
|
|
|
+ if(r.nextBool()) {
|
|
|
+ i32 r2 = r.nextI32(0, LIMIT);
|
|
|
+ map.add(r1, 2);
|
|
|
+ map.add(r2, 2);
|
|
|
+ a[static_cast<size_t>(r1)] = 1;
|
|
|
+ a[static_cast<size_t>(r2)] = 1;
|
|
|
+ } else {
|
|
|
+ map.remove(r1);
|
|
|
+ a[static_cast<size_t>(r1)] = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ Core::Array<i32, LIMIT> copy = a;
|
|
|
+ for(int key : map.getKeys()) {
|
|
|
+ TEST_TRUE(copy[static_cast<size_t>(key)]);
|
|
|
+ copy[static_cast<size_t>(key)] = 0;
|
|
|
+ }
|
|
|
+ for(int key : copy) {
|
|
|
+ TEST(0, key);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
template<typename T>
|
|
|
static void testMap(bool light) {
|
|
|
testAdd<T>();
|
|
@@ -356,64 +406,35 @@ static void testMap(bool light) {
|
|
|
testInvalid<T>();
|
|
|
testInvalidPut<T>();
|
|
|
testAddCollisions<T>();
|
|
|
+ testRemove<T>();
|
|
|
+ testRemoveLong<T>();
|
|
|
}
|
|
|
|
|
|
// static void testEmplace() {
|
|
|
-// Core::HashMap<int, HashMapTest> map;
|
|
|
+// Core::ProbingHashMap<int, HashMapTest> map;
|
|
|
//
|
|
|
// HashMapTest* ar = nullptr;
|
|
|
-// CORE_TEST_TRUE(map.tryEmplace(ar, 0, 3, 4));
|
|
|
-// CORE_TEST_TRUE(map.tryEmplace(ar, 3, 4, 5));
|
|
|
-// CORE_TEST_TRUE(map.tryEmplace(ar, 20, 5, 6));
|
|
|
-// CORE_TEST_FALSE(map.tryEmplace(ar, 3, 6, 7));
|
|
|
-// CORE_TEST_FALSE(map.tryEmplace(ar, 20, 7, 8));
|
|
|
+// TEST_TRUE(map.tryEmplace(ar, 0, 3, 4));
|
|
|
+// TEST_TRUE(map.tryEmplace(ar, 3, 4, 5));
|
|
|
+// TEST_TRUE(map.tryEmplace(ar, 20, 5, 6));
|
|
|
+// TEST_FALSE(map.tryEmplace(ar, 3, 6, 7));
|
|
|
+// TEST_FALSE(map.tryEmplace(ar, 20, 7, 8));
|
|
|
//
|
|
|
// HashMapTest* a = map.search(0);
|
|
|
// HashMapTest* b = map.search(3);
|
|
|
// HashMapTest* c = map.search(20);
|
|
|
//
|
|
|
-// if(CORE_TEST_NOT_NULL(a) && CORE_TEST_NOT_NULL(b) &&
|
|
|
-// CORE_TEST_NOT_NULL(c)) {
|
|
|
-// CORE_TEST_EQUAL(HashMapTest(3, 4), *a);
|
|
|
-// CORE_TEST_EQUAL(HashMapTest(4, 5), *b);
|
|
|
-// CORE_TEST_EQUAL(HashMapTest(5, 6), *c);
|
|
|
-// }
|
|
|
-// }
|
|
|
-
|
|
|
-// static void testRemove() {
|
|
|
-// IntMap map;
|
|
|
-// map.add(1, 3).add(2, 4).add(3, 5);
|
|
|
-//
|
|
|
-// TEST_TRUE(map.remove(2));
|
|
|
-// TEST_FALSE(map.remove(7));
|
|
|
-//
|
|
|
-// int* a = map.search(1);
|
|
|
-// int* b = map.search(2);
|
|
|
-// int* c = map.search(3);
|
|
|
-//
|
|
|
-// TEST_NULL(b);
|
|
|
-// if(TEST_NOT_NULL(a) && TEST_NOT_NULL(c)) {
|
|
|
-// TEST(3, *a);
|
|
|
-// TEST(5, *c);
|
|
|
+// if(TEST_NOT_NULL(a) && TEST_NOT_NULL(b) && TEST_NOT_NULL(c)) {
|
|
|
+// TEST(HashMapTest(3, 4), *a);
|
|
|
+// TEST(HashMapTest(4, 5), *b);
|
|
|
+// TEST(HashMapTest(5, 6), *c);
|
|
|
// }
|
|
|
// }
|
|
|
|
|
|
void testHashMap(bool light) {
|
|
|
- if(!light) {
|
|
|
- // testHash();
|
|
|
- testMap<ProbingIntMap>(light);
|
|
|
- // testMap<IntMap>(light);
|
|
|
- // testEmplace();
|
|
|
- testEmplaceProbing();
|
|
|
- // testRemove();
|
|
|
- }
|
|
|
- ProbingIntMap map;
|
|
|
- map.add(1, 2);
|
|
|
- map.add(17, 3);
|
|
|
- map.add(33, 4);
|
|
|
- map.add(33, 6);
|
|
|
- map.add(49, 5);
|
|
|
- LOG_WARNING("#", map);
|
|
|
- LOG_WARNING("#", map.keys);
|
|
|
- LOG_WARNING("#", map.jumps);
|
|
|
+ // testHash();
|
|
|
+ testMap<ProbingIntMap>(light);
|
|
|
+ // testMap<IntMap>(light);
|
|
|
+ // testEmplace();
|
|
|
+ testEmplaceProbing();
|
|
|
}
|