2014-04-30 16:25:02 +00:00
|
|
|
//===----------- StringTableBuilderTest.cpp -------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-07-03 02:01:39 +00:00
|
|
|
#include "llvm/MC/StringTableBuilder.h"
|
2014-09-29 22:43:20 +00:00
|
|
|
#include "llvm/Support/Endian.h"
|
2015-01-14 11:23:27 +00:00
|
|
|
#include "gtest/gtest.h"
|
2014-04-30 16:25:02 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2014-09-29 22:43:20 +00:00
|
|
|
TEST(StringTableBuilderTest, BasicELF) {
|
2014-04-30 16:25:02 +00:00
|
|
|
StringTableBuilder B;
|
|
|
|
|
|
|
|
B.add("foo");
|
|
|
|
B.add("bar");
|
|
|
|
B.add("foobar");
|
|
|
|
|
2014-09-29 22:43:20 +00:00
|
|
|
B.finalize(StringTableBuilder::ELF);
|
2014-04-30 16:25:02 +00:00
|
|
|
|
|
|
|
std::string Expected;
|
|
|
|
Expected += '\x00';
|
|
|
|
Expected += "foobar";
|
|
|
|
Expected += '\x00';
|
|
|
|
Expected += "foo";
|
|
|
|
Expected += '\x00';
|
|
|
|
|
|
|
|
EXPECT_EQ(Expected, B.data());
|
|
|
|
EXPECT_EQ(1U, B.getOffset("foobar"));
|
|
|
|
EXPECT_EQ(4U, B.getOffset("bar"));
|
|
|
|
EXPECT_EQ(8U, B.getOffset("foo"));
|
|
|
|
}
|
|
|
|
|
2014-09-29 22:43:20 +00:00
|
|
|
TEST(StringTableBuilderTest, BasicWinCOFF) {
|
|
|
|
StringTableBuilder B;
|
|
|
|
|
|
|
|
// Strings must be 9 chars or longer to go in the table.
|
|
|
|
B.add("hippopotamus");
|
|
|
|
B.add("pygmy hippopotamus");
|
|
|
|
B.add("river horse");
|
|
|
|
|
|
|
|
B.finalize(StringTableBuilder::WinCOFF);
|
|
|
|
|
|
|
|
// size_field + "pygmy hippopotamus\0" + "river horse\0"
|
|
|
|
uint32_t ExpectedSize = 4 + 19 + 12;
|
|
|
|
EXPECT_EQ(ExpectedSize, B.data().size());
|
|
|
|
|
|
|
|
std::string Expected;
|
|
|
|
|
|
|
|
ExpectedSize =
|
|
|
|
support::endian::byte_swap<uint32_t, support::little>(ExpectedSize);
|
|
|
|
Expected.append((const char*)&ExpectedSize, 4);
|
|
|
|
Expected += "pygmy hippopotamus";
|
|
|
|
Expected += '\x00';
|
|
|
|
Expected += "river horse";
|
|
|
|
Expected += '\x00';
|
|
|
|
|
|
|
|
EXPECT_EQ(Expected, B.data());
|
|
|
|
EXPECT_EQ(4U, B.getOffset("pygmy hippopotamus"));
|
|
|
|
EXPECT_EQ(10U, B.getOffset("hippopotamus"));
|
|
|
|
EXPECT_EQ(23U, B.getOffset("river horse"));
|
|
|
|
}
|
|
|
|
|
2014-04-30 16:25:02 +00:00
|
|
|
}
|