1 //===----------- StringTableBuilderTest.cpp -------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/StringTableBuilder.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/Support/Endian.h"
13 #include "gtest/gtest.h"
20 TEST(StringTableBuilderTest
, BasicELF
) {
21 StringTableBuilder
B(StringTableBuilder::ELF
);
37 raw_svector_ostream
OS(Data
);
40 EXPECT_EQ(Expected
, Data
);
41 EXPECT_EQ(1U, B
.getOffset("foobar"));
42 EXPECT_EQ(4U, B
.getOffset("bar"));
43 EXPECT_EQ(8U, B
.getOffset("foo"));
46 TEST(StringTableBuilderTest
, BasicWinCOFF
) {
47 StringTableBuilder
B(StringTableBuilder::WinCOFF
);
49 // Strings must be 9 chars or longer to go in the table.
50 B
.add("hippopotamus");
51 B
.add("pygmy hippopotamus");
56 // size_field + "pygmy hippopotamus\0" + "river horse\0"
57 uint32_t ExpectedSize
= 4 + 19 + 12;
58 EXPECT_EQ(ExpectedSize
, B
.getSize());
63 support::endian::byte_swap
<uint32_t, support::little
>(ExpectedSize
);
64 Expected
.append((const char*)&ExpectedSize
, 4);
65 Expected
+= "pygmy hippopotamus";
67 Expected
+= "river horse";
71 raw_svector_ostream
OS(Data
);
74 EXPECT_EQ(Expected
, Data
);
75 EXPECT_EQ(4U, B
.getOffset("pygmy hippopotamus"));
76 EXPECT_EQ(10U, B
.getOffset("hippopotamus"));
77 EXPECT_EQ(23U, B
.getOffset("river horse"));
80 TEST(StringTableBuilderTest
, ELFInOrder
) {
81 StringTableBuilder
B(StringTableBuilder::ELF
);
82 EXPECT_EQ(1U, B
.add("foo"));
83 EXPECT_EQ(5U, B
.add("bar"));
84 EXPECT_EQ(9U, B
.add("foobar"));
98 raw_svector_ostream
OS(Data
);
101 EXPECT_EQ(Expected
, Data
);
102 EXPECT_EQ(1U, B
.getOffset("foo"));
103 EXPECT_EQ(5U, B
.getOffset("bar"));
104 EXPECT_EQ(9U, B
.getOffset("foobar"));