1 //===- llvm/unittest/ADT/CombinationGeneratorTest.cpp ---------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/CombinationGenerator.h"
10 #include "llvm/ADT/ArrayRef.h"
11 #include "llvm/ADT/STLExtras.h"
12 #include "llvm/ADT/STLForwardCompat.h"
13 #include "llvm/ADT/iterator_range.h"
14 #include "llvm/Support/ErrorHandling.h"
15 #include "gmock/gmock.h"
16 #include "gtest/gtest.h"
27 TEST(CombinationGenerator
, Square
) {
28 const std::vector
<std::vector
<int>> Choices
{{0, 1}, {2, 3}};
30 std::vector
<std::vector
<int>> Variants
;
31 CombinationGenerator
<int, std::vector
<int>, 4> G(Choices
);
32 const size_t NumVariants
= G
.numCombinations();
33 G
.generate([&](ArrayRef
<int> State
) -> bool {
34 Variants
.emplace_back(State
);
35 return false; // keep going
38 const std::vector
<std::vector
<int>> ExpectedVariants
{
44 ASSERT_THAT(Variants
, ::testing::SizeIs(NumVariants
));
45 ASSERT_THAT(Variants
, ::testing::ContainerEq(ExpectedVariants
));
48 TEST(CombinationGenerator
, MiddleColumn
) {
49 const std::vector
<std::vector
<int>> Choices
{{0}, {1, 2}, {3}};
51 std::vector
<std::vector
<int>> Variants
;
52 CombinationGenerator
<int, std::vector
<int>, 4> G(Choices
);
53 const size_t NumVariants
= G
.numCombinations();
54 G
.generate([&](ArrayRef
<int> State
) -> bool {
55 Variants
.emplace_back(State
);
56 return false; // keep going
59 const std::vector
<std::vector
<int>> ExpectedVariants
{
63 ASSERT_THAT(Variants
, ::testing::SizeIs(NumVariants
));
64 ASSERT_THAT(Variants
, ::testing::ContainerEq(ExpectedVariants
));
67 TEST(CombinationGenerator
, SideColumns
) {
68 const std::vector
<std::vector
<int>> Choices
{{0, 1}, {2}, {3, 4}};
70 std::vector
<std::vector
<int>> Variants
;
71 CombinationGenerator
<int, std::vector
<int>, 4> G(Choices
);
72 const size_t NumVariants
= G
.numCombinations();
73 G
.generate([&](ArrayRef
<int> State
) -> bool {
74 Variants
.emplace_back(State
);
75 return false; // keep going
78 const std::vector
<std::vector
<int>> ExpectedVariants
{
84 ASSERT_THAT(Variants
, ::testing::SizeIs(NumVariants
));
85 ASSERT_THAT(Variants
, ::testing::ContainerEq(ExpectedVariants
));
88 TEST(CombinationGenerator
, LeftColumn
) {
89 const std::vector
<std::vector
<int>> Choices
{{0, 1}, {2}};
91 std::vector
<std::vector
<int>> Variants
;
92 CombinationGenerator
<int, std::vector
<int>, 4> G(Choices
);
93 const size_t NumVariants
= G
.numCombinations();
94 G
.generate([&](ArrayRef
<int> State
) -> bool {
95 Variants
.emplace_back(State
);
96 return false; // keep going
99 const std::vector
<std::vector
<int>> ExpectedVariants
{
103 ASSERT_THAT(Variants
, ::testing::SizeIs(NumVariants
));
104 ASSERT_THAT(Variants
, ::testing::ContainerEq(ExpectedVariants
));
107 TEST(CombinationGenerator
, RightColumn
) {
108 const std::vector
<std::vector
<int>> Choices
{{0}, {1, 2}};
110 std::vector
<std::vector
<int>> Variants
;
111 CombinationGenerator
<int, std::vector
<int>, 4> G(Choices
);
112 const size_t NumVariants
= G
.numCombinations();
113 G
.generate([&](ArrayRef
<int> State
) -> bool {
114 Variants
.emplace_back(State
);
115 return false; // keep going
118 const std::vector
<std::vector
<int>> ExpectedVariants
{
122 ASSERT_THAT(Variants
, ::testing::SizeIs(NumVariants
));
123 ASSERT_THAT(Variants
, ::testing::ContainerEq(ExpectedVariants
));
126 TEST(CombinationGenerator
, Column
) {
127 const std::vector
<std::vector
<int>> Choices
{{0, 1}};
129 std::vector
<std::vector
<int>> Variants
;
130 CombinationGenerator
<int, std::vector
<int>, 4> G(Choices
);
131 const size_t NumVariants
= G
.numCombinations();
132 G
.generate([&](ArrayRef
<int> State
) -> bool {
133 Variants
.emplace_back(State
);
134 return false; // keep going
137 const std::vector
<std::vector
<int>> ExpectedVariants
{
141 ASSERT_THAT(Variants
, ::testing::SizeIs(NumVariants
));
142 ASSERT_THAT(Variants
, ::testing::ContainerEq(ExpectedVariants
));
145 TEST(CombinationGenerator
, Row
) {
146 const std::vector
<std::vector
<int>> Choices
{{0}, {1}};
148 std::vector
<std::vector
<int>> Variants
;
149 CombinationGenerator
<int, std::vector
<int>, 4> G(Choices
);
150 const size_t NumVariants
= G
.numCombinations();
151 G
.generate([&](ArrayRef
<int> State
) -> bool {
152 Variants
.emplace_back(State
);
153 return false; // keep going
156 const std::vector
<std::vector
<int>> ExpectedVariants
{
159 ASSERT_THAT(Variants
, ::testing::SizeIs(NumVariants
));
160 ASSERT_THAT(Variants
, ::testing::ContainerEq(ExpectedVariants
));
163 TEST(CombinationGenerator
, Singleton
) {
164 const std::vector
<std::vector
<int>> Choices
{{0}};
166 std::vector
<std::vector
<int>> Variants
;
167 CombinationGenerator
<int, std::vector
<int>, 4> G(Choices
);
168 const size_t NumVariants
= G
.numCombinations();
169 G
.generate([&](ArrayRef
<int> State
) -> bool {
170 Variants
.emplace_back(State
);
171 return false; // keep going
174 const std::vector
<std::vector
<int>> ExpectedVariants
{
177 ASSERT_THAT(Variants
, ::testing::SizeIs(NumVariants
));
178 ASSERT_THAT(Variants
, ::testing::ContainerEq(ExpectedVariants
));
181 } // end anonymous namespace