MathTests.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "tests/MathTests.h"
  2. #include "math/Math.h"
  3. #include "test/Test.h"
  4. constexpr float eps = 0.0001f;
  5. static void testInterpolate(Core::Test& test) {
  6. test.checkFloat(7.5f, Core::Math::interpolate(5.0f, 10.0f, 0.5f), eps,
  7. "interpolate 1");
  8. test.checkFloat(-2.0, Core::Math::interpolate(-10.0f, 10.0f, 0.4f), eps,
  9. "interpolate 2");
  10. test.checkFloat(10.0f, Core::Math::interpolate(-3.0f, 10.0f, 1.0f), eps,
  11. "interpolate 3");
  12. test.checkFloat(7.0f, Core::Math::interpolate(7.0f, 10.0f, 0.0f), eps,
  13. "interpolate 4");
  14. test.checkFloat(6.0f, Core::Math::interpolate(0.0f, 10.0f, 0.6f), eps,
  15. "interpolate 5");
  16. }
  17. static void testRoundUpLog2(Core::Test& test) {
  18. test.checkEqual(0, Core::Math::roundUpLog2(-5), "round up log2 1");
  19. test.checkEqual(0, Core::Math::roundUpLog2(0), "round up log2 2");
  20. test.checkEqual(1, Core::Math::roundUpLog2(1), "round up log2 3");
  21. test.checkEqual(1, Core::Math::roundUpLog2(2), "round up log2 4");
  22. test.checkEqual(2, Core::Math::roundUpLog2(3), "round up log2 5");
  23. test.checkEqual(2, Core::Math::roundUpLog2(4), "round up log2 6");
  24. test.checkEqual(3, Core::Math::roundUpLog2(5), "round up log2 7");
  25. test.checkEqual(4, Core::Math::roundUpLog2(10), "round up log2 8");
  26. test.checkEqual(5, Core::Math::roundUpLog2(20), "round up log2 9");
  27. test.checkEqual(16, Core::Math::roundUpLog2(35345), "round up log2 10");
  28. test.checkEqual(31, Core::Math::roundUpLog2(0x7FFFFFFF),
  29. "round up log2 11");
  30. }
  31. static void testMin(Core::Test& test) {
  32. test.checkEqual(-5, Core::Math::min(-5), "min 1");
  33. test.checkEqual(-10, Core::Math::min(-5, 4, 3, 2, -10), "min 2");
  34. test.checkEqual(4, Core::Math::min(5, 20, 4, 30), "min 3");
  35. }
  36. static void testMax(Core::Test& test) {
  37. test.checkEqual(-5, Core::Math::max(-5), "max 1");
  38. test.checkEqual(4, Core::Math::max(-5, 4, 3, 2, -10), "max 2");
  39. test.checkEqual(30, Core::Math::max(5, 20, 4, 30), "max 3");
  40. }
  41. static void testClamp(Core::Test& test) {
  42. test.checkEqual(5, Core::Math::clamp(1, 5, 10), "clamp 1");
  43. test.checkEqual(7, Core::Math::clamp(7, 5, 10), "clamp 2");
  44. test.checkEqual(10, Core::Math::clamp(20, 5, 10), "clamp 3");
  45. test.checkEqual(5, Core::Math::clamp(1, 10, 5), "clamp 4");
  46. test.checkEqual(7, Core::Math::clamp(7, 10, 5), "clamp 5");
  47. test.checkEqual(10, Core::Math::clamp(20, 10, 5), "clamp 6");
  48. }
  49. void Core::MathTests::test() {
  50. Core::Test test("Math");
  51. testInterpolate(test);
  52. testRoundUpLog2(test);
  53. testMin(test);
  54. testMax(test);
  55. testClamp(test);
  56. test.finalize();
  57. }