|
@@ -1,28 +1,18 @@
|
|
|
#include "../Tests.hpp"
|
|
|
-#include "core/ProbingHashMap.hpp"
|
|
|
+#include "core/HashMap.hpp"
|
|
|
#include "core/Random.hpp"
|
|
|
#include "core/Test.hpp"
|
|
|
|
|
|
-template struct Core::ProbingHashMap<int, int>;
|
|
|
-using ProbingIntMap = Core::ProbingHashMap<int, int>;
|
|
|
+template struct Core::HashMap<int, int>;
|
|
|
+using IntMap = Core::HashMap<int, int>;
|
|
|
|
|
|
-// template struct Core::HashMap<int, int>;
|
|
|
-// using IntMap = Core::HashMap<int, int>;
|
|
|
-
|
|
|
-// static void testHash() {
|
|
|
-// const char* s = "wusi";
|
|
|
-// TEST_TRUE(hashString(s) != 0);
|
|
|
-// }
|
|
|
-
|
|
|
-template<typename T>
|
|
|
-static T getTestIntMap() {
|
|
|
- T map;
|
|
|
+static IntMap getTestIntMap() {
|
|
|
+ IntMap map;
|
|
|
map.add(1, 3).add(2, 4).add(3, 5).add(0, 20);
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
-template<typename T>
|
|
|
-static void checkIntMap(T& map) {
|
|
|
+static void checkIntMap(IntMap& map) {
|
|
|
int* a = map.search(1);
|
|
|
int* b = map.search(2);
|
|
|
int* c = map.search(3);
|
|
@@ -36,9 +26,8 @@ static void checkIntMap(T& map) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-template<typename T>
|
|
|
static void testAdd() {
|
|
|
- T map;
|
|
|
+ IntMap map;
|
|
|
map.add(5, 4);
|
|
|
int* value = map.search(5);
|
|
|
if(TEST_NOT_NULL(value)) {
|
|
@@ -46,9 +35,8 @@ static void testAdd() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-template<typename T>
|
|
|
static void testMultipleAdd() {
|
|
|
- T map = getTestIntMap<T>();
|
|
|
+ IntMap map = getTestIntMap();
|
|
|
TEST_TRUE(map.contains(0));
|
|
|
TEST_TRUE(map.contains(1));
|
|
|
TEST_TRUE(map.contains(2));
|
|
@@ -56,17 +44,15 @@ static void testMultipleAdd() {
|
|
|
checkIntMap(map);
|
|
|
}
|
|
|
|
|
|
-template<typename T>
|
|
|
static void testSearch() {
|
|
|
- T map;
|
|
|
+ IntMap map;
|
|
|
TEST_NULL(map.search(6));
|
|
|
map.add(5, 4).add(10, 3).add(15, 2);
|
|
|
TEST_NULL(map.search(6));
|
|
|
}
|
|
|
|
|
|
-template<typename T>
|
|
|
static void testAddReplace() {
|
|
|
- T map;
|
|
|
+ IntMap map;
|
|
|
map.add(5, 4).add(5, 10);
|
|
|
TEST_TRUE(map.contains(5));
|
|
|
int* a = map.search(5);
|
|
@@ -75,9 +61,8 @@ static void testAddReplace() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-template<typename T>
|
|
|
static void testClear() {
|
|
|
- T map;
|
|
|
+ IntMap map;
|
|
|
map.clear();
|
|
|
map.add(5, 4).add(4, 10);
|
|
|
map.clear();
|
|
@@ -85,10 +70,9 @@ static void testClear() {
|
|
|
TEST_FALSE(map.contains(4));
|
|
|
}
|
|
|
|
|
|
-template<typename T>
|
|
|
static void testOverflow(bool light) {
|
|
|
int limit = light ? 10'000 : 100'000;
|
|
|
- T map;
|
|
|
+ IntMap map;
|
|
|
for(int i = 0; i < limit; i++) {
|
|
|
map.add(i, i);
|
|
|
}
|
|
@@ -99,44 +83,19 @@ static void testOverflow(bool light) {
|
|
|
|
|
|
static int aInstances = 0;
|
|
|
|
|
|
-struct HashMapTest {
|
|
|
+struct ProbingTest final {
|
|
|
int a;
|
|
|
int b;
|
|
|
|
|
|
- HashMapTest(int a_, int b_) : a(a_), b(b_) {
|
|
|
- }
|
|
|
-
|
|
|
- // none of these should be needed for the hashmap
|
|
|
- HashMapTest(const HashMapTest&) = delete;
|
|
|
- HashMapTest(HashMapTest&&) = delete;
|
|
|
- HashMapTest& operator=(const HashMapTest&) = delete;
|
|
|
- HashMapTest& operator=(HashMapTest&&) = delete;
|
|
|
-
|
|
|
- bool operator==(const HashMapTest& other) const {
|
|
|
- return a == other.a && b == other.b;
|
|
|
- }
|
|
|
-
|
|
|
- size_t toString(char* s, size_t n) const {
|
|
|
- size_t total = 0;
|
|
|
- addString("A(", s, n, total);
|
|
|
- addString(a, s, n, total);
|
|
|
- addString(", ", s, n, total);
|
|
|
- addString(b, s, n, total);
|
|
|
- addString(")", s, n, total);
|
|
|
- return total;
|
|
|
- }
|
|
|
-};
|
|
|
-
|
|
|
-struct ProbingTest final : public HashMapTest {
|
|
|
- ProbingTest(int a_, int b_) : HashMapTest(a_, b_) {
|
|
|
+ ProbingTest(int a_, int b_) : a(a_), b(b_) {
|
|
|
aInstances++;
|
|
|
}
|
|
|
|
|
|
- ProbingTest(const ProbingTest& o) : HashMapTest(o.a, o.b) {
|
|
|
+ ProbingTest(const ProbingTest& o) : ProbingTest(o.a, o.b) {
|
|
|
aInstances++;
|
|
|
}
|
|
|
|
|
|
- ProbingTest(ProbingTest&& o) : HashMapTest(o.a, o.b) {
|
|
|
+ ProbingTest(ProbingTest&& o) : ProbingTest(o.a, o.b) {
|
|
|
aInstances++;
|
|
|
}
|
|
|
|
|
@@ -149,11 +108,25 @@ struct ProbingTest final : public HashMapTest {
|
|
|
b = o.b;
|
|
|
return *this;
|
|
|
}
|
|
|
+
|
|
|
+ bool operator==(const ProbingTest& other) const {
|
|
|
+ return a == other.a && b == other.b;
|
|
|
+ }
|
|
|
+
|
|
|
+ size_t toString(char* s, size_t n) const {
|
|
|
+ size_t total = 0;
|
|
|
+ Core::addString("A(", s, n, total);
|
|
|
+ Core::addString(a, s, n, total);
|
|
|
+ Core::addString(", ", s, n, total);
|
|
|
+ Core::addString(b, s, n, total);
|
|
|
+ Core::addString(")", s, n, total);
|
|
|
+ return total;
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
static void testEmplaceProbing() {
|
|
|
{
|
|
|
- Core::ProbingHashMap<int, ProbingTest> map;
|
|
|
+ Core::HashMap<int, ProbingTest> map;
|
|
|
|
|
|
ProbingTest* ar = nullptr;
|
|
|
TEST_TRUE(map.tryEmplace(ar, 0, 3, 4));
|
|
@@ -175,23 +148,16 @@ static void testEmplaceProbing() {
|
|
|
TEST(0, aInstances);
|
|
|
}
|
|
|
|
|
|
-template<typename T>
|
|
|
static void testToString() {
|
|
|
- if constexpr(Core::IsSame<T, int>) {
|
|
|
- TEST_STRING(
|
|
|
- "[1 = 3, 2 = 4, 3 = 5, 2147483647 = 20]", getTestIntMap<T>());
|
|
|
- } else {
|
|
|
- TEST_STRING("[0 = 20, 2 = 4, 1 = 3, 3 = 5]", getTestIntMap<T>());
|
|
|
- }
|
|
|
- TEST_STRING("[1 = 3]", T().add(1, 3));
|
|
|
- TEST_STRING("[]", T());
|
|
|
+ TEST_STRING("[0 = 20, 2 = 4, 1 = 3, 3 = 5]", getTestIntMap());
|
|
|
+ TEST_STRING("[1 = 3]", IntMap().add(1, 3));
|
|
|
+ TEST_STRING("[]", IntMap());
|
|
|
}
|
|
|
|
|
|
-template<typename T>
|
|
|
static void testCopy() {
|
|
|
- T map = getTestIntMap<T>();
|
|
|
- T copy = map;
|
|
|
- T copyA;
|
|
|
+ IntMap map = getTestIntMap();
|
|
|
+ IntMap copy = map;
|
|
|
+ IntMap copyA;
|
|
|
copyA = map;
|
|
|
checkIntMap(map);
|
|
|
checkIntMap(copy);
|
|
@@ -201,24 +167,21 @@ static void testCopy() {
|
|
|
checkIntMap(copyA);
|
|
|
}
|
|
|
|
|
|
-template<typename T>
|
|
|
static void testMove() {
|
|
|
- T map = getTestIntMap<T>();
|
|
|
- T move(Core::move(map));
|
|
|
+ IntMap map = getTestIntMap();
|
|
|
+ IntMap move(Core::move(map));
|
|
|
checkIntMap(move);
|
|
|
}
|
|
|
|
|
|
-template<typename T>
|
|
|
static void testMoveAssignment() {
|
|
|
- T map = getTestIntMap<T>();
|
|
|
- T move;
|
|
|
+ IntMap map = getTestIntMap();
|
|
|
+ IntMap move;
|
|
|
move = Core::move(map);
|
|
|
checkIntMap(move);
|
|
|
}
|
|
|
|
|
|
-template<typename T>
|
|
|
static void testEntryForEach() {
|
|
|
- T map;
|
|
|
+ IntMap map;
|
|
|
map.add(0, 1).add(5, 4).add(10, 3).add(15, 2);
|
|
|
|
|
|
int counter = 0;
|
|
@@ -227,7 +190,7 @@ static void testEntryForEach() {
|
|
|
}
|
|
|
TEST(40, counter);
|
|
|
|
|
|
- const T& cmap = map;
|
|
|
+ const IntMap& cmap = map;
|
|
|
counter = 0;
|
|
|
for(auto entry : cmap) {
|
|
|
counter += entry.getKey() + entry.value;
|
|
@@ -235,9 +198,8 @@ static void testEntryForEach() {
|
|
|
TEST(40, counter);
|
|
|
}
|
|
|
|
|
|
-template<typename T>
|
|
|
static void testKeyForEach() {
|
|
|
- T map;
|
|
|
+ IntMap map;
|
|
|
map.add(5, 4).add(10, 3).add(15, 2);
|
|
|
|
|
|
int counter = 0;
|
|
@@ -246,7 +208,7 @@ static void testKeyForEach() {
|
|
|
}
|
|
|
TEST(30, counter);
|
|
|
|
|
|
- const T& cmap = map;
|
|
|
+ const IntMap& cmap = map;
|
|
|
counter = 0;
|
|
|
for(const int& key : cmap.getKeys()) {
|
|
|
counter += key;
|
|
@@ -254,9 +216,8 @@ static void testKeyForEach() {
|
|
|
TEST(30, counter);
|
|
|
}
|
|
|
|
|
|
-template<typename T>
|
|
|
static void testValueForEach() {
|
|
|
- T map;
|
|
|
+ IntMap map;
|
|
|
map.add(5, 4).add(10, 3).add(15, 2);
|
|
|
|
|
|
int counter = 0;
|
|
@@ -265,7 +226,7 @@ static void testValueForEach() {
|
|
|
}
|
|
|
TEST(9, counter);
|
|
|
|
|
|
- const T& cmap = map;
|
|
|
+ const IntMap& cmap = map;
|
|
|
counter = 0;
|
|
|
for(const int& value : cmap.getValues()) {
|
|
|
counter += value;
|
|
@@ -275,11 +236,10 @@ static void testValueForEach() {
|
|
|
|
|
|
template<typename T>
|
|
|
static void testType() {
|
|
|
- Core::ProbingHashMap<T, int> m;
|
|
|
+ Core::HashMap<T, int> m;
|
|
|
m.add(1, 3);
|
|
|
}
|
|
|
|
|
|
-template<typename T>
|
|
|
static void testTypes() {
|
|
|
testType<char>();
|
|
|
testType<signed char>();
|
|
@@ -294,10 +254,9 @@ static void testTypes() {
|
|
|
testType<unsigned long long>();
|
|
|
}
|
|
|
|
|
|
-template<typename T>
|
|
|
static void testInvalid() {
|
|
|
- T map;
|
|
|
- int* v;
|
|
|
+ IntMap map;
|
|
|
+ int* v = nullptr;
|
|
|
TEST_TRUE(map.tryEmplace(v, 0, 2));
|
|
|
if(TEST_NOT_NULL(v)) {
|
|
|
TEST(2, *v);
|
|
@@ -315,9 +274,8 @@ static void testInvalid() {
|
|
|
TEST_NULL(map.search(0));
|
|
|
}
|
|
|
|
|
|
-template<typename T>
|
|
|
static void testInvalidPut() {
|
|
|
- T map;
|
|
|
+ IntMap map;
|
|
|
TEST_STRING("[]", map);
|
|
|
TEST(3, map.put(0, 3));
|
|
|
TEST_STRING("[0 = 3]", map);
|
|
@@ -330,17 +288,15 @@ static void testInvalidPut() {
|
|
|
TEST_STRING("[]", map);
|
|
|
}
|
|
|
|
|
|
-template<typename T>
|
|
|
static void testAddCollisions() {
|
|
|
- T map;
|
|
|
+ IntMap map;
|
|
|
for(int i = 0; i < 16; i++) {
|
|
|
map.add(i * 64, i);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-template<typename T>
|
|
|
static void testRemove() {
|
|
|
- T map;
|
|
|
+ IntMap map;
|
|
|
map.add(1, 3).add(2, 4).add(3, 5);
|
|
|
|
|
|
TEST_TRUE(map.remove(2));
|
|
@@ -357,9 +313,8 @@ static void testRemove() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-template<typename T>
|
|
|
static void testRemoveLong() {
|
|
|
- T map;
|
|
|
+ IntMap map;
|
|
|
Core::Random r(5);
|
|
|
constexpr size_t LIMIT = 75;
|
|
|
Core::Array<i32, LIMIT> a;
|
|
@@ -387,54 +342,25 @@ static void testRemoveLong() {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-template<typename T>
|
|
|
-static void testMap(bool light) {
|
|
|
- testAdd<T>();
|
|
|
- testMultipleAdd<T>();
|
|
|
- testSearch<T>();
|
|
|
- testAddReplace<T>();
|
|
|
- testClear<T>();
|
|
|
- testOverflow<T>(light);
|
|
|
- testToString<T>();
|
|
|
- testCopy<T>();
|
|
|
- testMove<T>();
|
|
|
- testMoveAssignment<T>();
|
|
|
- testEntryForEach<T>();
|
|
|
- testKeyForEach<T>();
|
|
|
- testValueForEach<T>();
|
|
|
- testTypes<T>();
|
|
|
- testInvalid<T>();
|
|
|
- testInvalidPut<T>();
|
|
|
- testAddCollisions<T>();
|
|
|
- testRemove<T>();
|
|
|
- testRemoveLong<T>();
|
|
|
-}
|
|
|
-
|
|
|
-// static void testEmplace() {
|
|
|
-// Core::ProbingHashMap<int, HashMapTest> map;
|
|
|
-//
|
|
|
-// HashMapTest* ar = nullptr;
|
|
|
-// 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(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) {
|
|
|
- // testHash();
|
|
|
- testMap<ProbingIntMap>(light);
|
|
|
- // testMap<IntMap>(light);
|
|
|
- // testEmplace();
|
|
|
+ testAdd();
|
|
|
+ testMultipleAdd();
|
|
|
+ testSearch();
|
|
|
+ testAddReplace();
|
|
|
+ testClear();
|
|
|
+ testOverflow(light);
|
|
|
+ testToString();
|
|
|
+ testCopy();
|
|
|
+ testMove();
|
|
|
+ testMoveAssignment();
|
|
|
+ testEntryForEach();
|
|
|
+ testKeyForEach();
|
|
|
+ testValueForEach();
|
|
|
+ testTypes();
|
|
|
+ testInvalid();
|
|
|
+ testInvalidPut();
|
|
|
+ testAddCollisions();
|
|
|
+ testRemove();
|
|
|
+ testRemoveLong();
|
|
|
testEmplaceProbing();
|
|
|
}
|