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/ADT/SmallString.h" 11 #include "llvm/MC/StringTableBuilder.h" 12 #include "llvm/Support/Endian.h" 13 #include "gtest/gtest.h" 14 #include <string> 15 16 using namespace llvm; 17 18 namespace { 19 20 TEST(StringTableBuilderTest, BasicELF) { 21 StringTableBuilder B(StringTableBuilder::ELF); 22 23 B.add("foo"); 24 B.add("bar"); 25 B.add("foobar"); 26 27 B.finalize(); 28 29 std::string Expected; 30 Expected += '\x00'; 31 Expected += "foobar"; 32 Expected += '\x00'; 33 Expected += "foo"; 34 Expected += '\x00'; 35 36 SmallString<64> Data; 37 raw_svector_ostream OS(Data); 38 B.write(OS); 39 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")); 44 } 45 46 TEST(StringTableBuilderTest, BasicWinCOFF) { 47 StringTableBuilder B(StringTableBuilder::WinCOFF); 48 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"); 53 54 B.finalize(); 55 56 // size_field + "pygmy hippopotamus\0" + "river horse\0" 57 uint32_t ExpectedSize = 4 + 19 + 12; 58 EXPECT_EQ(ExpectedSize, B.getSize()); 59 60 std::string Expected; 61 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'; 69 70 SmallString<64> Data; 71 raw_svector_ostream OS(Data); 72 B.write(OS); 73 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")); 78 } 79 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")); 85 86 B.finalizeInOrder(); 87 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'; 96 97 SmallString<64> Data; 98 raw_svector_ostream OS(Data); 99 B.write(OS); 100 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")); 105 } 106 107 } 108