1 | //===- KindMappingTest.cpp ------------------------------------------------===// |
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 "flang/Optimizer/Dialect/Support/KindMapping.h" |
10 | #include "gtest/gtest.h" |
11 | #include <string> |
12 | |
13 | using namespace fir; |
14 | namespace llvm { |
15 | struct fltSemantics; |
16 | } // namespace llvm |
17 | |
18 | namespace mlir { |
19 | class MLIRContext; |
20 | } // namespace mlir |
21 | |
22 | using Bitsize = fir::KindMapping::Bitsize; |
23 | using LLVMTypeID = fir::KindMapping::LLVMTypeID; |
24 | |
25 | struct DefaultStringTests : public testing::Test { |
26 | public: |
27 | void SetUp() { defaultString = new KindMapping(context); } |
28 | void TearDown() { delete defaultString; } |
29 | |
30 | KindMapping *defaultString{}; |
31 | mlir::MLIRContext *context{}; |
32 | }; |
33 | |
34 | struct CommandLineStringTests : public testing::Test { |
35 | public: |
36 | void SetUp() { |
37 | commandLineString = new KindMapping(context, |
38 | "i10:80,l3:24,a1:8,r54:Double,c20:X86_FP80,r11:PPC_FP128," |
39 | "r12:FP128,r13:X86_FP80,r14:Double,r15:Float,r16:Half,r23:BFloat" ); |
40 | clStringConflict = |
41 | new KindMapping(context, "i10:80,i10:40,r54:Double,r54:X86_FP80" ); |
42 | } |
43 | void TearDown() { |
44 | delete commandLineString; |
45 | delete clStringConflict; |
46 | } |
47 | |
48 | KindMapping *commandLineString{}; |
49 | KindMapping *clStringConflict{}; |
50 | mlir::MLIRContext *context{}; |
51 | }; |
52 | |
53 | struct KindDefaultsTests : public testing::Test { |
54 | public: |
55 | void SetUp() { |
56 | defaultDefaultKinds = new KindMapping(context); |
57 | overrideDefaultKinds = |
58 | new KindMapping(context, {20, 121, 32, 133, 44, 145}); |
59 | } |
60 | void TearDown() { |
61 | delete defaultDefaultKinds; |
62 | delete overrideDefaultKinds; |
63 | } |
64 | |
65 | mlir::MLIRContext *context{}; |
66 | KindMapping *defaultDefaultKinds{}; |
67 | KindMapping *overrideDefaultKinds{}; |
68 | }; |
69 | |
70 | TEST_F(DefaultStringTests, getIntegerBitsizeTest) { |
71 | EXPECT_EQ(defaultString->getIntegerBitsize(10), 80u); |
72 | EXPECT_EQ(defaultString->getIntegerBitsize(0), 0u); |
73 | } |
74 | |
75 | TEST_F(DefaultStringTests, getCharacterBitsizeTest) { |
76 | EXPECT_EQ(defaultString->getCharacterBitsize(10), 80u); |
77 | EXPECT_EQ(defaultString->getCharacterBitsize(0), 0u); |
78 | } |
79 | |
80 | TEST_F(DefaultStringTests, getLogicalBitsizeTest) { |
81 | EXPECT_EQ(defaultString->getLogicalBitsize(10), 80u); |
82 | // Unsigned values are expected |
83 | std::string actual = std::to_string(defaultString->getLogicalBitsize(-10)); |
84 | std::string expect = "-80" ; |
85 | EXPECT_NE(actual, expect); |
86 | } |
87 | |
88 | TEST_F(DefaultStringTests, getRealTypeIDTest) { |
89 | EXPECT_EQ(defaultString->getRealTypeID(2), LLVMTypeID::HalfTyID); |
90 | EXPECT_EQ(defaultString->getRealTypeID(3), LLVMTypeID::BFloatTyID); |
91 | EXPECT_EQ(defaultString->getRealTypeID(4), LLVMTypeID::FloatTyID); |
92 | EXPECT_EQ(defaultString->getRealTypeID(8), LLVMTypeID::DoubleTyID); |
93 | EXPECT_EQ(defaultString->getRealTypeID(10), LLVMTypeID::X86_FP80TyID); |
94 | EXPECT_EQ(defaultString->getRealTypeID(16), LLVMTypeID::FP128TyID); |
95 | // Default cases |
96 | EXPECT_EQ(defaultString->getRealTypeID(-1), LLVMTypeID::FloatTyID); |
97 | EXPECT_EQ(defaultString->getRealTypeID(1), LLVMTypeID::FloatTyID); |
98 | } |
99 | |
100 | TEST_F(DefaultStringTests, getComplexTypeIDTest) { |
101 | EXPECT_EQ(defaultString->getComplexTypeID(2), LLVMTypeID::HalfTyID); |
102 | EXPECT_EQ(defaultString->getComplexTypeID(3), LLVMTypeID::BFloatTyID); |
103 | EXPECT_EQ(defaultString->getComplexTypeID(4), LLVMTypeID::FloatTyID); |
104 | EXPECT_EQ(defaultString->getComplexTypeID(8), LLVMTypeID::DoubleTyID); |
105 | EXPECT_EQ(defaultString->getComplexTypeID(10), LLVMTypeID::X86_FP80TyID); |
106 | EXPECT_EQ(defaultString->getComplexTypeID(16), LLVMTypeID::FP128TyID); |
107 | // Default cases |
108 | EXPECT_EQ(defaultString->getComplexTypeID(-1), LLVMTypeID::FloatTyID); |
109 | EXPECT_EQ(defaultString->getComplexTypeID(1), LLVMTypeID::FloatTyID); |
110 | } |
111 | |
112 | TEST_F(DefaultStringTests, getFloatSemanticsTest) { |
113 | EXPECT_EQ(&defaultString->getFloatSemantics(2), &llvm::APFloat::IEEEhalf()); |
114 | EXPECT_EQ(&defaultString->getFloatSemantics(3), &llvm::APFloat::BFloat()); |
115 | EXPECT_EQ(&defaultString->getFloatSemantics(4), &llvm::APFloat::IEEEsingle()); |
116 | EXPECT_EQ(&defaultString->getFloatSemantics(8), &llvm::APFloat::IEEEdouble()); |
117 | EXPECT_EQ(&defaultString->getFloatSemantics(10), |
118 | &llvm::APFloat::x87DoubleExtended()); |
119 | EXPECT_EQ(&defaultString->getFloatSemantics(16), &llvm::APFloat::IEEEquad()); |
120 | |
121 | // Default cases |
122 | EXPECT_EQ( |
123 | &defaultString->getFloatSemantics(-1), &llvm::APFloat::IEEEsingle()); |
124 | EXPECT_EQ(&defaultString->getFloatSemantics(1), &llvm::APFloat::IEEEsingle()); |
125 | } |
126 | |
127 | TEST_F(CommandLineStringTests, getIntegerBitsizeTest) { |
128 | // KEY is present in map. |
129 | EXPECT_EQ(commandLineString->getIntegerBitsize(10), 80u); |
130 | EXPECT_EQ(commandLineString->getCharacterBitsize(1), 8u); |
131 | EXPECT_EQ(commandLineString->getLogicalBitsize(3), 24u); |
132 | EXPECT_EQ(commandLineString->getComplexTypeID(20), LLVMTypeID::X86_FP80TyID); |
133 | EXPECT_EQ(commandLineString->getRealTypeID(54), LLVMTypeID::DoubleTyID); |
134 | EXPECT_EQ(commandLineString->getRealTypeID(11), LLVMTypeID::PPC_FP128TyID); |
135 | EXPECT_EQ(&commandLineString->getFloatSemantics(11), |
136 | &llvm::APFloat::PPCDoubleDouble()); |
137 | EXPECT_EQ( |
138 | &commandLineString->getFloatSemantics(12), &llvm::APFloat::IEEEquad()); |
139 | EXPECT_EQ(&commandLineString->getFloatSemantics(13), |
140 | &llvm::APFloat::x87DoubleExtended()); |
141 | EXPECT_EQ( |
142 | &commandLineString->getFloatSemantics(14), &llvm::APFloat::IEEEdouble()); |
143 | EXPECT_EQ( |
144 | &commandLineString->getFloatSemantics(15), &llvm::APFloat::IEEEsingle()); |
145 | EXPECT_EQ( |
146 | &commandLineString->getFloatSemantics(16), &llvm::APFloat::IEEEhalf()); |
147 | EXPECT_EQ( |
148 | &commandLineString->getFloatSemantics(23), &llvm::APFloat::BFloat()); |
149 | |
150 | // Converts to default case |
151 | EXPECT_EQ( |
152 | &commandLineString->getFloatSemantics(20), &llvm::APFloat::IEEEsingle()); |
153 | |
154 | // KEY is absent from map, Default values are expected. |
155 | EXPECT_EQ(commandLineString->getIntegerBitsize(9), 72u); |
156 | EXPECT_EQ(commandLineString->getCharacterBitsize(9), 72u); |
157 | EXPECT_EQ(commandLineString->getLogicalBitsize(9), 72u); |
158 | EXPECT_EQ(commandLineString->getComplexTypeID(9), LLVMTypeID::FloatTyID); |
159 | EXPECT_EQ(commandLineString->getRealTypeID(9), LLVMTypeID::FloatTyID); |
160 | |
161 | // KEY repeats in map. |
162 | EXPECT_NE(clStringConflict->getIntegerBitsize(10), 80u); |
163 | EXPECT_NE(clStringConflict->getRealTypeID(10), LLVMTypeID::DoubleTyID); |
164 | } |
165 | |
166 | TEST(KindMappingDeathTests, mapTest) { |
167 | mlir::MLIRContext *context{}; |
168 | // Catch parsing errors |
169 | ASSERT_DEATH(new KindMapping(context, "r10:Double,r20:Doubl" ), "" ); |
170 | ASSERT_DEATH(new KindMapping(context, "10:Double" ), "" ); |
171 | ASSERT_DEATH(new KindMapping(context, "rr:Double" ), "" ); |
172 | ASSERT_DEATH(new KindMapping(context, "rr:" ), "" ); |
173 | ASSERT_DEATH(new KindMapping(context, "rr:Double MoreContent" ), "" ); |
174 | // length of 'size' > 10 |
175 | ASSERT_DEATH(new KindMapping(context, "i11111111111:10" ), "" ); |
176 | } |
177 | |
178 | TEST_F(KindDefaultsTests, getIntegerBitsizeTest) { |
179 | EXPECT_EQ(defaultDefaultKinds->defaultCharacterKind(), 1u); |
180 | EXPECT_EQ(defaultDefaultKinds->defaultComplexKind(), 4u); |
181 | EXPECT_EQ(defaultDefaultKinds->defaultDoubleKind(), 8u); |
182 | EXPECT_EQ(defaultDefaultKinds->defaultIntegerKind(), 4u); |
183 | EXPECT_EQ(defaultDefaultKinds->defaultLogicalKind(), 4u); |
184 | EXPECT_EQ(defaultDefaultKinds->defaultRealKind(), 4u); |
185 | |
186 | EXPECT_EQ(overrideDefaultKinds->defaultCharacterKind(), 20u); |
187 | EXPECT_EQ(overrideDefaultKinds->defaultComplexKind(), 121u); |
188 | EXPECT_EQ(overrideDefaultKinds->defaultDoubleKind(), 32u); |
189 | EXPECT_EQ(overrideDefaultKinds->defaultIntegerKind(), 133u); |
190 | EXPECT_EQ(overrideDefaultKinds->defaultLogicalKind(), 44u); |
191 | EXPECT_EQ(overrideDefaultKinds->defaultRealKind(), 145u); |
192 | } |
193 | |
194 | // main() from gtest_main |
195 | |