| 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 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 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 <vector> |
| 19 |
|
| 20 |
using namespace llvm; |
| 21 |
|
| 22 |
namespace { |
| 23 |
|
| 24 |
TEST(CombinationGenerator, Square) { |
| 25 |
const std::vector<std::vector<int>> Choices{{0, 1}, {2, 3}}; |
| 26 |
|
| 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 |
| 33 |
}); |
| 34 |
|
| 35 |
const std::vector<std::vector<int>> ExpectedVariants{ |
| 36 |
{0, 2}, |
| 37 |
{0, 3}, |
| 38 |
{1, 2}, |
| 39 |
{1, 3}, |
| 40 |
}; |
| 41 |
ASSERT_THAT(Variants, ::testing::SizeIs(NumVariants)); |
| 42 |
ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants)); |
| 43 |
} |
| 44 |
|
| 45 |
TEST(CombinationGenerator, MiddleColumn) { |
| 46 |
const std::vector<std::vector<int>> Choices{{0}, {1, 2}, {3}}; |
| 47 |
|
| 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 |
| 54 |
}); |
| 55 |
|
| 56 |
const std::vector<std::vector<int>> ExpectedVariants{ |
| 57 |
{0, 1, 3}, |
| 58 |
{0, 2, 3}, |
| 59 |
}; |
| 60 |
ASSERT_THAT(Variants, ::testing::SizeIs(NumVariants)); |
| 61 |
ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants)); |
| 62 |
} |
| 63 |
|
| 64 |
TEST(CombinationGenerator, SideColumns) { |
| 65 |
const std::vector<std::vector<int>> Choices{{0, 1}, {2}, {3, 4}}; |
| 66 |
|
| 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 |
| 73 |
}); |
| 74 |
|
| 75 |
const std::vector<std::vector<int>> ExpectedVariants{ |
| 76 |
{0, 2, 3}, |
| 77 |
{0, 2, 4}, |
| 78 |
{1, 2, 3}, |
| 79 |
{1, 2, 4}, |
| 80 |
}; |
| 81 |
ASSERT_THAT(Variants, ::testing::SizeIs(NumVariants)); |
| 82 |
ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants)); |
| 83 |
} |
| 84 |
|
| 85 |
TEST(CombinationGenerator, LeftColumn) { |
| 86 |
const std::vector<std::vector<int>> Choices{{0, 1}, {2}}; |
| 87 |
|
| 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 |
| 94 |
}); |
| 95 |
|
| 96 |
const std::vector<std::vector<int>> ExpectedVariants{ |
| 97 |
{0, 2}, |
| 98 |
{1, 2}, |
| 99 |
}; |
| 100 |
ASSERT_THAT(Variants, ::testing::SizeIs(NumVariants)); |
| 101 |
ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants)); |
| 102 |
} |
| 103 |
|
| 104 |
TEST(CombinationGenerator, RightColumn) { |
| 105 |
const std::vector<std::vector<int>> Choices{{0}, {1, 2}}; |
| 106 |
|
| 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 |
| 113 |
}); |
| 114 |
|
| 115 |
const std::vector<std::vector<int>> ExpectedVariants{ |
| 116 |
{0, 1}, |
| 117 |
{0, 2}, |
| 118 |
}; |
| 119 |
ASSERT_THAT(Variants, ::testing::SizeIs(NumVariants)); |
| 120 |
ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants)); |
| 121 |
} |
| 122 |
|
| 123 |
TEST(CombinationGenerator, Column) { |
| 124 |
const std::vector<std::vector<int>> Choices{{0, 1}}; |
| 125 |
|
| 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 |
| 132 |
}); |
| 133 |
|
| 134 |
const std::vector<std::vector<int>> ExpectedVariants{ |
| 135 |
{0}, |
| 136 |
{1}, |
| 137 |
}; |
| 138 |
ASSERT_THAT(Variants, ::testing::SizeIs(NumVariants)); |
| 139 |
ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants)); |
| 140 |
} |
| 141 |
|
| 142 |
TEST(CombinationGenerator, Row) { |
| 143 |
const std::vector<std::vector<int>> Choices{{0}, {1}}; |
| 144 |
|
| 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 |
| 151 |
}); |
| 152 |
|
| 153 |
const std::vector<std::vector<int>> ExpectedVariants{ |
| 154 |
{0, 1}, |
| 155 |
}; |
| 156 |
ASSERT_THAT(Variants, ::testing::SizeIs(NumVariants)); |
| 157 |
ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants)); |
| 158 |
} |
| 159 |
|
| 160 |
TEST(CombinationGenerator, Singleton) { |
| 161 |
const std::vector<std::vector<int>> Choices{{0}}; |
| 162 |
|
| 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 |
| 169 |
}); |
| 170 |
|
| 171 |
const std::vector<std::vector<int>> ExpectedVariants{ |
| 172 |
{0}, |
| 173 |
}; |
| 174 |
ASSERT_THAT(Variants, ::testing::SizeIs(NumVariants)); |
| 175 |
ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants)); |
| 176 |
} |
| 177 |
|
| 178 |
} // end anonymous namespace |