#include "tests/HashMapTests.h"

#include "data/HashMap.h"
#include "tests/Test.h"
#include "utils/StringBuffer.h"
#include "utils/Utility.h"

typedef HashMap<int, int> IntMap;
typedef StringBuffer<50> String;

static void testAdd(Test& test) {
    IntMap map;
    map.add(5, 4);
    int* value = map.search(5);
    test.checkEqual(true, value != nullptr, "contains added value");
    if(value != nullptr) {
        test.checkEqual(4, *value, "search finds added value");
    }
}

static void testMultipleAdd(Test& test) {
    IntMap map;
    map.add(5, 4).add(10, 3).add(15, 2);
    test.checkEqual(true, map.contains(5), "contains added value 1");
    test.checkEqual(true, map.contains(10), "contains added value 2");
    test.checkEqual(true, map.contains(15), "contains added value 3");
    int* a = map.search(5);
    int* b = map.search(10);
    int* c = map.search(15);
    test.checkEqual(true, a != nullptr, "contains added value 1");
    test.checkEqual(true, b != nullptr, "contains added value 2");
    test.checkEqual(true, c != nullptr, "contains added value 3");
    if(a != nullptr && b != nullptr && c != nullptr) {
        test.checkEqual(4, *a, "search finds added value 1");
        test.checkEqual(3, *b, "search finds added value 2");
        test.checkEqual(2, *c, "search finds added value 3");
    }
}

static void testSearch(Test& test) {
    IntMap map;
    test.checkEqual(true, nullptr == map.search(6),
                    "search does not find missing key");
}

static void testAddReplace(Test& test) {
    IntMap map;
    map.add(5, 4).add(5, 10);
    test.checkEqual(true, map.contains(5), "contains replaced value");
    int* a = map.search(5);
    test.checkEqual(true, a != nullptr, "contains replaced value");
    if(a != nullptr) {
        test.checkEqual(10, *a, "search finds replaced value");
    }
}

static void testClear(Test& test) {
    IntMap map;
    map.add(5, 4).add(4, 10).clear();
    test.checkEqual(false, map.contains(5),
                    "does not contain cleared values 1");
    test.checkEqual(false, map.contains(4),
                    "does not contain cleared values 2");
}

static void testOverflow(Test& test) {
    IntMap map;
    for(int i = 0; i < 1000000; i++) {
        map.add(i, i);
    }
    for(int i = 0; i < 1000000; i++) {
        test.checkEqual(true, map.contains(i),
                        "still contains values after overflow");
    }
    test.checkEqual(true, true, "survives overflow");
}

struct A {
    int a;
    int b;

    A(int a_, int b_) : a(a_), b(b_) {
    }

    bool operator==(const A& other) const {
        return a == other.a && b == other.b;
    }

    template<int L>
    void toString(StringBuffer<L>& s) const {
        s.append("A(").append(a).append(", ").append(b).append(")");
    }
};

static void testEmplace(Test& test) {
    HashMap<int, A> map;

    bool r1 = map.tryEmplace(0, 3, 4);
    bool r2 = map.tryEmplace(3, 4, 5);
    bool r3 = map.tryEmplace(20, 5, 6);
    bool r4 = map.tryEmplace(3, 6, 7);
    bool r5 = map.tryEmplace(20, 7, 8);

    A* a = map.search(0);
    A* b = map.search(3);
    A* c = map.search(20);

    test.checkEqual(true, a != nullptr, "contains emplaced value 1");
    test.checkEqual(true, b != nullptr, "contains emplaced value 2");
    test.checkEqual(true, c != nullptr, "contains emplaced value 3");

    if(a != nullptr && b != nullptr && c != nullptr) {
        test.checkEqual(A(3, 4), *a, "contains emplaced value 1");
        test.checkEqual(A(4, 5), *b, "contains emplaced value 2");
        test.checkEqual(A(5, 6), *c, "contains emplaced value 3");
    }

    test.checkEqual(false, r1, "emplacing returns correct value 1");
    test.checkEqual(false, r2, "emplacing returns correct value 2");
    test.checkEqual(false, r3, "emplacing returns correct value 3");
    test.checkEqual(true, r4, "emplacing returns correct value 4");
    test.checkEqual(true, r5, "emplacing returns correct value 5");
}

static void testToString1(Test& test) {
    IntMap map;
    map.add(1, 3).add(2, 4).add(3, 5);
    test.checkEqual(String("[1 = 3, 2 = 4, 3 = 5]"), String(map),
                    "to string 1");
}

static void testToString2(Test& test) {
    IntMap map;
    map.add(1, 3);
    test.checkEqual(String("[1 = 3]"), String(map), "to string 2");
}

static void testToString3(Test& test) {
    IntMap map;
    test.checkEqual(String("[]"), String(map), "to string 3");
}

