#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) : ProbingTest(o.a, o.b) {
        aInstances++;
    }

    ProbingTest(ProbingTest&& o) : ProbingTest(o.a, o.b) {
        aInstances++;
    }

    ~ProbingTest() {
        aInstances--;
    }

    ProbingTest& operator=(ProbingTest o) {
        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();
}