blob: 3cfbd4453a7d5e2fb6056cec1e0887c6f4b92b71 [file] [log] [blame]
Jan Voung08c3bcd2014-12-01 17:55:16 -08001//===- unittest/IceELFSectionTest.cpp - ELF Section unit tests ------------===//
2//
3// The Subzero Code Generator
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 <algorithm>
11
12#include "gtest/gtest.h"
13
14#include "IceDefs.h"
15#include "IceELFSection.h"
16
Jim Stichnoth467ffe52016-03-29 15:01:06 -070017#include "llvm/Support/raw_os_ostream.h"
18
Jan Voung08c3bcd2014-12-01 17:55:16 -080019namespace Ice {
20namespace {
21
22// Test string table layout for various permutations. Test that pop,
23// lollipop, and lipop are able to share data, while the other strings do not.
24void CheckStringTablePermLayout(const ELFStringTableSection &Strtab) {
25 size_t pop_index = Strtab.getIndex("pop");
Jim Stichnoth467ffe52016-03-29 15:01:06 -070026 size_t pop_size = std::string("pop").size();
Jan Voung08c3bcd2014-12-01 17:55:16 -080027 size_t lollipop_index = Strtab.getIndex("lollipop");
Jim Stichnoth467ffe52016-03-29 15:01:06 -070028 size_t lollipop_size = std::string("lollipop").size();
Jan Voung08c3bcd2014-12-01 17:55:16 -080029 size_t lipop_index = Strtab.getIndex("lipop");
Jim Stichnoth467ffe52016-03-29 15:01:06 -070030 size_t lipop_size = std::string("lipop").size();
Jan Voung08c3bcd2014-12-01 17:55:16 -080031 size_t pops_index = Strtab.getIndex("pops");
Jim Stichnoth467ffe52016-03-29 15:01:06 -070032 size_t pops_size = std::string("pops").size();
Jan Voung08c3bcd2014-12-01 17:55:16 -080033 size_t unpop_index = Strtab.getIndex("unpop");
Jim Stichnoth467ffe52016-03-29 15:01:06 -070034 size_t unpop_size = std::string("unpop").size();
Jan Voung08c3bcd2014-12-01 17:55:16 -080035 size_t popular_index = Strtab.getIndex("popular");
Jim Stichnoth467ffe52016-03-29 15:01:06 -070036 size_t popular_size = std::string("popular").size();
Jan Voung08c3bcd2014-12-01 17:55:16 -080037 size_t strtab_index = Strtab.getIndex(".strtab");
Jim Stichnoth467ffe52016-03-29 15:01:06 -070038 size_t strtab_size = std::string(".strtab").size();
Jan Voung08c3bcd2014-12-01 17:55:16 -080039 size_t shstrtab_index = Strtab.getIndex(".shstrtab");
Jim Stichnoth467ffe52016-03-29 15:01:06 -070040 size_t shstrtab_size = std::string(".shstrtab").size();
Jan Voung08c3bcd2014-12-01 17:55:16 -080041 size_t symtab_index = Strtab.getIndex(".symtab");
Jim Stichnoth467ffe52016-03-29 15:01:06 -070042 size_t symtab_size = std::string(".symtab").size();
Jan Voung08c3bcd2014-12-01 17:55:16 -080043
44 // Check that some sharing exists.
45 EXPECT_EQ(pop_index, lollipop_index + (lollipop_size - pop_size));
46 EXPECT_EQ(lipop_index, lollipop_index + (lollipop_size - lipop_size));
47
48 // Check that .strtab does not share with .shstrtab (the dot throws it off).
49 EXPECT_NE(strtab_index, shstrtab_index + (shstrtab_size - strtab_size));
50
51 // Check contents make sense.
52 EXPECT_EQ(Strtab.getSectionData().slice(pop_index, pop_index + pop_size),
53 llvm::StringRef("pop"));
54 EXPECT_EQ(Strtab.getSectionData().slice(lollipop_index,
55 lollipop_index + lollipop_size),
56 llvm::StringRef("lollipop"));
57 EXPECT_EQ(Strtab.getSectionData().slice(pops_index, pops_index + pops_size),
58 llvm::StringRef("pops"));
59 EXPECT_EQ(
60 Strtab.getSectionData().slice(unpop_index, unpop_index + unpop_size),
61 llvm::StringRef("unpop"));
62 EXPECT_EQ(Strtab.getSectionData().slice(popular_index,
63 popular_index + popular_size),
64 llvm::StringRef("popular"));
65 EXPECT_EQ(
66 Strtab.getSectionData().slice(strtab_index, strtab_index + strtab_size),
67 llvm::StringRef(".strtab"));
68 EXPECT_EQ(Strtab.getSectionData().slice(shstrtab_index,
69 shstrtab_index + shstrtab_size),
70 llvm::StringRef(".shstrtab"));
71 EXPECT_EQ(
72 Strtab.getSectionData().slice(symtab_index, symtab_index + symtab_size),
73 llvm::StringRef(".symtab"));
74}
75
76// Test that the order in which strings are added doesn't matter.
77TEST(IceELFSectionTest, StringTableBuilderPermSeveral) {
Jim Stichnoth467ffe52016-03-29 15:01:06 -070078 std::vector<std::string> Strings;
Jan Voung08c3bcd2014-12-01 17:55:16 -080079 Strings.push_back("pop");
80 Strings.push_back("lollipop");
81 Strings.push_back("lipop");
82 Strings.push_back("pops");
83 Strings.push_back("unpop");
84 Strings.push_back("popular");
85 Strings.push_back("a");
86 Strings.push_back("z");
87 Strings.push_back("foo");
88 Strings.push_back("bar");
89 Strings.push_back(".text");
90 Strings.push_back(".symtab");
91 Strings.push_back(".strtab");
92 Strings.push_back(".shstrtab");
93 Strings.push_back("_start");
94 const SizeT NumTests = 128;
Jim Stichnothcaef3482015-04-09 11:19:38 -070095 const uint64_t RandomSeed = 12345; // arbitrary value for now
96 RandomNumberGenerator R(RandomSeed);
97 RandomNumberGeneratorWrapper RNG(R);
Jan Voung08c3bcd2014-12-01 17:55:16 -080098 for (SizeT i = 0; i < NumTests; ++i) {
Jim Stichnoth467ffe52016-03-29 15:01:06 -070099 auto Str = std::unique_ptr<Ostream>(new llvm::raw_os_ostream(std::cout));
Jim Stichnothcaef3482015-04-09 11:19:38 -0700100 RandomShuffle(Strings.begin(), Strings.end(), RNG);
Jan Voung08c3bcd2014-12-01 17:55:16 -0800101 ELFStringTableSection Strtab(".strtab", SHT_STRTAB, 0, 1, 0);
102 for (auto &S : Strings) {
103 Strtab.add(S);
104 }
105 Strtab.doLayout();
106 CheckStringTablePermLayout(Strtab);
107 }
108}
109
110// Test that adding duplicate strings is fine.
111TEST(IceELFSectionTest, StringTableBuilderDuplicates) {
Jim Stichnoth467ffe52016-03-29 15:01:06 -0700112 auto Str = std::unique_ptr<Ostream>(new llvm::raw_os_ostream(std::cout));
Jan Voung08c3bcd2014-12-01 17:55:16 -0800113 ELFStringTableSection Strtab(".strtab", SHT_STRTAB, 0, 1, 0);
114 Strtab.add("unpop");
115 Strtab.add("pop");
116 Strtab.add("lollipop");
117 Strtab.add("a");
118 Strtab.add("popular");
119 Strtab.add("pops");
120 Strtab.add("lipop");
121 Strtab.add(".strtab");
122 Strtab.add(".shstrtab");
123 Strtab.add(".symtab");
124
125 Strtab.add(".symtab");
126 Strtab.add(".shstrtab");
127 Strtab.add(".strtab");
128 Strtab.add("lipop");
129 Strtab.add("pops");
130 Strtab.add("popular");
131 Strtab.add("a");
132 Strtab.add("lollipop");
133 Strtab.add("pop");
134 Strtab.add("unpop");
135
136 Strtab.doLayout();
137 CheckStringTablePermLayout(Strtab);
138}
139
140} // end of anonymous namespace
141} // end of namespace Ice