MathTests.cpp 3.5 KB

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