static void testCopy(Test& test) {
    IntMap map;
    map.add(1, 3).add(2, 4).add(3, 5);
    IntMap copy(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++) {
        test.checkEqual(true, a[i] != nullptr && a[i + 3] != nullptr,
                        "copy has same values");
        if(a[i] != nullptr && a[i + 3] != nullptr) {
            test.checkEqual(*(a[i]), *(a[i + 3]), "copy has same values");
        }
    }
}

static void testCopyAssignment(Test& test) {
    IntMap map;
    map.add(1, 3).add(2, 4).add(3, 5);
    IntMap copy;
    copy = 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++) {
        test.checkEqual(true, a[i] != nullptr && a[i + 3] != nullptr,
                        "copy assignment has same values");
        if(a[i] != nullptr && a[i + 3] != nullptr) {
            test.checkEqual(*(a[i]), *(a[i + 3]),
                            "copy assignment has same values");
        }
    }
}

static void testMove(Test& test) {
    IntMap map;
    map.add(1, 3).add(2, 4).add(3, 5);
    IntMap move(Core::move(map));

    int* a = move.search(1);
    int* b = move.search(2);
    int* c = move.search(3);

    test.checkEqual(true, a != nullptr, "move moves values 1");
    test.checkEqual(true, b != nullptr, "move moves values 2");
    test.checkEqual(true, c != nullptr, "move moves values 3");

    if(a != nullptr && b != nullptr && c != nullptr) {
        test.checkEqual(3, *a, "move moves values 1");
        test.checkEqual(4, *b, "move moves values 2");
        test.checkEqual(5, *c, "move moves values 3");
    }
}

static void testMoveAssignment(Test& test) {
    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);

    test.checkEqual(true, a != nullptr, "move moves values 1");
    test.checkEqual(true, b != nullptr, "move moves values 2");
    test.checkEqual(true, c != nullptr, "move moves values 3");

    if(a != nullptr && b != nullptr && c != nullptr) {
        test.checkEqual(3, *a, "move moves values 1");
        test.checkEqual(4, *b, "move moves values 2");
        test.checkEqual(5, *c, "move moves values 3");
    }
}

static void testRemove(Test& test) {
    IntMap map;
    map.add(1, 3).add(2, 4).add(3, 5);

    bool remove1 = map.remove(2);
    bool remove2 = map.remove(7);

    int* a = map.search(1);
    int* b = map.search(2);
    int* c = map.search(3);

    test.checkEqual(true, a != nullptr, "move moves values 1");
    test.checkEqual(true, b == nullptr, "move moves values 2");
    test.checkEqual(true, c != nullptr, "move moves values 3");

    test.checkEqual(true, remove1, "remove returns true");
    test.checkEqual(false, remove2, "remove returns false");

    if(a != nullptr && c != nullptr) {
        test.checkEqual(3, *a, "move moves values 1");
        test.checkEqual(5, *c, "move moves values 3");
    }
}

static void testEntryForEach(Test& test) {
    IntMap map;
    map.add(5, 4).add(10, 3).add(15, 2);

    int counter = 0;
    for(auto& entry : map.entries()) {
        counter += entry.getKey() + entry.value;
    }
    test.checkEqual(39, counter, "entry iterator");

    const IntMap& cmap = map;
    counter = 0;
    for(const auto& entry : cmap.entries()) {
        counter += entry.getKey() + entry.value;
    }
    test.checkEqual(39, counter, "const entry iterator");
}

static void testKeyForEach(Test& test) {
    IntMap map;
    map.add(5, 4).add(10, 3).add(15, 2);

    int counter = 0;
    for(const int& key : map.keys()) {
        counter += key;
    }
    test.checkEqual(30, counter, "key iterator");

    const IntMap& cmap = map;
    counter = 0;
    for(const int& key : cmap.keys()) {
        counter += key;
    }
    test.checkEqual(30, counter, "const key iterator");
}

static void testValueForEach(Test& test) {
    IntMap map;
    map.add(5, 4).add(10, 3).add(15, 2);

    int counter = 0;
    for(int& value : map.values()) {
        counter += value;
    }
    test.checkEqual(9, counter, "value iterator");

    const IntMap& cmap = map;
    counter = 0;
    for(const int& value : cmap.values()) {
        counter += value;
    }
    test.checkEqual(9, counter, "const value iterator");
}

void HashMapTests::test() {
    Test test("HashMap");
    testAdd(test);
    testMultipleAdd(test);
    testSearch(test);
    testAddReplace(test);
    testClear(test);
    testOverflow(test);
    testEmplace(test);
    testToString1(test);
    testToString2(test);
    testToString3(test);
    testCopy(test);
    testCopyAssignment(test);
    testMove(test);
    testMoveAssignment(test);
    testRemove(test);
    testEntryForEach(test);
    testKeyForEach(test);
    testValueForEach(test);
    test.finalize();
}