TestCocktail.cxx
Go to the documentation of this file.
1 /*===========================================================================================================
2  *
3  * HUC - Hurna Core
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/Hurna/Hurna-Core/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 <cocktail.hxx>
22 
23 // STD includes
24 #include <functional>
25 #include <vector>
26 #include <string>
27 
28 // Testing namespace
29 using namespace huc::sort;
30 
31 #ifndef DOXYGEN_SKIP
32 namespace {
33  typedef std::vector<int> Container;
34  typedef Container::iterator IT;
35  typedef std::greater<IT::value_type> GE_Comparator;
36 
37  const Container ArraySort = {-3, -2, 0, 2, 8, 15, 36, 212, 366}; // sorted with negative values
38  const Container ArrayRand = {4, 3, 5, 2, -18, 3, 2, 3, 4, 5, -5}; // random with negative values
39  const std::string StrRand = "xacvgeze";
40 }
41 #endif /* DOXYGEN_SKIP */
42 
43 // Basic Cocktail-Sort tests
44 TEST(TestCocktail, CocktailSorts)
45 {
46  // Normal Run
47  {
48  Container randomdArray(ArrayRand);
49  Cocktail<IT>(randomdArray.begin(), randomdArray.end());
50 
51  // All elements are sorted
52  for (auto it = randomdArray.begin(); it < randomdArray.end() - 1; ++it)
53  EXPECT_LE(*it, *(it + 1));
54  }
55 
56  // Already ArraySort - Array should not be affected
57  {
58  Container ArraySort(ArraySort);
59  Cocktail<IT>(ArraySort.begin(), ArraySort.end());
60 
61  // All elements are still sorted
62  for (auto it = ArraySort.begin(); it < ArraySort.end() - 1; ++it)
63  EXPECT_LE(*it, *(it + 1));
64  }
65 
66  // Inverse iterator order - Array should not be affected
67  {
68  Container randomdArray(ArrayRand);
69  Cocktail<IT>(randomdArray.end(), randomdArray.begin());
70 
71  int i = 0;
72  for (auto it = randomdArray.begin(); it < randomdArray.end(); ++it, ++i)
73  EXPECT_EQ(ArrayRand[i], *it);
74  }
75 
76  // No error unitialized array
77  {
78  Container emptyArray;
79  Cocktail<IT>(emptyArray.begin(), emptyArray.end());
80  }
81 
82  // Unique value array - Array should not be affected
83  {
84  Container uniqueValueArray(1, 511);
85  Cocktail<IT>(uniqueValueArray.begin(), uniqueValueArray.end());
86  EXPECT_EQ(511, uniqueValueArray[0]);
87  }
88 
89  // String - String should be sorted as an array
90  {
91  std::string stringToSort = StrRand;
92  Cocktail<std::string::iterator, std::less<char>>(stringToSort.begin(), stringToSort.end());
93  for (auto it = stringToSort.begin(); it < stringToSort.end() - 1; ++it)
94  EXPECT_LE(*it, *(it + 1));
95  }
96 }
97 
98 // Basic Cocktail-Sort tests - Inverse Order
99 TEST(TestCocktail, CocktailGreaterComparator)
100 {
101  // Normal Run - Elements should be sorted in inverse order
102  {
103  Container randomdArray(ArrayRand);
104  Cocktail<IT, GE_Comparator>(randomdArray.begin(), randomdArray.end());
105 
106  // All elements are sorted in inverse order
107  for (auto it = randomdArray.begin(); it < randomdArray.end() - 1; ++it)
108  EXPECT_GE(*it, *(it + 1));
109  }
110 
111  // Already sorted Array in inverse order - Array should not be affected
112  {
113  Container invArraySort(ArraySort);
114  Cocktail<IT, GE_Comparator>(invArraySort.begin(), invArraySort.end());
115 
116  // All elements are still sorted in inverse order
117  for (auto it = invArraySort.begin(); it < invArraySort.end() - 1; ++it)
118  EXPECT_GE(*it, *(it + 1));
119  }
120 
121  // String - String should be sorted in inverse order
122  {
123  std::string stringToSort = StrRand;
124  Cocktail<std::string::iterator, std::greater<char>>(stringToSort.begin(), stringToSort.end());
125 
126  // All elements are sorted in inverse order
127  for (auto it = stringToSort.begin(); it < stringToSort.end() - 1; ++it)
128  EXPECT_GE(*it, *(it + 1));
129  }
130 }
TEST(TestCocktail, CocktailSorts)