UtilsTests.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #include "tests/UtilsTests.h"
  2. #include "tests/Test.h"
  3. #include "utils/Utils.h"
  4. const float eps = 0.0001f;
  5. static void testInterpolate(Test& test) {
  6. test.checkFloat(7.5f, Utils::interpolate(5.0f, 10.0f, 0.5f), eps, "interpolate 1");
  7. test.checkFloat(-2.0, Utils::interpolate(-10.0f, 10.0f, 0.4f), eps, "interpolate 2");
  8. test.checkFloat(10.0f, Utils::interpolate(-3.0f, 10.0f, 1.0f), eps, "interpolate 3");
  9. test.checkFloat(7.0f, Utils::interpolate(7.0f, 10.0f, 0.0f), eps, "interpolate 4");
  10. test.checkFloat(6.0f, Utils::interpolate(0.0f, 10.0f, 0.6f), eps, "interpolate 5");
  11. }
  12. static void testPopCount(Test& test) {
  13. test.checkEqual(4, Utils::popCount(0xF), "pop count 1");
  14. test.checkEqual(0, Utils::popCount(0x0), "pop count 2");
  15. test.checkEqual(2, Utils::popCount(0x6), "pop count 3");
  16. test.checkEqual(7, Utils::popCount(0x7F), "pop count 4");
  17. test.checkEqual(3, Utils::popCount(0x2A), "pop count 5");
  18. test.checkEqual(32, Utils::popCount(0xFFFFFFFF), "pop count 6");
  19. test.checkEqual(64, Utils::popCount(0xFFFFFFFFFFFFFFFFL), "pop count 7");
  20. test.checkEqual(44, Utils::popCount(0xFFFF0FFFFFFF), "pop count 8");
  21. test.checkEqual(32, Utils::popCount(-1), "pop count 9");
  22. }
  23. static void testRoundUpLog2(Test& test) {
  24. test.checkEqual(0, Utils::roundUpLog2(-5), "round up log2 1");
  25. test.checkEqual(0, Utils::roundUpLog2(0), "round up log2 2");
  26. test.checkEqual(1, Utils::roundUpLog2(1), "round up log2 3");
  27. test.checkEqual(1, Utils::roundUpLog2(2), "round up log2 4");
  28. test.checkEqual(2, Utils::roundUpLog2(3), "round up log2 5");
  29. test.checkEqual(2, Utils::roundUpLog2(4), "round up log2 6");
  30. test.checkEqual(3, Utils::roundUpLog2(5), "round up log2 7");
  31. test.checkEqual(4, Utils::roundUpLog2(10), "round up log2 8");
  32. test.checkEqual(5, Utils::roundUpLog2(20), "round up log2 9");
  33. test.checkEqual(16, Utils::roundUpLog2(35345), "round up log2 10");
  34. test.checkEqual(31, Utils::roundUpLog2(0x7FFFFFFF), "round up log2 11");
  35. }
  36. void UtilsTests::test() {
  37. Test test("Utils");
  38. testInterpolate(test);
  39. testPopCount(test);
  40. testRoundUpLog2(test);
  41. test.finalize();
  42. }