MathTests.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. TEST_CASE("TestCenterAngleAroundZero")
  50. {
  51. for (int i = 0; i < 10; i += 2)
  52. {
  53. CHECK_APPROX_EQUAL(CenterAngleAroundZero(i * JPH_PI), 0, 1.0e-5f);
  54. CHECK_APPROX_EQUAL(CenterAngleAroundZero((0.5f + i) * JPH_PI), 0.5f * JPH_PI, 1.0e-5f);
  55. CHECK_APPROX_EQUAL(CenterAngleAroundZero((1.5f + i) * JPH_PI), -0.5f * JPH_PI, 1.0e-5f);
  56. CHECK_APPROX_EQUAL(CenterAngleAroundZero(-(0.5f + i) * JPH_PI), -0.5f * JPH_PI, 1.0e-5f);
  57. CHECK_APPROX_EQUAL(CenterAngleAroundZero(-(1.5f + i) * JPH_PI), 0.5f * JPH_PI, 1.0e-5f);
  58. CHECK_APPROX_EQUAL(CenterAngleAroundZero(-(0.99f + i) * JPH_PI), -0.99f * JPH_PI, 1.0e-5f);
  59. CHECK_APPROX_EQUAL(CenterAngleAroundZero((0.99f + i) * JPH_PI), 0.99f * JPH_PI, 1.0e-5f);
  60. }
  61. }
  62. TEST_CASE("TestIsPowerOf2")
  63. {
  64. for (int i = 0; i < 63; ++i)
  65. CHECK(IsPowerOf2(uint64(1) << 1));
  66. CHECK(!IsPowerOf2(-2));
  67. CHECK(!IsPowerOf2(0));
  68. CHECK(!IsPowerOf2(3));
  69. CHECK(!IsPowerOf2(5));
  70. CHECK(!IsPowerOf2(15));
  71. CHECK(!IsPowerOf2(17));
  72. CHECK(!IsPowerOf2(65535));
  73. CHECK(!IsPowerOf2(65537));
  74. }
  75. }