1554aea52SAlexey Lapshin //===- llvm/unittest/CodeGen/DwarfStringPoolEntryRefTest.cpp --------------===//
2554aea52SAlexey Lapshin //
3554aea52SAlexey Lapshin // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4554aea52SAlexey Lapshin // See https://llvm.org/LICENSE.txt for license information.
5554aea52SAlexey Lapshin // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6554aea52SAlexey Lapshin //
7554aea52SAlexey Lapshin //===----------------------------------------------------------------------===//
8554aea52SAlexey Lapshin
9554aea52SAlexey Lapshin #include "llvm/CodeGen/DwarfStringPoolEntry.h"
10554aea52SAlexey Lapshin #include "llvm/Support/Allocator.h"
11554aea52SAlexey Lapshin #include "llvm/Testing/Support/Error.h"
12554aea52SAlexey Lapshin
13554aea52SAlexey Lapshin #include "gmock/gmock.h"
14554aea52SAlexey Lapshin #include "gtest/gtest.h"
15554aea52SAlexey Lapshin
16554aea52SAlexey Lapshin using namespace llvm;
17554aea52SAlexey Lapshin
TEST(DwarfStringPoolEntryRefTest,TestFullEntry)18554aea52SAlexey Lapshin TEST(DwarfStringPoolEntryRefTest, TestFullEntry) {
19554aea52SAlexey Lapshin BumpPtrAllocator Allocator;
20554aea52SAlexey Lapshin StringMapEntry<DwarfStringPoolEntry> *StringEntry1 =
213284adf6SAlexey Lapshin StringMapEntry<DwarfStringPoolEntry>::create(
22554aea52SAlexey Lapshin "Key1", Allocator, DwarfStringPoolEntry{nullptr, 0, 0});
23554aea52SAlexey Lapshin
24554aea52SAlexey Lapshin EXPECT_TRUE(StringEntry1->getKey() == "Key1");
25554aea52SAlexey Lapshin EXPECT_TRUE(StringEntry1->second.Symbol == nullptr);
26554aea52SAlexey Lapshin EXPECT_TRUE(StringEntry1->second.Offset == 0);
27554aea52SAlexey Lapshin EXPECT_TRUE(StringEntry1->second.Index == 0);
28554aea52SAlexey Lapshin
29554aea52SAlexey Lapshin DwarfStringPoolEntryRef Ref1(*StringEntry1);
30554aea52SAlexey Lapshin EXPECT_TRUE(Ref1.getString() == "Key1");
31554aea52SAlexey Lapshin EXPECT_TRUE(Ref1.getOffset() == 0);
32554aea52SAlexey Lapshin EXPECT_TRUE(Ref1.getIndex() == 0);
33554aea52SAlexey Lapshin
34554aea52SAlexey Lapshin DwarfStringPoolEntryRef Ref2(*StringEntry1);
35554aea52SAlexey Lapshin EXPECT_TRUE(Ref2.getString() == "Key1");
36554aea52SAlexey Lapshin EXPECT_TRUE(Ref2.getOffset() == 0);
37554aea52SAlexey Lapshin EXPECT_TRUE(Ref2.getIndex() == 0);
38554aea52SAlexey Lapshin EXPECT_TRUE(Ref1 == Ref2);
39554aea52SAlexey Lapshin EXPECT_FALSE(Ref1 != Ref2);
40554aea52SAlexey Lapshin
41554aea52SAlexey Lapshin StringMapEntry<DwarfStringPoolEntry> *StringEntry2 =
423284adf6SAlexey Lapshin StringMapEntry<DwarfStringPoolEntry>::create(
43554aea52SAlexey Lapshin "Key2", Allocator, DwarfStringPoolEntry{nullptr, 0x1000, 1});
44554aea52SAlexey Lapshin EXPECT_TRUE(StringEntry2->getKey() == "Key2");
45554aea52SAlexey Lapshin EXPECT_TRUE(StringEntry2->second.Symbol == nullptr);
46554aea52SAlexey Lapshin EXPECT_TRUE(StringEntry2->second.Offset == 0x1000);
47554aea52SAlexey Lapshin EXPECT_TRUE(StringEntry2->second.Index == 1);
48554aea52SAlexey Lapshin
49554aea52SAlexey Lapshin DwarfStringPoolEntryRef Ref3(*StringEntry2);
50554aea52SAlexey Lapshin EXPECT_TRUE(Ref3.getString() == "Key2");
51554aea52SAlexey Lapshin EXPECT_TRUE(Ref3.getOffset() == 0x1000);
52554aea52SAlexey Lapshin EXPECT_TRUE(Ref3.getIndex() == 1);
53554aea52SAlexey Lapshin EXPECT_TRUE(Ref1 != Ref3);
54554aea52SAlexey Lapshin }
55554aea52SAlexey Lapshin
isEntryEqual(const DwarfStringPoolEntry & LHS,const DwarfStringPoolEntry & RHS)56554aea52SAlexey Lapshin bool isEntryEqual(const DwarfStringPoolEntry &LHS,
57554aea52SAlexey Lapshin const DwarfStringPoolEntry &RHS) {
58554aea52SAlexey Lapshin return LHS.Symbol == RHS.Symbol && LHS.Offset == RHS.Offset &&
59554aea52SAlexey Lapshin LHS.Index == RHS.Index;
60554aea52SAlexey Lapshin }
61554aea52SAlexey Lapshin
TEST(DwarfStringPoolEntryRefTest,TestShortEntry)62554aea52SAlexey Lapshin TEST(DwarfStringPoolEntryRefTest, TestShortEntry) {
63*5f2a7fa6SAlexey Lapshin DwarfStringPoolEntryWithExtString DwarfEntry1 = {{nullptr, 0, 0}, "Key1"};
64554aea52SAlexey Lapshin
65*5f2a7fa6SAlexey Lapshin DwarfStringPoolEntryRef Ref1(DwarfEntry1);
66554aea52SAlexey Lapshin EXPECT_TRUE(Ref1.getString() == "Key1");
67554aea52SAlexey Lapshin EXPECT_TRUE(Ref1.getOffset() == 0);
68554aea52SAlexey Lapshin EXPECT_TRUE(Ref1.getIndex() == 0);
69554aea52SAlexey Lapshin EXPECT_TRUE(isEntryEqual(Ref1.getEntry(), DwarfEntry1));
70554aea52SAlexey Lapshin
71*5f2a7fa6SAlexey Lapshin DwarfStringPoolEntryRef Ref2(DwarfEntry1);
72554aea52SAlexey Lapshin EXPECT_TRUE(Ref2.getString() == "Key1");
73554aea52SAlexey Lapshin EXPECT_TRUE(Ref2.getOffset() == 0);
74554aea52SAlexey Lapshin EXPECT_TRUE(isEntryEqual(Ref2.getEntry(), DwarfEntry1));
75554aea52SAlexey Lapshin EXPECT_TRUE(Ref1 == Ref2);
76554aea52SAlexey Lapshin EXPECT_FALSE(Ref1 != Ref2);
77554aea52SAlexey Lapshin
78*5f2a7fa6SAlexey Lapshin DwarfStringPoolEntryWithExtString DwarfEntry2 = {{nullptr, 0x1000, 1},
79*5f2a7fa6SAlexey Lapshin "Key2"};
80554aea52SAlexey Lapshin
81*5f2a7fa6SAlexey Lapshin DwarfStringPoolEntryRef Ref3(DwarfEntry2);
82554aea52SAlexey Lapshin EXPECT_TRUE(Ref3.getString() == "Key2");
83554aea52SAlexey Lapshin EXPECT_TRUE(Ref3.getOffset() == 0x1000);
84554aea52SAlexey Lapshin EXPECT_TRUE(Ref3.getIndex() == 1);
85554aea52SAlexey Lapshin EXPECT_TRUE(isEntryEqual(Ref3.getEntry(), DwarfEntry2));
86554aea52SAlexey Lapshin EXPECT_TRUE(Ref1 != Ref3);
87554aea52SAlexey Lapshin }
88554aea52SAlexey Lapshin
TEST(DwarfStringPoolEntryRefTest,CompareFullAndShort)89554aea52SAlexey Lapshin TEST(DwarfStringPoolEntryRefTest, CompareFullAndShort) {
90554aea52SAlexey Lapshin BumpPtrAllocator Allocator;
91554aea52SAlexey Lapshin
92*5f2a7fa6SAlexey Lapshin DwarfStringPoolEntryWithExtString DwarfEntry1 = {{nullptr, 0, 0}, "Key1"};
93*5f2a7fa6SAlexey Lapshin DwarfStringPoolEntryRef Ref1(DwarfEntry1);
94554aea52SAlexey Lapshin
95554aea52SAlexey Lapshin StringMapEntry<DwarfStringPoolEntry> *StringEntry2 =
963284adf6SAlexey Lapshin StringMapEntry<DwarfStringPoolEntry>::create(
97554aea52SAlexey Lapshin "Key1", Allocator, DwarfStringPoolEntry{nullptr, 0, 0});
98554aea52SAlexey Lapshin DwarfStringPoolEntryRef Ref2(*StringEntry2);
99554aea52SAlexey Lapshin
100554aea52SAlexey Lapshin EXPECT_FALSE(Ref1 == Ref2);
101554aea52SAlexey Lapshin }
102