MathTests.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #include "tests/MathTests.h"
  2. #include "math/Math.h"
  3. #include "tests/Test.h"
  4. const float eps = 0.0001f;
  5. static void testInterpolate(Test& test) {
  6. test.checkFloat(7.5f, Math::interpolate(5.0f, 10.0f, 0.5f), eps,
  7. "interpolate 1");
  8. test.checkFloat(-2.0, Math::interpolate(-10.0f, 10.0f, 0.4f), eps,
  9. "interpolate 2");
  10. test.checkFloat(10.0f, Math::interpolate(-3.0f, 10.0f, 1.0f), eps,
  11. "interpolate 3");
  12. test.checkFloat(7.0f, Math::interpolate(7.0f, 10.0f, 0.0f), eps,
  13. "interpolate 4");
  14. test.checkFloat(6.0f, Math::interpolate(0.0f, 10.0f, 0.6f), eps,
  15. "interpolate 5");
  16. }
  17. static void testPopCount(Test& test) {
  18. test.checkEqual(4, Math::popCount(0xF), "pop count 1");
  19. test.checkEqual(0, Math::popCount(0x0), "pop count 2");
  20. test.checkEqual(2, Math::popCount(0x6), "pop count 3");
  21. test.checkEqual(7, Math::popCount(0x7F), "pop count 4");
  22. test.checkEqual(3, Math::popCount(0x2A), "pop count 5");
  23. test.checkEqual(32, Math::popCount(0xFFFFFFFF), "pop count 6");
  24. test.checkEqual(64, Math::popCount(0xFFFFFFFFFFFFFFFFL), "pop count 7");
  25. test.checkEqual(44, Math::popCount(0xFFFF0FFFFFFF), "pop count 8");
  26. test.checkEqual(32, Math::popCount(-1), "pop count 9");
  27. }
  28. static void testRoundUpLog2(Test& test) {
  29. test.checkEqual(0, Math::roundUpLog2(-5), "round up log2 1");
  30. test.checkEqual(0, Math::roundUpLog2(0), "round up log2 2");
  31. test.checkEqual(1, Math::roundUpLog2(1), "round up log2 3");
  32. test.checkEqual(1, Math::roundUpLog2(2), "round up log2 4");
  33. test.checkEqual(2, Math::roundUpLog2(3), "round up log2 5");
  34. test.checkEqual(2, Math::roundUpLog2(4), "round up log2 6");
  35. test.checkEqual(3, Math::roundUpLog2(5), "round up log2 7");
  36. test.checkEqual(4, Math::roundUpLog2(10), "round up log2 8");
  37. test.checkEqual(5, Math::roundUpLog2(20), "round up log2 9");
  38. test.checkEqual(16, Math::roundUpLog2(35345), "round up log2 10");
  39. test.checkEqual(31, Math::roundUpLog2(0x7FFFFFFF), "round up log2 11");
  40. }
  41. static void testMin(Test& test) {
  42. test.checkEqual(-5, Math::min(-5), "min 1");
  43. test.checkEqual(-10, Math::min(-5, 4, 3, 2, -10), "min 2");
  44. test.checkEqual(4, Math::min(5, 20, 4, 30), "min 3");
  45. }
  46. static void testMax(Test& test) {
  47. test.checkEqual(-5, Math::max(-5), "max 1");
  48. test.checkEqual(4, Math::max(-5, 4, 3, 2, -10), "max 2");
  49. test.checkEqual(30, Math::max(5, 20, 4, 30), "max 3");
  50. }
  51. static void testClamp(Test& test) {
  52. test.checkEqual(5, Math::clamp(1, 5, 10), "clamp 1");
  53. test.checkEqual(7, Math::clamp(7, 5, 10), "clamp 2");
  54. test.checkEqual(10, Math::clamp(20, 5, 10), "clamp 3");
  55. test.checkEqual(5, Math::clamp(1, 10, 5), "clamp 4");
  56. test.checkEqual(7, Math::clamp(7, 10, 5), "clamp 5");
  57. test.checkEqual(10, Math::clamp(20, 10, 5), "clamp 6");
  58. }
  59. void MathTests::test() {
  60. Test test("Math");
  61. testInterpolate(test);
  62. testPopCount(test);
  63. testRoundUpLog2(test);
  64. testMin(test);
  65. testMax(test);
  66. testClamp(test);
  67. test.finalize();
  68. }