TestMerge.cxx File Reference
#include <gtest/gtest.h>
#include <merge.hxx>
#include <functional>
#include <vector>
#include <string>
Include dependency graph for TestMerge.cxx:

Go to the source code of this file.

Functions

 TEST (TestMerge, MergeInPlaces)
 
 TEST (TestMerge, MergeWithBuffers)
 
 TEST (TestMerge, MergeSorts)
 

Function Documentation

TEST ( TestMerge  ,
MergeInPlaces   
)

Definition at line 55 of file TestMerge.cxx.

56 {
57  // Normal Run - All elements should be sorted in order
58  {
59  Container sortedArrayWithRot
60  (SortedArrayIntWithRot, SortedArrayIntWithRot + sizeof(SortedArrayIntWithRot) / sizeof(int));
62  (sortedArrayWithRot.begin(), sortedArrayWithRot.begin() + 4, sortedArrayWithRot.end());
63 
64  // All elements of the final array are sorted
65  for (auto it = sortedArrayWithRot.begin(); it < sortedArrayWithRot.end() - 1; ++it)
66  EXPECT_LE(*it, *(it + 1));
67  }
68 
69  // Already sortedArray - Array should not be affected
70  {
71  Container sortedArrayPos
72  (SortedArrayIntPos, SortedArrayIntPos + sizeof(SortedArrayIntPos) / sizeof(int));
73  MergeInPlace<IT>()(sortedArrayPos.begin(), sortedArrayPos.begin() + 5, sortedArrayPos.end());
74 
75  // All elements are still sorted
76  for (auto it = sortedArrayPos.begin(); it < sortedArrayPos.end() - 1; ++it)
77  EXPECT_LE(*it, *(it + 1));
78  }
79 
80  // Inverse iterator order - Array should not be affected
81  {
82  Container randomArrayPos
83  (RandomArrayIntPos, RandomArrayIntPos + sizeof(RandomArrayIntPos) / sizeof(int));
84  MergeInPlace<IT>()(randomArrayPos.end(), randomArrayPos.begin() + 3, randomArrayPos.begin());
85 
86  int i = 0;
87  for (auto it = randomArrayPos.begin(); it < randomArrayPos.end(); ++it, ++i)
88  EXPECT_EQ(RandomArrayIntPos[i], *it);
89  }
90 
91  // No error empty array
92  {
93  Container emptyArray;
94  MergeInPlace<IT>()(emptyArray.begin(), emptyArray.begin(), emptyArray.end());
95  }
96 
97  // Unique value array - Array should not be affected
98  {
99  Container uniqueValueArray(1, 511);
100  MergeInPlace<IT>()(uniqueValueArray.begin(), uniqueValueArray.end(), uniqueValueArray.end());
101  EXPECT_EQ(511, uniqueValueArray[0]);
102  }
103 
104  // Double values array - Order should be made
105  {
106  Container doubleValuesArray(1, 511);
107  doubleValuesArray.push_back(66);
108 
110  (doubleValuesArray.begin(), doubleValuesArray.begin() + 1, doubleValuesArray.end());
111 
112  EXPECT_EQ(66, doubleValuesArray[0]);
113  EXPECT_EQ(511, doubleValuesArray[1]);
114  }
115 
116  // String Collection - All elements should be sorted in order
117  {
118  std::string stringToMerge = StringWithPivot;
120  (stringToMerge.begin(), stringToMerge.begin() + 4, stringToMerge.end());
121 
122  // All elements of the final array are sorted
123  for (auto it = stringToMerge.begin(); it < stringToMerge.end() - 1; ++it)
124  EXPECT_LE(*it, *(it + 1));
125  }
126 }
TEST ( TestMerge  ,
MergeWithBuffers   
)

Definition at line 130 of file TestMerge.cxx.

