| 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/ADT/iterator_range.h" |
| 13 |
#include "llvm/Support/ErrorHandling.h" |
| 14 |
#include "gmock/gmock.h" |
| 15 |
#include "gtest/gtest.h" |
| 16 |
#include <algorithm> |
| 17 |
#include <cstddef> |
| 18 |
#include <iterator> |
| 19 |
#include <tuple> |
| 20 |
#include <vector> |
| 21 |
|
| 22 |
using namespace llvm; |
| 23 |
|
| 24 |
namespace { |
| 25 |
|
| 26 |
TEST(CombinationGenerator, Square) { |
| 27 |
const std::vector<std::vector<int>> Choices{{0, 1}, {2, 3}}; |
| 28 |
|
| 29 |
std::vector<std::vector<int>> Variants; |
| 30 |
CombinationGenerator<int, std::vector<int>, 4> G(Choices); |
| 31 |
const size_t NumVariants = G.numCombinations(); |
| 32 |
G.generate([&](ArrayRef<int> State) -> bool { |
| 33 |
Variants.emplace_back(State); |
| 34 |
return false; // keep going |
| 35 |
}); |
| 36 |
|
| 37 |
const std::vector<std::vector<int>> ExpectedVariants{ |
| 38 |
{0, 2}, |
| 39 |
{0, 3}, |
| 40 |
{1, 2}, |
| 41 |
{1, 3}, |
| 42 |
}; |
| 43 |
ASSERT_THAT(Variants, ::testing::SizeIs(NumVariants)); |
| 44 |
ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants)); |
| 45 |
} |
| 46 |
|
| 47 |
TEST(CombinationGenerator, MiddleColumn) { |
| 48 |
const std::vector<std::vector<int>> Choices{{0}, {1, 2}, {3}}; |
| 49 |
|
| 50 |
std::vector<std::vector<int>> Variants; |
| 51 |
CombinationGenerator<int, std::vector<int>, 4> G(Choices); |
| 52 |
const size_t NumVariants = G.numCombinations(); |
| 53 |
G.generate([&](ArrayRef<int> State) -> bool { |
| 54 |
Variants.emplace_back(State); |
| 55 |
return false; // keep going |
| 56 |
}); |
| 57 |
|
| 58 |
const std::vector<std::vector<int>> ExpectedVariants{ |
| 59 |
{0, 1, 3}, |
| 60 |
{0, 2, 3}, |
| 61 |
}; |
| 62 |
ASSERT_THAT(Variants, ::testing::SizeIs(NumVariants)); |
| 63 |
ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants)); |
| 64 |
} |
| 65 |
|
| 66 |
TEST(CombinationGenerator, SideColumns) { |
| 67 |
const std::vector<std::vector<int>> Choices{{0, 1}, {2}, {3, 4}}; |
| 68 |
|
| 69 |
std::vector<std::vector<int>> Variants; |
| 70 |
CombinationGenerator<int, std::vector<int>, 4> G(Choices); |
| 71 |
const size_t NumVariants = G.numCombinations(); |
| 72 |
G.generate([&](ArrayRef<int> State) -> bool { |
| 73 |
Variants.emplace_back(State); |
| 74 |
return false; // keep going |
| 75 |
}); |
| 76 |
|
| 77 |
const std::vector<std::vector<int>> ExpectedVariants{ |
| 78 |
{0, 2, 3}, |
| 79 |
{0, 2, 4}, |
| 80 |
{1, 2, 3}, |
| 81 |
{1, 2, 4}, |
| 82 |
}; |
| 83 |
ASSERT_THAT(Variants, ::testing::SizeIs(NumVariants)); |
| 84 |
ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants)); |
| 85 |
} |
| 86 |
|
| 87 |
TEST(CombinationGenerator, LeftColumn) { |
| 88 |
const std::vector<std::vector<int>> Choices{{0, 1}, {2}}; |
| 89 |
|
| 90 |
std::vector<std::vector<int>> Variants; |
| 91 |
CombinationGenerator<int, std::vector<int>, 4> G(Choices); |
| 92 |
const size_t NumVariants = G.numCombinations(); |
| 93 |
G.generate([&](ArrayRef<int> State) -> bool { |
| 94 |
Variants.emplace_back(State); |
| 95 |
return false; // keep going |
| 96 |
}); |
| 97 |
|
| 98 |
const std::vector<std::vector<int>> ExpectedVariants{ |
| 99 |
{0, 2}, |
| 100 |
{1, 2}, |
| 101 |
}; |
| 102 |
ASSERT_THAT(Variants, ::testing::SizeIs(NumVariants)); |
| 103 |
ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants)); |
| 104 |
} |
| 105 |
|
| 106 |
TEST(CombinationGenerator, RightColumn) { |
| 107 |
const std::vector<std::vector<int>> Choices{{0}, {1, 2}}; |
| 108 |
|
| 109 |
std::vector<std::vector<int>> Variants; |
| 110 |
CombinationGenerator<int, std::vector<int>, 4> G(Choices); |
| 111 |
const size_t NumVariants = G.numCombinations(); |
| 112 |
G.generate([&](ArrayRef<int> State) -> bool { |
| 113 |
Variants.emplace_back(State); |
| 114 |
return false; // keep going |
| 115 |
}); |
| 116 |
|
| 117 |
const std::vector<std::vector<int>> ExpectedVariants{ |
| 118 |
{0, 1}, |
| 119 |
{0, 2}, |
| 120 |
}; |
| 121 |
ASSERT_THAT(Variants, ::testing::SizeIs(NumVariants)); |
| 122 |
ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants)); |
| 123 |
} |
| 124 |
|
| 125 |
TEST(CombinationGenerator, Column) { |
| 126 |
const std::vector<std::vector<int>> Choices{{0, 1}}; |
| 127 |
|
| 128 |
std::vector<std::vector<int>> Variants; |
| 129 |
CombinationGenerator<int, std::vector<int>, 4> G(Choices); |
| 130 |
const size_t NumVariants = G.numCombinations(); |
| 131 |
G.generate([&](ArrayRef<int> State) -> bool { |
| 132 |
Variants.emplace_back(State); |
| 133 |
return false; // keep going |
| 134 |
}); |
| 135 |
|
| 136 |
const std::vector<std::vector<int>> ExpectedVariants{ |
| 137 |
{0}, |
| 138 |
{1}, |
| 139 |
}; |
| 140 |
ASSERT_THAT(Variants, ::testing::SizeIs(NumVariants)); |
| 141 |
ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants)); |
| 142 |
} |
| 143 |
|
| 144 |
TEST(CombinationGenerator, Row) { |
| 145 |
const std::vector<std::vector<int>> Choices{{0}, {1}}; |
| 146 |
|
| 147 |
std::vector<std::vector<int>> Variants; |
| 148 |
CombinationGenerator<int, std::vector<int>, 4> G(Choices); |
| 149 |
const size_t NumVariants = G.numCombinations(); |
| 150 |
G.generate([&](ArrayRef<int> State) -> bool { |
| 151 |
Variants.emplace_back(State); |
| 152 |
return false; // keep going |
| 153 |
}); |
| 154 |
|
| 155 |
const std::vector<std::vector<int>> ExpectedVariants{ |
| 156 |
{0, 1}, |
| 157 |
}; |
| 158 |
ASSERT_THAT(Variants, ::testing::SizeIs(NumVariants)); |
| 159 |
ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants)); |
| 160 |
} |
| 161 |
|
| 162 |
TEST(CombinationGenerator, Singleton) { |
| 163 |
const std::vector<std::vector<int>> Choices{{0}}; |
| 164 |
|
| 165 |
std::vector<std::vector<int>> Variants; |
| 166 |
CombinationGenerator<int, std::vector<int>, 4> G(Choices); |
| 167 |
const size_t NumVariants = G.numCombinations(); |
| 168 |
G.generate([&](ArrayRef<int> State) -> bool { |
| 169 |
Variants.emplace_back(State); |
| 170 |
return false; // keep going |
| 171 |
}); |
| 172 |
|
| 173 |
const std::vector<std::vector<int>> ExpectedVariants{ |
| 174 |
{0}, |
| 175 |
}; |
| 176 |
ASSERT_THAT(Variants, ::testing::SizeIs(NumVariants)); |
| 177 |
ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants)); |
| 178 |
} |
| 179 |
|
| 180 |
} // end anonymous namespace |