algorithm.cpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include <array>
  2. #include <vector>
  3. #include <gtest/gtest.h>
  4. #include <entt/core/algorithm.hpp>
  5. #include "../common/boxed_type.h"
  6. TEST(Algorithm, StdSort) {
  7. // well, I'm pretty sure it works, it's std::sort!!
  8. std::array arr{4, 1, 3, 2, 0};
  9. const entt::std_sort sort;
  10. sort(arr.begin(), arr.end());
  11. for(auto it = arr.begin(), last = (arr.end() - 1u); it != last; ++it) {
  12. ASSERT_LT(*it, *(it + 1u));
  13. }
  14. }
  15. TEST(Algorithm, StdSortBoxedInt) {
  16. // well, I'm pretty sure it works, it's std::sort!!
  17. std::array arr{test::boxed_int{4}, test::boxed_int{1}, test::boxed_int{3}, test::boxed_int{2}, test::boxed_int{0}, test::boxed_int{8}};
  18. const entt::std_sort sort;
  19. sort(arr.begin(), arr.end(), [](const auto &lhs, const auto &rhs) {
  20. return lhs.value > rhs.value;
  21. });
  22. for(auto it = arr.begin(), last = (arr.end() - 1u); it != last; ++it) {
  23. ASSERT_GT(it->value, (it + 1u)->value);
  24. }
  25. }
  26. TEST(Algorithm, InsertionSort) {
  27. std::array arr{4, 1, 3, 2, 0};
  28. const entt::insertion_sort sort;
  29. sort(arr.begin(), arr.end());
  30. for(auto it = arr.begin(), last = (arr.end() - 1u); it != last; ++it) {
  31. ASSERT_LT(*it, *(it + 1u));
  32. }
  33. }
  34. TEST(Algorithm, InsertionSortBoxedInt) {
  35. std::array arr{test::boxed_int{4}, test::boxed_int{1}, test::boxed_int{3}, test::boxed_int{2}, test::boxed_int{0}, test::boxed_int{8}};
  36. const entt::insertion_sort sort;
  37. sort(arr.begin(), arr.end(), [](const auto &lhs, const auto &rhs) {
  38. return lhs.value > rhs.value;
  39. });
  40. for(auto it = arr.begin(), last = (arr.end() - 1u); it != last; ++it) {
  41. ASSERT_GT(it->value, (it + 1u)->value);
  42. }
  43. }
  44. TEST(Algorithm, InsertionSortEmptyContainer) {
  45. std::vector<int> vec{};
  46. const entt::insertion_sort sort;
  47. // this should crash with asan enabled if we break the constraint
  48. sort(vec.begin(), vec.end());
  49. }
  50. TEST(Algorithm, RadixSort) {
  51. std::array arr{4u, 1u, 3u, 2u, 0u};
  52. const entt::radix_sort<8, 32> sort;
  53. sort(arr.begin(), arr.end(), [](const auto &value) {
  54. return value;
  55. });
  56. for(auto it = arr.begin(), last = (arr.end() - 1u); it != last; ++it) {
  57. ASSERT_LT(*it, *(it + 1u));
  58. }
  59. }
  60. TEST(Algorithm, RadixSortBoxedInt) {
  61. std::array arr{test::boxed_int{4}, test::boxed_int{1}, test::boxed_int{3}, test::boxed_int{2}, test::boxed_int{0}, test::boxed_int{8}};
  62. const entt::radix_sort<2, 6> sort;
  63. sort(arr.rbegin(), arr.rend(), [](const auto &instance) {
  64. return instance.value;
  65. });
  66. for(auto it = arr.begin(), last = (arr.end() - 1u); it != last; ++it) {
  67. ASSERT_GT(it->value, (it + 1u)->value);
  68. }
  69. }
  70. TEST(Algorithm, RadixSortEmptyContainer) {
  71. std::vector<int> vec{};
  72. const entt::radix_sort<8, 32> sort;
  73. // this should crash with asan enabled if we break the constraint
  74. sort(vec.begin(), vec.end());
  75. }