#include "../../src/ErrorSimulator.hpp"
#include "../Tests.hpp"
#include "core/data/BitArray.hpp"

static void testSetRead() {
    Core::BitArray bits;
    CORE_TEST_ERROR(bits.resize(4, 3));
    bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
    CORE_TEST_EQUAL(1, bits.get(0));
    CORE_TEST_EQUAL(2, bits.get(1));
    CORE_TEST_EQUAL(3, bits.get(2));
    CORE_TEST_EQUAL(4, bits.get(3));
}

static void testOutOfBoundsSetRead() {
    Core::BitArray bits;
    bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
    CORE_TEST_EQUAL(0, bits.get(0));
    CORE_TEST_EQUAL(0, bits.get(1));
    CORE_TEST_EQUAL(0, bits.get(2));
    CORE_TEST_EQUAL(0, bits.get(3));
}

static void testBigSetRead() {
    Core::BitArray bits;
    CORE_TEST_ERROR(bits.resize(100, 13));
    for(u64 i = 0; i < bits.getLength(); i++) {
        bits.set(i, i);
    }
    for(u64 i = 0; i < bits.getLength(); i++) {
        CORE_TEST_EQUAL(i, bits.get(i));
    }
}

static void testRandomSetReadResize() {
    const int length = 100;
    u64 data[length];
    Core::BitArray bits;
    CORE_TEST_ERROR(bits.resize(100, 13));
    u64 seed = 534;
    for(int k = 0; k < 20; k++) {
        for(u64 i = 0; i < bits.getLength(); i++) {
            seed = seed * 636455 + 53453;
            bits.set(i, seed & (0x1FFF));
            data[i] = seed & (0x1FFF);
        }
    }
    for(u64 i = 0; i < bits.getLength(); i++) {
        CORE_TEST_EQUAL(data[i], bits.get(i));
    }
    CORE_TEST_ERROR(bits.resize(bits.getLength(), bits.getBits() + 1));
    CORE_TEST_EQUAL(14, bits.getBits());
    CORE_TEST_EQUAL(100, bits.getLength());
    for(u64 i = 0; i < bits.getLength(); i++) {
        CORE_TEST_EQUAL(data[i], bits.get(i));
    }
}

static void testReadOnly() {
    Core::BitArray bits;
    CORE_TEST_ERROR(bits.resize(4, 3));
    bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
    Core::BitArray copy;
    CORE_TEST_ERROR(copy.copyFrom(bits));
    const Core::BitArray bits2 = Core::move(copy);
    CORE_TEST_EQUAL(1, bits2.get(0));
    CORE_TEST_EQUAL(2, bits2.get(1));
    CORE_TEST_EQUAL(3, bits2.get(2));
    CORE_TEST_EQUAL(4, bits2.get(3));
}

static void testSelect() {
    Core::BitArray bits;
    CORE_TEST_ERROR(bits.resize(90, 1));
    bits.fill(0);
    bits.set(0, 1).set(5, 1).set(20, 1).set(31, 1);
    bits.set(32, 1).set(33, 1).set(60, 1);
    CORE_TEST_EQUAL(-1, bits.select(0));
    CORE_TEST_EQUAL(0, bits.select(1));
    CORE_TEST_EQUAL(5, bits.select(2));
    CORE_TEST_EQUAL(20, bits.select(3));
    CORE_TEST_EQUAL(31, bits.select(4));
    CORE_TEST_EQUAL(32, bits.select(5));
    CORE_TEST_EQUAL(33, bits.select(6));
    CORE_TEST_EQUAL(60, bits.select(7));
    CORE_TEST_EQUAL(-1, bits.select(8));
}

static void testToString1() {
    Core::BitArray bits;
    CORE_TEST_ERROR(bits.resize(4, 3));
    bits.set(0, 1).set(1, 2).set(2, 3).set(3, 4);
    CORE_TEST_STRING("[1, 2, 3, 4]", bits);
}

static void testToString2() {
    Core::BitArray bits;
    CORE_TEST_ERROR(bits.resize(1, 3));
    bits.set(0, 1);
    CORE_TEST_STRING("[1]", bits);
}

static void testToString3() {
    Core::BitArray bits;
    CORE_TEST_STRING("[]", bits);
}

static void testResizeExact() {
    Core::BitArray bits;
    CORE_TEST_EQUAL(0, bits.getInternalByteSize());
    // the size in bytes matches the internal storage type
    u64 elements = sizeof(u64);
    CORE_TEST_ERROR(bits.resize(elements, 8));
    for(u64 i = 0; i < elements; i++) {
        bits.set(i, i);
    }
    for(u64 i = 0; i < elements; i++) {
        CORE_TEST_EQUAL(i, bits.get(i));
    }
    CORE_TEST_EQUAL(CORE_SIZE(u64), bits.getInternalByteSize());
}

static void testMoveAssignment() {
    Core::BitArray bits;
    CORE_TEST_ERROR(bits.resize(8, 8));
    for(u64 i = 0; i < bits.getLength(); i++) {
        bits.set(i, i);
    }
    Core::BitArray m;
    m = Core::move(bits);
    CORE_TEST_EQUAL(8, m.getLength());
    for(u64 i = 0; i < m.getLength(); i++) {
        CORE_TEST_EQUAL(i, m.get(i));
    }
}

static void testInvalidArgument() {
    Core::BitArray bits;
    CORE_TEST_EQUAL(Core::ErrorCode::INVALID_ARGUMENT, bits.resize(0, 5));
    CORE_TEST_EQUAL(Core::ErrorCode::INVALID_ARGUMENT, bits.resize(5, 0));
    CORE_TEST_EQUAL(Core::ErrorCode::INVALID_ARGUMENT, bits.resize(0, 0));
    CORE_TEST_EQUAL(Core::ErrorCode::INVALID_ARGUMENT, bits.resize(1, 65));
    CORE_TEST_EQUAL(Core::ErrorCode::INVALID_ARGUMENT, bits.resize(5, 68));
}

void Core::testBitArray() {
    testSetRead();
    testOutOfBoundsSetRead();
    testBigSetRead();
    testRandomSetReadResize();
    testReadOnly();
    testSelect();
    testToString1();
    testToString2();
    testToString3();
    testResizeExact();
    testMoveAssignment();
    testInvalidArgument();
}