SubShapeIDTest.cpp 2.0 KB

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