1 //===- EnumsGenTest.cpp - TableGen EnumsGen Tests -------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "mlir/IR/BuiltinAttributes.h"
10 #include "mlir/IR/BuiltinTypes.h"
11 #include "mlir/IR/MLIRContext.h"
12 #include "mlir/Support/LLVM.h"
14 #include "llvm/ADT/DenseMap.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/ADT/StringSwitch.h"
18 #include "gmock/gmock.h"
20 #include <type_traits>
22 /// Pull in generated enum utility declarations and definitions.
23 #include "EnumsGenTest.h.inc"
25 #include "EnumsGenTest.cpp.inc"
27 /// Test namespaces and enum class/utility names.
28 using Outer::Inner::ConvertToEnum
;
29 using Outer::Inner::ConvertToString
;
30 using Outer::Inner::StrEnum
;
31 using Outer::Inner::StrEnumAttr
;
33 TEST(EnumsGenTest
, GeneratedStrEnumDefinition
) {
34 EXPECT_EQ(0u, static_cast<uint64_t>(StrEnum::CaseA
));
35 EXPECT_EQ(10u, static_cast<uint64_t>(StrEnum::CaseB
));
38 TEST(EnumsGenTest
, GeneratedI32EnumDefinition
) {
39 EXPECT_EQ(5u, static_cast<uint64_t>(I32Enum::Case5
));
40 EXPECT_EQ(10u, static_cast<uint64_t>(I32Enum::Case10
));
43 TEST(EnumsGenTest
, GeneratedDenseMapInfo
) {
44 llvm::DenseMap
<StrEnum
, std::string
> myMap
;
46 myMap
[StrEnum::CaseA
] = "zero";
47 myMap
[StrEnum::CaseB
] = "one";
49 EXPECT_EQ(myMap
[StrEnum::CaseA
], "zero");
50 EXPECT_EQ(myMap
[StrEnum::CaseB
], "one");
53 TEST(EnumsGenTest
, GeneratedSymbolToStringFn
) {
54 EXPECT_EQ(ConvertToString(StrEnum::CaseA
), "CaseA");
55 EXPECT_EQ(ConvertToString(StrEnum::CaseB
), "CaseB");
58 TEST(EnumsGenTest
, GeneratedStringToSymbolFn
) {
59 EXPECT_EQ(llvm::Optional
<StrEnum
>(StrEnum::CaseA
), ConvertToEnum("CaseA"));
60 EXPECT_EQ(llvm::Optional
<StrEnum
>(StrEnum::CaseB
), ConvertToEnum("CaseB"));
61 EXPECT_EQ(llvm::None
, ConvertToEnum("X"));
64 TEST(EnumsGenTest
, GeneratedUnderlyingType
) {
65 bool v
= std::is_same
<uint32_t, std::underlying_type
<I32Enum
>::type
>::value
;
69 TEST(EnumsGenTest
, GeneratedBitEnumDefinition
) {
70 EXPECT_EQ(0u, static_cast<uint32_t>(BitEnumWithNone::None
));
71 EXPECT_EQ(1u, static_cast<uint32_t>(BitEnumWithNone::Bit0
));
72 EXPECT_EQ(8u, static_cast<uint32_t>(BitEnumWithNone::Bit3
));
75 TEST(EnumsGenTest
, GeneratedSymbolToStringFnForBitEnum
) {
76 EXPECT_EQ(stringifyBitEnumWithNone(BitEnumWithNone::None
), "None");
77 EXPECT_EQ(stringifyBitEnumWithNone(BitEnumWithNone::Bit0
), "Bit0");
78 EXPECT_EQ(stringifyBitEnumWithNone(BitEnumWithNone::Bit3
), "Bit3");
80 stringifyBitEnumWithNone(BitEnumWithNone::Bit0
| BitEnumWithNone::Bit3
),
82 EXPECT_EQ(2u, static_cast<uint64_t>(BitEnum64_Test::Bit1
));
83 EXPECT_EQ(144115188075855872u, static_cast<uint64_t>(BitEnum64_Test::Bit57
));
86 TEST(EnumsGenTest
, GeneratedStringToSymbolForBitEnum
) {
87 EXPECT_EQ(symbolizeBitEnumWithNone("None"), BitEnumWithNone::None
);
88 EXPECT_EQ(symbolizeBitEnumWithNone("Bit0"), BitEnumWithNone::Bit0
);
89 EXPECT_EQ(symbolizeBitEnumWithNone("Bit3"), BitEnumWithNone::Bit3
);
90 EXPECT_EQ(symbolizeBitEnumWithNone("Bit3|Bit0"),
91 BitEnumWithNone::Bit3
| BitEnumWithNone::Bit0
);
93 EXPECT_EQ(symbolizeBitEnumWithNone("Bit2"), llvm::None
);
94 EXPECT_EQ(symbolizeBitEnumWithNone("Bit3|Bit4"), llvm::None
);
96 EXPECT_EQ(symbolizeBitEnumWithoutNone("None"), llvm::None
);
99 TEST(EnumsGenTest
, GeneratedSymbolToStringFnForGroupedBitEnum
) {
100 EXPECT_EQ(stringifyBitEnumWithGroup(BitEnumWithGroup::Bit0
), "Bit0");
101 EXPECT_EQ(stringifyBitEnumWithGroup(BitEnumWithGroup::Bit3
), "Bit3");
102 EXPECT_EQ(stringifyBitEnumWithGroup(BitEnumWithGroup::Bits0To3
),
103 "Bit0|Bit1|Bit2|Bit3|Bits0To3");
104 EXPECT_EQ(stringifyBitEnumWithGroup(BitEnumWithGroup::Bit4
), "Bit4");
105 EXPECT_EQ(stringifyBitEnumWithGroup(
106 BitEnumWithGroup::Bit0
| BitEnumWithGroup::Bit1
|
107 BitEnumWithGroup::Bit2
| BitEnumWithGroup::Bit4
),
108 "Bit0|Bit1|Bit2|Bit4");
111 TEST(EnumsGenTest
, GeneratedStringToSymbolForGroupedBitEnum
) {
112 EXPECT_EQ(symbolizeBitEnumWithGroup("Bit0"), BitEnumWithGroup::Bit0
);
113 EXPECT_EQ(symbolizeBitEnumWithGroup("Bit3"), BitEnumWithGroup::Bit3
);
114 EXPECT_EQ(symbolizeBitEnumWithGroup("Bit5"), llvm::None
);
115 EXPECT_EQ(symbolizeBitEnumWithGroup("Bit3|Bit0"),
116 BitEnumWithGroup::Bit3
| BitEnumWithGroup::Bit0
);
119 TEST(EnumsGenTest
, GeneratedOperator
) {
120 EXPECT_TRUE(bitEnumContains(BitEnumWithNone::Bit0
| BitEnumWithNone::Bit3
,
121 BitEnumWithNone::Bit0
));
122 EXPECT_FALSE(bitEnumContains(BitEnumWithNone::Bit0
& BitEnumWithNone::Bit3
,
123 BitEnumWithNone::Bit0
));
126 TEST(EnumsGenTest
, GeneratedSymbolToCustomStringFn
) {
127 EXPECT_EQ(stringifyPrettyIntEnum(PrettyIntEnum::Case1
), "case_one");
128 EXPECT_EQ(stringifyPrettyIntEnum(PrettyIntEnum::Case2
), "case_two");
131 TEST(EnumsGenTest
, GeneratedCustomStringToSymbolFn
) {
132 auto one
= symbolizePrettyIntEnum("case_one");
134 EXPECT_EQ(*one
, PrettyIntEnum::Case1
);
136 auto two
= symbolizePrettyIntEnum("case_two");
138 EXPECT_EQ(*two
, PrettyIntEnum::Case2
);
140 auto none
= symbolizePrettyIntEnum("Case1");
144 TEST(EnumsGenTest
, GeneratedIntAttributeClass
) {
145 mlir::MLIRContext ctx
;
146 I32Enum rawVal
= I32Enum::Case5
;
148 I32EnumAttr enumAttr
= I32EnumAttr::get(&ctx
, rawVal
);
149 EXPECT_NE(enumAttr
, nullptr);
150 EXPECT_EQ(enumAttr
.getValue(), rawVal
);
152 mlir::Type intType
= mlir::IntegerType::get(&ctx
, 32);
153 mlir::Attribute intAttr
= mlir::IntegerAttr::get(intType
, 5);
154 EXPECT_TRUE(intAttr
.isa
<I32EnumAttr
>());
155 EXPECT_EQ(intAttr
, enumAttr
);
158 TEST(EnumsGenTest
, GeneratedStringAttributeClass
) {
159 mlir::MLIRContext ctx
;
160 StrEnum rawVal
= StrEnum::CaseA
;
162 StrEnumAttr enumAttr
= StrEnumAttr::get(&ctx
, rawVal
);
163 EXPECT_NE(enumAttr
, nullptr);
164 EXPECT_EQ(enumAttr
.getValue(), rawVal
);
166 mlir::Attribute strAttr
= mlir::StringAttr::get(&ctx
, "CaseA");
167 EXPECT_TRUE(strAttr
.isa
<StrEnumAttr
>());
168 EXPECT_EQ(strAttr
, enumAttr
);
171 TEST(EnumsGenTest
, GeneratedBitAttributeClass
) {
172 mlir::MLIRContext ctx
;
174 mlir::Type intType
= mlir::IntegerType::get(&ctx
, 32);
175 mlir::Attribute intAttr
= mlir::IntegerAttr::get(
177 static_cast<uint32_t>(BitEnumWithNone::Bit0
| BitEnumWithNone::Bit3
));
178 EXPECT_TRUE(intAttr
.isa
<BitEnumWithNoneAttr
>());
179 EXPECT_TRUE(intAttr
.isa
<BitEnumWithoutNoneAttr
>());
181 intAttr
= mlir::IntegerAttr::get(
182 intType
, static_cast<uint32_t>(BitEnumWithGroup::Bits0To3
) | (1u << 6));
183 EXPECT_FALSE(intAttr
.isa
<BitEnumWithGroupAttr
>());