20 #include <gtest/gtest.h> 33 typedef std::vector<int> Container;
34 typedef Container::iterator IT;
37 const Container ArraySort = {-3, -2, 0, 2, 8, 15, 36, 212, 366};
38 const Container ArraySortWithRot = {-3, 2, 7, 20, 0, 2, 8, 15, 36};
39 const Container ArraySortU = {0, 2, 8, 15, 36, 212, 366, 15478};
40 const Container ArrayRand = {4, 3, 5, 2, -18, 3, 2, 3, 4, 5, -5};
41 const Container ArrayRandU = {4520, 30, 500, 20, 3, 2, 3, 4, 5, 15};
43 const std::string StrRand =
"xacvgeze";
45 const std::string StrRandPivot =
"eknxasuw";
50 TEST(TestMerge, MergeInPlaces)
54 Container vector(ArraySortWithRot);
58 for (
auto it = vector.begin(); it < vector.end() - 1; ++it)
59 EXPECT_LE(*it, *(it + 1));
64 Container vector(ArraySortU);
68 for (
auto it = vector.begin(); it < vector.end() - 1; ++it)
69 EXPECT_LE(*it, *(it + 1));
74 Container vector(ArrayRandU);
78 for (
auto it = vector.begin(); it < vector.end(); ++it, ++i)
79 EXPECT_EQ(ArrayRandU[i], *it);
85 MergeInPlace<IT>()(emptyArray.begin(), emptyArray.begin(), emptyArray.end());
90 Container uniqueValueArray(1, 511);
91 MergeInPlace<IT>()(uniqueValueArray.begin(), uniqueValueArray.end(), uniqueValueArray.end());
92 EXPECT_EQ(511, uniqueValueArray[0]);
97 Container doubleValues(1, 511);
98 doubleValues.push_back(66);
100 MergeInPlace<IT>()(doubleValues.begin(), doubleValues.begin() + 1, doubleValues.end());
102 EXPECT_EQ(66, doubleValues[0]);
103 EXPECT_EQ(511, doubleValues[1]);
108 std::string str = StrRandPivot;
112 for (
auto it = str.begin(); it < str.end() - 1; ++it)
113 EXPECT_LE(*it, *(it + 1));
119 TEST(TestMerge, MergeWithBuffers)
123 Container vector(ArraySortWithRot);
127 for (
auto it = vector.begin(); it < vector.end() - 1; ++it)
128 EXPECT_LE(*it, *(it + 1));
133 Container vector(ArraySortU);
137 for (
auto it = vector.begin(); it < vector.end() - 1; ++it)
138 EXPECT_LE(*it, *(it + 1));
143 Container vector(ArrayRandU);
147 for (
auto it = vector.begin(); it < vector.end(); ++it, ++i)
148 EXPECT_EQ(ArrayRandU[i], *it);
153 Container emptyArray;
159 Container uniqueValueArray(1, 511);
160 MergeWithBuffer<IT>()(uniqueValueArray.begin(), uniqueValueArray.end(), uniqueValueArray.end());
161 EXPECT_EQ(511, uniqueValueArray[0]);
166 Container doubleValuesArray(1, 511);
167 doubleValuesArray.push_back(66);
169 MergeWithBuffer<IT>()(doubleValuesArray.begin(), doubleValuesArray.begin() + 1, doubleValuesArray.end());
171 EXPECT_EQ(66, doubleValuesArray[0]);
172 EXPECT_EQ(511, doubleValuesArray[1]);
177 std::string stringToMerge =
"eknxasuw";
179 (stringToMerge.begin(), stringToMerge.begin() + 4, stringToMerge.end());
182 for (std::string::iterator it = stringToMerge.begin(); it < stringToMerge.end() - 1; ++it)
183 EXPECT_LE(*it, *(it + 1));
192 Container vector(ArrayRand);
193 MergeSort<IT>(vector.begin(), vector.end());
196 for (
auto it = vector.begin(); it < vector.end() - 1; ++it)
197 EXPECT_LE(*it, *(it + 1));
202 Container vector(ArraySort);
203 MergeSort<IT>(vector.begin(), vector.end());
206 for (
auto it = vector.begin(); it < vector.end() - 1; ++it)
207 EXPECT_LE(*it, *(it + 1));
212 Container vector(ArrayRand);
213 MergeSort<IT>(vector.end(), vector.begin());
216 for (
auto it = vector.begin(); it < vector.end(); ++it, ++i)
217 EXPECT_EQ(ArrayRand[i], *it);
222 Container emptyArray;
223 MergeSort<IT>(emptyArray.begin(), emptyArray.end());
228 Container uniqueValueArray(1, 511);
229 MergeSort<IT>(uniqueValueArray.begin(), uniqueValueArray.end());
230 EXPECT_EQ(511, uniqueValueArray[0]);
235 std::string str = StrRand;
236 MergeSort<std::string::iterator, Aggregator_Str>(str.begin(), str.end());
239 for (
auto it = str.begin(); it < str.end() - 1; ++it)
240 EXPECT_LE(*it, *(it + 1));
TEST(TestMerge, MergeInPlaces)