[llvm-readobj] - Stop treating ".stack_sizes.*" sections as stack sizes sections.
[llvm-complete.git] / unittests / MC / StringTableBuilderTest.cpp
blob81b7fa36beeb83cd07bb4e03e728ffdf397bd652
1 //===----------- StringTableBuilderTest.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 //===----------------------------------------------------------------------===//
9 #include "llvm/MC/StringTableBuilder.h"
10 #include "llvm/ADT/SmallString.h"
11 #include "llvm/Support/Endian.h"
12 #include "gtest/gtest.h"
13 #include <string>
15 using namespace llvm;
17 namespace {
19 TEST(StringTableBuilderTest, BasicELF) {
20 StringTableBuilder B(StringTableBuilder::ELF);
22 B.add("foo");
23 B.add("bar");
24 B.add("foobar");
26 B.finalize();
28 std::string Expected;
29 Expected += '\x00';
30 Expected += "foobar";
31 Expected += '\x00';
32 Expected += "foo";
33 Expected += '\x00';
35 SmallString<64> Data;
36 raw_svector_ostream OS(Data);
37 B.write(OS);
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");
51 B.add("river horse");
53 B.finalize();
55 // size_field + "pygmy hippopotamus\0" + "river horse\0"
56 uint32_t ExpectedSize = 4 + 19 + 12;
57 EXPECT_EQ(ExpectedSize, B.getSize());
59 std::string Expected;
61 ExpectedSize =
62 support::endian::byte_swap<uint32_t, support::little>(ExpectedSize);
63 Expected.append((const char*)&ExpectedSize, 4);
64 Expected += "pygmy hippopotamus";
65 Expected += '\x00';
66 Expected += "river horse";
67 Expected += '\x00';
69 SmallString<64> Data;
70 raw_svector_ostream OS(Data);
71 B.write(OS);
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"));
85 B.finalizeInOrder();
87 std::string Expected;
88 Expected += '\x00';
89 Expected += "foo";
90 Expected += '\x00';
91 Expected += "bar";
92 Expected += '\x00';
93 Expected += "foobar";
94 Expected += '\x00';
96 SmallString<64> Data;
97 raw_svector_ostream OS(Data);
98 B.write(OS);
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"));