123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- #include "../Tests.hpp"
- #include "core/HashMap.hpp"
- #include "core/Random.hpp"
- #include "core/Test.hpp"
- template struct Core::HashMap<int, int>;
- using IntMap = Core::HashMap<int, int>;
- static IntMap getTestIntMap() {
- IntMap map;
- map.add(1, 3).add(2, 4).add(3, 5).add(0, 20);
- return map;
- }
- static void checkIntMap(IntMap& map) {
- int* a = map.search(1);
- int* b = map.search(2);
- int* c = map.search(3);
- int* d = map.search(0);
- if(TEST_NOT_NULL(a) && TEST_NOT_NULL(b) && TEST_NOT_NULL(c) &&
- TEST_NOT_NULL(d)) {
- TEST(3, *a);
- TEST(4, *b);
- TEST(5, *c);
- TEST(20, *d);
- }
- }
- static void testAdd() {
- IntMap map;
- map.add(5, 4);
- int* value = map.search(5);
- if(TEST_NOT_NULL(value)) {
- TEST(4, *value);
- }
- }
- static void testMultipleAdd() {
- IntMap map = getTestIntMap();
- TEST_TRUE(map.contains(0));
- TEST_TRUE(map.contains(1));
- TEST_TRUE(map.contains(2));
- TEST_TRUE(map.contains(3));
- checkIntMap(map);
- }
- static void testSearch() {
- IntMap map;
- TEST_NULL(map.search(6));
- map.add(5, 4).add(10, 3).add(15, 2);
- TEST_NULL(map.search(6));
- }
- static void testAddReplace() {
- IntMap map;
- map.add(5, 4).add(5, 10);
- TEST_TRUE(map.contains(5));
- int* a = map.search(5);
- if(TEST_NOT_NULL(a)) {
- TEST(10, *a);
- }
- }
- static void testClear() {
- IntMap map;
- map.clear();
- map.add(5, 4).add(4, 10);
- map.clear();
- TEST_FALSE(map.contains(5));
- TEST_FALSE(map.contains(4));
- }
- static void testOverflow(bool light) {
- int limit = light ? 10'000 : 100'000;
- IntMap map;
- for(int i = 0; i < limit; i++) {
- map.add(i, i);
- }
- for(int i = 0; i < limit; i++) {
- TEST_TRUE(map.contains(i));
- }
- }
- static int aInstances = 0;
- struct ProbingTest final {
- int a;
- int b;
- ProbingTest(int a_, int b_) : a(a_), b(b_) {
- aInstances++;
- }
- ProbingTest(const ProbingTest& o) = delete;
- ProbingTest(ProbingTest&& o) noexcept : a(o.a), b(o.b) {
- aInstances++;
- }
- ~ProbingTest() {
- aInstances--;
- }
- ProbingTest& operator=(const ProbingTest& o) = delete;
- ProbingTest& operator=(ProbingTest&& o) noexcept {
- a = o.a;
- 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::HashMap<int, ProbingTest> map;
- ProbingTest* 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));
- ProbingTest* a = map.search(0);
- ProbingTest* b = map.search(3);
- ProbingTest* c = map.search(20);
- if(TEST_NOT_NULL(a) && TEST_NOT_NULL(b) && TEST_NOT_NULL(c)) {
- TEST(ProbingTest(3, 4), *a);
- TEST(ProbingTest(4, 5), *b);
- TEST(ProbingTest(5, 6), *c);
- }
- }
- TEST(0, aInstances);
- }
- static void testToString() {
- TEST_STRING("[0 = 20, 2 = 4, 1 = 3, 3 = 5]", getTestIntMap());
- TEST_STRING("[1 = 3]", IntMap().add(1, 3));
- TEST_STRING("[]", IntMap());
- }
- static void testCopy() {
- IntMap map = getTestIntMap();
- IntMap copy = map;
- IntMap copyA;
- copyA = map;
- checkIntMap(map);
- checkIntMap(copy);
- checkIntMap(copyA);
- map.add(1, 20).add(2, 30).add(3, 40);
- checkIntMap(copy);
- checkIntMap(copyA);
- }
- static void testMove() {
- IntMap map = getTestIntMap();
- IntMap move(Core::move(map));
- checkIntMap(move);
- }
- static void testMoveAssignment() {
- IntMap map = getTestIntMap();
- IntMap move;
- move = Core::move(map);
- checkIntMap(move);
- }
- static void testEntryForEach() {
- IntMap map;
- map.add(0, 1).add(5, 4).add(10, 3).add(15, 2);
- int counter = 0;
- for(auto entry : map) {
- counter += entry.getKey() + entry.value;
- }
- TEST(40, counter);
- const IntMap& cmap = map;
- counter = 0;
- for(auto entry : cmap) {
- counter += entry.getKey() + entry.value;
- }
- TEST(40, counter);
- }
- static void testKeyForEach() {
- IntMap map;
- map.add(5, 4).add(10, 3).add(15, 2);
- int counter = 0;
- for(const int& key : map.getKeys()) {
- counter += key;
- }
- TEST(30, counter);
- const IntMap& cmap = map;
- counter = 0;
- for(const int& key : cmap.getKeys()) {
- counter += key;
- }
- TEST(30, counter);
- }
- static void testValueForEach() {
- IntMap map;
- map.add(5, 4).add(10, 3).add(15, 2);
- int counter = 0;
- for(int& value : map.getValues()) {
- counter += value;
- }
- TEST(9, counter);
- const IntMap& cmap = map;
- counter = 0;
- for(const int& value : cmap.getValues()) {
- counter += value;
- }
- TEST(9, counter);
- }
- template<typename T>
- static void testType() {
- Core::HashMap<T, int> m;
- m.add(1, 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 testInvalid() {
- IntMap map;
- int* v = nullptr;
- TEST_TRUE(map.tryEmplace(v, 0, 2));
- if(TEST_NOT_NULL(v)) {
- TEST(2, *v);
- }
- TEST_FALSE(map.tryEmplace(v, 0, 6));
- if(TEST_NOT_NULL(v)) {
- TEST(2, *v);
- }
- TEST(3, map.put(0, 3));
- v = map.search(0);
- if(TEST_NOT_NULL(v)) {
- TEST(3, *v);
- }
- map.clear();
- TEST_NULL(map.search(0));
- }
- static void testInvalidPut() {
- IntMap map;
- TEST_STRING("[]", map);
- TEST(3, map.put(0, 3));
- TEST_STRING("[0 = 3]", map);
- int* v = map.search(0);
- if(TEST_NOT_NULL(v)) {
- TEST(3, *v);
- }
- map.clear();
- TEST_FALSE(map.contains(0));
- TEST_STRING("[]", map);
- }
- static void testAddCollisions() {
- IntMap map;
- for(int i = 0; i < 16; i++) {
- map.add(i * 64, i);
- }
- }
- 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);
- }
- }
- static void testRemoveLong() {
- IntMap 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);
- }
- }
- }
- void testHashMap(bool light) {
- testAdd();
- testMultipleAdd();
- testSearch();
- testAddReplace();
- testClear();
- testOverflow(light);
- testToString();
- testCopy();
- testMove();
- testMoveAssignment();
- testEntryForEach();
- testKeyForEach();
- testValueForEach();
- testTypes();
- testInvalid();
- testInvalidPut();
- testAddCollisions();
- testRemove();
- testRemoveLong();
- testEmplaceProbing();
- }
|