#include "../Tests.hpp"
#include "core/data/HashMap.hpp"
#include "core/utils/HashedString.hpp"

template class Core::HashedString<32>;

using HString = Core::HashedString<32>;

static void testComparison() {
    HString a("test");
    HString b("testA");
    HString c("");
    HString d("Btest");

    CORE_TEST_TRUE(a == a);
    CORE_TEST_TRUE(b == b);
    CORE_TEST_TRUE(c == c);
    CORE_TEST_TRUE(d == d);
    CORE_TEST_TRUE(a != b);
    CORE_TEST_TRUE(a != c);
    CORE_TEST_TRUE(a != d);
    CORE_TEST_TRUE(b != a);
    CORE_TEST_TRUE(b != c);
    CORE_TEST_TRUE(b != d);
    CORE_TEST_TRUE(c != a);
    CORE_TEST_TRUE(c != b);
    CORE_TEST_TRUE(c != d);
    CORE_TEST_TRUE(d != a);
    CORE_TEST_TRUE(d != b);
    CORE_TEST_TRUE(d != c);
}

static void testLength() {
    HString s("test");
    CORE_TEST_EQUAL(4, s.getLength());
    CORE_TEST_EQUAL(31, s.getCapacity());
}

static void testHashCode() {
    HString a;
    HString b("wusi");
    CORE_TEST_EQUAL(a.hashCode(), 0lu);
    CORE_TEST_TRUE(b.hashCode() != 0u);
}

static void testAsHashMapKey() {
    Core::HashMap<HString, int> map;
    map.add("wusi", 3).add("hiThere", 7).add("baum123", 5);

    int* a = map.search("wusi");
    int* b = map.search("hiThere");
    int* c = map.search("baum123");
    int* d = map.search("423hifd");

    CORE_TEST_NOT_NULL(a);
    CORE_TEST_NOT_NULL(b);
    CORE_TEST_NOT_NULL(c);
    CORE_TEST_NULL(d);

    if(a != nullptr && b != nullptr && c != nullptr) {
        CORE_TEST_EQUAL(3, *a);
        CORE_TEST_EQUAL(7, *b);
        CORE_TEST_EQUAL(5, *c);
    }
}

void Core::testHashedString() {
    testComparison();
    testLength();
    testHashCode();
    testAsHashMapKey();
}