#include "tests/BufferedValueTests.h"

#include "math/BufferedValue.h"
#include "math/Vector.h"
#include "test/Test.h"

const float eps = 0.0001f;

static void testInit() {
    Core::BufferedValue<float> b = 5.0f;
    CORE_TEST_FLOAT(5.0f, b.get(0.0f), eps);
    CORE_TEST_FLOAT(5.0f, b.get(0.5f), eps);
    CORE_TEST_FLOAT(5.0f, b.get(1.0f), eps);
    CORE_TEST_FLOAT(5.0f, b, eps);
}

static void testInterpolate() {
    Core::BufferedValue<float> b = 5.0f;
    b = 7.0f;
    CORE_TEST_FLOAT(5.0f, b.get(0.0f), eps);
    CORE_TEST_FLOAT(6.0f, b.get(0.5f), eps);
    CORE_TEST_FLOAT(7.0f, b.get(1.0f), eps);
    CORE_TEST_FLOAT(7.0f, b, eps);
}

static void testUpdate() {
    Core::BufferedValue<float> b = 5.0f;
    b = 7.0f;
    b.update();
    CORE_TEST_FLOAT(7.0f, b.get(0.0f), eps);
    CORE_TEST_FLOAT(7.0f, b.get(0.5f), eps);
    CORE_TEST_FLOAT(7.0f, b.get(1.0f), eps);
    CORE_TEST_FLOAT(7.0f, b, eps);
}

static void testCalculate() {
    Core::BufferedValue<float> b = 5.0f;
    b = 7.0f;
    b += 3.0f;
    CORE_TEST_FLOAT(5.0f, b.get(0.0f), eps);
    CORE_TEST_FLOAT(7.5f, b.get(0.5f), eps);
    CORE_TEST_FLOAT(10.0f, b.get(1.0f), eps);
    CORE_TEST_FLOAT(10.0f, b, eps);
    CORE_TEST_FLOAT(12.0f, b + 2.0f, eps);
}

static void testVector() {
    Core::Vector2 base(5.0f, 6.0f);
    Core::BufferedValue<Core::Vector2> b = base;

    CORE_TEST_VECTOR(base, b.get(1.0f));
    b = Core::Vector2(7.0f, 5.0);
    CORE_TEST_VECTOR(Core::Vector2(7.0f, 5.0f), b.get(1.0f));
    b += Core::Vector2(1.0f, 1.0f);
    CORE_TEST_VECTOR(Core::Vector2(8.0f, 6.0f), b.get(1.0f));
    b -= Core::Vector2(1.0f, 1.0f);
    CORE_TEST_VECTOR(Core::Vector2(7.0f, 5.0f), b.get(1.0f));
    b *= Core::Vector2(2.0f, 2.0f);
    CORE_TEST_VECTOR(Core::Vector2(14.0f, 10.0f), b.get(1.0f));
    b /= Core::Vector2(0.5f, 0.5f);
    CORE_TEST_VECTOR(Core::Vector2(28.0f, 20.0f), b.get(1.0f));
    b = b + Core::Vector2(1.0f, 1.0f);
    CORE_TEST_VECTOR(Core::Vector2(29.0f, 21.0f), b.get(1.0f));
    b = b - Core::Vector2(1.0f, 1.0f);
    CORE_TEST_VECTOR(Core::Vector2(28.0f, 20.0f), b.get(1.0f));
    b = b * Core::Vector2(2.0f, 2.0f);
    CORE_TEST_VECTOR(Core::Vector2(56.0f, 40.0f), b.get(1.0f));
    b = b / Core::Vector2(0.5f, 0.5f);
    CORE_TEST_VECTOR(Core::Vector2(112.0f, 80.0f), b.get(1.0f));
    b = Core::Vector2(1.0f, 1.0f) + b;
    CORE_TEST_VECTOR(Core::Vector2(113.0f, 81.0f), b.get(1.0f));
    b = Core::Vector2(1.0f, 1.0f) - b;
    CORE_TEST_VECTOR(Core::Vector2(-112.0f, -80.0f), b.get(1.0f));
    b = Core::Vector2(2.0f, 2.0f) * b;
    CORE_TEST_VECTOR(Core::Vector2(-224.0f, -160.0f), b.get(1.0f));
    b = Core::Vector2(224.0f, 160.0f) / b;
    CORE_TEST_VECTOR(Core::Vector2(-1.0f, -1.0f), b.get(1.0f));
    b = -b;
    CORE_TEST_VECTOR(Core::Vector2(1.0f, 1.0f), b.get(1.0f));
    b[0] += 3;
    CORE_TEST_VECTOR(Core::Vector2(4.0f, 1.0f), b.get(1.0f));
}

void Core::BufferedValueTests::test() {
    testInit();
    testInterpolate();
    testUpdate();
    testCalculate();
    testVector();
}