[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / llvm / unittests / ADT / CombinationGeneratorTest.cpp
blob8d25457e67a3b6b539be561dda2c912d7d990b47
1 //===- llvm/unittest/ADT/CombinationGeneratorTest.cpp ---------------------===//
2 //
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
6 //
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"
17 #include <algorithm>
18 #include <cstddef>
19 #include <iterator>
20 #include <tuple>
21 #include <vector>
23 using namespace llvm;
25 namespace {
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
36 });
38 const std::vector<std::vector<int>> ExpectedVariants{
39 {0, 2},
40 {0, 3},
41 {1, 2},
42 {1, 3},
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
57 });
59 const std::vector<std::vector<int>> ExpectedVariants{
60 {0, 1, 3},
61 {0, 2, 3},
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
76 });
78 const std::vector<std::vector<int>> ExpectedVariants{
79 {0, 2, 3},
80 {0, 2, 4},
81 {1, 2, 3},
82 {1, 2, 4},
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
97 });
99 const std::vector<std::vector<int>> ExpectedVariants{
100 {0, 2},
101 {1, 2},
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{
119 {0, 1},
120 {0, 2},
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{
138 {0},
139 {1},
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{
157 {0, 1},
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{
175 {0},
177 ASSERT_THAT(Variants, ::testing::SizeIs(NumVariants));
178 ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants));
181 } // end anonymous namespace