20 #include <gtest/gtest.h> 34 const int SortedArrayInt[] = {-3, -2, 0, 2, 8, 15, 36, 212, 366};
36 const int InvSortedArrayInt[] = {366, 212, 36, 15, 8, 2, 0, -2, -3};
38 const int RandomArrayInt[] = {4, 3, 5, 2, -18, 3, 2, 3, 4, 5, -5};
40 const std::string RandomStr =
"xacvgeze";
42 typedef std::vector<int> Container;
43 typedef Container::iterator IT;
44 typedef std::greater_equal<IT::value_type> GE_Compare;
47 void CheckPartition (
const IT& begin,
const IT& end,
const IT& newPivot,
48 typename std::iterator_traits<IT>::value_type pivotVal,
bool inOrder =
true)
51 EXPECT_EQ(pivotVal, *newPivot);
55 for (
auto it = begin; it < newPivot; ++it)
56 EXPECT_GE(pivotVal, *it);
58 for (
auto it = begin; it < newPivot; ++it)
59 EXPECT_LE(pivotVal, *it);
63 for (
auto it = newPivot; it < end; ++it)
64 EXPECT_LE(pivotVal, *it);
66 for (
auto it = newPivot; it < end; ++it)
67 EXPECT_GE(pivotVal, *it);
73 TEST(TestPartition, Partitions)
77 Container randomdArray(RandomArrayInt, RandomArrayInt +
sizeof(RandomArrayInt) /
sizeof(
int));
78 auto pivot = randomdArray.begin() + 5;
79 auto pivotVal = *pivot;
82 auto newPivot = Partition<IT>(randomdArray.begin(), pivot, randomdArray.end());
83 CheckPartition<IT>(randomdArray.begin(), randomdArray.end(), newPivot, pivotVal);
88 Container sortedArray(SortedArrayInt, SortedArrayInt +
sizeof(SortedArrayInt) /
sizeof(
int));
89 auto pivot = sortedArray.begin() + 5;
91 Partition<IT>(sortedArray.begin(), pivot, sortedArray.end());
94 for (
auto it = sortedArray.begin(); it < sortedArray.end(); ++it, ++i)
95 EXPECT_EQ(SortedArrayInt[i], *it);
100 Container randomdArray(RandomArrayInt, RandomArrayInt +
sizeof(RandomArrayInt) /
sizeof(
int));
101 auto pivot = randomdArray.begin() + 5;
103 Partition<IT>(randomdArray.end(), pivot, randomdArray.begin());
106 for (
auto it = randomdArray.begin(); it < randomdArray.end(); ++it, ++i)
107 EXPECT_EQ(RandomArrayInt[i], *it);
112 TEST(TestPartition, PartitionString)
114 std::string randomStr = RandomStr;
115 auto pivot = randomStr.begin() + 5;
116 auto pivotVal = *pivot;
118 auto newPivot = Partition<std::string::iterator>(randomStr.begin(), pivot, randomStr.end());
119 CheckPartition<std::string::iterator>(randomStr.begin(), randomStr.end(), newPivot, pivotVal);
123 TEST(TestPartition, PartitionBoudaryPivots)
127 Container randomdArray(RandomArrayInt, RandomArrayInt +
sizeof(RandomArrayInt) /
sizeof(
int));
128 auto pivot = randomdArray.begin();
129 const int pivotVal = *pivot;
132 auto newPivot = Partition<IT>(randomdArray.begin(), pivot, randomdArray.end());
133 CheckPartition<IT>(randomdArray.begin(), randomdArray.end(), newPivot, pivotVal);
139 Container randomdArray(RandomArrayInt, RandomArrayInt +
sizeof(RandomArrayInt) /
sizeof(
int));
140 auto pivot = randomdArray.end() - 1;
141 const int pivotVal = *pivot;
144 auto newPivot = Partition<IT>(randomdArray.begin(), pivot, randomdArray.end());
145 CheckPartition<IT>(randomdArray.begin(), randomdArray.end(), newPivot, pivotVal);
150 Container randomdArray(RandomArrayInt, RandomArrayInt +
sizeof(RandomArrayInt) /
sizeof(
int));
151 auto pivot = randomdArray.end();
154 Partition<IT>(randomdArray.begin(), pivot, randomdArray.end());
157 for (
auto it = randomdArray.begin(); it < randomdArray.end(); ++it, ++i)
158 EXPECT_EQ(RandomArrayInt[i], *it);
163 TEST(TestPartition, PartitionGreaterComparator)
167 Container randomdArray(RandomArrayInt, RandomArrayInt +
sizeof(RandomArrayInt) /
sizeof(
int));
168 auto pivot = randomdArray.begin() + 5;
169 const auto pivotVal = *pivot;
172 auto newPivot = Partition<IT, GE_Compare>(randomdArray.begin(), pivot, randomdArray.end());
173 CheckPartition<IT>(randomdArray.begin(), randomdArray.end(), newPivot, pivotVal,
false);
178 Container invSortedArray(InvSortedArrayInt, InvSortedArrayInt +
sizeof(InvSortedArrayInt) /
sizeof(
int));
179 auto pivot = invSortedArray.begin() + 5;
181 Partition<IT, GE_Compare>(invSortedArray.begin(), pivot, invSortedArray.end());
184 for (
auto it = invSortedArray.begin(); it < invSortedArray.end(); ++it, ++i)
185 EXPECT_EQ(invSortedArray[i], *it);
190 std::string randomStr = RandomStr;
191 auto pivot = randomStr.begin() + 5;
192 const auto pivotVal = *pivot;
195 auto newPivot = Partition<std::string::iterator, std::greater_equal<char>>(randomStr.begin(), pivot, randomStr.end());
196 CheckPartition<std::string::iterator>(randomStr.begin(), randomStr.end(), newPivot, pivotVal,
false);
TEST(TestPartition, Partitions)