Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / unittests / ADT / CombinationGeneratorTest.cpp
bloba3e36fb322c3679c3043f2d28de1a55066940152
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/Support/ErrorHandling.h"
13 #include "gmock/gmock.h"
14 #include "gtest/gtest.h"
15 #include <algorithm>
16 #include <cstddef>
17 #include <iterator>
18 #include <tuple>
19 #include <vector>
21 using namespace llvm;
23 namespace {
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
34 });
36 const std::vector<std::vector<int>> ExpectedVariants{
37 {0, 2},
38 {0, 3},
39 {1, 2},
40 {1, 3},
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
55 });
57 const std::vector<std::vector<int>> ExpectedVariants{
58 {0, 1, 3},
59 {0, 2, 3},
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
74 });
76 const std::vector<std::vector<int>> ExpectedVariants{
77 {0, 2, 3},
78 {0, 2, 4},
79 {1, 2, 3},
80 {1, 2, 4},
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
95 });
97 const std::vector<std::vector<int>> ExpectedVariants{
98 {0, 2},
99 {1, 2},
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{
117 {0, 1},
118 {0, 2},
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{
136 {0},
137 {1},
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{
155 {0, 1},
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{
173 {0},
175 ASSERT_THAT(Variants, ::testing::SizeIs(NumVariants));
176 ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants));
179 } // end anonymous namespace