131 {
132  // Normal Run - All elements should be sorted in order
133  {
134  Container sortedArrayWithRot
135  (SortedArrayIntWithRot, SortedArrayIntWithRot + sizeof(SortedArrayIntWithRot) / sizeof(int));
137  (sortedArrayWithRot.begin(), sortedArrayWithRot.begin() + 4, sortedArrayWithRot.end());
138 
139  // All elements of the final array are sorted
140  for (auto it = sortedArrayWithRot.begin(); it < sortedArrayWithRot.end() - 1; ++it)
141  EXPECT_LE(*it, *(it + 1));
142  }
143 
144  // Already sortedArray - Array should not be affected
145  {
146  Container sortedArrayPos
147  (SortedArrayIntPos, SortedArrayIntPos + sizeof(SortedArrayIntPos) / sizeof(int));
149  (sortedArrayPos.begin(), sortedArrayPos.begin() + 5, sortedArrayPos.end());
150 
151  // All elements are still sorted
152  for (auto it = sortedArrayPos.begin(); it < sortedArrayPos.end() - 1; ++it)
153  EXPECT_LE(*it, *(it + 1));
154  }
155 
156  // Inverse iterator order - Array should not be affected
157  {
158  Container randomArrayPos
159  (RandomArrayIntPos, RandomArrayIntPos + sizeof(RandomArrayIntPos) / sizeof(int));
161  (randomArrayPos.end(), randomArrayPos.begin() + 3, randomArrayPos.begin());
162 
163  int i = 0;
164  for (auto it = randomArrayPos.begin(); it < randomArrayPos.end(); ++it, ++i)
165  EXPECT_EQ(RandomArrayIntPos[i], *it);
166  }
167 
168  // No error empty array
169  {
170  Container emptyArray;
171  MergeWithBuffer<Container, IT>()(emptyArray.begin(), emptyArray.begin(), emptyArray.end());
172  }
173 
174  // Unique value array - Array should not be affected
175  {
176  Container uniqueValueArray(1, 511);
178  (uniqueValueArray.begin(), uniqueValueArray.end(), uniqueValueArray.end());
179  EXPECT_EQ(511, uniqueValueArray[0]);
180  }
181 
182  // Double values array - Order should be made
183  {
184  Container doubleValuesArray(1, 511);
185  doubleValuesArray.push_back(66);
186 
188  (doubleValuesArray.begin(), doubleValuesArray.begin() + 1, doubleValuesArray.end());
189 
190  EXPECT_EQ(66, doubleValuesArray[0]);
191  EXPECT_EQ(511, doubleValuesArray[1]);
192  }
193 
194  // String Collection - All elements should be sorted in order
195  {
196  std::string stringToMerge = "eknxasuw"; // Left sorted part [e,k,n,x] - Right sorted part [a,s,u,w]
198  (stringToMerge.begin(), stringToMerge.begin() + 4, stringToMerge.end());
199 
200  // All elements of the final array are sorted
201  for (std::string::iterator it = stringToMerge.begin(); it < stringToMerge.end() - 1; ++it)
202  EXPECT_LE(*it, *(it + 1));
203  }
204 }
TEST ( TestMerge  ,
MergeSorts   
)

Definition at line 207 of file TestMerge.cxx.

208 {
209  // Normal Run - all elements should be sorter in order
210  {
211  Container randomdArray(RandomArrayInt, RandomArrayInt + sizeof(RandomArrayInt) / sizeof(int));
212  MergeSort<Container, IT>(randomdArray.begin(), randomdArray.end());
213 
214  // All elements are sorted
215  for (auto it = randomdArray.begin(); it < randomdArray.end() - 1; ++it)
216  EXPECT_LE(*it, *(it + 1));
217  }
218 
219  // Already sortedArray - Array should not be affected
220  {
221  Container sortedArray(SortedArrayInt, SortedArrayInt + sizeof(SortedArrayInt) / sizeof(int));
222  MergeSort<Container, IT>(sortedArray.begin(), sortedArray.end());
223 
224  // All elements are still sorted
225  for (auto it = sortedArray.begin(); it < sortedArray.end() - 1; ++it)
226  EXPECT_LE(*it, *(it + 1));
227  }
228 
229  // Inverse iterator order - Array should not be affected
230  {
231  Container randomdArray(RandomArrayInt, RandomArrayInt + sizeof(RandomArrayInt) / sizeof(int));
232  MergeSort<Container, IT>(randomdArray.end(), randomdArray.begin());
233 
234  int i = 0;
235  for (auto it = randomdArray.begin(); it < randomdArray.end(); ++it, ++i)
236  EXPECT_EQ(RandomArrayInt[i], *it);
237  }
238 
239  // No error empty array
240  {
241  Container emptyArray;
242  MergeSort<Container, IT>(emptyArray.begin(), emptyArray.end());
243  }
244 
245  // Unique value array - Array should not be affected
246  {
247  Container uniqueValueArray(1, 511);
248  MergeSort<Container, IT>(uniqueValueArray.begin(), uniqueValueArray.end());
249  EXPECT_EQ(511, uniqueValueArray[0]);
250  }
251 
252  // String collection - all elements should be sorter in order
253  {
254  std::string randomStr = RandomStr;
255  MergeSort<std::string, std::string::iterator, Aggregator_Str>(randomStr.begin(), randomStr.end());
256 
257  // All elements are sorted
258  for (auto it = randomStr.begin(); it < randomStr.end() - 1; ++it)
259  EXPECT_LE(*it, *(it + 1));
260  }
261 }