TestCombinations.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 <combinations.hxx>
22 
23 using namespace huc::combinatory;
24 
25 #ifndef DOXYGEN_SKIP
26 namespace {
27  const int SmallIntArray[] = {2, 1, 3}; // Small array containing 2, 1, 3 values
28 
29  typedef std::vector<int> Container;
30  typedef Container::value_type Value;
31  typedef Container::const_iterator Const_IT;
32  typedef std::list<Container> List;
33 }
34 #endif /* DOXYGEN_SKIP */
35 
36 // Test combinations
37 TEST(TestCombinations, Combinations)
38 {
39  // Empty vector - no combinations
40  {
41  const Container kEmptyCollection = Container();
42  List combinations = Combinations<Container, Const_IT>(kEmptyCollection.begin(), kEmptyCollection.end());
43  EXPECT_EQ(static_cast<size_t>(0), combinations.size());
44  }
45 
46  // Inversed iterator - no combinations
47  {
48  const Container kUnicCollection = Container(1, 10);
49  List combinations = Combinations<Container, Const_IT>(kUnicCollection.end(), kUnicCollection.begin());
50  EXPECT_EQ(static_cast<size_t>(0), combinations.size());
51  }
52 
53  // Unic element vector - Unique object returned as combinations
54  {
55  const Container kUnicCollection = Container(1, 10);
56  List combinations = Combinations<Container, Const_IT>(kUnicCollection.begin(), kUnicCollection.end());
57  EXPECT_EQ(static_cast<size_t>(1), combinations.size());
58  EXPECT_EQ(static_cast<size_t>(1), combinations.begin()->size());
59  EXPECT_EQ(10, *(combinations.begin()->begin()));
60  }
61 
62  // Run with value 2, 1, 3
63  {
64  const Container kSmallArray(SmallIntArray, SmallIntArray + sizeof(SmallIntArray) / sizeof(Value));
65  List combinations = Combinations<Container, Const_IT>(kSmallArray.begin(), kSmallArray.end());
66  EXPECT_EQ(static_cast<size_t>(7), combinations.size());
67 
68  int countEls = 0;
69  for (List::const_iterator it = combinations.begin(); it != combinations.end(); ++it)
70  countEls += static_cast<int>(it->size());
71  EXPECT_EQ(12, countEls);
72  //@TODO check sequence by sequence? (non ordered)
73  }
74 
75  // String run
76  {
77  const std::string abcStr = "abc";
78  std::list<std::string> combinations =
79  Combinations<std::string, std::string::const_iterator>(abcStr.begin(), abcStr.end());
80  EXPECT_EQ(static_cast<size_t>(7), combinations.size());
81 
82  int countEls = 0;
83  for (std::list<std::string>::const_iterator it = combinations.begin(); it != combinations.end(); ++it)
84  countEls += static_cast<int>(it->size());
85  EXPECT_EQ(12, countEls);
86  //@TODO check sequence by sequence? (non ordered)
87  }
88 }
TEST(TestCombinations, Combinations)
std::list< Container > Combinations(const IT &begin, const IT &end)