MathTests.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 testRadianToDegree() {
  62. CORE_TEST_FLOAT(45.0f, CMath::radianToDegree(CMath::PI * 0.25f), eps);
  63. CORE_TEST_FLOAT(90.0f, CMath::radianToDegree(CMath::PI * 0.5f), eps);
  64. CORE_TEST_FLOAT(180.0f, CMath::radianToDegree(CMath::PI), eps);
  65. CORE_TEST_FLOAT(360.0f, CMath::radianToDegree(CMath::PI * 2.0f), eps);
  66. }
  67. static void testDegreeToRadian() {
  68. CORE_TEST_FLOAT(CMath::PI * 0.25f, CMath::degreeToRadian(45.0f), eps);
  69. CORE_TEST_FLOAT(CMath::PI * 0.5f, CMath::degreeToRadian(90.0f), eps);
  70. CORE_TEST_FLOAT(CMath::PI, CMath::degreeToRadian(180.0f), eps);
  71. CORE_TEST_FLOAT(CMath::PI * 2.0f, CMath::degreeToRadian(360.0f), eps);
  72. }
  73. void Core::testMath() {
  74. testInterpolate();
  75. testIsPowerOf2();
  76. testRoundUpLog2();
  77. testMin();
  78. testMax();
  79. testClamp();
  80. testRadianToDegree();
  81. testDegreeToRadian();
  82. }