TestBubble.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 <bubble.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 RandStr = "xacvgeze";
40 }
41 #endif /* DOXYGEN_SKIP */
42 
43 // Basic Bubble-Sort tests
44 TEST(TestBubble, BubbleSorts)
45 {
46  // Normal Run
47  {
48  Container vector(ArrayRand);
49  Bubble<IT>(vector.begin(), vector.end());
50 
51  // All elements are sorted
52  for (auto it = vector.begin(); it < vector.end() - 1; ++it)
53  EXPECT_LE(*it, *(it + 1));
54  }
55 
56  // Already ArraySort - Array should not be affected
57  {
58  Container vector(ArraySort);
59  Bubble<IT>(vector.begin(), vector.end());
60 
61  // All elements are still sorted
62  for (auto it = vector.begin(); it < vector.end() - 1; ++it)
63  EXPECT_LE(*it, *(it + 1));
64  }
65 
66  // Inverse iterator order - Array should not be affected
67  {
68  Container vector(ArrayRand);
69  Bubble<IT>(vector.end(), vector.begin());
70 
71  int i = 0;
72  for (auto it = vector.begin(); it < vector.end(); ++it, ++i)
73  EXPECT_EQ(ArrayRand[i], *it);
74  }
75 
76  // No error unitialized array
77  {
78  Container emptyArray;
79  Bubble<IT>(emptyArray.begin(), emptyArray.end());
80  }
81 
82  // Unique value array - Array should not be affected
83  {
84  Container uniqueValueArray(1, 511);
85  Bubble<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 = RandStr;
92  Bubble<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 Bubble-Sort tests - Inverse Order
99 TEST(TestBubble, BubbleGreaterComparator)
100 {
101  // Normal Run - Elements should be sorted in inverse order
102  {
103  Container vector(ArrayRand);
104  Bubble<IT, GE_Comparator>(vector.begin(), vector.end());
105 
106  // All elements are sorted in inverse order
107  for (auto it = vector.begin(); it < vector.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 vector(ArraySort);
114  Bubble<IT, GE_Comparator>(vector.begin(), vector.end());
115 
116  // All elements are still sorted in inverse order
117  for (auto it = vector.begin(); it < vector.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 = RandStr;
124  Bubble<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(TestBubble, BubbleSorts)
Definition: TestBubble.cxx:44