• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- 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 
14 namespace llvm {
15 namespace exegesis {
16 
17 namespace {
18 
TEST(CombinationGenerator,Square)19 TEST(CombinationGenerator, Square) {
20   const std::vector<std::vector<int>> Choices{{0, 1}, {2, 3}};
21 
22   std::vector<std::vector<int>> Variants;
23   CombinationGenerator<int, std::vector<int>, 4> G(Choices);
24   const size_t NumVariants = G.numCombinations();
25   G.generate([&](ArrayRef<int> State) -> bool {
26     Variants.emplace_back(State);
27     return false; // keep going
28   });
29 
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 
TEST(CombinationGenerator,MiddleColumn)40 TEST(CombinationGenerator, MiddleColumn) {
41   const std::vector<std::vector<int>> Choices{{0}, {1, 2}, {3}};
42 
43   std::vector<std::vector<int>> Variants;
44   CombinationGenerator<int, std::vector<int>, 4> G(Choices);
45   const size_t NumVariants = G.numCombinations();
46   G.generate([&](ArrayRef<int> State) -> bool {
47     Variants.emplace_back(State);
48     return false; // keep going
49   });
50 
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 
TEST(CombinationGenerator,SideColumns)59 TEST(CombinationGenerator, SideColumns) {
60   const std::vector<std::vector<int>> Choices{{0, 1}, {2}, {3, 4}};
61 
62   std::vector<std::vector<int>> Variants;
63   CombinationGenerator<int, std::vector<int>, 4> G(Choices);
64   const size_t NumVariants = G.numCombinations();
65   G.generate([&](ArrayRef<int> State) -> bool {
66     Variants.emplace_back(State);
67     return false; // keep going
68   });
69 
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 
TEST(CombinationGenerator,LeftColumn)80 TEST(CombinationGenerator, LeftColumn) {
81   const std::vector<std::vector<int>> Choices{{0, 1}, {2}};
82 
83   std::vector<std::vector<int>> Variants;
84   CombinationGenerator<int, std::vector<int>, 4> G(Choices);
85   const size_t NumVariants = G.numCombinations();
86   G.generate([&](ArrayRef<int> State) -> bool {
87     Variants.emplace_back(State);
88     return false; // keep going
89   });
90 
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 
TEST(CombinationGenerator,RightColumn)99 TEST(CombinationGenerator, RightColumn) {
100   const std::vector<std::vector<int>> Choices{{0}, {1, 2}};
101 
102   std::vector<std::vector<int>> Variants;
103   CombinationGenerator<int, std::vector<int>, 4> G(Choices);
104   const size_t NumVariants = G.numCombinations();
105   G.generate([&](ArrayRef<int> State) -> bool {
106     Variants.emplace_back(State);
107     return false; // keep going
108   });
109 
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 
TEST(CombinationGenerator,Column)118 TEST(CombinationGenerator, Column) {
119   const std::vector<std::vector<int>> Choices{{0, 1}};
120 
121   std::vector<std::vector<int>> Variants;
122   CombinationGenerator<int, std::vector<int>, 4> G(Choices);
123   const size_t NumVariants = G.numCombinations();
124   G.generate([&](ArrayRef<int> State) -> bool {
125     Variants.emplace_back(State);
126     return false; // keep going
127   });
128 
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 
TEST(CombinationGenerator,Row)137 TEST(CombinationGenerator, Row) {
138   const std::vector<std::vector<int>> Choices{{0}, {1}};
139 
140   std::vector<std::vector<int>> Variants;
141   CombinationGenerator<int, std::vector<int>, 4> G(Choices);
142   const size_t NumVariants = G.numCombinations();
143   G.generate([&](ArrayRef<int> State) -> bool {
144     Variants.emplace_back(State);
145     return false; // keep going
146   });
147 
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 
TEST(CombinationGenerator,Singleton)155 TEST(CombinationGenerator, Singleton) {
156   const std::vector<std::vector<int>> Choices{{0}};
157 
158   std::vector<std::vector<int>> Variants;
159   CombinationGenerator<int, std::vector<int>, 4> G(Choices);
160   const size_t NumVariants = G.numCombinations();
161   G.generate([&](ArrayRef<int> State) -> bool {
162     Variants.emplace_back(State);
163     return false; // keep going
164   });
165 
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
176