1 //===- EnumsGenTest.cpp - TableGen EnumsGen 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 "mlir/Support/LLVM.h"
10 #include "llvm/ADT/DenseMap.h"
11 #include "llvm/ADT/StringExtras.h"
12 #include "llvm/ADT/StringSwitch.h"
13 #include "gmock/gmock.h"
14 #include <type_traits>
15
16 /// Pull in generated enum utility declarations and definitions.
17 #include "EnumsGenTest.h.inc"
18 #include "EnumsGenTest.cpp.inc"
19
20 /// Test namespaces and enum class/utility names.
21 using Outer::Inner::ConvertToEnum;
22 using Outer::Inner::ConvertToString;
23 using Outer::Inner::StrEnum;
24
TEST(EnumsGenTest,GeneratedStrEnumDefinition)25 TEST(EnumsGenTest, GeneratedStrEnumDefinition) {
26 EXPECT_EQ(0u, static_cast<uint64_t>(StrEnum::CaseA));
27 EXPECT_EQ(10u, static_cast<uint64_t>(StrEnum::CaseB));
28 }
29
TEST(EnumsGenTest,GeneratedI32EnumDefinition)30 TEST(EnumsGenTest, GeneratedI32EnumDefinition) {
31 EXPECT_EQ(5u, static_cast<uint64_t>(I32Enum::Case5));
32 EXPECT_EQ(10u, static_cast<uint64_t>(I32Enum::Case10));
33 }
34
TEST(EnumsGenTest,GeneratedDenseMapInfo)35 TEST(EnumsGenTest, GeneratedDenseMapInfo) {
36 llvm::DenseMap<StrEnum, std::string> myMap;
37
38 myMap[StrEnum::CaseA] = "zero";
39 myMap[StrEnum::CaseB] = "one";
40
41 EXPECT_EQ(myMap[StrEnum::CaseA], "zero");
42 EXPECT_EQ(myMap[StrEnum::CaseB], "one");
43 }
44
TEST(EnumsGenTest,GeneratedSymbolToStringFn)45 TEST(EnumsGenTest, GeneratedSymbolToStringFn) {
46 EXPECT_EQ(ConvertToString(StrEnum::CaseA), "CaseA");
47 EXPECT_EQ(ConvertToString(StrEnum::CaseB), "CaseB");
48 }
49
TEST(EnumsGenTest,GeneratedStringToSymbolFn)50 TEST(EnumsGenTest, GeneratedStringToSymbolFn) {
51 EXPECT_EQ(llvm::Optional<StrEnum>(StrEnum::CaseA), ConvertToEnum("CaseA"));
52 EXPECT_EQ(llvm::Optional<StrEnum>(StrEnum::CaseB), ConvertToEnum("CaseB"));
53 EXPECT_EQ(llvm::None, ConvertToEnum("X"));
54 }
55
TEST(EnumsGenTest,GeneratedUnderlyingType)56 TEST(EnumsGenTest, GeneratedUnderlyingType) {
57 bool v = std::is_same<uint32_t, std::underlying_type<I32Enum>::type>::value;
58 EXPECT_TRUE(v);
59 }
60
TEST(EnumsGenTest,GeneratedBitEnumDefinition)61 TEST(EnumsGenTest, GeneratedBitEnumDefinition) {
62 EXPECT_EQ(0u, static_cast<uint32_t>(BitEnumWithNone::None));
63 EXPECT_EQ(1u, static_cast<uint32_t>(BitEnumWithNone::Bit1));
64 EXPECT_EQ(4u, static_cast<uint32_t>(BitEnumWithNone::Bit3));
65 }
66
TEST(EnumsGenTest,GeneratedSymbolToStringFnForBitEnum)67 TEST(EnumsGenTest, GeneratedSymbolToStringFnForBitEnum) {
68 EXPECT_EQ(stringifyBitEnumWithNone(BitEnumWithNone::None), "None");
69 EXPECT_EQ(stringifyBitEnumWithNone(BitEnumWithNone::Bit1), "Bit1");
70 EXPECT_EQ(stringifyBitEnumWithNone(BitEnumWithNone::Bit3), "Bit3");
71 EXPECT_EQ(
72 stringifyBitEnumWithNone(BitEnumWithNone::Bit1 | BitEnumWithNone::Bit3),
73 "Bit1|Bit3");
74 }
75
TEST(EnumsGenTest,GeneratedStringToSymbolForBitEnum)76 TEST(EnumsGenTest, GeneratedStringToSymbolForBitEnum) {
77 EXPECT_EQ(symbolizeBitEnumWithNone("None"), BitEnumWithNone::None);
78 EXPECT_EQ(symbolizeBitEnumWithNone("Bit1"), BitEnumWithNone::Bit1);
79 EXPECT_EQ(symbolizeBitEnumWithNone("Bit3"), BitEnumWithNone::Bit3);
80 EXPECT_EQ(symbolizeBitEnumWithNone("Bit3|Bit1"),
81 BitEnumWithNone::Bit3 | BitEnumWithNone::Bit1);
82
83 EXPECT_EQ(symbolizeBitEnumWithNone("Bit2"), llvm::None);
84 EXPECT_EQ(symbolizeBitEnumWithNone("Bit3|Bit4"), llvm::None);
85
86 EXPECT_EQ(symbolizeBitEnumWithoutNone("None"), llvm::None);
87 }
88
TEST(EnumsGenTest,GeneratedOperator)89 TEST(EnumsGenTest, GeneratedOperator) {
90 EXPECT_TRUE(bitEnumContains(BitEnumWithNone::Bit1 | BitEnumWithNone::Bit3,
91 BitEnumWithNone::Bit1));
92 EXPECT_FALSE(bitEnumContains(BitEnumWithNone::Bit1 & BitEnumWithNone::Bit3,
93 BitEnumWithNone::Bit1));
94 }
95
TEST(EnumsGenTest,GeneratedSymbolToCustomStringFn)96 TEST(EnumsGenTest, GeneratedSymbolToCustomStringFn) {
97 EXPECT_EQ(stringifyPrettyIntEnum(PrettyIntEnum::Case1), "case_one");
98 EXPECT_EQ(stringifyPrettyIntEnum(PrettyIntEnum::Case2), "case_two");
99 }
100
TEST(EnumsGenTest,GeneratedCustomStringToSymbolFn)101 TEST(EnumsGenTest, GeneratedCustomStringToSymbolFn) {
102 auto one = symbolizePrettyIntEnum("case_one");
103 EXPECT_TRUE(one);
104 EXPECT_EQ(*one, PrettyIntEnum::Case1);
105
106 auto two = symbolizePrettyIntEnum("case_two");
107 EXPECT_TRUE(two);
108 EXPECT_EQ(*two, PrettyIntEnum::Case2);
109
110 auto none = symbolizePrettyIntEnum("Case1");
111 EXPECT_FALSE(none);
112 }
113