1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- #include "tests/MathTests.h"
- #include "math/Math.h"
- #include "test/Test.h"
- constexpr float eps = 0.0001f;
- static void testInterpolate(Core::Test& test) {
- test.checkFloat(7.5f, Core::Math::interpolate(5.0f, 10.0f, 0.5f), eps,
- "interpolate 1");
- test.checkFloat(-2.0, Core::Math::interpolate(-10.0f, 10.0f, 0.4f), eps,
- "interpolate 2");
- test.checkFloat(10.0f, Core::Math::interpolate(-3.0f, 10.0f, 1.0f), eps,
- "interpolate 3");
- test.checkFloat(7.0f, Core::Math::interpolate(7.0f, 10.0f, 0.0f), eps,
- "interpolate 4");
- test.checkFloat(6.0f, Core::Math::interpolate(0.0f, 10.0f, 0.6f), eps,
- "interpolate 5");
- }
- static void testRoundUpLog2(Core::Test& test) {
- test.checkEqual(0, Core::Math::roundUpLog2(-5), "round up log2 1");
- test.checkEqual(0, Core::Math::roundUpLog2(0), "round up log2 2");
- test.checkEqual(1, Core::Math::roundUpLog2(1), "round up log2 3");
- test.checkEqual(1, Core::Math::roundUpLog2(2), "round up log2 4");
- test.checkEqual(2, Core::Math::roundUpLog2(3), "round up log2 5");
- test.checkEqual(2, Core::Math::roundUpLog2(4), "round up log2 6");
- test.checkEqual(3, Core::Math::roundUpLog2(5), "round up log2 7");
- test.checkEqual(4, Core::Math::roundUpLog2(10), "round up log2 8");
- test.checkEqual(5, Core::Math::roundUpLog2(20), "round up log2 9");
- test.checkEqual(16, Core::Math::roundUpLog2(35345), "round up log2 10");
- test.checkEqual(31, Core::Math::roundUpLog2(0x7FFFFFFF),
- "round up log2 11");
- }
- static void testMin(Core::Test& test) {
- test.checkEqual(-5, Core::Math::min(-5), "min 1");
- test.checkEqual(-10, Core::Math::min(-5, 4, 3, 2, -10), "min 2");
- test.checkEqual(4, Core::Math::min(5, 20, 4, 30), "min 3");
- }
- static void testMax(Core::Test& test) {
- test.checkEqual(-5, Core::Math::max(-5), "max 1");
- test.checkEqual(4, Core::Math::max(-5, 4, 3, 2, -10), "max 2");
- test.checkEqual(30, Core::Math::max(5, 20, 4, 30), "max 3");
- }
- static void testClamp(Core::Test& test) {
- test.checkEqual(5, Core::Math::clamp(1, 5, 10), "clamp 1");
- test.checkEqual(7, Core::Math::clamp(7, 5, 10), "clamp 2");
- test.checkEqual(10, Core::Math::clamp(20, 5, 10), "clamp 3");
- test.checkEqual(5, Core::Math::clamp(1, 10, 5), "clamp 4");
- test.checkEqual(7, Core::Math::clamp(7, 10, 5), "clamp 5");
- test.checkEqual(10, Core::Math::clamp(20, 10, 5), "clamp 6");
- }
- void Core::MathTests::test() {
- Core::Test test("Math");
- testInterpolate(test);
- testRoundUpLog2(test);
- testMin(test);
- testMax(test);
- testClamp(test);
- test.finalize();
- }
|