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

Go to the source code of this file.

Functions

 TEST (TestPartition, Partitions)
 
 TEST (TestPartition, PartitionString)
 
 TEST (TestPartition, PartitionBoudaryPivots)
 
 TEST (TestPartition, PartitionGreaterComparator)
 

Function Documentation

TEST ( TestPartition  ,
Partitions   
)

Definition at line 73 of file TestPartition.cxx.

74 {
75  // Normal Run - Random Array
76  {
77  Container randomdArray(RandomArrayInt, RandomArrayInt + sizeof(RandomArrayInt) / sizeof(int));
78  auto pivot = randomdArray.begin() + 5;
79  auto pivotVal = *pivot;
80 
81  // Run partition - Should result in: max[begin, pivot[ <= pivot <= min]pivot, end]
82  auto newPivot = Partition<IT>(randomdArray.begin(), pivot, randomdArray.end());
83  CheckPartition<IT>(randomdArray.begin(), randomdArray.end(), newPivot, pivotVal);
84  }
85 
86  // Already sortedArray - Array should not be affected
87  {
88  Container sortedArray(SortedArrayInt, SortedArrayInt + sizeof(SortedArrayInt) / sizeof(int));
89  auto pivot = sortedArray.begin() + 5;
90 
91  Partition<IT>(sortedArray.begin(), pivot, sortedArray.end());
92 
93  int i = 0;
94  for (auto it = sortedArray.begin(); it < sortedArray.end(); ++it, ++i)
95  EXPECT_EQ(SortedArrayInt[i], *it);
96  }
97 
98  // Begin and End inversed - Array should not be affected
99  {
100  Container randomdArray(RandomArrayInt, RandomArrayInt + sizeof(RandomArrayInt) / sizeof(int));
101  auto pivot = randomdArray.begin() + 5;
102 
103  Partition<IT>(randomdArray.end(), pivot, randomdArray.begin());
104 
105  int i = 0;
106  for (auto it = randomdArray.begin(); it < randomdArray.end(); ++it, ++i)
107  EXPECT_EQ(RandomArrayInt[i], *it);
108  }
109 }
TEST ( TestPartition  ,
PartitionString   
)

Definition at line 112 of file TestPartition.cxx.

113 {
114  std::string randomStr = RandomStr;
115  auto pivot = randomStr.begin() + 5;
116  auto pivotVal = *pivot;
117 
118  auto newPivot = Partition<std::string::iterator>(randomStr.begin(), pivot, randomStr.end());
119  CheckPartition<std::string::iterator>(randomStr.begin(), randomStr.end(), newPivot, pivotVal);
120 }
TEST ( TestPartition  ,
PartitionBoudaryPivots   
)

Definition at line 123 of file TestPartition.cxx.

124 {
125  // Pivot choose as begin
126  {
127  Container randomdArray(RandomArrayInt, RandomArrayInt + sizeof(RandomArrayInt) / sizeof(int));
128  auto pivot = randomdArray.begin();
129  const int pivotVal = *pivot;
130 
131  // Run partition
132  auto newPivot = Partition<IT>(randomdArray.begin(), pivot, randomdArray.end());
133  CheckPartition<IT>(randomdArray.begin(), randomdArray.end(), newPivot, pivotVal);
134 
135  }
136 
137  // Pivot choose as last element
138  {
139  Container randomdArray(RandomArrayInt, RandomArrayInt + sizeof(RandomArrayInt) / sizeof(int));
140  auto pivot = randomdArray.end() - 1;
141  const int pivotVal = *pivot;
142 
143  // Run partition
144  auto newPivot = Partition<IT>(randomdArray.begin(), pivot, randomdArray.end());
145  CheckPartition<IT>(randomdArray.begin(), randomdArray.end(), newPivot, pivotVal);
146  }
147 
148  // Pivot choose as end - cannot process
149  {
150  Container randomdArray(RandomArrayInt, RandomArrayInt + sizeof(RandomArrayInt) / sizeof(int));
151  auto pivot = randomdArray.end();
152 
153  // Run partition
154  Partition<IT>(randomdArray.begin(), pivot, randomdArray.end());
155 
156  int i = 0;
157  for (auto it = randomdArray.begin(); it < randomdArray.end(); ++it, ++i)
158  EXPECT_EQ(RandomArrayInt[i], *it);
159  }
160 }
TEST ( TestPartition  ,
PartitionGreaterComparator   
)

Definition at line 163 of file TestPartition.cxx.

164 {
165  // Normal Run - Should result in: min[begin, pivot[ >= pivot >= max]pivot, end]
166  {
167  Container randomdArray(RandomArrayInt, RandomArrayInt + sizeof(RandomArrayInt) / sizeof(int));
168  auto pivot = randomdArray.begin() + 5;
169  const auto pivotVal = *pivot;
170 
171  // Run partition
172  auto newPivot = Partition<IT, GE_Compare>(randomdArray.begin(), pivot, randomdArray.end());
173  CheckPartition<IT>(randomdArray.begin(), randomdArray.end(), newPivot, pivotVal, false);
174  }
175 
176  // Already InverseSortedArray - Array should not be affected
177  {
178  Container invSortedArray(InvSortedArrayInt, InvSortedArrayInt + sizeof(InvSortedArrayInt) / sizeof(int));
179  auto pivot = invSortedArray.begin() + 5;
180 
181  Partition<IT, GE_Compare>(invSortedArray.begin(), pivot, invSortedArray.end());
182 
183  int i = 0;
184  for (auto it = invSortedArray.begin(); it < invSortedArray.end(); ++it, ++i)
185  EXPECT_EQ(invSortedArray[i], *it);
186  }
187 
188  // String collection - Should result in: min[begin, pivot[ >= pivot >= max]pivot, end]
189  {
190  std::string randomStr = RandomStr;
191  auto pivot = randomStr.begin() + 5;
192  const auto pivotVal = *pivot;
193 
194  // Run partition
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);
197  }
198 }