TestPermutations.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 <permutations.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::const_iterator Const_IT;
31 }
32 #endif /* DOXYGEN_SKIP */
33 
34 // Test permutations
35 TEST(TestCore, Permutations)
36 {
37  // Empty vector - no permutations
38  {
39  const Container kEmptyCollection = Container();
40  auto permutations = Permutations<Container, Const_IT>(kEmptyCollection.begin(), kEmptyCollection.end());
41  EXPECT_EQ(0, permutations.size());
42  }
43 
44  // Inversed iterator - no permutations
45  {
46  const Container kUnicCollection = Container(1,10);
47  auto permutations = Permutations<Container, Const_IT>(kUnicCollection.end(), kUnicCollection.begin());
48  EXPECT_EQ(0, permutations.size());
49  }
50 
51  // Unic element vector - Unique object returned as permutation
52  {
53  const Container kUnicCollection = Container(1,10);
54  auto permutations = Permutations<Container, Const_IT>(kUnicCollection.begin(), kUnicCollection.end());
55  EXPECT_EQ(1, permutations.size());
56  EXPECT_EQ(1, permutations.begin()->size());
57  EXPECT_EQ(10, *(permutations.begin()->begin()));
58  }
59 
60  // Same elements vector - n! list of n equivalent elements
61  {
62  const Container kSameElCollection = Container(3,10);
63  auto permutations = Permutations<Container, Const_IT>(kSameElCollection.begin(), kSameElCollection.end());
64  EXPECT_EQ(6, permutations.size());
65  for (auto it = permutations.begin(); it != permutations.end(); ++it)
66  {
67  EXPECT_EQ(3, it->size());
68  for (Container::const_iterator itEl = it->begin(); itEl != it->end(); ++itEl)
69  EXPECT_EQ(10, *itEl);
70  }
71  }
72 
73  // Run with value 2, 1, 3
74  {
75  const Container kSmallArray(SmallIntArray, SmallIntArray + sizeof(SmallIntArray) /
76  sizeof(Container::value_type));
77  auto permutations = Permutations<Container, Const_IT>(kSmallArray.begin(), kSmallArray.end());
78  EXPECT_EQ(6, permutations.size());
79  for (auto it = permutations.begin(); it != permutations.end(); ++it)
80  EXPECT_EQ(3, it->size());
81  }
82 
83  // String run
84  {
85  const std::string abcStr = "abc";
86  std::list<std::string> permutations =
87  Permutations<std::string, std::string::const_iterator>(abcStr.begin(), abcStr.end());
88  EXPECT_EQ(6, permutations.size());
89  for (std::list<std::string>::const_iterator it = permutations.begin(); it != permutations.end(); ++it)
90  EXPECT_EQ(3, it->size());
91  }
92 }
TEST(TestCore, Permutations)
std::list< Container > Permutations(const IT &begin, const IT &end)