MathTests.cpp 2.8 KB

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