#include "tests/StackAllocatorTests.h"
#include "tests/Test.h"
#include "memory/StackAllocator.h"

static void testBasicAllocation(Test& test) {
    int elements = 6;
    StackAllocator::Pointer p = StackAllocator::allocate(4, elements);
    test.checkEqual(0, p.pointer, "first int pointer is 0");
    test.checkEqual(6, elements, "enough storage for all elements");
    StackAllocator::free(p);
}

static void testRepeatedAllocation(Test& test) {
    int elements[] = {4, 7, 8, 10, 12};
    StackAllocator::Pointer p[5];
    for(int i = 0; i < 5; i++) {
        p[i] = StackAllocator::allocate(4, elements[i]);
    }
    test.checkEqual(0, p[0].pointer, "repeated allocation pointer check 1");
    test.checkEqual(16, p[1].pointer, "repeated allocation pointer check 2");
    test.checkEqual(48, p[2].pointer, "repeated allocation pointer check 3");
    test.checkEqual(80, p[3].pointer, "repeated allocation pointer check 4");
    test.checkEqual(128, p[4].pointer, "repeated allocation pointer check 5");
    test.checkEqual(4, elements[0], "repeated allocation element check 1");
    test.checkEqual(7, elements[1], "repeated allocation element check 2");
    test.checkEqual(8, elements[2], "repeated allocation element check 3");
    test.checkEqual(10, elements[3], "repeated allocation element check 4");
    test.checkEqual(12, elements[4], "repeated allocation element check 5");
    for(int i = 4; i >= 0; i--) {
        StackAllocator::free(p[i]);
    }
}

static void testFree(Test& test) {
    for(int i = 0; i < 100; i++) {
        int a = 3;
        StackAllocator::Pointer ap = StackAllocator::allocate(4, a);
        test.checkEqual(0, ap.pointer, "free int pointer 1");

        int b = 5;
        StackAllocator::Pointer bp = StackAllocator::allocate(4, b);
        test.checkEqual(16, bp.pointer, "free int pointer 2");

        int c = 7;
        StackAllocator::Pointer cp = StackAllocator::allocate(4, c);
        test.checkEqual(48, cp.pointer, "free int pointer 3");

        StackAllocator::free(cp);
        StackAllocator::free(bp);
        StackAllocator::free(ap);
    }
}

static void testOverflow(Test& test) {
    int elements = 99999999;
    StackAllocator::Pointer p = StackAllocator::allocate(4, elements);
    test.checkEqual(true, elements < 99999999, "survives overflow, capped");
    StackAllocator::free(p);
}

static void testGrow(Test& test) {
    for(int k = 0; k < 1000; k++) {
        int elements = 7;
        StackAllocator::Pointer p = StackAllocator::allocate(4, elements);
        for(int i = 0; i < 100; i++) {
            elements += StackAllocator::grow(p, 4, 4);
        }
        test.checkEqual(0, p.pointer, "grow 1");
        test.checkEqual(407, elements, "grow 2");
        StackAllocator::free(p);
    }
}

static void testNotGrow(Test& test) {
    int a = 7;
    StackAllocator::Pointer ap = StackAllocator::allocate(4, a);
    int b = 7;
    StackAllocator::Pointer bp = StackAllocator::allocate(4, b);
    int c = 7;
    StackAllocator::Pointer cp = StackAllocator::allocate(4, c);

    test.checkEqual(0, StackAllocator::grow(ap, 4, 4), "no growth when the pointer is not the last on the stack 1");
    test.checkEqual(0, StackAllocator::grow(bp, 4, 4), "no growth when the pointer is not the last on the stack 2");
    test.checkEqual(4, StackAllocator::grow(cp, 4, 4), "last element on stack can grow 1");

    StackAllocator::free(cp);

    test.checkEqual(0, StackAllocator::grow(ap, 4, 4), "no growth when the pointer is not the last on the stack 3");
    test.checkEqual(4, StackAllocator::grow(bp, 4, 4), "last element on stack can grow 2");

    StackAllocator::free(bp);

    test.checkEqual(4, StackAllocator::grow(ap, 4, 4), "last element on stack can grow 3");

    StackAllocator::free(ap);
}

static void testArray(Test& test) {
    for(int i = 0; i < 100000; i++) {
        StackAllocator::Array<int> a1(30);
        StackAllocator::Array<int> a2(50);
        StackAllocator::Array<int> a3(70);
        test.checkEqual(30, a1.getLength(), "free array 1");
        test.checkEqual(50, a2.getLength(), "free array 2");
        test.checkEqual(70, a3.getLength(), "free array 3");
    }
}

static void testArrayGrow(Test& test) {
    StackAllocator::Array<int> a(700);
    for(int i = 0; i < 100; i++) {
        a.grow(2000);
    }
    test.checkEqual(200700, a.getLength(), "array grow");
}

static void testArrayNotGrow(Test& test) {
    StackAllocator::Array<int> a(7);
    {
        StackAllocator::Array<int> b(7);
        {
            StackAllocator::Array<int> c(7);
            test.checkEqual(0, a.grow(4), "no growth when the pointer is not the last on the stack 1");
            test.checkEqual(0, b.grow(4), "no growth when the pointer is not the last on the stack 2");
            test.checkEqual(4, c.grow(4), "last element on stack can grow 1");
        }
        test.checkEqual(0, a.grow(4), "no growth when the pointer is not the last on the stack 3");
        test.checkEqual(4, b.grow(4), "last element on stack can grow 2");
    }
    test.checkEqual(4, a.grow(4), "last element on stack can grow 3");
}

void StackAllocatorTests::test() {
    Test test("StackAllocator");
    testBasicAllocation(test);
    testRepeatedAllocation(test);
    testFree(test);
    testOverflow(test);
    testGrow(test);
    testNotGrow(test);
    testArray(test);
    testArrayGrow(test);
    testArrayNotGrow(test);
    test.finalize();
}