1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
| //===----------- StringTableBuilderTest.cpp -------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/MC/StringTableBuilder.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/Endian.h"
#include "gtest/gtest.h"
#include <string>
using namespace llvm;
namespace {
TEST(StringTableBuilderTest, BasicELF) {
StringTableBuilder B(StringTableBuilder::ELF);
B.add("foo");
B.add("bar");
B.add("foobar");
B.finalize();
std::string Expected;
Expected += '\x00';
Expected += "foobar";
Expected += '\x00';
Expected += "foo";
Expected += '\x00';
SmallString<64> Data;
raw_svector_ostream OS(Data);
B.write(OS);
EXPECT_EQ(Expected, Data);
EXPECT_EQ(1U, B.getOffset("foobar"));
EXPECT_EQ(4U, B.getOffset("bar"));
EXPECT_EQ(8U, B.getOffset("foo"));
}
TEST(StringTableBuilderTest, BasicWinCOFF) {
StringTableBuilder B(StringTableBuilder::WinCOFF);
// Strings must be 9 chars or longer to go in the table.
B.add("hippopotamus");
B.add("pygmy hippopotamus");
B.add("river horse");
B.finalize();
// size_field + "pygmy hippopotamus\0" + "river horse\0"
uint32_t ExpectedSize = 4 + 19 + 12;
EXPECT_EQ(ExpectedSize, B.getSize());
std::string Expected;
ExpectedSize =
support::endian::byte_swap<uint32_t, support::little>(ExpectedSize);
Expected.append((const char*)&ExpectedSize, 4);
Expected += "pygmy hippopotamus";
Expected += '\x00';
Expected += "river horse";
Expected += '\x00';
SmallString<64> Data;
raw_svector_ostream OS(Data);
B.write(OS);
EXPECT_EQ(Expected, Data);
EXPECT_EQ(4U, B.getOffset("pygmy hippopotamus"));
EXPECT_EQ(10U, B.getOffset("hippopotamus"));
EXPECT_EQ(23U, B.getOffset("river horse"));
}
TEST(StringTableBuilderTest, ELFInOrder) {
StringTableBuilder B(StringTableBuilder::ELF);
EXPECT_EQ(1U, B.add("foo"));
EXPECT_EQ(5U, B.add("bar"));
EXPECT_EQ(9U, B.add("foobar"));
B.finalizeInOrder();
std::string Expected;
Expected += '\x00';
Expected += "foo";
Expected += '\x00';
Expected += "bar";
Expected += '\x00';
Expected += "foobar";
Expected += '\x00';
SmallString<64> Data;
raw_svector_ostream OS(Data);
B.write(OS);
EXPECT_EQ(Expected, Data);
EXPECT_EQ(1U, B.getOffset("foo"));
EXPECT_EQ(5U, B.getOffset("bar"));
EXPECT_EQ(9U, B.getOffset("foobar"));
}
}
|