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/Support/ErrorHandling.h"
13 #include "gmock/gmock.h"
14 #include "gtest/gtest.h"
25 TEST(CombinationGenerator
, Square
) {
26 const std::vector
<std::vector
<int>> Choices
{{0, 1}, {2, 3}};
28 std::vector
<std::vector
<int>> Variants
;
29 CombinationGenerator
<int, std::vector
<int>, 4> G(Choices
);
30 const size_t NumVariants
= G
.numCombinations();
31 G
.generate([&](ArrayRef
<int> State
) -> bool {
32 Variants
.emplace_back(State
);
33 return false; // keep going
36 const std::vector
<std::vector
<int>> ExpectedVariants
{
42 ASSERT_THAT(Variants
, ::testing::SizeIs(NumVariants
));
43 ASSERT_THAT(Variants
, ::testing::ContainerEq(ExpectedVariants
));
46 TEST(CombinationGenerator
, MiddleColumn
) {
47 const std::vector
<std::vector
<int>> Choices
{{0}, {1, 2}, {3}};
49 std::vector
<std::vector
<int>> Variants
;
50 CombinationGenerator
<int, std::vector
<int>, 4> G(Choices
);
51 const size_t NumVariants
= G
.numCombinations();
52 G
.generate([&](ArrayRef
<int> State
) -> bool {
53 Variants
.emplace_back(State
);
54 return false; // keep going
57 const std::vector
<std::vector
<int>> ExpectedVariants
{
61 ASSERT_THAT(Variants
, ::testing::SizeIs(NumVariants
));
62 ASSERT_THAT(Variants
, ::testing::ContainerEq(ExpectedVariants
));
65 TEST(CombinationGenerator
, SideColumns
) {
66 const std::vector
<std::vector
<int>> Choices
{{0, 1}, {2}, {3, 4}};
68 std::vector
<std::vector
<int>> Variants
;
69 CombinationGenerator
<int, std::vector
<int>, 4> G(Choices
);
70 const size_t NumVariants
= G
.numCombinations();
71 G
.generate([&](ArrayRef
<int> State
) -> bool {
72 Variants
.emplace_back(State
);
73 return false; // keep going
76 const std::vector
<std::vector
<int>> ExpectedVariants
{
82 ASSERT_THAT(Variants
, ::testing::SizeIs(NumVariants
));
83 ASSERT_THAT(Variants
, ::testing::ContainerEq(ExpectedVariants
));
86 TEST(CombinationGenerator
, LeftColumn
) {
87 const std::vector
<std::vector
<int>> Choices
{{0, 1}, {2}};
89 std::vector
<std::vector
<int>> Variants
;
90 CombinationGenerator
<int, std::vector
<int>, 4> G(Choices
);
91 const size_t NumVariants
= G
.numCombinations();
92 G
.generate([&](ArrayRef
<int> State
) -> bool {
93 Variants
.emplace_back(State
);
94 return false; // keep going
97 const std::vector
<std::vector
<int>> ExpectedVariants
{
101 ASSERT_THAT(Variants
, ::testing::SizeIs(NumVariants
));
102 ASSERT_THAT(Variants
, ::testing::ContainerEq(ExpectedVariants
));
105 TEST(CombinationGenerator
, RightColumn
) {
106 const std::vector
<std::vector
<int>> Choices
{{0}, {1, 2}};
108 std::vector
<std::vector
<int>> Variants
;
109 CombinationGenerator
<int, std::vector
<int>, 4> G(Choices
);
110 const size_t NumVariants
= G
.numCombinations();
111 G
.generate([&](ArrayRef
<int> State
) -> bool {
112 Variants
.emplace_back(State
);
113 return false; // keep going
116 const std::vector
<std::vector
<int>> ExpectedVariants
{
120 ASSERT_THAT(Variants
, ::testing::SizeIs(NumVariants
));
121 ASSERT_THAT(Variants
, ::testing::ContainerEq(ExpectedVariants
));
124 TEST(CombinationGenerator
, Column
) {
125 const std::vector
<std::vector
<int>> Choices
{{0, 1}};
127 std::vector
<std::vector
<int>> Variants
;
128 CombinationGenerator
<int, std::vector
<int>, 4> G(Choices
);
129 const size_t NumVariants
= G
.numCombinations();
130 G
.generate([&](ArrayRef
<int> State
) -> bool {
131 Variants
.emplace_back(State
);
132 return false; // keep going
135 const std::vector
<std::vector
<int>> ExpectedVariants
{
139 ASSERT_THAT(Variants
, ::testing::SizeIs(NumVariants
));
140 ASSERT_THAT(Variants
, ::testing::ContainerEq(ExpectedVariants
));
143 TEST(CombinationGenerator
, Row
) {
144 const std::vector
<std::vector
<int>> Choices
{{0}, {1}};
146 std::vector
<std::vector
<int>> Variants
;
147 CombinationGenerator
<int, std::vector
<int>, 4> G(Choices
);
148 const size_t NumVariants
= G
.numCombinations();
149 G
.generate([&](ArrayRef
<int> State
) -> bool {
150 Variants
.emplace_back(State
);
151 return false; // keep going
154 const std::vector
<std::vector
<int>> ExpectedVariants
{
157 ASSERT_THAT(Variants
, ::testing::SizeIs(NumVariants
));
158 ASSERT_THAT(Variants
, ::testing::ContainerEq(ExpectedVariants
));
161 TEST(CombinationGenerator
, Singleton
) {
162 const std::vector
<std::vector
<int>> Choices
{{0}};
164 std::vector
<std::vector
<int>> Variants
;
165 CombinationGenerator
<int, std::vector
<int>, 4> G(Choices
);
166 const size_t NumVariants
= G
.numCombinations();
167 G
.generate([&](ArrayRef
<int> State
) -> bool {
168 Variants
.emplace_back(State
);
169 return false; // keep going
172 const std::vector
<std::vector
<int>> ExpectedVariants
{
175 ASSERT_THAT(Variants
, ::testing::SizeIs(NumVariants
));
176 ASSERT_THAT(Variants
, ::testing::ContainerEq(ExpectedVariants
));
179 } // end anonymous namespace