123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 |
- #include "../../src/ErrorSimulator.hpp"
- #include "../Tests.hpp"
- #include "core/data/ProbingHashMap.hpp"
- template class Core::ProbingHashMap<int, int>;
- using IntMap = Core::ProbingHashMap<int, int>;
- static void testAdd() {
- IntMap map;
- CORE_TEST_ERROR(map.add(5, 4));
- int* value = map.search(5);
- CORE_TEST_NOT_NULL(value);
- if(value != nullptr) {
- CORE_TEST_EQUAL(4, *value);
- }
- }
- static void testMultipleAdd() {
- IntMap map;
- CORE_TEST_ERROR(map.add(5, 4));
- CORE_TEST_ERROR(map.add(10, 3));
- CORE_TEST_ERROR(map.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);
- }
- }
- static void testSearch() {
- IntMap map;
- 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() {
- IntMap map;
- CORE_TEST_ERROR(map.add(5, 4));
- CORE_TEST_ERROR(map.add(5, 10));
- CORE_TEST_TRUE(map.contains(5));
- int* a = map.search(5);
- CORE_TEST_NOT_NULL(a);
- if(a != nullptr) {
- CORE_TEST_EQUAL(10, *a);
- }
- }
- static void testClear() {
- IntMap map;
- CORE_TEST_ERROR(map.add(5, 4));
- CORE_TEST_ERROR(map.add(4, 10));
- map.clear();
- CORE_TEST_FALSE(map.contains(5));
- CORE_TEST_FALSE(map.contains(4));
- }
- static void testOverflow(bool light) {
- int limit = light ? 10000 : 100000;
- IntMap map;
- for(int i = 0; i < limit; i++) {
- CORE_TEST_ERROR(map.add(i, i));
- }
- for(int i = 0; i < limit; i++) {
- CORE_TEST_TRUE(map.contains(i));
- }
- }
- static int aInstances = 0;
- struct ProbingHashMapTestStruct final {
- int a;
- int b;
- ProbingHashMapTestStruct(int a_, int b_) : a(a_), b(b_) {
- aInstances++;
- }
- ProbingHashMapTestStruct(const ProbingHashMapTestStruct& o)
- : a(o.a), b(o.b) {
- aInstances++;
- }
- ProbingHashMapTestStruct(ProbingHashMapTestStruct&& o) : a(o.a), b(o.b) {
- aInstances++;
- }
- ~ProbingHashMapTestStruct() {
- aInstances--;
- }
- ProbingHashMapTestStruct&
- operator=(const ProbingHashMapTestStruct& o) = default;
- ProbingHashMapTestStruct& operator=(ProbingHashMapTestStruct&& o) = default;
- bool operator==(const ProbingHashMapTestStruct& other) const {
- return a == other.a && b == other.b;
- }
- template<typename String>
- check_return Core::Error toString(String& s) const {
- CORE_RETURN_ERROR(s.append("A("));
- CORE_RETURN_ERROR(s.append(a));
- CORE_RETURN_ERROR(s.append(", "));
- CORE_RETURN_ERROR(s.append(b));
- CORE_RETURN_ERROR(s.append(")"));
- return Core::Error::NONE;
- }
- };
- static void testEmplace() {
- {
- Core::ProbingHashMap<int, ProbingHashMapTestStruct> map;
- ProbingHashMapTestStruct* 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::Error::EXISTING_KEY, map.tryEmplace(ar, 3, 6, 7));
- CORE_TEST_EQUAL(Core::Error::EXISTING_KEY,
- map.tryEmplace(ar, 20, 7, 8));
- ProbingHashMapTestStruct* a = map.search(0);
- ProbingHashMapTestStruct* b = map.search(3);
- ProbingHashMapTestStruct* 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(ProbingHashMapTestStruct(3, 4), *a);
- CORE_TEST_EQUAL(ProbingHashMapTestStruct(4, 5), *b);
- CORE_TEST_EQUAL(ProbingHashMapTestStruct(5, 6), *c);
- }
- }
- CORE_TEST_EQUAL(0, aInstances);
- }
- static void testToString1() {
- IntMap map;
- CORE_TEST_ERROR(map.add(1, 3));
- CORE_TEST_ERROR(map.add(2, 4));
- CORE_TEST_ERROR(map.add(3, 5));
- CORE_TEST_STRING("[2 = 4, 1 = 3, 3 = 5]", map);
- }
- static void testToString2() {
- IntMap map;
- CORE_TEST_ERROR(map.add(1, 3));
- CORE_TEST_STRING("[1 = 3]", map);
- }
- static void testToString3() {
- IntMap map;
- CORE_TEST_STRING("[]", map);
- }
- static void testCopy() {
- IntMap map;
- CORE_TEST_ERROR(map.add(1, 3));
- CORE_TEST_ERROR(map.add(2, 4));
- CORE_TEST_ERROR(map.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]));
- }
- }
- }
- static void testMove() {
- IntMap map;
- CORE_TEST_ERROR(map.add(1, 3));
- CORE_TEST_ERROR(map.add(2, 4));
- CORE_TEST_ERROR(map.add(3, 5));
- IntMap 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);
- }
- }
- static void testMoveAssignment() {
- IntMap map;
- CORE_TEST_ERROR(map.add(1, 3));
- CORE_TEST_ERROR(map.add(2, 4));
- CORE_TEST_ERROR(map.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);
- }
- }
- static void testEntryForEach() {
- IntMap map;
- CORE_TEST_ERROR(map.add(5, 4));
- CORE_TEST_ERROR(map.add(10, 3));
- CORE_TEST_ERROR(map.add(15, 2));
- int counter = 0;
- for(auto entry : map) {
- counter += entry.getKey() + entry.value;
- }
- CORE_TEST_EQUAL(39, counter);
- const IntMap& cmap = map;
- counter = 0;
- for(auto entry : cmap) {
- counter += entry.getKey() + entry.value;
- }
- CORE_TEST_EQUAL(39, counter);
- }
- static void testKeyForEach() {
- IntMap map;
- CORE_TEST_ERROR(map.add(5, 4));
- CORE_TEST_ERROR(map.add(10, 3));
- CORE_TEST_ERROR(map.add(15, 2));
- int counter = 0;
- for(const int& key : map.getKeys()) {
- counter += key;
- }
- CORE_TEST_EQUAL(30, counter);
- const IntMap& cmap = map;
- counter = 0;
- for(const int& key : cmap.getKeys()) {
- counter += key;
- }
- CORE_TEST_EQUAL(30, counter);
- }
- static void testValueForEach() {
- IntMap map;
- CORE_TEST_ERROR(map.add(5, 4));
- CORE_TEST_ERROR(map.add(10, 3));
- CORE_TEST_ERROR(map.add(15, 2));
- int counter = 0;
- for(int& value : map.getValues()) {
- counter += value;
- }
- CORE_TEST_EQUAL(9, counter);
- const IntMap& cmap = map;
- counter = 0;
- for(const int& value : cmap.getValues()) {
- counter += value;
- }
- CORE_TEST_EQUAL(9, counter);
- }
- template<typename T>
- static void testType() {
- Core::ProbingHashMap<T, int> m;
- CORE_TEST_ERROR(m.add(T(), 3));
- }
- static void testTypes() {
- testType<char>();
- testType<signed char>();
- testType<signed short>();
- testType<signed int>();
- testType<signed long>();
- testType<signed long long>();
- testType<unsigned char>();
- testType<unsigned short>();
- testType<unsigned int>();
- testType<unsigned long>();
- testType<unsigned long long>();
- }
- static void testOutOfMemory() {
- #ifdef ERROR_SIMULATOR
- 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);
- #endif
- }
- 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, bool outOfMemoryTest) {
- testAdd();
- testMultipleAdd();
- testSearch();
- testAddReplace();
- testClear();
- testOverflow(light);
- testEmplace();
- testToString1();
- testToString2();
- testToString3();
- testCopy();
- testMove();
- testMoveAssignment();
- testEntryForEach();
- testKeyForEach();
- testValueForEach();
- testTypes();
- if(outOfMemoryTest) {
- testOutOfMemory();
- }
- testInsertInvalid();
- testAddCollisions();
- }
|