20 #include <gtest/gtest.h> 34 const int SortedArrayInt[] = {-3, -2, 0, 2, 8, 15, 36, 212, 366};
36 const int RandomArrayInt[] = {4, 3, 5, 2, -18, 3, 2, 3, 4, 5, -5};
38 const std::string RandomStr =
"xacvgeze";
40 typedef std::vector<int> Container;
41 typedef Container::iterator IT;
42 typedef std::greater_equal<IT::value_type> GE_Comparator;
51 Container randomdArray(RandomArrayInt, RandomArrayInt +
sizeof(RandomArrayInt) /
sizeof(
int));
52 QuickSort<IT>(randomdArray.begin(), randomdArray.end());
55 for (
auto it = randomdArray.begin(); it < randomdArray.end() - 1; ++it)
56 EXPECT_LE(*it, *(it + 1));
61 Container sortedArray(SortedArrayInt, SortedArrayInt +
sizeof(SortedArrayInt) /
sizeof(
int));
62 QuickSort<IT>(sortedArray.begin(), sortedArray.end());
65 for (
auto it = sortedArray.begin(); it < sortedArray.end() - 1; ++it)
66 EXPECT_LE(*it, *(it + 1));
71 Container randomdArray(RandomArrayInt, RandomArrayInt +
sizeof(RandomArrayInt) /
sizeof(
int));
72 QuickSort<IT>(randomdArray.end(), randomdArray.begin());
75 for (
auto it = randomdArray.begin(); it < randomdArray.end(); ++it, ++i)
76 EXPECT_EQ(RandomArrayInt[i], *it);
82 QuickSort<IT>(emptyArray.begin(), emptyArray.end());
87 Container uniqueValueArray(1, 511);
88 QuickSort<IT>(uniqueValueArray.begin(), uniqueValueArray.end());
89 EXPECT_EQ(511, uniqueValueArray[0]);
94 std::string stringToSort = RandomStr;
95 QuickSort<std::string::iterator, std::less_equal<char>>(stringToSort.begin(), stringToSort.end());
96 for (
auto it = stringToSort.begin(); it < stringToSort.end() - 1; ++it)
97 EXPECT_LE(*it, *(it + 1));
102 TEST(TestSort, QuickSortGreaterComparator)
106 Container randomdArray(RandomArrayInt, RandomArrayInt +
sizeof(RandomArrayInt) /
sizeof(
int));
107 QuickSort<IT, GE_Comparator>(randomdArray.begin(), randomdArray.end());
110 for (
auto it = randomdArray.begin(); it < randomdArray.end() - 1; ++it)
111 EXPECT_GE(*it, *(it + 1));
116 Container invSortedArray(SortedArrayInt, SortedArrayInt +
sizeof(SortedArrayInt) /
sizeof(
int));
117 QuickSort<IT, GE_Comparator>(invSortedArray.begin(), invSortedArray.end());
120 for (
auto it = invSortedArray.begin(); it < invSortedArray.end() - 1; ++it)
121 EXPECT_GE(*it, *(it + 1));
126 std::string stringToSort = RandomStr;
127 QuickSort<std::string::iterator, std::greater_equal<char>>(stringToSort.begin(), stringToSort.end());
130 for (
auto it = stringToSort.begin(); it < stringToSort.end() - 1; ++it)
131 EXPECT_GE(*it, *(it + 1));
TEST(TestSort, QuickSorts)