BinaryHeapTest.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
  2. // SPDX-FileCopyrightText: 2024 Jorrit Rouwe
  3. // SPDX-License-Identifier: MIT
  4. #include "UnitTestFramework.h"
  5. #include <Jolt/Core/BinaryHeap.h>
  6. TEST_SUITE("BinaryHeapTest")
  7. {
  8. TEST_CASE("TestBinaryHeap")
  9. {
  10. // Add some numbers
  11. Array<int> array;
  12. array.reserve(1100);
  13. for (int i = 0; i < 1000; ++i)
  14. array.push_back(i);
  15. // Ensure we have some duplicates
  16. for (int i = 0; i < 1000; i += 10)
  17. array.push_back(i);
  18. // Shuffle the array
  19. UnitTestRandom random(123);
  20. std::shuffle(array.begin(), array.end(), random);
  21. // Add the numbers to the heap
  22. Array<int> heap;
  23. for (int i : array)
  24. {
  25. heap.push_back(i);
  26. BinaryHeapPush(heap.begin(), heap.end(), std::less<int> { });
  27. }
  28. // Check that the heap is sorted
  29. int last = INT_MAX;
  30. int seen[1000] { 0 };
  31. while (!heap.empty())
  32. {
  33. BinaryHeapPop(heap.begin(), heap.end(), std::less<int> { });
  34. int current = heap.back();
  35. CHECK(++seen[current] <= (current % 10 == 0? 2 : 1));
  36. heap.pop_back();
  37. CHECK(current <= last);
  38. last = current;
  39. }
  40. }
  41. }