TestMaxMElements.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 <max_m_elements.hxx>
22 
23 using namespace huc::search;
24 
25 #ifndef DOXYGEN_SKIP
26 namespace {
27  // Simple sorted array of integers with negative values
28  const int SortedArrayInt[] = {-3, -2, 0, 2, 8, 15, 36, 212, 366};
29  // Simple random array of integers with negative values
30  const int RandomArrayInt[] = {4, 3, 5, 2, -18, 3, 2, 3, 4, 5, -5};
31 
32  typedef std::vector<int> Container;
33  typedef Container::iterator IT;
34 }
35 #endif /* DOXYGEN_SKIP */
36 
37 // Test MaxMElements
38 TEST(TestSearch, MaxMElements)
39 {
40  // Should return the max value [5] for a unique element search
41  {
42  Container kRandomElements(RandomArrayInt, RandomArrayInt + sizeof(RandomArrayInt) / sizeof(int));
43  Container kMaxElements = MaxMElements<Container, IT>(kRandomElements.begin(), kRandomElements.end(), 1);
44  EXPECT_EQ(5, kMaxElements[0]);
45  }
46 
47  // Should return [5,5,4]
48  {
49  Container kRandomElements (RandomArrayInt, RandomArrayInt + sizeof(RandomArrayInt) / sizeof(int));
50  Container kMaxElements =MaxMElements<Container, IT>(kRandomElements.begin(), kRandomElements.end(), 3);
51  EXPECT_EQ(5, kMaxElements[0]);
52  EXPECT_EQ(5, kMaxElements[1]);
53  EXPECT_EQ(4, kMaxElements[2]);
54  }
55 
56  // Should return the last elements on sorted vector
57  {
58  Container kSortedArray(SortedArrayInt, SortedArrayInt + sizeof(SortedArrayInt) / sizeof(int));
59  Container kMaxElements = MaxMElements<Container, IT>(kSortedArray.begin(), kSortedArray.end(), 4);
60  EXPECT_EQ(366, kMaxElements[0]);
61  EXPECT_EQ(212, kMaxElements[1]);
62  EXPECT_EQ(36, kMaxElements[2]);
63  EXPECT_EQ(15, kMaxElements[3]);
64  }
65 
66  // Should return empty vector on insufficient vector
67  {
68  Container uniqueEl = Container(1, 2);
69  Container kMaxElements = MaxMElements<Container, IT>(uniqueEl.begin(), uniqueEl.end(), 2);
70  EXPECT_EQ(0, static_cast<int>(kMaxElements.size()));
71  }
72 
73  // Should return empty vector when looking for less than 1 elements
74  {
75  Container uniqueEl = Container(1, 2);
76  Container kMaxElements = MaxMElements<Container, IT>(uniqueEl.begin(), uniqueEl.end(), 0);
77  EXPECT_EQ(0, static_cast<int>(kMaxElements.size()));
78  }
79 }
80 
81 // Test MaxMElements to find the lowest values
82 TEST(TestSearch, MaxNElementsLowestValues)
83 {
84  // Should return the min value [-18] for a unique element search
85  {
86  Container kRandomElements(RandomArrayInt, RandomArrayInt + sizeof(RandomArrayInt) / sizeof(int));
87  Container kMaxElements =
88  MaxMElements<Container, IT, std::less_equal<int>>
89  (kRandomElements.begin(), kRandomElements.end(), 1);
90  EXPECT_EQ(-18, kMaxElements[0]);
91  }
92 
93  // Should return [5,5,4]
94  {
95  Container kRandomElements(RandomArrayInt, RandomArrayInt + sizeof(RandomArrayInt) / sizeof(int));
96  Container kMaxElements = MaxMElements<Container, IT, std::less_equal<int>>
97  (kRandomElements.begin(), kRandomElements.end(), 3);
98  EXPECT_EQ(-18, kMaxElements[0]);
99  EXPECT_EQ(-5, kMaxElements[1]);
100  EXPECT_EQ(2, kMaxElements[2]);
101  }
102 
103  // Should return the first elements on sorted vector
104  {
105  Container kSortedArray(SortedArrayInt, SortedArrayInt + sizeof(SortedArrayInt) / sizeof(int));
106  Container kMaxElements = MaxMElements<Container, IT, std::less_equal<int>>
107  (kSortedArray.begin(), kSortedArray.end(), 4);
108  EXPECT_EQ(-3, kMaxElements[0]);
109  EXPECT_EQ(-2, kMaxElements[1]);
110  EXPECT_EQ(0, kMaxElements[2]);
111  EXPECT_EQ(2, kMaxElements[3]);
112  }
113 }
TEST(TestSearch, MaxMElements)
Container MaxMElements(const IT &begin, const IT &end, const int m)