SubShapeIDTest.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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/Physics/Collision/Shape/SubShapeID.h>
  6. TEST_SUITE("SubShapeIDTest")
  7. {
  8. struct SSPair
  9. {
  10. uint32 mValue;
  11. uint mNumBits;
  12. };
  13. using SSPairs = Array<SSPair>;
  14. // Helper function that pushes sub shape ID's on the creator and checks that they come out again
  15. static void TestPushPop(const SSPairs &inPairs)
  16. {
  17. // Push all id's on the creator
  18. SubShapeIDCreator creator;
  19. int total_bits = 0;
  20. for (const SSPair &p : inPairs)
  21. {
  22. creator = creator.PushID(p.mValue, p.mNumBits);
  23. total_bits += p.mNumBits;
  24. }
  25. CHECK(creator.GetNumBitsWritten() == total_bits);
  26. // Now pop all parts
  27. SubShapeID id = creator.GetID();
  28. for (const SSPair &p : inPairs)
  29. {
  30. // There should be data (note there is a possibility of a false positive if the bit pattern is all 1's)
  31. CHECK(!id.IsEmpty());
  32. // Pop the part
  33. SubShapeID remainder;
  34. uint32 value = id.PopID(p.mNumBits, remainder);
  35. // Check value
  36. CHECK(value == p.mValue);
  37. // Continue with the remainder
  38. id = remainder;
  39. }
  40. CHECK(id.IsEmpty());
  41. }
  42. TEST_CASE("SubShapeIDTest")
  43. {
  44. // Test storing some values
  45. TestPushPop({ { 0b110101010, 9 }, { 0b0101010101, 10 }, { 0b10110101010, 11 } });
  46. // Test storing some values with a different pattern
  47. TestPushPop({ { 0b001010101, 9 }, { 0b1010101010, 10 }, { 0b01001010101, 11 } });
  48. // Test storing up to 32 bits
  49. TestPushPop({ { 0b10, 2 }, { 0b1110101010, 10 }, { 0b0101010101, 10 }, { 0b1010101010, 10 } });
  50. // Test storing up to 32 bits with a different pattern
  51. TestPushPop({ { 0b0001010101, 10 }, { 0b1010101010, 10 }, { 0b0101010101, 10 }, { 0b01, 2 } });
  52. // Test storing 0 bits
  53. TestPushPop({ { 0b10, 2 }, { 0b1110101010, 10 }, { 0, 0 }, { 0b0101010101, 10 }, { 0, 0 }, { 0b1010101010, 10 } });
  54. // Test 32 bits at once
  55. TestPushPop({ { 0b10101010101010101010101010101010, 32 } });
  56. // Test 32 bits at once with a different pattern
  57. TestPushPop({ { 0b01010101010101010101010101010101, 32 } });
  58. }
  59. }