MathTests.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include "UnitTestFramework.h"
  5. #include <Jolt/Math/Mat44.h>
  6. TEST_SUITE("Mat44Tests")
  7. {
  8. TEST_CASE("TestCountTrailingZeros")
  9. {
  10. CHECK(CountTrailingZeros(0) == 32);
  11. for (int i = 0; i < 32; ++i)
  12. CHECK(CountTrailingZeros(1U << i) == i);
  13. }
  14. TEST_CASE("TestCountLeadingZeros")
  15. {
  16. CHECK(CountLeadingZeros(0) == 32);
  17. for (int i = 0; i < 32; ++i)
  18. CHECK(CountLeadingZeros(1U << i) == 31 - i);
  19. }
  20. TEST_CASE("TestCountBits")
  21. {
  22. CHECK(CountBits(0) == 0);
  23. CHECK(CountBits(0b10000000000000000000000000000000) == 1);
  24. CHECK(CountBits(0b00000000000000000000000000000001) == 1);
  25. CHECK(CountBits(0b10000000000000001000000000000000) == 2);
  26. CHECK(CountBits(0b00000000000000010000000000000001) == 2);
  27. CHECK(CountBits(0b10000000100000001000000010000000) == 4);
  28. CHECK(CountBits(0b00000001000000010000000100000001) == 4);
  29. CHECK(CountBits(0b10001000100010001000100010001000) == 8);
  30. CHECK(CountBits(0b00010001000100010001000100010001) == 8);
  31. CHECK(CountBits(0b10101010101010101010101010101010) == 16);
  32. CHECK(CountBits(0b01010101010101010101010101010101) == 16);
  33. CHECK(CountBits(0b11111111111111111111111111111111) == 32);
  34. }
  35. TEST_CASE("TestNextPowerOf2")
  36. {
  37. CHECK(GetNextPowerOf2(0) == 1);
  38. for (int i = 0; i < 31; ++i)
  39. {
  40. uint32 pow = uint32(1) << i;
  41. if (pow > 2)
  42. CHECK(GetNextPowerOf2(pow - 1) == pow);
  43. CHECK(GetNextPowerOf2(pow) == pow);
  44. CHECK(GetNextPowerOf2(pow + 1) == pow << 1);
  45. }
  46. CHECK(GetNextPowerOf2(0x8000000U - 1) == 0x8000000U);
  47. CHECK(GetNextPowerOf2(0x8000000U) == 0x8000000U);
  48. }
  49. }