TestQuick.cxx
Go to the documentation of this file.
1 /*===========================================================================================================
2  *
3  * SHA - Simple Hybesis Algorithms
4  *
5  * Copyright (c) Michael Jeulin-Lagarrigue
6  *
7  * Licensed under the MIT License, you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * https://github.com/michael-jeulinl/Simple-Hybesis-Algorithms/blob/master/LICENSE
11  *
12  * Unless required by applicable law or agreed to in writing, software distributed under the License is
13  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and limitations under the License.
15  *
16  * The above copyright notice and this permission notice shall be included in all copies or
17  * substantial portions of the Software.
18  *
19  *=========================================================================================================*/
20 #include <gtest/gtest.h>
21 #include <quick.hxx>
22 
23 // STD includes
24 #include <functional>
25 #include <vector>
26 #include <string>
27 
28 // Testing namespace
29 using namespace SHA_Sort;
30 
31 #ifndef DOXYGEN_SKIP
32 namespace {
33  // Simple sorted array of integers with negative values
34  const int SortedArrayInt[] = {-3, -2, 0, 2, 8, 15, 36, 212, 366};
35  // Simple random array of integers with negative values
36  const int RandomArrayInt[] = {4, 3, 5, 2, -18, 3, 2, 3, 4, 5, -5};
37  // Random string
38  const std::string RandomStr = "xacvgeze";
39 
40  typedef std::vector<int> Container;
41  typedef Container::iterator IT;
42  typedef std::greater_equal<IT::value_type> GE_Comparator;
43 }
44 #endif /* DOXYGEN_SKIP */
45 
46 // Basic Quick-Sort tests
47 TEST(TestSort, QuickSorts)
48 {
49  // Normal Run
50  {
51  Container randomdArray(RandomArrayInt, RandomArrayInt + sizeof(RandomArrayInt) / sizeof(int));
52  QuickSort<IT>(randomdArray.begin(), randomdArray.end());
53 
54  // All elements are sorted
55  for (auto it = randomdArray.begin(); it < randomdArray.end() - 1; ++it)
56  EXPECT_LE(*it, *(it + 1));
57  }
58 
59  // Already sortedArray - Array should not be affected
60  {
61  Container sortedArray(SortedArrayInt, SortedArrayInt + sizeof(SortedArrayInt) / sizeof(int));
62  QuickSort<IT>(sortedArray.begin(), sortedArray.end());
63 
64  // All elements are still sorted
65  for (auto it = sortedArray.begin(); it < sortedArray.end() - 1; ++it)
66  EXPECT_LE(*it, *(it + 1));
67  }
68 
69  // Inverse iterator order - Array should not be affected
70  {
71  Container randomdArray(RandomArrayInt, RandomArrayInt + sizeof(RandomArrayInt) / sizeof(int));
72  QuickSort<IT>(randomdArray.end(), randomdArray.begin());
73 
74  int i = 0;
75  for (auto it = randomdArray.begin(); it < randomdArray.end(); ++it, ++i)
76  EXPECT_EQ(RandomArrayInt[i], *it);
77  }
78 
79  // No error unitialized array
80  {
81  Container emptyArray;
82  QuickSort<IT>(emptyArray.begin(), emptyArray.end());
83  }
84 
85  // Unique value array - Array should not be affected
86  {
87  Container uniqueValueArray(1, 511);
88  QuickSort<IT>(uniqueValueArray.begin(), uniqueValueArray.end());
89  EXPECT_EQ(511, uniqueValueArray[0]);
90  }
91 
92  // String - String should be sorted as an array
93  {
94  std::string stringToSort = RandomStr;
95  QuickSort<std::string::iterator, std::less_equal<char>>(stringToSort.begin(), stringToSort.end());
96  for (auto it = stringToSort.begin(); it < stringToSort.end() - 1; ++it)
97  EXPECT_LE(*it, *(it + 1));
98  }
99 }
100 
101 // Basic Quick-Sort tests - Inverse Order
102 TEST(TestSort, QuickSortGreaterComparator)
103 {
104  // Normal Run - Elements should be sorted in inverse order
105  {
106  Container randomdArray(RandomArrayInt, RandomArrayInt + sizeof(RandomArrayInt) / sizeof(int));
107  QuickSort<IT, GE_Comparator>(randomdArray.begin(), randomdArray.end());
108 
109  // All elements are sorted in inverse order
110  for (auto it = randomdArray.begin(); it < randomdArray.end() - 1; ++it)
111  EXPECT_GE(*it, *(it + 1));
112  }
113 
114  // Already sorted Array in inverse order - Array should not be affected
115  {
116  Container invSortedArray(SortedArrayInt, SortedArrayInt + sizeof(SortedArrayInt) / sizeof(int));
117  QuickSort<IT, GE_Comparator>(invSortedArray.begin(), invSortedArray.end());
118 
119  // All elements are still sorted in inverse order
120  for (auto it = invSortedArray.begin(); it < invSortedArray.end() - 1; ++it)
121  EXPECT_GE(*it, *(it + 1));
122  }
123 
124  // String - String should be sorted in inverse order
125  {
126  std::string stringToSort = RandomStr;
127  QuickSort<std::string::iterator, std::greater_equal<char>>(stringToSort.begin(), stringToSort.end());
128 
129  // All elements are sorted in inverse order
130  for (auto it = stringToSort.begin(); it < stringToSort.end() - 1; ++it)
131  EXPECT_GE(*it, *(it + 1));
132  }
133 }
TEST(TestSort, QuickSorts)
Definition: TestQuick.cxx:47