blob: 760caa8ce325a23bc04d1d9cdf5aaf9eea7d93b4 [file] [log] [blame]
Roman Lebedev6030fe02020-02-12 20:54:39 +03001//===-- SnippetGeneratorTest.cpp --------------------------------*- C++ -*-===//
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 "SnippetGenerator.h"
10#include "gmock/gmock.h"
11#include "gtest/gtest.h"
12#include <initializer_list>
13
14namespace llvm {
15namespace exegesis {
16
17namespace {
18
19TEST(CombinationGenerator, Square) {
20 const std::vector<std::vector<int>> Choices{{0, 1}, {2, 3}};
21
22 std::vector<std::vector<int>> Variants;
Roman Lebedev687bbf82020-02-12 23:30:22 +030023 CombinationGenerator<int, std::vector<int>, 4> G(Choices);
Roman Lebedev6030fe02020-02-12 20:54:39 +030024 const size_t NumVariants = G.numCombinations();
Roman Lebedev687bbf82020-02-12 23:30:22 +030025 G.generate([&](ArrayRef<int> State) -> bool {
26 Variants.emplace_back(State);
27 return false; // keep going
28 });
Roman Lebedev6030fe02020-02-12 20:54:39 +030029
30 const std::vector<std::vector<int>> ExpectedVariants{
31 {0, 2},
32 {0, 3},
33 {1, 2},
34 {1, 3},
35 };
36 ASSERT_THAT(Variants, ::testing::SizeIs(NumVariants));
37 ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants));
38}
39
40TEST(CombinationGenerator, MiddleColumn) {
41 const std::vector<std::vector<int>> Choices{{0}, {1, 2}, {3}};
42
43 std::vector<std::vector<int>> Variants;
Roman Lebedev687bbf82020-02-12 23:30:22 +030044 CombinationGenerator<int, std::vector<int>, 4> G(Choices);
Roman Lebedev6030fe02020-02-12 20:54:39 +030045 const size_t NumVariants = G.numCombinations();
Roman Lebedev687bbf82020-02-12 23:30:22 +030046 G.generate([&](ArrayRef<int> State) -> bool {
47 Variants.emplace_back(State);
48 return false; // keep going
49 });
Roman Lebedev6030fe02020-02-12 20:54:39 +030050
51 const std::vector<std::vector<int>> ExpectedVariants{
52 {0, 1, 3},
53 {0, 2, 3},
54 };
55 ASSERT_THAT(Variants, ::testing::SizeIs(NumVariants));
56 ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants));
57}
58
59TEST(CombinationGenerator, SideColumns) {
60 const std::vector<std::vector<int>> Choices{{0, 1}, {2}, {3, 4}};
61
62 std::vector<std::vector<int>> Variants;
Roman Lebedev687bbf82020-02-12 23:30:22 +030063 CombinationGenerator<int, std::vector<int>, 4> G(Choices);
Roman Lebedev6030fe02020-02-12 20:54:39 +030064 const size_t NumVariants = G.numCombinations();
Roman Lebedev687bbf82020-02-12 23:30:22 +030065 G.generate([&](ArrayRef<int> State) -> bool {
66 Variants.emplace_back(State);
67 return false; // keep going
68 });
Roman Lebedev6030fe02020-02-12 20:54:39 +030069
70 const std::vector<std::vector<int>> ExpectedVariants{
71 {0, 2, 3},
72 {0, 2, 4},
73 {1, 2, 3},
74 {1, 2, 4},
75 };
76 ASSERT_THAT(Variants, ::testing::SizeIs(NumVariants));
77 ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants));
78}
79
80TEST(CombinationGenerator, LeftColumn) {
81 const std::vector<std::vector<int>> Choices{{0, 1}, {2}};
82
83 std::vector<std::vector<int>> Variants;
Roman Lebedev687bbf82020-02-12 23:30:22 +030084 CombinationGenerator<int, std::vector<int>, 4> G(Choices);
Roman Lebedev6030fe02020-02-12 20:54:39 +030085 const size_t NumVariants = G.numCombinations();
Roman Lebedev687bbf82020-02-12 23:30:22 +030086 G.generate([&](ArrayRef<int> State) -> bool {
87 Variants.emplace_back(State);
88 return false; // keep going
89 });
Roman Lebedev6030fe02020-02-12 20:54:39 +030090
91 const std::vector<std::vector<int>> ExpectedVariants{
92 {0, 2},
93 {1, 2},
94 };
95 ASSERT_THAT(Variants, ::testing::SizeIs(NumVariants));
96 ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants));
97}
98
99TEST(CombinationGenerator, RightColumn) {
100 const std::vector<std::vector<int>> Choices{{0}, {1, 2}};
101
102 std::vector<std::vector<int>> Variants;
Roman Lebedev687bbf82020-02-12 23:30:22 +0300103 CombinationGenerator<int, std::vector<int>, 4> G(Choices);
Roman Lebedev6030fe02020-02-12 20:54:39 +0300104 const size_t NumVariants = G.numCombinations();
Roman Lebedev687bbf82020-02-12 23:30:22 +0300105 G.generate([&](ArrayRef<int> State) -> bool {
106 Variants.emplace_back(State);
107 return false; // keep going
108 });
Roman Lebedev6030fe02020-02-12 20:54:39 +0300109
110 const std::vector<std::vector<int>> ExpectedVariants{
111 {0, 1},
112 {0, 2},
113 };
114 ASSERT_THAT(Variants, ::testing::SizeIs(NumVariants));
115 ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants));
116}
117
118TEST(CombinationGenerator, Column) {
119 const std::vector<std::vector<int>> Choices{{0, 1}};
120
121 std::vector<std::vector<int>> Variants;
Roman Lebedev687bbf82020-02-12 23:30:22 +0300122 CombinationGenerator<int, std::vector<int>, 4> G(Choices);
Roman Lebedev6030fe02020-02-12 20:54:39 +0300123 const size_t NumVariants = G.numCombinations();
Roman Lebedev687bbf82020-02-12 23:30:22 +0300124 G.generate([&](ArrayRef<int> State) -> bool {
125 Variants.emplace_back(State);
126 return false; // keep going
127 });
Roman Lebedev6030fe02020-02-12 20:54:39 +0300128
129 const std::vector<std::vector<int>> ExpectedVariants{
130 {0},
131 {1},
132 };
133 ASSERT_THAT(Variants, ::testing::SizeIs(NumVariants));
134 ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants));
135}
136
137TEST(CombinationGenerator, Row) {
138 const std::vector<std::vector<int>> Choices{{0}, {1}};
139
140 std::vector<std::vector<int>> Variants;
Roman Lebedev687bbf82020-02-12 23:30:22 +0300141 CombinationGenerator<int, std::vector<int>, 4> G(Choices);
Roman Lebedev6030fe02020-02-12 20:54:39 +0300142 const size_t NumVariants = G.numCombinations();
Roman Lebedev687bbf82020-02-12 23:30:22 +0300143 G.generate([&](ArrayRef<int> State) -> bool {
144 Variants.emplace_back(State);
145 return false; // keep going
146 });
Roman Lebedev6030fe02020-02-12 20:54:39 +0300147
148 const std::vector<std::vector<int>> ExpectedVariants{
149 {0, 1},
150 };
151 ASSERT_THAT(Variants, ::testing::SizeIs(NumVariants));
152 ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants));
153}
154
155TEST(CombinationGenerator, Singleton) {
156 const std::vector<std::vector<int>> Choices{{0}};
157
158 std::vector<std::vector<int>> Variants;
Roman Lebedev687bbf82020-02-12 23:30:22 +0300159 CombinationGenerator<int, std::vector<int>, 4> G(Choices);
Roman Lebedev6030fe02020-02-12 20:54:39 +0300160 const size_t NumVariants = G.numCombinations();
Roman Lebedev687bbf82020-02-12 23:30:22 +0300161 G.generate([&](ArrayRef<int> State) -> bool {
162 Variants.emplace_back(State);
163 return false; // keep going
164 });
Roman Lebedev6030fe02020-02-12 20:54:39 +0300165
166 const std::vector<std::vector<int>> ExpectedVariants{
167 {0},
168 };
169 ASSERT_THAT(Variants, ::testing::SizeIs(NumVariants));
170 ASSERT_THAT(Variants, ::testing::ContainerEq(ExpectedVariants));
171}
172
173} // namespace
174} // namespace exegesis
175} // namespace llvm