MathTests.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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() {
  6. CORE_TEST_FLOAT(7.5f, Core::Math::interpolate(5.0f, 10.0f, 0.5f), eps);
  7. CORE_TEST_FLOAT(-2.0, Core::Math::interpolate(-10.0f, 10.0f, 0.4f), eps);
  8. CORE_TEST_FLOAT(10.0f, Core::Math::interpolate(-3.0f, 10.0f, 1.0f), eps);
  9. CORE_TEST_FLOAT(7.0f, Core::Math::interpolate(7.0f, 10.0f, 0.0f), eps);
  10. CORE_TEST_FLOAT(6.0f, Core::Math::interpolate(0.0f, 10.0f, 0.6f), eps);
  11. }
  12. static void testRoundUpLog2() {
  13. CORE_TEST_EQUAL(0, Core::Math::roundUpLog2(-5));
  14. CORE_TEST_EQUAL(0, Core::Math::roundUpLog2(0));
  15. CORE_TEST_EQUAL(1, Core::Math::roundUpLog2(1));
  16. CORE_TEST_EQUAL(1, Core::Math::roundUpLog2(2));
  17. CORE_TEST_EQUAL(2, Core::Math::roundUpLog2(3));
  18. CORE_TEST_EQUAL(2, Core::Math::roundUpLog2(4));
  19. CORE_TEST_EQUAL(3, Core::Math::roundUpLog2(5));
  20. CORE_TEST_EQUAL(4, Core::Math::roundUpLog2(10));
  21. CORE_TEST_EQUAL(5, Core::Math::roundUpLog2(20));
  22. CORE_TEST_EQUAL(16, Core::Math::roundUpLog2(35345));
  23. CORE_TEST_EQUAL(31, Core::Math::roundUpLog2(0x7FFFFFFF));
  24. }
  25. static void testMin() {
  26. CORE_TEST_EQUAL(-5, Core::Math::min(-5));
  27. CORE_TEST_EQUAL(-10, Core::Math::min(-5, 4, 3, 2, -10));
  28. CORE_TEST_EQUAL(4, Core::Math::min(5, 20, 4, 30));
  29. }
  30. static void testMax() {
  31. CORE_TEST_EQUAL(-5, Core::Math::max(-5));
  32. CORE_TEST_EQUAL(4, Core::Math::max(-5, 4, 3, 2, -10));
  33. CORE_TEST_EQUAL(30, Core::Math::max(5, 20, 4, 30));
  34. }
  35. static void testClamp() {
  36. CORE_TEST_EQUAL(5, Core::Math::clamp(1, 5, 10));
  37. CORE_TEST_EQUAL(7, Core::Math::clamp(7, 5, 10));
  38. CORE_TEST_EQUAL(10, Core::Math::clamp(20, 5, 10));
  39. CORE_TEST_EQUAL(5, Core::Math::clamp(1, 10, 5));
  40. CORE_TEST_EQUAL(7, Core::Math::clamp(7, 10, 5));
  41. CORE_TEST_EQUAL(10, Core::Math::clamp(20, 10, 5));
  42. }
  43. void Core::MathTests::test() {
  44. testInterpolate();
  45. testRoundUpLog2();
  46. testMin();
  47. testMax();
  48. testClamp();
  49. }