2014-05-01 00:25:02 +08: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 10:01:39 +08:00
|
|
|
#include "llvm/MC/StringTableBuilder.h"
|
2017-06-06 19:06:56 +08:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2014-09-30 06:43:20 +08:00
|
|
|
#include "llvm/Support/Endian.h"
|
2015-01-14 19:23:27 +08:00
|
|
|
#include "gtest/gtest.h"
|
2014-05-01 00:25:02 +08:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2014-09-30 06:43:20 +08:00
|
|
|
TEST(StringTableBuilderTest, BasicELF) {
|
2015-10-24 05:48:05 +08:00
|
|
|
StringTableBuilder B(StringTableBuilder::ELF);
|
2014-05-01 00:25:02 +08:00
|
|
|
|
|
|
|
B.add("foo");
|
|
|
|
B.add("bar");
|
|
|
|
B.add("foobar");
|
|
|
|
|
2015-10-24 05:48:05 +08:00
|
|
|
B.finalize();
|
2014-05-01 00:25:02 +08:00
|
|
|
|
|
|
|
std::string Expected;
|
|
|
|
Expected += '\x00';
|
|
|
|
Expected += "foobar";
|
|
|
|
Expected += '\x00';
|
|
|
|
Expected += "foo";
|
|
|
|
Expected += '\x00';
|
|
|
|
|
2016-10-05 06:43:25 +08:00
|
|
|
SmallString<64> Data;
|
|
|
|
raw_svector_ostream OS(Data);
|
|
|
|
B.write(OS);
|
|
|
|
|
|
|
|
EXPECT_EQ(Expected, Data);
|
2014-05-01 00:25:02 +08:00
|
|
|
EXPECT_EQ(1U, B.getOffset("foobar"));
|
|
|
|
EXPECT_EQ(4U, B.getOffset("bar"));
|
|
|
|
EXPECT_EQ(8U, B.getOffset("foo"));
|
|
|
|
}
|
|
|
|
|
2014-09-30 06:43:20 +08:00
|
|
|
TEST(StringTableBuilderTest, BasicWinCOFF) {
|
2015-10-24 05:48:05 +08:00
|
|
|
StringTableBuilder B(StringTableBuilder::WinCOFF);
|
2014-09-30 06:43:20 +08:00
|
|
|
|
|
|
|
// Strings must be 9 chars or longer to go in the table.
|
|
|
|
B.add("hippopotamus");
|
|
|
|
B.add("pygmy hippopotamus");
|
|
|
|
B.add("river horse");
|
|
|
|
|
2015-10-24 05:48:05 +08:00
|
|
|
B.finalize();
|
2014-09-30 06:43:20 +08:00
|
|
|
|
|
|
|
// size_field + "pygmy hippopotamus\0" + "river horse\0"
|
|
|
|
uint32_t ExpectedSize = 4 + 19 + 12;
|
2016-10-05 06:43:25 +08:00
|
|
|
EXPECT_EQ(ExpectedSize, B.getSize());
|
2014-09-30 06:43:20 +08:00
|
|
|
|
|
|
|
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';
|
|
|
|
|
2016-10-05 06:43:25 +08:00
|
|
|
SmallString<64> Data;
|
|
|
|
raw_svector_ostream OS(Data);
|
|
|
|
B.write(OS);
|
|
|
|
|
|
|
|
EXPECT_EQ(Expected, Data);
|
2014-09-30 06:43:20 +08:00
|
|
|
EXPECT_EQ(4U, B.getOffset("pygmy hippopotamus"));
|
|
|
|
EXPECT_EQ(10U, B.getOffset("hippopotamus"));
|
|
|
|
EXPECT_EQ(23U, B.getOffset("river horse"));
|
|
|
|
}
|
|
|
|
|
2016-01-29 08:49:42 +08:00
|
|
|
TEST(StringTableBuilderTest, ELFInOrder) {
|
|
|
|
StringTableBuilder B(StringTableBuilder::ELF);
|
|
|
|
EXPECT_EQ(1U, B.add("foo"));
|
|
|
|
EXPECT_EQ(5U, B.add("bar"));
|
|
|
|
EXPECT_EQ(9U, B.add("foobar"));
|
|
|
|
|
|
|
|
B.finalizeInOrder();
|
|
|
|
|
|
|
|
std::string Expected;
|
|
|
|
Expected += '\x00';
|
|
|
|
Expected += "foo";
|
|
|
|
Expected += '\x00';
|
|
|
|
Expected += "bar";
|
|
|
|
Expected += '\x00';
|
|
|
|
Expected += "foobar";
|
|
|
|
Expected += '\x00';
|
|
|
|
|
2016-10-05 06:43:25 +08:00
|
|
|
SmallString<64> Data;
|
|
|
|
raw_svector_ostream OS(Data);
|
|
|
|
B.write(OS);
|
|
|
|
|
|
|
|
EXPECT_EQ(Expected, Data);
|
2016-01-29 08:49:42 +08:00
|
|
|
EXPECT_EQ(1U, B.getOffset("foo"));
|
|
|
|
EXPECT_EQ(5U, B.getOffset("bar"));
|
|
|
|
EXPECT_EQ(9U, B.getOffset("foobar"));
|
|
|
|
}
|
|
|
|
|
2014-05-01 00:25:02 +08:00
|
|
|
}
|