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

source code of mlir/unittests/IR/AdaptorTest.cpp