20 #include <gtest/gtest.h> 33 typedef std::vector<int> Container;
34 typedef Container::iterator IT;
35 typedef std::greater_equal<IT::value_type> GE_Compare;
37 const Container ArraySort = {-3, -2, 0, 2, 8, 15, 36, 212, 366};
38 const Container ArrayInvSort = {366, 212, 36, 15, 8, 2, 0, -2, -3};
39 const Container ArrayRand = {4, 3, 5, 2, -18, 3, 2, 3, 4, 5, -5};
40 const std::string StrRand =
"xacvgeze";
45 void CheckPartition (
const IT& begin,
const IT& end,
const IT& newPivot,
46 typename std::iterator_traits<IT>::value_type pivotVal,
bool inOrder =
true)
49 EXPECT_EQ(pivotVal, *newPivot);
53 for (
auto it = begin; it < newPivot; ++it)
54 EXPECT_GE(pivotVal, *it);
56 for (
auto it = begin; it < newPivot; ++it)
57 EXPECT_LE(pivotVal, *it);
61 for (
auto it = newPivot; it < end; ++it)
62 EXPECT_LE(pivotVal, *it);
64 for (
auto it = newPivot; it < end; ++it)
65 EXPECT_GE(pivotVal, *it);
71 TEST(TestPartition, Partitions)
75 Container vector(ArrayRand);
76 auto pivot = vector.begin() + 5;
77 auto pivotVal = *pivot;
80 auto newPivot = Partition<IT>(vector.begin(), pivot, vector.end());
81 CheckPartition<IT>(vector.begin(), vector.end(), newPivot, pivotVal);
86 Container vector(ArraySort);
87 auto pivot = vector.begin() + 5;
89 Partition<IT>(vector.begin(), pivot, vector.end());
92 for (
auto it = vector.begin(); it < vector.end(); ++it, ++i)
93 EXPECT_EQ(ArraySort[i], *it);
98 Container vector(ArrayRand);
99 auto pivot = vector.begin() + 5;
101 Partition<IT>(vector.end(), pivot, vector.begin());
104 for (
auto it = vector.begin(); it < vector.end(); ++it, ++i)
105 EXPECT_EQ(ArrayRand[i], *it);
110 TEST(TestPartition, PartitionString)
112 std::string str = StrRand;
113 auto pivot = str.begin() + 5;
114 auto pivotVal = *pivot;
116 auto newPivot = Partition<std::string::iterator>(str.begin(), pivot, str.end());
117 CheckPartition<std::string::iterator>(str.begin(), str.end(), newPivot, pivotVal);
121 TEST(TestPartition, PartitionBoudaryPivots)
125 Container vector(ArrayRand);
126 auto pivot = vector.begin();
127 const int pivotVal = *pivot;
130 auto newPivot = Partition<IT>(vector.begin(), pivot, vector.end());
131 CheckPartition<IT>(vector.begin(), vector.end(), newPivot, pivotVal);
137 Container vector(ArrayRand);
138 auto pivot = vector.end() - 1;
139 const int pivotVal = *pivot;
142 auto newPivot = Partition<IT>(vector.begin(), pivot, vector.end());
143 CheckPartition<IT>(vector.begin(), vector.end(), newPivot, pivotVal);
148 Container vector(ArrayRand);
149 auto pivot = vector.end();
152 Partition<IT>(vector.begin(), pivot, vector.end());
155 for (
auto it = vector.begin(); it < vector.end(); ++it, ++i)
156 EXPECT_EQ(ArrayRand[i], *it);
161 TEST(TestPartition, PartitionGreaterComparator)
165 Container vector(ArrayRand);
166 auto pivot = vector.begin() + 5;
167 const auto pivotVal = *pivot;
170 auto newPivot = Partition<IT, GE_Compare>(vector.begin(), pivot, vector.end());
171 CheckPartition<IT>(vector.begin(), vector.end(), newPivot, pivotVal,
false);
176 Container invSortedArray(ArrayInvSort);
177 auto pivot = invSortedArray.begin() + 5;
179 Partition<IT, GE_Compare>(invSortedArray.begin(), pivot, invSortedArray.end());
182 for (
auto it = invSortedArray.begin(); it < invSortedArray.end(); ++it, ++i)
183 EXPECT_EQ(invSortedArray[i], *it);
188 std::string str = StrRand;
189 auto pivot = str.begin() + 5;
190 const auto pivotVal = *pivot;
194 Partition<std::string::iterator, std::greater_equal<char>>(str.begin(), pivot, str.end());
195 CheckPartition<std::string::iterator>(str.begin(), str.end(), newPivot, pivotVal,
false);
TEST(TestPartition, Partitions)