Hans Wennborg | 83e6e1e | 2014-04-30 16:25:02 +0000 | [diff] [blame] | 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 | |
Rafael Espindola | 97de474 | 2014-07-03 02:01:39 +0000 | [diff] [blame] | 10 | #include "llvm/MC/StringTableBuilder.h" |
Hans Wennborg | f26bfc1 | 2014-09-29 22:43:20 +0000 | [diff] [blame] | 11 | #include "llvm/Support/Endian.h" |
Chandler Carruth | d990388 | 2015-01-14 11:23:27 +0000 | [diff] [blame^] | 12 | #include "gtest/gtest.h" |
Hans Wennborg | 83e6e1e | 2014-04-30 16:25:02 +0000 | [diff] [blame] | 13 | #include <string> |
| 14 | |
| 15 | using namespace llvm; |
| 16 | |
| 17 | namespace { |
| 18 | |
Hans Wennborg | f26bfc1 | 2014-09-29 22:43:20 +0000 | [diff] [blame] | 19 | TEST(StringTableBuilderTest, BasicELF) { |
Hans Wennborg | 83e6e1e | 2014-04-30 16:25:02 +0000 | [diff] [blame] | 20 | StringTableBuilder B; |
| 21 | |
| 22 | B.add("foo"); |
| 23 | B.add("bar"); |
| 24 | B.add("foobar"); |
| 25 | |
Hans Wennborg | f26bfc1 | 2014-09-29 22:43:20 +0000 | [diff] [blame] | 26 | B.finalize(StringTableBuilder::ELF); |
Hans Wennborg | 83e6e1e | 2014-04-30 16:25:02 +0000 | [diff] [blame] | 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 | |
Hans Wennborg | f26bfc1 | 2014-09-29 22:43:20 +0000 | [diff] [blame] | 41 | TEST(StringTableBuilderTest, BasicWinCOFF) { |
| 42 | StringTableBuilder B; |
| 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(StringTableBuilder::WinCOFF); |
| 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 | |
Hans Wennborg | 83e6e1e | 2014-04-30 16:25:02 +0000 | [diff] [blame] | 71 | } |