1 //===----------- StringTableBuilderTest.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/MC/StringTableBuilder.h"
10 #include "llvm/ADT/SmallString.h"
11 #include "llvm/Support/Endian.h"
12 #include "gtest/gtest.h"
19 TEST(StringTableBuilderTest
, BasicELF
) {
20 StringTableBuilder
B(StringTableBuilder::ELF
);
36 raw_svector_ostream
OS(Data
);
39 EXPECT_EQ(Expected
, Data
);
40 EXPECT_EQ(1U, B
.getOffset("foobar"));
41 EXPECT_EQ(4U, B
.getOffset("bar"));
42 EXPECT_EQ(8U, B
.getOffset("foo"));
45 TEST(StringTableBuilderTest
, BasicWinCOFF
) {
46 StringTableBuilder
B(StringTableBuilder::WinCOFF
);
48 // Strings must be 9 chars or longer to go in the table.
49 B
.add("hippopotamus");
50 B
.add("pygmy hippopotamus");
55 // size_field + "pygmy hippopotamus\0" + "river horse\0"
56 uint32_t ExpectedSize
= 4 + 19 + 12;
57 EXPECT_EQ(ExpectedSize
, B
.getSize());
62 support::endian::byte_swap
<uint32_t, support::little
>(ExpectedSize
);
63 Expected
.append((const char*)&ExpectedSize
, 4);
64 Expected
+= "pygmy hippopotamus";
66 Expected
+= "river horse";
70 raw_svector_ostream
OS(Data
);
73 EXPECT_EQ(Expected
, Data
);
74 EXPECT_EQ(4U, B
.getOffset("pygmy hippopotamus"));
75 EXPECT_EQ(10U, B
.getOffset("hippopotamus"));
76 EXPECT_EQ(23U, B
.getOffset("river horse"));
79 TEST(StringTableBuilderTest
, ELFInOrder
) {
80 StringTableBuilder
B(StringTableBuilder::ELF
);
81 EXPECT_EQ(1U, B
.add("foo"));
82 EXPECT_EQ(5U, B
.add("bar"));
83 EXPECT_EQ(9U, B
.add("foobar"));
97 raw_svector_ostream
OS(Data
);
100 EXPECT_EQ(Expected
, Data
);
101 EXPECT_EQ(1U, B
.getOffset("foo"));
102 EXPECT_EQ(5U, B
.getOffset("bar"));
103 EXPECT_EQ(9U, B
.getOffset("foobar"));
106 TEST(StringTableBuilderTest
, MachOInOrder
) {
107 StringTableBuilder
B(StringTableBuilder::MachO
);
115 std::string Expected
;
124 // Mach-O pads to 4 bytes
127 SmallString
<64> Data
;
128 raw_svector_ostream
OS(Data
);
131 EXPECT_EQ(Expected
, Data
);
132 EXPECT_EQ(1U, B
.getOffset("foo"));
133 EXPECT_EQ(5U, B
.getOffset("bar"));
134 EXPECT_EQ(9U, B
.getOffset("fooba"));
137 TEST(StringTableBuilderTest
, MachO64InOrder
) {
138 StringTableBuilder
B(StringTableBuilder::MachO64
);
146 std::string Expected
;
155 // 64 bit Mach-O pads to 8 bytes
162 SmallString
<64> Data
;
163 raw_svector_ostream
OS(Data
);
166 EXPECT_EQ(Expected
, Data
);
167 EXPECT_EQ(1U, B
.getOffset("foo"));
168 EXPECT_EQ(5U, B
.getOffset("bar"));
169 EXPECT_EQ(9U, B
.getOffset("f"));
172 TEST(StringTableBuilderTest
, MachOLinkedInOrder
) {
173 StringTableBuilder
B(StringTableBuilder::MachOLinked
);
181 std::string Expected
;
191 // Mach-O pads to 4 bytes
194 SmallString
<64> Data
;
195 raw_svector_ostream
OS(Data
);
198 EXPECT_EQ(Expected
, Data
);
199 EXPECT_EQ(2U, B
.getOffset("foo"));
200 EXPECT_EQ(6U, B
.getOffset("bar"));
201 EXPECT_EQ(10U, B
.getOffset("foob"));
204 TEST(StringTableBuilderTest
, MachO64LinkedInOrder
) {
205 StringTableBuilder
B(StringTableBuilder::MachO64Linked
);
213 std::string Expected
;
223 // 64 bit Mach-O pads to 8 bytes
230 SmallString
<64> Data
;
231 raw_svector_ostream
OS(Data
);
234 EXPECT_EQ(Expected
, Data
);
235 EXPECT_EQ(2U, B
.getOffset("foo"));
236 EXPECT_EQ(6U, B
.getOffset("ba"));
237 EXPECT_EQ(9U, B
.getOffset("f"));