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"
24 TEST(CombinationGenerator
, Square
) {
25 const std::vector
<std::vector
<int>> Choices
{{0, 1}, {2, 3}};
27 std::vector
<std::vector
<int>> Variants
;
28 CombinationGenerator
<int, std::vector
<int>, 4> G(Choices
);
29 const size_t NumVariants
= G
.numCombinations();
30 G
.generate([&](ArrayRef
<int> State
) -> bool {
31 Variants
.emplace_back(State
);
32 return false; // keep going
35 const std::vector
<std::vector
<int>> ExpectedVariants
{
41 ASSERT_THAT(Variants
, ::testing::SizeIs(NumVariants
));
42 ASSERT_THAT(Variants
, ::testing::ContainerEq(ExpectedVariants
));
45 TEST(CombinationGenerator
, MiddleColumn
) {
46 const std::vector
<std::vector
<int>> Choices
{{0}, {1, 2}, {3}};
48 std::vector
<std::vector
<int>> Variants
;
49 CombinationGenerator
<int, std::vector
<int>, 4> G(Choices
);
50 const size_t NumVariants
= G
.numCombinations();
51 G
.generate([&](ArrayRef
<int> State
) -> bool {
52 Variants
.emplace_back(State
);
53 return false; // keep going
56 const std::vector
<std::vector
<int>> ExpectedVariants
{
60 ASSERT_THAT(Variants
, ::testing::SizeIs(NumVariants
));
61 ASSERT_THAT(Variants
, ::testing::ContainerEq(ExpectedVariants
));
64 TEST(CombinationGenerator
, SideColumns
) {
65 const std::vector
<std::vector
<int>> Choices
{{0, 1}, {2}, {3, 4}};
67 std::vector
<std::vector
<int>> Variants
;
68 CombinationGenerator
<int, std::vector
<int>, 4> G(Choices
);
69 const size_t NumVariants
= G
.numCombinations();
70 G
.generate([&](ArrayRef
<int> State
) -> bool {
71 Variants
.emplace_back(State
);
72 return false; // keep going
75 const std::vector
<std::vector
<int>> ExpectedVariants
{
81 ASSERT_THAT(Variants
, ::testing::SizeIs(NumVariants
));
82 ASSERT_THAT(Variants
, ::testing::ContainerEq(ExpectedVariants
));
85 TEST(CombinationGenerator
, LeftColumn
) {
86 const std::vector
<std::vector
<int>> Choices
{{0, 1}, {2}};
88 std::vector
<std::vector
<int>> Variants
;
89 CombinationGenerator
<int, std::vector
<int>, 4> G(Choices
);
90 const size_t NumVariants
= G
.numCombinations();
91 G
.generate([&](ArrayRef
<int> State
) -> bool {
92 Variants
.emplace_back(State
);
93 return false; // keep going
96 const std::vector
<std::vector
<int>> ExpectedVariants
{
100 ASSERT_THAT(Variants
, ::testing::SizeIs(NumVariants
));
101 ASSERT_THAT(Variants
, ::testing::ContainerEq(ExpectedVariants
));
104 TEST(CombinationGenerator
, RightColumn
) {
105 const std::vector
<std::vector
<int>> Choices
{{0}, {1, 2}};
107 std::vector
<std::vector
<int>> Variants
;
108 CombinationGenerator
<int, std::vector
<int>, 4> G(Choices
);
109 const size_t NumVariants
= G
.numCombinations();
110 G
.generate([&](ArrayRef
<int> State
) -> bool {
111 Variants
.emplace_back(State
);
112 return false; // keep going
115 const std::vector
<std::vector
<int>> ExpectedVariants
{
119 ASSERT_THAT(Variants
, ::testing::SizeIs(NumVariants
));
120 ASSERT_THAT(Variants
, ::testing::ContainerEq(ExpectedVariants
));
123 TEST(CombinationGenerator
, Column
) {
124 const std::vector
<std::vector
<int>> Choices
{{0, 1}};
126 std::vector
<std::vector
<int>> Variants
;
127 CombinationGenerator
<int, std::vector
<int>, 4> G(Choices
);
128 const size_t NumVariants
= G
.numCombinations();
129 G
.generate([&](ArrayRef
<int> State
) -> bool {
130 Variants
.emplace_back(State
);
131 return false; // keep going
134 const std::vector
<std::vector
<int>> ExpectedVariants
{
138 ASSERT_THAT(Variants
, ::testing::SizeIs(NumVariants
));
139 ASSERT_THAT(Variants
, ::testing::ContainerEq(ExpectedVariants
));
142 TEST(CombinationGenerator
, Row
) {
143 const std::vector
<std::vector
<int>> Choices
{{0}, {1}};
145 std::vector
<std::vector
<int>> Variants
;
146 CombinationGenerator
<int, std::vector
<int>, 4> G(Choices
);
147 const size_t NumVariants
= G
.numCombinations();
148 G
.generate([&](ArrayRef
<int> State
) -> bool {
149 Variants
.emplace_back(State
);
150 return false; // keep going
153 const std::vector
<std::vector
<int>> ExpectedVariants
{
156 ASSERT_THAT(Variants
, ::testing::SizeIs(NumVariants
));
157 ASSERT_THAT(Variants
, ::testing::ContainerEq(ExpectedVariants
));
160 TEST(CombinationGenerator
, Singleton
) {
161 const std::vector
<std::vector
<int>> Choices
{{0}};
163 std::vector
<std::vector
<int>> Variants
;
164 CombinationGenerator
<int, std::vector
<int>, 4> G(Choices
);
165 const size_t NumVariants
= G
.numCombinations();
166 G
.generate([&](ArrayRef
<int> State
) -> bool {
167 Variants
.emplace_back(State
);
168 return false; // keep going
171 const std::vector
<std::vector
<int>> ExpectedVariants
{
174 ASSERT_THAT(Variants
, ::testing::SizeIs(NumVariants
));
175 ASSERT_THAT(Variants
, ::testing::ContainerEq(ExpectedVariants
));
178 } // end anonymous namespace