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