|
@@ -1,10 +1,8 @@
|
|
-#include "tests/ProbingHashMapTests.hpp"
|
|
|
|
-
|
|
|
|
-#include <stdio.h>
|
|
|
|
-
|
|
|
|
-#include "data/ProbingHashMap.hpp"
|
|
|
|
-#include "test/Test.hpp"
|
|
|
|
|
|
+#include "../../src/ErrorSimulator.hpp"
|
|
|
|
+#include "../Tests.hpp"
|
|
|
|
+#include "core/data/ProbingHashMap.hpp"
|
|
|
|
|
|
|
|
+template class Core::ProbingHashMap<int, int>;
|
|
using IntMap = Core::ProbingHashMap<int, int>;
|
|
using IntMap = Core::ProbingHashMap<int, int>;
|
|
|
|
|
|
static void testAdd() {
|
|
static void testAdd() {
|
|
@@ -41,6 +39,10 @@ static void testMultipleAdd() {
|
|
static void testSearch() {
|
|
static void testSearch() {
|
|
IntMap map;
|
|
IntMap map;
|
|
CORE_TEST_NULL(map.search(6));
|
|
CORE_TEST_NULL(map.search(6));
|
|
|
|
+ CORE_TEST_ERROR(map.add(5, 4));
|
|
|
|
+ CORE_TEST_ERROR(map.add(10, 3));
|
|
|
|
+ CORE_TEST_ERROR(map.add(15, 2));
|
|
|
|
+ CORE_TEST_NULL(map.search(6));
|
|
}
|
|
}
|
|
|
|
|
|
static void testAddReplace() {
|
|
static void testAddReplace() {
|
|
@@ -64,42 +66,45 @@ static void testClear() {
|
|
CORE_TEST_FALSE(map.contains(4));
|
|
CORE_TEST_FALSE(map.contains(4));
|
|
}
|
|
}
|
|
|
|
|
|
-static void testOverflow() {
|
|
|
|
|
|
+static void testOverflow(bool light) {
|
|
|
|
+ int limit = light ? 10000 : 100000;
|
|
IntMap map;
|
|
IntMap map;
|
|
- for(int i = 0; i < 100000; i++) {
|
|
|
|
|
|
+ for(int i = 0; i < limit; i++) {
|
|
CORE_TEST_ERROR(map.add(i, i));
|
|
CORE_TEST_ERROR(map.add(i, i));
|
|
}
|
|
}
|
|
- for(int i = 0; i < 100000; i++) {
|
|
|
|
|
|
+ for(int i = 0; i < limit; i++) {
|
|
CORE_TEST_TRUE(map.contains(i));
|
|
CORE_TEST_TRUE(map.contains(i));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
static int aInstances = 0;
|
|
static int aInstances = 0;
|
|
|
|
|
|
-struct A final {
|
|
|
|
|
|
+struct ProbingHashMapTestStruct final {
|
|
int a;
|
|
int a;
|
|
int b;
|
|
int b;
|
|
|
|
|
|
- A(int a_, int b_) : a(a_), b(b_) {
|
|
|
|
|
|
+ ProbingHashMapTestStruct(int a_, int b_) : a(a_), b(b_) {
|
|
aInstances++;
|
|
aInstances++;
|
|
}
|
|
}
|
|
|
|
|
|
- A(const A& o) : a(o.a), b(o.b) {
|
|
|
|
|
|
+ ProbingHashMapTestStruct(const ProbingHashMapTestStruct& o)
|
|
|
|
+ : a(o.a), b(o.b) {
|
|
aInstances++;
|
|
aInstances++;
|
|
}
|
|
}
|
|
|
|
|
|
- A(A&& o) : a(o.a), b(o.b) {
|
|
|
|
|
|
+ ProbingHashMapTestStruct(ProbingHashMapTestStruct&& o) : a(o.a), b(o.b) {
|
|
aInstances++;
|
|
aInstances++;
|
|
}
|
|
}
|
|
|
|
|
|
- ~A() {
|
|
|
|
|
|
+ ~ProbingHashMapTestStruct() {
|
|
aInstances--;
|
|
aInstances--;
|
|
}
|
|
}
|
|
|
|
|
|
- A& operator=(const A& o) = default;
|
|
|
|
- A& operator=(A&& o) = default;
|
|
|
|
|
|
+ ProbingHashMapTestStruct&
|
|
|
|
+ operator=(const ProbingHashMapTestStruct& o) = default;
|
|
|
|
+ ProbingHashMapTestStruct& operator=(ProbingHashMapTestStruct&& o) = default;
|
|
|
|
|
|
- bool operator==(const A& other) const {
|
|
|
|
|
|
+ bool operator==(const ProbingHashMapTestStruct& other) const {
|
|
return a == other.a && b == other.b;
|
|
return a == other.a && b == other.b;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -116,9 +121,9 @@ struct A final {
|
|
|
|
|
|
static void testEmplace() {
|
|
static void testEmplace() {
|
|
{
|
|
{
|
|
- Core::ProbingHashMap<int, A> map;
|
|
|
|
|
|
+ Core::ProbingHashMap<int, ProbingHashMapTestStruct> map;
|
|
|
|
|
|
- A* ar = nullptr;
|
|
|
|
|
|
+ ProbingHashMapTestStruct* ar = nullptr;
|
|
CORE_TEST_ERROR(map.tryEmplace(ar, 0, 3, 4));
|
|
CORE_TEST_ERROR(map.tryEmplace(ar, 0, 3, 4));
|
|
CORE_TEST_ERROR(map.tryEmplace(ar, 3, 4, 5));
|
|
CORE_TEST_ERROR(map.tryEmplace(ar, 3, 4, 5));
|
|
CORE_TEST_ERROR(map.tryEmplace(ar, 20, 5, 6));
|
|
CORE_TEST_ERROR(map.tryEmplace(ar, 20, 5, 6));
|
|
@@ -126,18 +131,18 @@ static void testEmplace() {
|
|
CORE_TEST_EQUAL(Core::Error::EXISTING_KEY,
|
|
CORE_TEST_EQUAL(Core::Error::EXISTING_KEY,
|
|
map.tryEmplace(ar, 20, 7, 8));
|
|
map.tryEmplace(ar, 20, 7, 8));
|
|
|
|
|
|
- A* a = map.search(0);
|
|
|
|
- A* b = map.search(3);
|
|
|
|
- A* c = map.search(20);
|
|
|
|
|
|
+ ProbingHashMapTestStruct* a = map.search(0);
|
|
|
|
+ ProbingHashMapTestStruct* b = map.search(3);
|
|
|
|
+ ProbingHashMapTestStruct* c = map.search(20);
|
|
|
|
|
|
CORE_TEST_NOT_NULL(a);
|
|
CORE_TEST_NOT_NULL(a);
|
|
CORE_TEST_NOT_NULL(b);
|
|
CORE_TEST_NOT_NULL(b);
|
|
CORE_TEST_NOT_NULL(c);
|
|
CORE_TEST_NOT_NULL(c);
|
|
|
|
|
|
if(a != nullptr && b != nullptr && c != nullptr) {
|
|
if(a != nullptr && b != nullptr && c != nullptr) {
|
|
- CORE_TEST_EQUAL(A(3, 4), *a);
|
|
|
|
- CORE_TEST_EQUAL(A(4, 5), *b);
|
|
|
|
- CORE_TEST_EQUAL(A(5, 6), *c);
|
|
|
|
|
|
+ CORE_TEST_EQUAL(ProbingHashMapTestStruct(3, 4), *a);
|
|
|
|
+ CORE_TEST_EQUAL(ProbingHashMapTestStruct(4, 5), *b);
|
|
|
|
+ CORE_TEST_EQUAL(ProbingHashMapTestStruct(5, 6), *c);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
CORE_TEST_EQUAL(0, aInstances);
|
|
CORE_TEST_EQUAL(0, aInstances);
|
|
@@ -307,13 +312,48 @@ static void testTypes() {
|
|
testType<unsigned long long>();
|
|
testType<unsigned long long>();
|
|
}
|
|
}
|
|
|
|
|
|
-void Core::ProbingHashMapTests::test() {
|
|
|
|
|
|
+static void testOutOfMemory() {
|
|
|
|
+ IntMap map;
|
|
|
|
+ int memFails = 0;
|
|
|
|
+ for(int i = 0; i < 40; i++) {
|
|
|
|
+ Core::Fail::leftAllocations = i;
|
|
|
|
+ int* v = nullptr;
|
|
|
|
+ Core::Error e = map.put(v, 1, 1);
|
|
|
|
+ if(e == Core::Error::OUT_OF_MEMORY) {
|
|
|
|
+ memFails++;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ int* found = map.search(1);
|
|
|
|
+ if(CORE_TEST_NOT_NULL(found)) {
|
|
|
|
+ CORE_TEST_EQUAL(1, *found);
|
|
|
|
+ }
|
|
|
|
+ Core::Fail::leftAllocations = -1;
|
|
|
|
+ CORE_TEST_TRUE(memFails > 0);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void testInsertInvalid() {
|
|
|
|
+ IntMap map;
|
|
|
|
+ int* v;
|
|
|
|
+ CORE_TEST_EQUAL(Core::Error::INVALID_ARGUMENT,
|
|
|
|
+ map.tryEmplace(v, Core::emptyValue<int>(), 2));
|
|
|
|
+ CORE_TEST_EQUAL(Core::Error::INVALID_ARGUMENT,
|
|
|
|
+ map.put(v, Core::emptyValue<int>(), 2));
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+static void testAddCollisions() {
|
|
|
|
+ IntMap map;
|
|
|
|
+ for(int i = 0; i < 8; i++) {
|
|
|
|
+ CORE_TEST_ERROR(map.add(i * 16, i));
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void Core::testProbingHashMap(bool light) {
|
|
testAdd();
|
|
testAdd();
|
|
testMultipleAdd();
|
|
testMultipleAdd();
|
|
testSearch();
|
|
testSearch();
|
|
testAddReplace();
|
|
testAddReplace();
|
|
testClear();
|
|
testClear();
|
|
- testOverflow();
|
|
|
|
|
|
+ testOverflow(light);
|
|
testEmplace();
|
|
testEmplace();
|
|
testToString1();
|
|
testToString1();
|
|
testToString2();
|
|
testToString2();
|
|
@@ -325,4 +365,7 @@ void Core::ProbingHashMapTests::test() {
|
|
testKeyForEach();
|
|
testKeyForEach();
|
|
testValueForEach();
|
|
testValueForEach();
|
|
testTypes();
|
|
testTypes();
|
|
|
|
+ testOutOfMemory();
|
|
|
|
+ testInsertInvalid();
|
|
|
|
+ testAddCollisions();
|
|
}
|
|
}
|