MathTests.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 testIsPowerOf2() {
  14. CORE_TEST_TRUE(CMath::isPowerOf2(1));
  15. CORE_TEST_TRUE(CMath::isPowerOf2(2));
  16. CORE_TEST_FALSE(CMath::isPowerOf2(3));
  17. CORE_TEST_TRUE(CMath::isPowerOf2(4));
  18. CORE_TEST_FALSE(CMath::isPowerOf2(5));
  19. CORE_TEST_FALSE(CMath::isPowerOf2(6));
  20. CORE_TEST_FALSE(CMath::isPowerOf2(7));
  21. CORE_TEST_TRUE(CMath::isPowerOf2(8));
  22. CORE_TEST_FALSE(CMath::isPowerOf2(9));
  23. CORE_TEST_FALSE(CMath::isPowerOf2(10));
  24. for(int i = 16; i < 30000; i *= 2) {
  25. CORE_TEST_TRUE(CMath::isPowerOf2(i));
  26. CORE_TEST_FALSE(CMath::isPowerOf2(i + 1));
  27. CORE_TEST_FALSE(CMath::isPowerOf2(i + 2));
  28. CORE_TEST_FALSE(CMath::isPowerOf2(i + 3));
  29. }
  30. }
  31. static void testRoundUpLog2() {
  32. CORE_TEST_EQUAL(0, CMath::roundUpLog2(-5));
  33. CORE_TEST_EQUAL(0, CMath::roundUpLog2(0));
  34. CORE_TEST_EQUAL(1, CMath::roundUpLog2(1));
  35. CORE_TEST_EQUAL(1, CMath::roundUpLog2(2));
  36. CORE_TEST_EQUAL(2, CMath::roundUpLog2(3));
  37. CORE_TEST_EQUAL(2, CMath::roundUpLog2(4));
  38. CORE_TEST_EQUAL(3, CMath::roundUpLog2(5));
  39. CORE_TEST_EQUAL(4, CMath::roundUpLog2(10));
  40. CORE_TEST_EQUAL(5, CMath::roundUpLog2(20));
  41. CORE_TEST_EQUAL(16, CMath::roundUpLog2(35345));
  42. CORE_TEST_EQUAL(31, CMath::roundUpLog2(0x7FFFFFFF));
  43. }
  44. static void testMin() {
  45. CORE_TEST_EQUAL(-5, CMath::min(-5));
  46. CORE_TEST_EQUAL(-10, CMath::min(-5, 4, 3, 2, -10));
  47. CORE_TEST_EQUAL(4, CMath::min(5, 20, 4, 30));
  48. }
  49. static void testMax() {
  50. CORE_TEST_EQUAL(-5, CMath::max(-5));
  51. CORE_TEST_EQUAL(4, CMath::max(-5, 4, 3, 2, -10));
  52. CORE_TEST_EQUAL(30, CMath::max(5, 20, 4, 30));
  53. }
  54. static void testClamp() {
  55. CORE_TEST_EQUAL(5, CMath::clamp(1, 5, 10));
  56. CORE_TEST_EQUAL(7, CMath::clamp(7, 5, 10));
  57. CORE_TEST_EQUAL(10, CMath::clamp(20, 5, 10));
  58. CORE_TEST_EQUAL(5, CMath::clamp(1, 10, 5));
  59. CORE_TEST_EQUAL(7, CMath::clamp(7, 10, 5));
  60. CORE_TEST_EQUAL(10, CMath::clamp(20, 10, 5));
  61. }
  62. static void testSinCos() {
  63. for(float f = -10.0f; f < 10.0f; f += 0.1f) {
  64. float rSin = 0.0f;
  65. float rCos = 0.0f;
  66. CMath::sinCos(f, rSin, rCos);
  67. CORE_TEST_FLOAT(CMath::sin(f), rSin, eps);
  68. CORE_TEST_FLOAT(CMath::cos(f), rCos, eps);
  69. }
  70. }
  71. static void testRadianToDegree() {
  72. CORE_TEST_FLOAT(45.0f, CMath::radianToDegree(CMath::PI * 0.25f), eps);
  73. CORE_TEST_FLOAT(90.0f, CMath::radianToDegree(CMath::PI * 0.5f), eps);
  74. CORE_TEST_FLOAT(180.0f, CMath::radianToDegree(CMath::PI), eps);
  75. CORE_TEST_FLOAT(360.0f, CMath::radianToDegree(CMath::PI * 2.0f), eps);
  76. }
  77. static void testDegreeToRadian() {
  78. CORE_TEST_FLOAT(CMath::PI * 0.25f, CMath::degreeToRadian(45.0f), eps);
  79. CORE_TEST_FLOAT(CMath::PI * 0.5f, CMath::degreeToRadian(90.0f), eps);
  80. CORE_TEST_FLOAT(CMath::PI, CMath::degreeToRadian(180.0f), eps);
  81. CORE_TEST_FLOAT(CMath::PI * 2.0f, CMath::degreeToRadian(360.0f), eps);
  82. }
  83. void Core::MathTests::test() {
  84. testInterpolate();
  85. testIsPowerOf2();
  86. testRoundUpLog2();
  87. testMin();
  88. testMax();
  89. testClamp();
  90. testSinCos();
  91. testRadianToDegree();
  92. testDegreeToRadian();
  93. }