#include "../Tests.hpp"
#include "core/math/Math.hpp"

constexpr float eps = 0.0001f;

namespace CMath = Core::Math;

static void testInterpolate() {
    CORE_TEST_FLOAT(7.5f, CMath::interpolate(5.0f, 10.0f, 0.5f), eps);
    CORE_TEST_FLOAT(-2.0, CMath::interpolate(-10.0f, 10.0f, 0.4f), eps);
    CORE_TEST_FLOAT(10.0f, CMath::interpolate(-3.0f, 10.0f, 1.0f), eps);
    CORE_TEST_FLOAT(7.0f, CMath::interpolate(7.0f, 10.0f, 0.0f), eps);
    CORE_TEST_FLOAT(6.0f, CMath::interpolate(0.0f, 10.0f, 0.6f), eps);
}

static void testIsPowerOf2() {
    CORE_TEST_TRUE(CMath::isPowerOf2(1));
    CORE_TEST_TRUE(CMath::isPowerOf2(2));
    CORE_TEST_FALSE(CMath::isPowerOf2(3));
    CORE_TEST_TRUE(CMath::isPowerOf2(4));
    CORE_TEST_FALSE(CMath::isPowerOf2(5));
    CORE_TEST_FALSE(CMath::isPowerOf2(6));
    CORE_TEST_FALSE(CMath::isPowerOf2(7));
    CORE_TEST_TRUE(CMath::isPowerOf2(8));
    CORE_TEST_FALSE(CMath::isPowerOf2(9));
    CORE_TEST_FALSE(CMath::isPowerOf2(10));
    for(int i = 16; i < 30000; i *= 2) {
        CORE_TEST_TRUE(CMath::isPowerOf2(i));
        CORE_TEST_FALSE(CMath::isPowerOf2(i + 1));
        CORE_TEST_FALSE(CMath::isPowerOf2(i + 2));
        CORE_TEST_FALSE(CMath::isPowerOf2(i + 3));
    }
}

static void testRoundUpLog2() {
    CORE_TEST_EQUAL(0, CMath::roundUpLog2(-5));
    CORE_TEST_EQUAL(0, CMath::roundUpLog2(0));
    CORE_TEST_EQUAL(1, CMath::roundUpLog2(1));
    CORE_TEST_EQUAL(1, CMath::roundUpLog2(2));
    CORE_TEST_EQUAL(2, CMath::roundUpLog2(3));
    CORE_TEST_EQUAL(2, CMath::roundUpLog2(4));
    CORE_TEST_EQUAL(3, CMath::roundUpLog2(5));
    CORE_TEST_EQUAL(4, CMath::roundUpLog2(10));
    CORE_TEST_EQUAL(5, CMath::roundUpLog2(20));
    CORE_TEST_EQUAL(16, CMath::roundUpLog2(35345));
    CORE_TEST_EQUAL(31, CMath::roundUpLog2(0x7FFFFFFF));
}

static void testMin() {
    CORE_TEST_EQUAL(-5, CMath::min(-5));
    CORE_TEST_EQUAL(-10, CMath::min(-5, 4, 3, 2, -10));
    CORE_TEST_EQUAL(4, CMath::min(5, 20, 4, 30));
}

static void testMax() {
    CORE_TEST_EQUAL(-5, CMath::max(-5));
    CORE_TEST_EQUAL(4, CMath::max(-5, 4, 3, 2, -10));
    CORE_TEST_EQUAL(30, CMath::max(5, 20, 4, 30));
}

static void testClamp() {
    CORE_TEST_EQUAL(5, CMath::clamp(1, 5, 10));
    CORE_TEST_EQUAL(7, CMath::clamp(7, 5, 10));
    CORE_TEST_EQUAL(10, CMath::clamp(20, 5, 10));
    CORE_TEST_EQUAL(5, CMath::clamp(1, 10, 5));
    CORE_TEST_EQUAL(7, CMath::clamp(7, 10, 5));
    CORE_TEST_EQUAL(10, CMath::clamp(20, 10, 5));
}

static void testSinCos() {
    for(float f = -10.0f; f < 10.0f; f += 0.1f) {
        float rSin = 0.0f;
        float rCos = 0.0f;
        CMath::sinCos(f, rSin, rCos);
        CORE_TEST_FLOAT(CMath::sin(f), rSin, eps);
        CORE_TEST_FLOAT(CMath::cos(f), rCos, eps);
    }
}

static void testRadianToDegree() {
    CORE_TEST_FLOAT(45.0f, CMath::radianToDegree(CMath::PI * 0.25f), eps);
    CORE_TEST_FLOAT(90.0f, CMath::radianToDegree(CMath::PI * 0.5f), eps);
    CORE_TEST_FLOAT(180.0f, CMath::radianToDegree(CMath::PI), eps);
    CORE_TEST_FLOAT(360.0f, CMath::radianToDegree(CMath::PI * 2.0f), eps);
}

static void testDegreeToRadian() {
    CORE_TEST_FLOAT(CMath::PI * 0.25f, CMath::degreeToRadian(45.0f), eps);
    CORE_TEST_FLOAT(CMath::PI * 0.5f, CMath::degreeToRadian(90.0f), eps);
    CORE_TEST_FLOAT(CMath::PI, CMath::degreeToRadian(180.0f), eps);
    CORE_TEST_FLOAT(CMath::PI * 2.0f, CMath::degreeToRadian(360.0f), eps);
}

void Core::testMath() {
    testInterpolate();
    testIsPowerOf2();
    testRoundUpLog2();
    testMin();
    testMax();
    testClamp();
    testSinCos();
    testRadianToDegree();
    testDegreeToRadian();
}