#include "tests/UtilsTests.h"
#include "tests/Test.h"
#include "utils/Utils.h"

const float eps = 0.0001f;

static void testInterpolate(Test& test) {
    test.checkFloat(7.5f, Utils::interpolate(5.0f, 10.0f, 0.5f), eps, "interpolate 1");
    test.checkFloat(-2.0, Utils::interpolate(-10.0f, 10.0f, 0.4f), eps, "interpolate 2");
    test.checkFloat(10.0f, Utils::interpolate(-3.0f, 10.0f, 1.0f), eps, "interpolate 3");
    test.checkFloat(7.0f, Utils::interpolate(7.0f, 10.0f, 0.0f), eps, "interpolate 4");
    test.checkFloat(6.0f, Utils::interpolate(0.0f, 10.0f, 0.6f), eps, "interpolate 5");
}

static void testPopCount(Test& test) {
    test.checkEqual(4, Utils::popCount(0xF), "pop count 1");
    test.checkEqual(0, Utils::popCount(0x0), "pop count 2");
    test.checkEqual(2, Utils::popCount(0x6), "pop count 3");
    test.checkEqual(7, Utils::popCount(0x7F), "pop count 4");
    test.checkEqual(3, Utils::popCount(0x2A), "pop count 5");
    test.checkEqual(32, Utils::popCount(0xFFFFFFFF), "pop count 6");
    test.checkEqual(64, Utils::popCount(0xFFFFFFFFFFFFFFFFL), "pop count 7");
    test.checkEqual(44, Utils::popCount(0xFFFF0FFFFFFF), "pop count 8");
    test.checkEqual(32, Utils::popCount(-1), "pop count 9");
}

static void testRoundUpLog2(Test& test) {
    test.checkEqual(0, Utils::roundUpLog2(-5), "round up log2 1");
    test.checkEqual(0, Utils::roundUpLog2(0), "round up log2 2");
    test.checkEqual(1, Utils::roundUpLog2(1), "round up log2 3");
    test.checkEqual(1, Utils::roundUpLog2(2), "round up log2 4");
    test.checkEqual(2, Utils::roundUpLog2(3), "round up log2 5");
    test.checkEqual(2, Utils::roundUpLog2(4), "round up log2 6");
    test.checkEqual(3, Utils::roundUpLog2(5), "round up log2 7");
    test.checkEqual(4, Utils::roundUpLog2(10), "round up log2 8");
    test.checkEqual(5, Utils::roundUpLog2(20), "round up log2 9");
    test.checkEqual(16, Utils::roundUpLog2(35345), "round up log2 10");
    test.checkEqual(31, Utils::roundUpLog2(0x7FFFFFFF), "round up log2 11");
}

void UtilsTests::test() {
    Test test("Utils");
    testInterpolate(test);
    testPopCount(test);
    testRoundUpLog2(test);
    test.finalize();
}