1 //===- llvm/unittest/CodeGen/DwarfStringPoolEntryRefTest.cpp --------------===//
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 "llvm/CodeGen/DwarfStringPoolEntry.h"
10 #include "llvm/Support/Allocator.h"
11 #include "llvm/Testing/Support/Error.h"
13 #include "gmock/gmock.h"
14 #include "gtest/gtest.h"
19 TEST(DwarfStringPoolEntryRefTest
, TestFullEntry
) {
20 BumpPtrAllocator Allocator
;
21 StringMapEntry
<DwarfStringPoolEntry
> *StringEntry1
=
22 StringMapEntry
<DwarfStringPoolEntry
>::create(
23 "Key1", Allocator
, DwarfStringPoolEntry
{nullptr, 0, 0});
25 EXPECT_TRUE(StringEntry1
->getKey() == "Key1");
26 EXPECT_TRUE(StringEntry1
->second
.Symbol
== nullptr);
27 EXPECT_TRUE(StringEntry1
->second
.Offset
== 0);
28 EXPECT_TRUE(StringEntry1
->second
.Index
== 0);
30 DwarfStringPoolEntryRef
Ref1(*StringEntry1
);
31 EXPECT_TRUE(Ref1
.getString() == "Key1");
32 EXPECT_TRUE(Ref1
.getOffset() == 0);
33 EXPECT_TRUE(Ref1
.getIndex() == 0);
35 DwarfStringPoolEntryRef
Ref2(*StringEntry1
);
36 EXPECT_TRUE(Ref2
.getString() == "Key1");
37 EXPECT_TRUE(Ref2
.getOffset() == 0);
38 EXPECT_TRUE(Ref2
.getIndex() == 0);
39 EXPECT_TRUE(Ref1
== Ref2
);
40 EXPECT_FALSE(Ref1
!= Ref2
);
42 StringMapEntry
<DwarfStringPoolEntry
> *StringEntry2
=
43 StringMapEntry
<DwarfStringPoolEntry
>::create(
44 "Key2", Allocator
, DwarfStringPoolEntry
{nullptr, 0x1000, 1});
45 EXPECT_TRUE(StringEntry2
->getKey() == "Key2");
46 EXPECT_TRUE(StringEntry2
->second
.Symbol
== nullptr);
47 EXPECT_TRUE(StringEntry2
->second
.Offset
== 0x1000);
48 EXPECT_TRUE(StringEntry2
->second
.Index
== 1);
50 DwarfStringPoolEntryRef
Ref3(*StringEntry2
);
51 EXPECT_TRUE(Ref3
.getString() == "Key2");
52 EXPECT_TRUE(Ref3
.getOffset() == 0x1000);
53 EXPECT_TRUE(Ref3
.getIndex() == 1);
54 EXPECT_TRUE(Ref1
!= Ref3
);
57 bool isEntryEqual(const DwarfStringPoolEntry
&LHS
,
58 const DwarfStringPoolEntry
&RHS
) {
59 return LHS
.Symbol
== RHS
.Symbol
&& LHS
.Offset
== RHS
.Offset
&&
60 LHS
.Index
== RHS
.Index
;
63 TEST(DwarfStringPoolEntryRefTest
, TestShortEntry
) {
64 DwarfStringPoolEntryWithExtString DwarfEntry1
= {{nullptr, 0, 0}, "Key1"};
66 DwarfStringPoolEntryRef
Ref1(DwarfEntry1
);
67 EXPECT_TRUE(Ref1
.getString() == "Key1");
68 EXPECT_TRUE(Ref1
.getOffset() == 0);
69 EXPECT_TRUE(Ref1
.getIndex() == 0);
70 EXPECT_TRUE(isEntryEqual(Ref1
.getEntry(), DwarfEntry1
));
72 DwarfStringPoolEntryRef
Ref2(DwarfEntry1
);
73 EXPECT_TRUE(Ref2
.getString() == "Key1");
74 EXPECT_TRUE(Ref2
.getOffset() == 0);
75 EXPECT_TRUE(isEntryEqual(Ref2
.getEntry(), DwarfEntry1
));
76 EXPECT_TRUE(Ref1
== Ref2
);
77 EXPECT_FALSE(Ref1
!= Ref2
);
79 DwarfStringPoolEntryWithExtString DwarfEntry2
= {{nullptr, 0x1000, 1},
82 DwarfStringPoolEntryRef
Ref3(DwarfEntry2
);
83 EXPECT_TRUE(Ref3
.getString() == "Key2");
84 EXPECT_TRUE(Ref3
.getOffset() == 0x1000);
85 EXPECT_TRUE(Ref3
.getIndex() == 1);
86 EXPECT_TRUE(isEntryEqual(Ref3
.getEntry(), DwarfEntry2
));
87 EXPECT_TRUE(Ref1
!= Ref3
);
90 TEST(DwarfStringPoolEntryRefTest
, CompareFullAndShort
) {
91 BumpPtrAllocator Allocator
;
93 DwarfStringPoolEntryWithExtString DwarfEntry1
= {{nullptr, 0, 0}, "Key1"};
94 DwarfStringPoolEntryRef
Ref1(DwarfEntry1
);
96 StringMapEntry
<DwarfStringPoolEntry
> *StringEntry2
=
97 StringMapEntry
<DwarfStringPoolEntry
>::create(
98 "Key1", Allocator
, DwarfStringPoolEntry
{nullptr, 0, 0});
99 DwarfStringPoolEntryRef
Ref2(*StringEntry2
);
101 EXPECT_FALSE(Ref1
== Ref2
);