InsertionSortTest.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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/Core/InsertionSort.h>
  6. TEST_SUITE("InsertionSortTest")
  7. {
  8. TEST_CASE("TestOrderedArray")
  9. {
  10. Array<int> array;
  11. for (int i = 0; i < 10; i++)
  12. array.push_back(i);
  13. InsertionSort(array.begin(), array.end());
  14. for (int i = 0; i < 10; i++)
  15. CHECK(array[i] == i);
  16. }
  17. TEST_CASE("TestOrderedArrayComparator")
  18. {
  19. Array<int> array;
  20. for (int i = 0; i < 100; i++)
  21. array.push_back(i);
  22. InsertionSort(array.begin(), array.end(), greater<int> {});
  23. for (int i = 0; i < 100; i++)
  24. CHECK(array[i] == 99 - i);
  25. }
  26. TEST_CASE("TestReversedArray")
  27. {
  28. Array<int> array;
  29. for (int i = 0; i < 10; i++)
  30. array.push_back(9 - i);
  31. InsertionSort(array.begin(), array.end());
  32. for (int i = 0; i < 10; i++)
  33. CHECK(array[i] == i);
  34. }
  35. TEST_CASE("TestRandomArray")
  36. {
  37. UnitTestRandom random;
  38. Array<UnitTestRandom::result_type> array;
  39. for (int i = 0; i < 100; i++)
  40. {
  41. UnitTestRandom::result_type value = random();
  42. // Insert value at beginning
  43. array.insert(array.begin(), value);
  44. // Insert value at end
  45. array.push_back(value);
  46. }
  47. InsertionSort(array.begin(), array.end());
  48. for (Array<int>::size_type i = 0; i < array.size() - 2; i += 2)
  49. {
  50. // We inserted the same value twice so these elements should be the same
  51. CHECK(array[i] == array[i + 1]);
  52. // The next element should be bigger or equal
  53. CHECK(array[i] <= array[i + 2]);
  54. }
  55. }
  56. TEST_CASE("TestEmptyArray")
  57. {
  58. Array<int> array;
  59. InsertionSort(array.begin(), array.end());
  60. CHECK(array.empty());
  61. }
  62. TEST_CASE("Test1ElementArray")
  63. {
  64. Array<int> array { 1 };
  65. InsertionSort(array.begin(), array.end());
  66. CHECK(array[0] == 1);
  67. }
  68. TEST_CASE("Test2ElementArray")
  69. {
  70. Array<int> array { 2, 1 };
  71. InsertionSort(array.begin(), array.end());
  72. CHECK(array[0] == 1);
  73. CHECK(array[1] == 2);
  74. }
  75. }