|
@@ -1,45 +1,67 @@
|
|
-#include "../../src/ErrorSimulator.hpp"
|
|
|
|
#include "../Tests.hpp"
|
|
#include "../Tests.hpp"
|
|
#include "core/data/HashMap.hpp"
|
|
#include "core/data/HashMap.hpp"
|
|
|
|
+#include "core/data/ProbingHashMap.hpp"
|
|
|
|
|
|
|
|
+template struct Core::ProbingHashMap<int, int>;
|
|
|
|
+using ProbingIntMap = Core::ProbingHashMap<int, int>;
|
|
template struct Core::HashMap<int, int>;
|
|
template struct Core::HashMap<int, int>;
|
|
using IntMap = Core::HashMap<int, int>;
|
|
using IntMap = Core::HashMap<int, int>;
|
|
|
|
|
|
|
|
+constexpr int INVALID = Core::emptyValue<int>();
|
|
|
|
+
|
|
|
|
+template<typename T>
|
|
|
|
+static T getTestIntMap() {
|
|
|
|
+ T map;
|
|
|
|
+ map.add(1, 3).add(2, 4).add(3, 5).add(INVALID, 20);
|
|
|
|
+ return map;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+template<typename T>
|
|
|
|
+static void checkIntMap(T& map) {
|
|
|
|
+ int* a = map.search(1);
|
|
|
|
+ int* b = map.search(2);
|
|
|
|
+ int* c = map.search(3);
|
|
|
|
+ int* d = map.search(INVALID);
|
|
|
|
+ if(CORE_TEST_NOT_NULL(a) && CORE_TEST_NOT_NULL(b) &&
|
|
|
|
+ CORE_TEST_NOT_NULL(c) && CORE_TEST_NOT_NULL(d)) {
|
|
|
|
+ CORE_TEST_EQUAL(3, *a);
|
|
|
|
+ CORE_TEST_EQUAL(4, *b);
|
|
|
|
+ CORE_TEST_EQUAL(5, *c);
|
|
|
|
+ CORE_TEST_EQUAL(20, *d);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+template<typename T>
|
|
static void testAdd() {
|
|
static void testAdd() {
|
|
- IntMap map;
|
|
|
|
|
|
+ T map;
|
|
map.add(5, 4);
|
|
map.add(5, 4);
|
|
int* value = map.search(5);
|
|
int* value = map.search(5);
|
|
- if(CORE_TEST_NOT_NULL(value)) {
|
|
|
|
|
|
+ CORE_TEST_NOT_NULL(value);
|
|
|
|
+ if(value != nullptr) {
|
|
CORE_TEST_EQUAL(4, *value);
|
|
CORE_TEST_EQUAL(4, *value);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+template<typename T>
|
|
static void testMultipleAdd() {
|
|
static void testMultipleAdd() {
|
|
- IntMap map;
|
|
|
|
- map.add(5, 4).add(10, 3).add(15, 2);
|
|
|
|
- CORE_TEST_TRUE(map.contains(5));
|
|
|
|
- CORE_TEST_TRUE(map.contains(10));
|
|
|
|
- CORE_TEST_TRUE(map.contains(15));
|
|
|
|
- int* a = map.search(5);
|
|
|
|
- int* b = map.search(10);
|
|
|
|
- int* c = map.search(15);
|
|
|
|
- CORE_TEST_NOT_NULL(a);
|
|
|
|
- CORE_TEST_NOT_NULL(b);
|
|
|
|
- CORE_TEST_NOT_NULL(c);
|
|
|
|
- if(a != nullptr && b != nullptr && c != nullptr) {
|
|
|
|
- CORE_TEST_EQUAL(4, *a);
|
|
|
|
- CORE_TEST_EQUAL(3, *b);
|
|
|
|
- CORE_TEST_EQUAL(2, *c);
|
|
|
|
- }
|
|
|
|
|
|
+ T map = getTestIntMap<T>();
|
|
|
|
+ CORE_TEST_TRUE(map.contains(1));
|
|
|
|
+ CORE_TEST_TRUE(map.contains(2));
|
|
|
|
+ CORE_TEST_TRUE(map.contains(3));
|
|
|
|
+ checkIntMap(map);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+template<typename T>
|
|
static void testSearch() {
|
|
static void testSearch() {
|
|
- IntMap map;
|
|
|
|
|
|
+ T map;
|
|
|
|
+ CORE_TEST_NULL(map.search(6));
|
|
|
|
+ map.add(5, 4).add(10, 3).add(15, 2);
|
|
CORE_TEST_NULL(map.search(6));
|
|
CORE_TEST_NULL(map.search(6));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+template<typename T>
|
|
static void testAddReplace() {
|
|
static void testAddReplace() {
|
|
- IntMap map;
|
|
|
|
|
|
+ T map;
|
|
map.add(5, 4).add(5, 10);
|
|
map.add(5, 4).add(5, 10);
|
|
CORE_TEST_TRUE(map.contains(5));
|
|
CORE_TEST_TRUE(map.contains(5));
|
|
int* a = map.search(5);
|
|
int* a = map.search(5);
|
|
@@ -48,39 +70,45 @@ static void testAddReplace() {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+template<typename T>
|
|
static void testClear() {
|
|
static void testClear() {
|
|
- IntMap map;
|
|
|
|
|
|
+ T map;
|
|
map.add(5, 4).add(4, 10);
|
|
map.add(5, 4).add(4, 10);
|
|
map.clear();
|
|
map.clear();
|
|
CORE_TEST_FALSE(map.contains(5));
|
|
CORE_TEST_FALSE(map.contains(5));
|
|
CORE_TEST_FALSE(map.contains(4));
|
|
CORE_TEST_FALSE(map.contains(4));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+template<typename T>
|
|
static void testOverflow(bool light) {
|
|
static void testOverflow(bool light) {
|
|
- IntMap map;
|
|
|
|
int limit = light ? 10000 : 100000;
|
|
int limit = light ? 10000 : 100000;
|
|
|
|
+ T map;
|
|
|
|
+ map.add(INVALID, 42);
|
|
for(int i = 0; i < limit; i++) {
|
|
for(int i = 0; i < limit; i++) {
|
|
map.add(i, i);
|
|
map.add(i, i);
|
|
}
|
|
}
|
|
for(int i = 0; i < limit; i++) {
|
|
for(int i = 0; i < limit; i++) {
|
|
CORE_TEST_TRUE(map.contains(i));
|
|
CORE_TEST_TRUE(map.contains(i));
|
|
}
|
|
}
|
|
|
|
+ CORE_TEST_TRUE(map.contains(INVALID));
|
|
}
|
|
}
|
|
|
|
|
|
-struct HashMapTestStruct final {
|
|
|
|
|
|
+static int aInstances = 0;
|
|
|
|
+
|
|
|
|
+struct HashMapTest {
|
|
int a;
|
|
int a;
|
|
int b;
|
|
int b;
|
|
|
|
|
|
- HashMapTestStruct(int a_, int b_) : a(a_), b(b_) {
|
|
|
|
|
|
+ HashMapTest(int a_, int b_) : a(a_), b(b_) {
|
|
}
|
|
}
|
|
|
|
|
|
// none of these should be needed for the hashmap
|
|
// none of these should be needed for the hashmap
|
|
- HashMapTestStruct(const HashMapTestStruct&) = delete;
|
|
|
|
- HashMapTestStruct(HashMapTestStruct&&) = delete;
|
|
|
|
- HashMapTestStruct& operator=(const HashMapTestStruct&) = delete;
|
|
|
|
- HashMapTestStruct& operator=(HashMapTestStruct&&) = delete;
|
|
|
|
|
|
+ HashMapTest(const HashMapTest&) = delete;
|
|
|
|
+ HashMapTest(HashMapTest&&) = delete;
|
|
|
|
+ HashMapTest& operator=(const HashMapTest&) = delete;
|
|
|
|
+ HashMapTest& operator=(HashMapTest&&) = delete;
|
|
|
|
|
|
- bool operator==(const HashMapTestStruct& other) const {
|
|
|
|
|
|
+ bool operator==(const HashMapTest& other) const {
|
|
return a == other.a && b == other.b;
|
|
return a == other.a && b == other.b;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -95,149 +123,120 @@ struct HashMapTestStruct final {
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
-static void testEmplace() {
|
|
|
|
- Core::HashMap<int, HashMapTestStruct> map;
|
|
|
|
-
|
|
|
|
- HashMapTestStruct* ar = nullptr;
|
|
|
|
- CORE_TEST_ERROR(map.tryEmplace(ar, 0, 3, 4));
|
|
|
|
- CORE_TEST_ERROR(map.tryEmplace(ar, 3, 4, 5));
|
|
|
|
- CORE_TEST_ERROR(map.tryEmplace(ar, 20, 5, 6));
|
|
|
|
- CORE_TEST_EQUAL(Core::ErrorCode::EXISTING_KEY, map.tryEmplace(ar, 3, 6, 7));
|
|
|
|
- CORE_TEST_EQUAL(Core::ErrorCode::EXISTING_KEY,
|
|
|
|
- map.tryEmplace(ar, 20, 7, 8));
|
|
|
|
-
|
|
|
|
- HashMapTestStruct* a = map.search(0);
|
|
|
|
- HashMapTestStruct* b = map.search(3);
|
|
|
|
- HashMapTestStruct* c = map.search(20);
|
|
|
|
-
|
|
|
|
- CORE_TEST_NOT_NULL(a);
|
|
|
|
- CORE_TEST_NOT_NULL(b);
|
|
|
|
- CORE_TEST_NOT_NULL(c);
|
|
|
|
-
|
|
|
|
- if(a != nullptr && b != nullptr && c != nullptr) {
|
|
|
|
- CORE_TEST_EQUAL(HashMapTestStruct(3, 4), *a);
|
|
|
|
- CORE_TEST_EQUAL(HashMapTestStruct(4, 5), *b);
|
|
|
|
- CORE_TEST_EQUAL(HashMapTestStruct(5, 6), *c);
|
|
|
|
|
|
+struct ProbingTest final : public HashMapTest {
|
|
|
|
+ ProbingTest(int a_, int b_) : HashMapTest(a_, b_) {
|
|
|
|
+ aInstances++;
|
|
}
|
|
}
|
|
-}
|
|
|
|
-
|
|
|
|
-static void testToString1() {
|
|
|
|
- IntMap map;
|
|
|
|
- map.add(1, 3).add(2, 4).add(3, 5);
|
|
|
|
- CORE_TEST_STRING("[1 = 3, 2 = 4, 3 = 5]", map);
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-static void testToString2() {
|
|
|
|
- IntMap map;
|
|
|
|
- map.add(1, 3);
|
|
|
|
- CORE_TEST_STRING("[1 = 3]", map);
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-static void testToString3() {
|
|
|
|
- IntMap map;
|
|
|
|
- CORE_TEST_STRING("[]", map);
|
|
|
|
-}
|
|
|
|
|
|
|
|
-static void testCopy() {
|
|
|
|
- IntMap map;
|
|
|
|
- map.add(1, 3).add(2, 4).add(3, 5);
|
|
|
|
- IntMap copy;
|
|
|
|
- CORE_TEST_ERROR(copy.copyFrom(map));
|
|
|
|
-
|
|
|
|
- int* a[6] = {map.search(1), map.search(2), map.search(3),
|
|
|
|
- copy.search(1), copy.search(2), copy.search(3)};
|
|
|
|
- for(int i = 0; i < 3; i++) {
|
|
|
|
- CORE_TEST_NOT_NULL(a[i]);
|
|
|
|
- CORE_TEST_NOT_NULL(a[i + 3]);
|
|
|
|
- if(a[i] != nullptr && a[i + 3] != nullptr) {
|
|
|
|
- CORE_TEST_EQUAL(*(a[i]), *(a[i + 3]));
|
|
|
|
- }
|
|
|
|
|
|
+ ProbingTest(const ProbingTest& o) : HashMapTest(o.a, o.b) {
|
|
|
|
+ aInstances++;
|
|
}
|
|
}
|
|
-}
|
|
|
|
|
|
|
|
-static void testMove() {
|
|
|
|
- IntMap map;
|
|
|
|
- map.add(1, 3).add(2, 4).add(3, 5);
|
|
|
|
- IntMap move(Core::move(map));
|
|
|
|
|
|
+ ProbingTest(ProbingTest&& o) : HashMapTest(o.a, o.b) {
|
|
|
|
+ aInstances++;
|
|
|
|
+ }
|
|
|
|
|
|
- int* a = move.search(1);
|
|
|
|
- int* b = move.search(2);
|
|
|
|
- int* c = move.search(3);
|
|
|
|
|
|
+ ~ProbingTest() {
|
|
|
|
+ aInstances--;
|
|
|
|
+ }
|
|
|
|
|
|
- CORE_TEST_NOT_NULL(a);
|
|
|
|
- CORE_TEST_NOT_NULL(b);
|
|
|
|
- CORE_TEST_NOT_NULL(c);
|
|
|
|
|
|
+ ProbingTest& operator=(ProbingTest o) {
|
|
|
|
+ a = o.a;
|
|
|
|
+ b = o.b;
|
|
|
|
+ return *this;
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
|
|
- if(a != nullptr && b != nullptr && c != nullptr) {
|
|
|
|
- CORE_TEST_EQUAL(3, *a);
|
|
|
|
- CORE_TEST_EQUAL(4, *b);
|
|
|
|
- CORE_TEST_EQUAL(5, *c);
|
|
|
|
|
|
+template<typename T>
|
|
|
|
+static void testEmplace() {
|
|
|
|
+ {
|
|
|
|
+ Core::ProbingHashMap<int, ProbingTest> map;
|
|
|
|
+
|
|
|
|
+ ProbingTest* 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));
|
|
|
|
+
|
|
|
|
+ ProbingTest* a = map.search(0);
|
|
|
|
+ ProbingTest* b = map.search(3);
|
|
|
|
+ ProbingTest* c = map.search(20);
|
|
|
|
+
|
|
|
|
+ if(CORE_TEST_NOT_NULL(a) && CORE_TEST_NOT_NULL(b) &&
|
|
|
|
+ CORE_TEST_NOT_NULL(c)) {
|
|
|
|
+ CORE_TEST_EQUAL(ProbingTest(3, 4), *a);
|
|
|
|
+ CORE_TEST_EQUAL(ProbingTest(4, 5), *b);
|
|
|
|
+ CORE_TEST_EQUAL(ProbingTest(5, 6), *c);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
+ CORE_TEST_EQUAL(0, aInstances);
|
|
}
|
|
}
|
|
|
|
|
|
-static void testMoveAssignment() {
|
|
|
|
- IntMap map;
|
|
|
|
- map.add(1, 3).add(2, 4).add(3, 5);
|
|
|
|
-
|
|
|
|
- IntMap move;
|
|
|
|
- move = Core::move(map);
|
|
|
|
-
|
|
|
|
- int* a = move.search(1);
|
|
|
|
- int* b = move.search(2);
|
|
|
|
- int* c = move.search(3);
|
|
|
|
-
|
|
|
|
- CORE_TEST_NOT_NULL(a);
|
|
|
|
- CORE_TEST_NOT_NULL(b);
|
|
|
|
- CORE_TEST_NOT_NULL(c);
|
|
|
|
-
|
|
|
|
- if(a != nullptr && b != nullptr && c != nullptr) {
|
|
|
|
- CORE_TEST_EQUAL(3, *a);
|
|
|
|
- CORE_TEST_EQUAL(4, *b);
|
|
|
|
- CORE_TEST_EQUAL(5, *c);
|
|
|
|
|
|
+template<typename T>
|
|
|
|
+static void testToString() {
|
|
|
|
+ if constexpr(Core::IsSame<T, IntMap>) {
|
|
|
|
+ CORE_TEST_STRING("[1 = 3, 2 = 4, 3 = 5, 2147483647 = 20]",
|
|
|
|
+ getTestIntMap<T>());
|
|
|
|
+ } else {
|
|
|
|
+ CORE_TEST_STRING("[2 = 4, 1 = 3, 3 = 5, 2147483647 = 20]",
|
|
|
|
+ getTestIntMap<T>());
|
|
}
|
|
}
|
|
|
|
+ CORE_TEST_STRING("[1 = 3]", T().add(1, 3));
|
|
|
|
+ CORE_TEST_STRING("[]", T());
|
|
}
|
|
}
|
|
|
|
|
|
-static void testRemove() {
|
|
|
|
- IntMap map;
|
|
|
|
- map.add(1, 3).add(2, 4).add(3, 5);
|
|
|
|
-
|
|
|
|
- CORE_TEST_TRUE(map.remove(2));
|
|
|
|
- CORE_TEST_FALSE(map.remove(7));
|
|
|
|
-
|
|
|
|
- int* a = map.search(1);
|
|
|
|
- int* b = map.search(2);
|
|
|
|
- int* c = map.search(3);
|
|
|
|
|
|
+template<typename T>
|
|
|
|
+static void testCopy() {
|
|
|
|
+ T map = getTestIntMap<T>();
|
|
|
|
+ T copy = map;
|
|
|
|
+ T copyA;
|
|
|
|
+ copyA = map;
|
|
|
|
+ checkIntMap(map);
|
|
|
|
+ checkIntMap(copy);
|
|
|
|
+ checkIntMap(copyA);
|
|
|
|
+ map.add(1, 20).add(2, 30).add(3, 40);
|
|
|
|
+ checkIntMap(copy);
|
|
|
|
+ checkIntMap(copyA);
|
|
|
|
+}
|
|
|
|
|
|
- CORE_TEST_NOT_NULL(a);
|
|
|
|
- CORE_TEST_NULL(b);
|
|
|
|
- CORE_TEST_NOT_NULL(c);
|
|
|
|
|
|
+template<typename T>
|
|
|
|
+static void testMove() {
|
|
|
|
+ T map = getTestIntMap<T>();
|
|
|
|
+ T move(Core::move(map));
|
|
|
|
+ checkIntMap(move);
|
|
|
|
+}
|
|
|
|
|
|
- if(a != nullptr && c != nullptr) {
|
|
|
|
- CORE_TEST_EQUAL(3, *a);
|
|
|
|
- CORE_TEST_EQUAL(5, *c);
|
|
|
|
- }
|
|
|
|
|
|
+template<typename T>
|
|
|
|
+static void testMoveAssignment() {
|
|
|
|
+ T map = getTestIntMap<T>();
|
|
|
|
+ T move;
|
|
|
|
+ move = Core::move(map);
|
|
|
|
+ checkIntMap(move);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+template<typename T>
|
|
static void testEntryForEach() {
|
|
static void testEntryForEach() {
|
|
- IntMap map;
|
|
|
|
|
|
+ T map;
|
|
map.add(5, 4).add(10, 3).add(15, 2);
|
|
map.add(5, 4).add(10, 3).add(15, 2);
|
|
|
|
|
|
int counter = 0;
|
|
int counter = 0;
|
|
- for(auto& entry : map) {
|
|
|
|
|
|
+ for(auto entry : map) {
|
|
counter += entry.getKey() + entry.value;
|
|
counter += entry.getKey() + entry.value;
|
|
}
|
|
}
|
|
CORE_TEST_EQUAL(39, counter);
|
|
CORE_TEST_EQUAL(39, counter);
|
|
|
|
|
|
- const IntMap& cmap = map;
|
|
|
|
|
|
+ const T& cmap = map;
|
|
counter = 0;
|
|
counter = 0;
|
|
- for(const auto& entry : cmap) {
|
|
|
|
|
|
+ for(auto entry : cmap) {
|
|
counter += entry.getKey() + entry.value;
|
|
counter += entry.getKey() + entry.value;
|
|
}
|
|
}
|
|
CORE_TEST_EQUAL(39, counter);
|
|
CORE_TEST_EQUAL(39, counter);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+template<typename T>
|
|
static void testKeyForEach() {
|
|
static void testKeyForEach() {
|
|
- IntMap map;
|
|
|
|
|
|
+ T map;
|
|
map.add(5, 4).add(10, 3).add(15, 2);
|
|
map.add(5, 4).add(10, 3).add(15, 2);
|
|
|
|
|
|
int counter = 0;
|
|
int counter = 0;
|
|
@@ -246,7 +245,7 @@ static void testKeyForEach() {
|
|
}
|
|
}
|
|
CORE_TEST_EQUAL(30, counter);
|
|
CORE_TEST_EQUAL(30, counter);
|
|
|
|
|
|
- const IntMap& cmap = map;
|
|
|
|
|
|
+ const T& cmap = map;
|
|
counter = 0;
|
|
counter = 0;
|
|
for(const int& key : cmap.getKeys()) {
|
|
for(const int& key : cmap.getKeys()) {
|
|
counter += key;
|
|
counter += key;
|
|
@@ -254,8 +253,9 @@ static void testKeyForEach() {
|
|
CORE_TEST_EQUAL(30, counter);
|
|
CORE_TEST_EQUAL(30, counter);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+template<typename T>
|
|
static void testValueForEach() {
|
|
static void testValueForEach() {
|
|
- IntMap map;
|
|
|
|
|
|
+ T map;
|
|
map.add(5, 4).add(10, 3).add(15, 2);
|
|
map.add(5, 4).add(10, 3).add(15, 2);
|
|
|
|
|
|
int counter = 0;
|
|
int counter = 0;
|
|
@@ -264,7 +264,7 @@ static void testValueForEach() {
|
|
}
|
|
}
|
|
CORE_TEST_EQUAL(9, counter);
|
|
CORE_TEST_EQUAL(9, counter);
|
|
|
|
|
|
- const IntMap& cmap = map;
|
|
|
|
|
|
+ const T& cmap = map;
|
|
counter = 0;
|
|
counter = 0;
|
|
for(const int& value : cmap.getValues()) {
|
|
for(const int& value : cmap.getValues()) {
|
|
counter += value;
|
|
counter += value;
|
|
@@ -274,10 +274,11 @@ static void testValueForEach() {
|
|
|
|
|
|
template<typename T>
|
|
template<typename T>
|
|
static void testType() {
|
|
static void testType() {
|
|
- Core::HashMap<T, int> m;
|
|
|
|
|
|
+ Core::ProbingHashMap<T, int> m;
|
|
m.add(T(), 3);
|
|
m.add(T(), 3);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+template<typename T>
|
|
static void testTypes() {
|
|
static void testTypes() {
|
|
testType<char>();
|
|
testType<char>();
|
|
testType<signed char>();
|
|
testType<signed char>();
|
|
@@ -292,23 +293,110 @@ static void testTypes() {
|
|
testType<unsigned long long>();
|
|
testType<unsigned long long>();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+template<typename T>
|
|
|
|
+static void testInvalid() {
|
|
|
|
+ T map;
|
|
|
|
+ int* v;
|
|
|
|
+ CORE_TEST_TRUE(map.tryEmplace(v, INVALID, 2));
|
|
|
|
+ if(CORE_TEST_NOT_NULL(v)) {
|
|
|
|
+ CORE_TEST_EQUAL(2, *v);
|
|
|
|
+ }
|
|
|
|
+ CORE_TEST_FALSE(map.tryEmplace(v, INVALID, 6));
|
|
|
|
+ if(CORE_TEST_NOT_NULL(v)) {
|
|
|
|
+ CORE_TEST_EQUAL(2, *v);
|
|
|
|
+ }
|
|
|
|
+ CORE_TEST_EQUAL(3, map.put(INVALID, 3));
|
|
|
|
+ v = map.search(INVALID);
|
|
|
|
+ if(CORE_TEST_NOT_NULL(v)) {
|
|
|
|
+ CORE_TEST_EQUAL(3, *v);
|
|
|
|
+ }
|
|
|
|
+ map.clear();
|
|
|
|
+ CORE_TEST_NULL(map.search(INVALID));
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+template<typename T>
|
|
|
|
+static void testInvalidPut() {
|
|
|
|
+ T map;
|
|
|
|
+ CORE_TEST_EQUAL(3, map.put(INVALID, 3));
|
|
|
|
+ int* v = map.search(INVALID);
|
|
|
|
+ if(CORE_TEST_NOT_NULL(v)) {
|
|
|
|
+ CORE_TEST_EQUAL(3, *v);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+template<typename T>
|
|
|
|
+static void testAddCollisions() {
|
|
|
|
+ T map;
|
|
|
|
+ for(int i = 0; i < 8; i++) {
|
|
|
|
+ map.add(i * 16, i);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+template<typename T>
|
|
|
|
+static void testMap(bool light) {
|
|
|
|
+ testAdd<T>();
|
|
|
|
+ testMultipleAdd<T>();
|
|
|
|
+ testSearch<T>();
|
|
|
|
+ testAddReplace<T>();
|
|
|
|
+ testClear<T>();
|
|
|
|
+ testOverflow<T>(light);
|
|
|
|
+ testEmplace<T>();
|
|
|
|
+ testToString<T>();
|
|
|
|
+ testCopy<T>();
|
|
|
|
+ testMove<T>();
|
|
|
|
+ testMoveAssignment<T>();
|
|
|
|
+ testEntryForEach<T>();
|
|
|
|
+ testKeyForEach<T>();
|
|
|
|
+ testValueForEach<T>();
|
|
|
|
+ testTypes<T>();
|
|
|
|
+ testInvalid<T>();
|
|
|
|
+ testInvalidPut<T>();
|
|
|
|
+ testAddCollisions<T>();
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void testEmplace() {
|
|
|
|
+ Core::HashMap<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));
|
|
|
|
+
|
|
|
|
+ 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);
|
|
|
|
+
|
|
|
|
+ CORE_TEST_TRUE(map.remove(2));
|
|
|
|
+ CORE_TEST_FALSE(map.remove(7));
|
|
|
|
+
|
|
|
|
+ int* a = map.search(1);
|
|
|
|
+ int* b = map.search(2);
|
|
|
|
+ int* c = map.search(3);
|
|
|
|
+
|
|
|
|
+ CORE_TEST_NULL(b);
|
|
|
|
+ if(CORE_TEST_NOT_NULL(a) && CORE_TEST_NOT_NULL(c)) {
|
|
|
|
+ CORE_TEST_EQUAL(3, *a);
|
|
|
|
+ CORE_TEST_EQUAL(5, *c);
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
void Core::testHashMap(bool light) {
|
|
void Core::testHashMap(bool light) {
|
|
- testAdd();
|
|
|
|
- testMultipleAdd();
|
|
|
|
- testSearch();
|
|
|
|
- testAddReplace();
|
|
|
|
- testClear();
|
|
|
|
- testOverflow(light);
|
|
|
|
|
|
+ testMap<ProbingIntMap>(light);
|
|
|
|
+ testMap<IntMap>(light);
|
|
testEmplace();
|
|
testEmplace();
|
|
- testToString1();
|
|
|
|
- testToString2();
|
|
|
|
- testToString3();
|
|
|
|
- testCopy();
|
|
|
|
- testMove();
|
|
|
|
- testMoveAssignment();
|
|
|
|
testRemove();
|
|
testRemove();
|
|
- testEntryForEach();
|
|
|
|
- testKeyForEach();
|
|
|
|
- testValueForEach();
|
|
|
|
- testTypes();
|
|
|
|
}
|
|
}
|