1 | //===- AdaptorTest.cpp - Adaptor unit tests -------------------------------===// |
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 "../../test/lib/Dialect/Test/TestDialect.h" |
10 | #include "../../test/lib/Dialect/Test/TestOps.h" |
11 | #include "../../test/lib/Dialect/Test/TestOpsSyntax.h" |
12 | #include "gmock/gmock.h" |
13 | #include "gtest/gtest.h" |
14 | |
15 | using namespace llvm; |
16 | using namespace mlir; |
17 | using namespace test; |
18 | |
19 | using testing::ElementsAre; |
20 | |
21 | TEST(Adaptor, GenericAdaptorsOperandAccess) { |
22 | MLIRContext context; |
23 | context.loadDialect<test::TestDialect>(); |
24 | Builder builder(&context); |
25 | |
26 | // Has normal and Variadic arguments. |
27 | MixedNormalVariadicOperandOp::FoldAdaptor a({}); |
28 | { |
29 | SmallVector<int> v = {0, 1, 2, 3, 4}; |
30 | MixedNormalVariadicOperandOp::GenericAdaptor<ArrayRef<int>> b(v); |
31 | EXPECT_THAT(b.getInput1(), ElementsAre(0, 1)); |
32 | EXPECT_EQ(b.getInput2(), 2); |
33 | EXPECT_THAT(b.getInput3(), ElementsAre(3, 4)); |
34 | } |
35 | |
36 | // Has optional arguments. |
37 | OIListSimple::FoldAdaptor c({}, nullptr); |
38 | { |
39 | // Optional arguments return the default constructed value if not present. |
40 | // Using optional instead of plain int here to differentiate absence of |
41 | // value from the value 0. |
42 | SmallVector<std::optional<int>> v = {0, 4}; |
43 | OIListSimple::Properties prop; |
44 | prop.operandSegmentSizes = {1, 0, 1}; |
45 | OIListSimple::GenericAdaptor<ArrayRef<std::optional<int>>> d(v, {}, prop, |
46 | {}); |
47 | EXPECT_EQ(d.getArg0(), 0); |
48 | EXPECT_EQ(d.getArg1(), std::nullopt); |
49 | EXPECT_EQ(d.getArg2(), 4); |
50 | |
51 | // Check the property comparison operator. |
52 | OIListSimple::Properties equivalentProp = {1, 0, 1}; |
53 | OIListSimple::Properties differentProp = {0, 0, 1}; |
54 | EXPECT_EQ(d.getProperties(), equivalentProp); |
55 | EXPECT_NE(d.getProperties(), differentProp); |
56 | } |
57 | |
58 | // Has VariadicOfVariadic arguments. |
59 | FormatVariadicOfVariadicOperand::FoldAdaptor e({}); |
60 | { |
61 | SmallVector<int> v = {0, 1, 2, 3, 4}; |
62 | FormatVariadicOfVariadicOperand::Properties prop; |
63 | prop.operand_segments = builder.getDenseI32ArrayAttr({3, 2, 0}); |
64 | FormatVariadicOfVariadicOperand::GenericAdaptor<ArrayRef<int>> f(v, {}, |
65 | prop, {}); |
66 | SmallVector<ArrayRef<int>> operand = f.getOperand(); |
67 | ASSERT_EQ(operand.size(), (std::size_t)3); |
68 | EXPECT_THAT(operand[0], ElementsAre(0, 1, 2)); |
69 | EXPECT_THAT(operand[1], ElementsAre(3, 4)); |
70 | EXPECT_THAT(operand[2], ElementsAre()); |
71 | } |
72 | } |
73 | |