[x86/SLH] Fix an issue where we wouldn't harden any loads if we found
[llvm-complete.git] / unittests / MC / StringTableBuilderTest.cpp
blobb547f934b02c88e674d837fef06eccfcfa49ec09
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 //===----------------------------------------------------------------------===//
10 #include "llvm/MC/StringTableBuilder.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/Support/Endian.h"
13 #include "gtest/gtest.h"
14 #include <string>
16 using namespace llvm;
18 namespace {
20 TEST(StringTableBuilderTest, BasicELF) {
21 StringTableBuilder B(StringTableBuilder::ELF);
23 B.add("foo");
24 B.add("bar");
25 B.add("foobar");
27 B.finalize();
29 std::string Expected;
30 Expected += '\x00';
31 Expected += "foobar";
32 Expected += '\x00';
33 Expected += "foo";
34 Expected += '\x00';
36 SmallString<64> Data;
37 raw_svector_ostream OS(Data);
38 B.write(OS);
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");
52 B.add("river horse");
54 B.finalize();
56 // size_field + "pygmy hippopotamus\0" + "river horse\0"
57 uint32_t ExpectedSize = 4 + 19 + 12;
58 EXPECT_EQ(ExpectedSize, B.getSize());
60 std::string Expected;
62 ExpectedSize =
63 support::endian::byte_swap<uint32_t, support::little>(ExpectedSize);
64 Expected.append((const char*)&ExpectedSize, 4);
65 Expected += "pygmy hippopotamus";
66 Expected += '\x00';
67 Expected += "river horse";
68 Expected += '\x00';
70 SmallString<64> Data;
71 raw_svector_ostream OS(Data);
72 B.write(OS);
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"));
86 B.finalizeInOrder();
88 std::string Expected;
89 Expected += '\x00';
90 Expected += "foo";
91 Expected += '\x00';
92 Expected += "bar";
93 Expected += '\x00';
94 Expected += "foobar";
95 Expected += '\x00';
97 SmallString<64> Data;
98 raw_svector_ostream OS(Data);
99 B.write(OS);
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"));