MathTests.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #include "tests/MathTests.h"
  2. #include "math/Math.h"
  3. #include "test/Test.h"
  4. constexpr float eps = 0.0001f;
  5. namespace CMath = Core::Math;
  6. static void testInterpolate() {
  7. CORE_TEST_FLOAT(7.5f, CMath::interpolate(5.0f, 10.0f, 0.5f), eps);
  8. CORE_TEST_FLOAT(-2.0, CMath::interpolate(-10.0f, 10.0f, 0.4f), eps);
  9. CORE_TEST_FLOAT(10.0f, CMath::interpolate(-3.0f, 10.0f, 1.0f), eps);
  10. CORE_TEST_FLOAT(7.0f, CMath::interpolate(7.0f, 10.0f, 0.0f), eps);
  11. CORE_TEST_FLOAT(6.0f, CMath::interpolate(0.0f, 10.0f, 0.6f), eps);
  12. }
  13. static void testRoundUpLog2() {
  14. CORE_TEST_EQUAL(0, CMath::roundUpLog2(-5));
  15. CORE_TEST_EQUAL(0, CMath::roundUpLog2(0));
  16. CORE_TEST_EQUAL(1, CMath::roundUpLog2(1));
  17. CORE_TEST_EQUAL(1, CMath::roundUpLog2(2));
  18. CORE_TEST_EQUAL(2, CMath::roundUpLog2(3));
  19. CORE_TEST_EQUAL(2, CMath::roundUpLog2(4));
  20. CORE_TEST_EQUAL(3, CMath::roundUpLog2(5));
  21. CORE_TEST_EQUAL(4, CMath::roundUpLog2(10));
  22. CORE_TEST_EQUAL(5, CMath::roundUpLog2(20));
  23. CORE_TEST_EQUAL(16, CMath::roundUpLog2(35345));
  24. CORE_TEST_EQUAL(31, CMath::roundUpLog2(0x7FFFFFFF));
  25. }
  26. static void testMin() {
  27. CORE_TEST_EQUAL(-5, CMath::min(-5));
  28. CORE_TEST_EQUAL(-10, CMath::min(-5, 4, 3, 2, -10));
  29. CORE_TEST_EQUAL(4, CMath::min(5, 20, 4, 30));
  30. }
  31. static void testMax() {
  32. CORE_TEST_EQUAL(-5, CMath::max(-5));
  33. CORE_TEST_EQUAL(4, CMath::max(-5, 4, 3, 2, -10));
  34. CORE_TEST_EQUAL(30, CMath::max(5, 20, 4, 30));
  35. }
  36. static void testClamp() {
  37. CORE_TEST_EQUAL(5, CMath::clamp(1, 5, 10));
  38. CORE_TEST_EQUAL(7, CMath::clamp(7, 5, 10));
  39. CORE_TEST_EQUAL(10, CMath::clamp(20, 5, 10));
  40. CORE_TEST_EQUAL(5, CMath::clamp(1, 10, 5));
  41. CORE_TEST_EQUAL(7, CMath::clamp(7, 10, 5));
  42. CORE_TEST_EQUAL(10, CMath::clamp(20, 10, 5));
  43. }
  44. static void testSinCos() {
  45. for(float f = -10.0f; f < 10.0f; f += 0.1f) {
  46. float rSin = 0.0f;
  47. float rCos = 0.0f;
  48. CMath::sinCos(f, rSin, rCos);
  49. CORE_TEST_FLOAT(CMath::sin(f), rSin, eps);
  50. CORE_TEST_FLOAT(CMath::cos(f), rCos, eps);
  51. }
  52. }
  53. static void testRadianToDegree() {
  54. CORE_TEST_FLOAT(45.0f, CMath::radianToDegree(CMath::PI * 0.25f), eps);
  55. CORE_TEST_FLOAT(90.0f, CMath::radianToDegree(CMath::PI * 0.5f), eps);
  56. CORE_TEST_FLOAT(180.0f, CMath::radianToDegree(CMath::PI), eps);
  57. CORE_TEST_FLOAT(360.0f, CMath::radianToDegree(CMath::PI * 2.0f), eps);
  58. }
  59. static void testDegreeToRadian() {
  60. CORE_TEST_FLOAT(CMath::PI * 0.25f, CMath::degreeToRadian(45.0f), eps);
  61. CORE_TEST_FLOAT(CMath::PI * 0.5f, CMath::degreeToRadian(90.0f), eps);
  62. CORE_TEST_FLOAT(CMath::PI, CMath::degreeToRadian(180.0f), eps);
  63. CORE_TEST_FLOAT(CMath::PI * 2.0f, CMath::degreeToRadian(360.0f), eps);
  64. }
  65. void Core::MathTests::test() {
  66. testInterpolate();
  67. testRoundUpLog2();
  68. testMin();
  69. testMax();
  70. testClamp();
  71. testSinCos();
  72. testRadianToDegree();
  73. testDegreeToRadian();
  74. }