[unittests/Object] - Simplify the code in ELFObjectFileTest.cpp, NFCI.

This refactors/rewrites the code to remove duplication.

Differential revision: https://reviews.llvm.org/D86623
This commit is contained in:
Georgii Rymar 2020-08-26 17:32:45 +03:00
parent e717fdb0f1
commit 154901c287
1 changed files with 34 additions and 57 deletions

View File

@ -14,13 +14,9 @@
using namespace llvm; using namespace llvm;
using namespace llvm::object; using namespace llvm::object;
template <class ELFT> namespace {
static Expected<ELFObjectFile<ELFT>> create(ArrayRef<uint8_t> Data) {
return ELFObjectFile<ELFT>::create(
MemoryBufferRef(toStringRef(Data), "Test buffer"));
}
// A class to initialize a buffer to represent an ELF object file. // A struct to initialize a buffer to represent an ELF object file.
struct DataForTest { struct DataForTest {
std::vector<uint8_t> Data; std::vector<uint8_t> Data;
@ -59,69 +55,50 @@ struct DataForTest {
Data = makeElfData<ELF::Elf64_Ehdr>(Class, Encoding, Machine); Data = makeElfData<ELF::Elf64_Ehdr>(Class, Encoding, Machine);
else { else {
assert(Class == ELF::ELFCLASS32); assert(Class == ELF::ELFCLASS32);
Data = makeElfData<ELF::Elf64_Ehdr>(Class, Encoding, Machine); Data = makeElfData<ELF::Elf32_Ehdr>(Class, Encoding, Machine);
} }
} }
}; };
void checkFormatAndArch(const DataForTest &D, StringRef Fmt,
Triple::ArchType Arch) {
Expected<std::unique_ptr<ObjectFile>> ELFObjOrErr =
object::ObjectFile::createELFObjectFile(
MemoryBufferRef(toStringRef(D.Data), "dummyELF"));
ASSERT_THAT_EXPECTED(ELFObjOrErr, Succeeded());
const ObjectFile &File = *(*ELFObjOrErr).get();
EXPECT_EQ(Fmt, File.getFileFormatName());
EXPECT_EQ(Arch, File.getArch());
}
} // namespace
TEST(ELFObjectFileTest, MachineTestForVE) { TEST(ELFObjectFileTest, MachineTestForVE) {
DataForTest Data(ELF::ELFCLASS64, ELF::ELFDATA2LSB, ELF::EM_VE); checkFormatAndArch({ELF::ELFCLASS64, ELF::ELFDATA2LSB, ELF::EM_VE},
auto ExpectedFile = create<ELF64LE>(Data.Data); "elf64-ve", Triple::ve);
ASSERT_THAT_EXPECTED(ExpectedFile, Succeeded());
const ELFObjectFile<ELF64LE> &File = *ExpectedFile;
EXPECT_EQ("elf64-ve", File.getFileFormatName());
EXPECT_EQ(Triple::ve, File.getArch());
} }
TEST(ELFObjectFileTest, MachineTestForX86_64) { TEST(ELFObjectFileTest, MachineTestForX86_64) {
DataForTest Data(ELF::ELFCLASS64, ELF::ELFDATA2LSB, ELF::EM_X86_64); checkFormatAndArch({ELF::ELFCLASS64, ELF::ELFDATA2LSB, ELF::EM_X86_64},
auto ExpectedFile = create<ELF64LE>(Data.Data); "elf64-x86-64", Triple::x86_64);
ASSERT_THAT_EXPECTED(ExpectedFile, Succeeded());
const ELFObjectFile<ELF64LE> &File = *ExpectedFile;
EXPECT_EQ("elf64-x86-64", File.getFileFormatName());
EXPECT_EQ(Triple::x86_64, File.getArch());
} }
TEST(ELFObjectFileTest, MachineTestFor386) { TEST(ELFObjectFileTest, MachineTestFor386) {
DataForTest Data(ELF::ELFCLASS32, ELF::ELFDATA2LSB, ELF::EM_386); checkFormatAndArch({ELF::ELFCLASS32, ELF::ELFDATA2LSB, ELF::EM_386},
auto ExpectedFile = create<ELF32LE>(Data.Data); "elf32-i386", Triple::x86);
ASSERT_THAT_EXPECTED(ExpectedFile, Succeeded());
const ELFObjectFile<ELF32LE> &File = *ExpectedFile;
EXPECT_EQ("elf32-i386", File.getFileFormatName());
EXPECT_EQ(Triple::x86, File.getArch());
} }
TEST(ELFObjectFileTest, MachineTestForMIPS) { TEST(ELFObjectFileTest, MachineTestForMIPS) {
{ checkFormatAndArch({ELF::ELFCLASS64, ELF::ELFDATA2LSB, ELF::EM_MIPS},
DataForTest Data(ELF::ELFCLASS64, ELF::ELFDATA2LSB, ELF::EM_MIPS); "elf64-mips", Triple::mips64el);
auto ExpectedFile = create<ELF64LE>(Data.Data);
ASSERT_THAT_EXPECTED(ExpectedFile, Succeeded()); checkFormatAndArch({ELF::ELFCLASS64, ELF::ELFDATA2MSB, ELF::EM_MIPS},
const ELFObjectFile<ELF64LE> &File = *ExpectedFile; "elf64-mips", Triple::mips64);
EXPECT_EQ("elf64-mips", File.getFileFormatName());
EXPECT_EQ(Triple::mips64el, File.getArch()); checkFormatAndArch({ELF::ELFCLASS32, ELF::ELFDATA2LSB, ELF::EM_MIPS},
} "elf32-mips", Triple::mipsel);
{
DataForTest Data(ELF::ELFCLASS64, ELF::ELFDATA2MSB, ELF::EM_MIPS); checkFormatAndArch({ELF::ELFCLASS32, ELF::ELFDATA2MSB, ELF::EM_MIPS},
auto ExpectedFile = create<ELF64BE>(Data.Data); "elf32-mips", Triple::mips);
ASSERT_THAT_EXPECTED(ExpectedFile, Succeeded());
const ELFObjectFile<ELF64BE> &File = *ExpectedFile;
EXPECT_EQ("elf64-mips", File.getFileFormatName());
EXPECT_EQ(Triple::mips64, File.getArch());
}
{
DataForTest Data(ELF::ELFCLASS32, ELF::ELFDATA2LSB, ELF::EM_MIPS);
auto ExpectedFile = create<ELF32LE>(Data.Data);
ASSERT_THAT_EXPECTED(ExpectedFile, Succeeded());
const ELFObjectFile<ELF32LE> &File = *ExpectedFile;
EXPECT_EQ("elf32-mips", File.getFileFormatName());
EXPECT_EQ(Triple::mipsel, File.getArch());
}
{
DataForTest Data(ELF::ELFCLASS32, ELF::ELFDATA2MSB, ELF::EM_MIPS);
auto ExpectedFile = create<ELF32BE>(Data.Data);
ASSERT_THAT_EXPECTED(ExpectedFile, Succeeded());
const ELFObjectFile<ELF32BE> &File = *ExpectedFile;
EXPECT_EQ("elf32-mips", File.getFileFormatName());
EXPECT_EQ(Triple::mips, File.getArch());
}
} }