1 //===----------- StringTableBuilderTest.cpp -------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/MC/StringTableBuilder.h"
11 #include "llvm/Support/Endian.h"
12 #include "gtest/gtest.h"
13 #include <string>
14
15 using namespace llvm;
16
17 namespace {
18
TEST(StringTableBuilderTest,BasicELF)19 TEST(StringTableBuilderTest, BasicELF) {
20 StringTableBuilder B(StringTableBuilder::ELF);
21
22 B.add("foo");
23 B.add("bar");
24 B.add("foobar");
25
26 B.finalize();
27
28 std::string Expected;
29 Expected += '\x00';
30 Expected += "foobar";
31 Expected += '\x00';
32 Expected += "foo";
33 Expected += '\x00';
34
35 EXPECT_EQ(Expected, B.data());
36 EXPECT_EQ(1U, B.getOffset("foobar"));
37 EXPECT_EQ(4U, B.getOffset("bar"));
38 EXPECT_EQ(8U, B.getOffset("foo"));
39 }
40
TEST(StringTableBuilderTest,BasicWinCOFF)41 TEST(StringTableBuilderTest, BasicWinCOFF) {
42 StringTableBuilder B(StringTableBuilder::WinCOFF);
43
44 // Strings must be 9 chars or longer to go in the table.
45 B.add("hippopotamus");
46 B.add("pygmy hippopotamus");
47 B.add("river horse");
48
49 B.finalize();
50
51 // size_field + "pygmy hippopotamus\0" + "river horse\0"
52 uint32_t ExpectedSize = 4 + 19 + 12;
53 EXPECT_EQ(ExpectedSize, B.data().size());
54
55 std::string Expected;
56
57 ExpectedSize =
58 support::endian::byte_swap<uint32_t, support::little>(ExpectedSize);
59 Expected.append((const char*)&ExpectedSize, 4);
60 Expected += "pygmy hippopotamus";
61 Expected += '\x00';
62 Expected += "river horse";
63 Expected += '\x00';
64
65 EXPECT_EQ(Expected, B.data());
66 EXPECT_EQ(4U, B.getOffset("pygmy hippopotamus"));
67 EXPECT_EQ(10U, B.getOffset("hippopotamus"));
68 EXPECT_EQ(23U, B.getOffset("river horse"));
69 }
70
71 }
72