TestIntersection.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 <intersection.hxx>
22 
23 // STD includes
24 #include <list>
25 
26 using namespace huc::combinatory;
27 
28 #ifndef DOXYGEN_SKIP
29 namespace {
30  // Simple sorted array of integers with negative values
31  const int SortedArrayInt[] = {-3, -2, 0, 2, 8, 15, 36, 212, 366};
32  // Simple random array of integers with negative values
33  const int RandomArrayInt[] = {4, 3, 5, 2, -18, 3, 2, 3, 4, 5, -5};
34  // Other random array of integers with negative values
35  const int RandomArrayInterInt[] = {5 , 5, -5, 3, -18, 10, 15};
36  // Ordered string
37  const std::string OrderedStr = "acegmnoop";
38  // Random string
39  const std::string RandomStr = "xacvgeze";
40 
41  typedef std::vector<int> Container;
42  typedef Container::value_type Value;
43  typedef Container::const_iterator Const_IT;
44 }
45 #endif /* DOXYGEN_SKIP */
46 
47 // Test intersections
48 TEST(TestIntersection, Intersections)
49 {
50  // Null intersection on empty vectors - empty intersection expected
51  {
52  const Container kEmptyEl = Container();
53  auto intersection = Intersection<Container, Const_IT>
54  (kEmptyEl.begin(), kEmptyEl.end(), kEmptyEl.begin(), kEmptyEl.end());
55  EXPECT_EQ(0, intersection.size());
56  }
57 
58  // Null intersection with one empty vector - empty intersection expected
59  {
60  const Container kEmptyEl = Container();
61  const Container kCollection = Container(10,1);
62  Container intersection = Intersection<Container, Const_IT>
63  (kEmptyEl.begin(), kEmptyEl.end(), kCollection.begin(), kCollection.end());
64  EXPECT_EQ(0, intersection.size());
65  }
66 
67  // Basic run with same collection - intersection as the collection itself expected
68  {
69  const Container kSortedArray(SortedArrayInt, SortedArrayInt + sizeof(SortedArrayInt) / sizeof(Value));
70  Container intersection = Intersection<Container, Const_IT>
71  (kSortedArray.begin(), kSortedArray.end(), kSortedArray.begin(), kSortedArray.end());
72 
73  // The intersection of the same vector should return the same vector
74  Const_IT kIntersectIt = intersection.begin();
75  for (Const_IT it = kSortedArray.begin(); it != kSortedArray.end(); ++it, ++kIntersectIt)
76  EXPECT_EQ(*it, *kIntersectIt);
77  }
78 
79  // Basic run with same collection containing duplicates- intersection as the collection itself expected
80  {
81  const Container kFirstRandom(RandomArrayInt, RandomArrayInt + sizeof(RandomArrayInt) / sizeof(Value));
82  const Container kSecondRandom(RandomArrayInt, RandomArrayInt + sizeof(RandomArrayInt) / sizeof(Value));
83 
84  Container intersection = Intersection<Container, Const_IT>
85  (kFirstRandom.begin(), kFirstRandom.end(), kSecondRandom.begin(), kSecondRandom.end());
86 
87  // The intersection with the copy vector should return the same vector
88  Const_IT kIntersectIt = intersection.begin();
89  for (Const_IT it = kFirstRandom.begin(); it != kFirstRandom.end(); ++it, ++kIntersectIt)
90  EXPECT_EQ(*it, *kIntersectIt);
91  }
92 
93  // Basic run with normal values
94  {
95  const Container kFirstRandom(RandomArrayInt, RandomArrayInt + sizeof(RandomArrayInt) / sizeof(Value));
96  const Container kSecondRandom
97  (RandomArrayInterInt, RandomArrayInterInt + sizeof(RandomArrayInterInt) / sizeof(Value));
98 
99  Container intersection = Intersection<Container, Const_IT>
100  (kFirstRandom.begin(), kFirstRandom.end(), kSecondRandom.begin(), kSecondRandom.end());
101 
102  // Should return [-18, -5, 3, 5, 5] after sorting
103  std::sort(intersection.begin(), intersection.end());
104  EXPECT_EQ(-18, intersection[0]);
105  EXPECT_EQ(-5, intersection[1]);
106  EXPECT_EQ(3, intersection[2]);
107  EXPECT_EQ(5, intersection[3]);
108  EXPECT_EQ(5, intersection[4]);
109  }
110 
111  // String run
112  {
113  std::string intersection = Intersection<std::string, std::string::const_iterator>
114  (OrderedStr.begin(), OrderedStr.end(), RandomStr.begin(), RandomStr.end());
115 
116  // Should return ['a', 'c', 'e', 'g'] after sorting
117  std::sort(intersection.begin(), intersection.end());
118  EXPECT_EQ('a', intersection[0]);
119  EXPECT_EQ('c', intersection[1]);
120  EXPECT_EQ('e', intersection[2]);
121  EXPECT_EQ('g', intersection[3]);
122  }
123 }
TEST(TestIntersection, Intersections)