Roman Lebedev | 6030fe0 | 2020-02-12 20:54:39 +0300 | [diff] [blame] | 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 | |
| 19 | TEST(CombinationGenerator, Square) { |
| 20 | const std::vector<std::vector<int>> Choices{{0, 1}, {2, 3}}; |
| 21 | |
| 22 | std::vector<std::vector<int>> Variants; |
Roman Lebedev | 687bbf8 | 2020-02-12 23:30:22 +0300 | [diff] [blame] | 23 | CombinationGenerator<int, std::vector<int>, 4> G(Choices); |
Roman Lebedev | 6030fe0 | 2020-02-12 20:54:39 +0300 | [diff] [blame] | 24 | const size_t NumVariants = G.numCombinations(); |
Roman Lebedev | 687bbf8 | 2020-02-12 23:30:22 +0300 | [diff] [blame] | 25 | G.generate([&](ArrayRef<int> State) -> bool { |
| 26 | Variants.emplace_back(State); |
| 27 | return false; // keep going |
| 28 | }); |
Roman Lebedev | 6030fe0 | 2020-02-12 20:54:39 +0300 | [diff] [blame] | 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 | |
| 40 | TEST(CombinationGenerator, MiddleColumn) { |
| 41 | const std::vector<std::vector<int>> Choices{{0}, {1, 2}, {3}}; |
| 42 | |
| 43 | std::vector<std::vector<int>> Variants; |
Roman Lebedev | 687bbf8 | 2020-02-12 23:30:22 +0300 | [diff] [blame] | 44 | CombinationGenerator<int, std::vector<int>, 4> G(Choices); |
Roman Lebedev | 6030fe0 | 2020-02-12 20:54:39 +0300 | [diff] [blame] | 45 | const size_t NumVariants = G.numCombinations(); |
Roman Lebedev | 687bbf8 | 2020-02-12 23:30:22 +0300 | [diff] [blame] | 46 | G.generate([&](ArrayRef<int> State) -> bool { |
| 47 | Variants.emplace_back(State); |
| 48 | return false; // keep going |
| 49 | }); |
Roman Lebedev | 6030fe0 | 2020-02-12 20:54:39 +0300 | [diff] [blame] | 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 | |
| 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; |
Roman Lebedev | 687bbf8 | 2020-02-12 23:30:22 +0300 | [diff] [blame] | 63 | CombinationGenerator<int, std::vector<int>, 4> G(Choices); |
Roman Lebedev | 6030fe0 | 2020-02-12 20:54:39 +0300 | [diff] [blame] | 64 | const size_t NumVariants = G.numCombinations(); |
Roman Lebedev | 687bbf8 | 2020-02-12 23:30:22 +0300 | [diff] [blame] | 65 | G.generate([&](ArrayRef<int> State) -> bool { |
| 66 | Variants.emplace_back(State); |
| 67 | return false; // keep going |
| 68 | }); |
Roman Lebedev | 6030fe0 | 2020-02-12 20:54:39 +0300 | [diff] [blame] | 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 | |
| 80 | TEST(CombinationGenerator, LeftColumn) { |
| 81 | const std::vector<std::vector<int>> Choices{{0, 1}, {2}}; |
| 82 | |
| 83 | std::vector<std::vector<int>> Variants; |
Roman Lebedev | 687bbf8 | 2020-02-12 23:30:22 +0300 | [diff] [blame] | 84 | CombinationGenerator<int, std::vector<int>, 4> G(Choices); |
Roman Lebedev | 6030fe0 | 2020-02-12 20:54:39 +0300 | [diff] [blame] | 85 | const size_t NumVariants = G.numCombinations(); |
Roman Lebedev | 687bbf8 | 2020-02-12 23:30:22 +0300 | [diff] [blame] | 86 | G.generate([&](ArrayRef<int> State) -> bool { |
| 87 | Variants.emplace_back(State); |
| 88 | return false; // keep going |
| 89 | }); |
Roman Lebedev | 6030fe0 | 2020-02-12 20:54:39 +0300 | [diff] [blame] | 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 | |
| 99 | TEST(CombinationGenerator, RightColumn) { |
| 100 | const std::vector<std::vector<int>> Choices{{0}, {1, 2}}; |
| 101 | |
| 102 | std::vector<std::vector<int>> Variants; |
Roman Lebedev | 687bbf8 | 2020-02-12 23:30:22 +0300 | [diff] [blame] | 103 | CombinationGenerator<int, std::vector<int>, 4> G(Choices); |
Roman Lebedev | 6030fe0 | 2020-02-12 20:54:39 +0300 | [diff] [blame] | 104 | const size_t NumVariants = G.numCombinations(); |
Roman Lebedev | 687bbf8 | 2020-02-12 23:30:22 +0300 | [diff] [blame] | 105 | G.generate([&](ArrayRef<int> State) -> bool { |
| 106 | Variants.emplace_back(State); |
| 107 | return false; // keep going |
| 108 | }); |
Roman Lebedev | 6030fe0 | 2020-02-12 20:54:39 +0300 | [diff] [blame] | 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 | |
| 118 | TEST(CombinationGenerator, Column) { |
| 119 | const std::vector<std::vector<int>> Choices{{0, 1}}; |
| 120 | |
| 121 | std::vector<std::vector<int>> Variants; |
Roman Lebedev | 687bbf8 | 2020-02-12 23:30:22 +0300 | [diff] [blame] | 122 | CombinationGenerator<int, std::vector<int>, 4> G(Choices); |
Roman Lebedev | 6030fe0 | 2020-02-12 20:54:39 +0300 | [diff] [blame] | 123 | const size_t NumVariants = G.numCombinations(); |
Roman Lebedev | 687bbf8 | 2020-02-12 23:30:22 +0300 | [diff] [blame] | 124 | G.generate([&](ArrayRef<int> State) -> bool { |
| 125 | Variants.emplace_back(State); |
| 126 | return false; // keep going |
| 127 | }); |
Roman Lebedev | 6030fe0 | 2020-02-12 20:54:39 +0300 | [diff] [blame] | 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 | |
| 137 | TEST(CombinationGenerator, Row) { |
| 138 | const std::vector<std::vector<int>> Choices{{0}, {1}}; |
| 139 | |
| 140 | std::vector<std::vector<int>> Variants; |
Roman Lebedev | 687bbf8 | 2020-02-12 23:30:22 +0300 | [diff] [blame] | 141 | CombinationGenerator<int, std::vector<int>, 4> G(Choices); |
Roman Lebedev | 6030fe0 | 2020-02-12 20:54:39 +0300 | [diff] [blame] | 142 | const size_t NumVariants = G.numCombinations(); |
Roman Lebedev | 687bbf8 | 2020-02-12 23:30:22 +0300 | [diff] [blame] | 143 | G.generate([&](ArrayRef<int> State) -> bool { |
| 144 | Variants.emplace_back(State); |
| 145 | return false; // keep going |
| 146 | }); |
Roman Lebedev | 6030fe0 | 2020-02-12 20:54:39 +0300 | [diff] [blame] | 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 | |
| 155 | TEST(CombinationGenerator, Singleton) { |
| 156 | const std::vector<std::vector<int>> Choices{{0}}; |
| 157 | |
| 158 | std::vector<std::vector<int>> Variants; |
Roman Lebedev | 687bbf8 | 2020-02-12 23:30:22 +0300 | [diff] [blame] | 159 | CombinationGenerator<int, std::vector<int>, 4> G(Choices); |
Roman Lebedev | 6030fe0 | 2020-02-12 20:54:39 +0300 | [diff] [blame] | 160 | const size_t NumVariants = G.numCombinations(); |
Roman Lebedev | 687bbf8 | 2020-02-12 23:30:22 +0300 | [diff] [blame] | 161 | G.generate([&](ArrayRef<int> State) -> bool { |
| 162 | Variants.emplace_back(State); |
| 163 | return false; // keep going |
| 164 | }); |
Roman Lebedev | 6030fe0 | 2020-02-12 20:54:39 +0300 | [diff] [blame] | 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 |