TestRaddix.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 <raddix.hxx>
22 
23 // Testing namespace
24 using namespace huc::sort;
25 
26 #ifndef DOXYGEN_SKIP
27 namespace {
28  // Simple sorted array of integers with positive values only
29  const int SortedArrayIntPos[] = {0, 2, 8, 15, 36, 212, 366, 15478};
30  // Simple random array of integers with positive values only
31  const int RandomArrayIntPos[] = {4520, 30, 500, 20, 3, 2, 3, 4, 5, 15};
32 
33  typedef std::vector<int> Container;
34  typedef Container::iterator IT;
35 }
36 #endif /* DOXYGEN_SKIP */
37 
38 // Basic Quick-Sort tests
39 TEST(TestRaddix, RaddixSorts)
40 {
41  // Normal Run - array should be sorted in order
42  {
43  Container randomdArrayPos(RandomArrayIntPos, RandomArrayIntPos + sizeof(RandomArrayIntPos) / sizeof(int));
44  RaddixSort<IT>(randomdArrayPos.begin(), randomdArrayPos.end());
45 
46  // All elements are sorted
47  for (IT it = randomdArrayPos.begin(); it < randomdArrayPos.end() - 1; ++it)
48  EXPECT_LE(*it, *(it + 1));
49  }
50 
51  // Already sortedArray - Array should not be affected
52  {
53  Container sortedArrayPos(SortedArrayIntPos, SortedArrayIntPos + sizeof(SortedArrayIntPos) / sizeof(int));
54  RaddixSort<IT>(sortedArrayPos.begin(), sortedArrayPos.end());
55 
56  // All elements are still sorted
57  for (IT it = sortedArrayPos.begin(); it < sortedArrayPos.end() - 1; ++it)
58  EXPECT_LE(*it, *(it + 1));
59  }
60 
61  // Inverse iterator order - Array should not be affected
62  {
63  Container randomArrayPos(RandomArrayIntPos, RandomArrayIntPos + sizeof(RandomArrayIntPos) / sizeof(int));
64  RaddixSort<IT>(randomArrayPos.end(), randomArrayPos.begin());
65 
66  int i = 0;
67  for (IT it = randomArrayPos.begin(); it < randomArrayPos.end(); ++it, ++i)
68  EXPECT_EQ(RandomArrayIntPos[i], *it);
69  }
70 
71  // No error empty array
72  {
73  Container emptyArray;
74  RaddixSort<IT>(emptyArray.begin(), emptyArray.end());
75  }
76 
77  // Unique value array - Array should not be affected
78  {
79  Container uniqueValueArray(1, 511);
80  RaddixSort<IT>(uniqueValueArray.begin(), uniqueValueArray.end());
81  EXPECT_EQ(511, uniqueValueArray[0]);
82  }
83 }
TEST(TestRaddix, RaddixSorts)
Definition: TestRaddix.cxx:39