Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 1 | //===- 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 | |
| 17 | namespace Ice { |
| 18 | namespace { |
| 19 | |
| 20 | // Test string table layout for various permutations. Test that pop, |
| 21 | // lollipop, and lipop are able to share data, while the other strings do not. |
| 22 | void CheckStringTablePermLayout(const ELFStringTableSection &Strtab) { |
| 23 | size_t pop_index = Strtab.getIndex("pop"); |
| 24 | size_t pop_size = IceString("pop").size(); |
| 25 | size_t lollipop_index = Strtab.getIndex("lollipop"); |
| 26 | size_t lollipop_size = IceString("lollipop").size(); |
| 27 | size_t lipop_index = Strtab.getIndex("lipop"); |
| 28 | size_t lipop_size = IceString("lipop").size(); |
| 29 | size_t pops_index = Strtab.getIndex("pops"); |
| 30 | size_t pops_size = IceString("pops").size(); |
| 31 | size_t unpop_index = Strtab.getIndex("unpop"); |
| 32 | size_t unpop_size = IceString("unpop").size(); |
| 33 | size_t popular_index = Strtab.getIndex("popular"); |
| 34 | size_t popular_size = IceString("popular").size(); |
| 35 | size_t strtab_index = Strtab.getIndex(".strtab"); |
| 36 | size_t strtab_size = IceString(".strtab").size(); |
| 37 | size_t shstrtab_index = Strtab.getIndex(".shstrtab"); |
| 38 | size_t shstrtab_size = IceString(".shstrtab").size(); |
| 39 | size_t symtab_index = Strtab.getIndex(".symtab"); |
| 40 | size_t symtab_size = IceString(".symtab").size(); |
| 41 | |
| 42 | // Check that some sharing exists. |
| 43 | EXPECT_EQ(pop_index, lollipop_index + (lollipop_size - pop_size)); |
| 44 | EXPECT_EQ(lipop_index, lollipop_index + (lollipop_size - lipop_size)); |
| 45 | |
| 46 | // Check that .strtab does not share with .shstrtab (the dot throws it off). |
| 47 | EXPECT_NE(strtab_index, shstrtab_index + (shstrtab_size - strtab_size)); |
| 48 | |
| 49 | // Check contents make sense. |
| 50 | EXPECT_EQ(Strtab.getSectionData().slice(pop_index, pop_index + pop_size), |
| 51 | llvm::StringRef("pop")); |
| 52 | EXPECT_EQ(Strtab.getSectionData().slice(lollipop_index, |
| 53 | lollipop_index + lollipop_size), |
| 54 | llvm::StringRef("lollipop")); |
| 55 | EXPECT_EQ(Strtab.getSectionData().slice(pops_index, pops_index + pops_size), |
| 56 | llvm::StringRef("pops")); |
| 57 | EXPECT_EQ( |
| 58 | Strtab.getSectionData().slice(unpop_index, unpop_index + unpop_size), |
| 59 | llvm::StringRef("unpop")); |
| 60 | EXPECT_EQ(Strtab.getSectionData().slice(popular_index, |
| 61 | popular_index + popular_size), |
| 62 | llvm::StringRef("popular")); |
| 63 | EXPECT_EQ( |
| 64 | Strtab.getSectionData().slice(strtab_index, strtab_index + strtab_size), |
| 65 | llvm::StringRef(".strtab")); |
| 66 | EXPECT_EQ(Strtab.getSectionData().slice(shstrtab_index, |
| 67 | shstrtab_index + shstrtab_size), |
| 68 | llvm::StringRef(".shstrtab")); |
| 69 | EXPECT_EQ( |
| 70 | Strtab.getSectionData().slice(symtab_index, symtab_index + symtab_size), |
| 71 | llvm::StringRef(".symtab")); |
| 72 | } |
| 73 | |
| 74 | // Test that the order in which strings are added doesn't matter. |
| 75 | TEST(IceELFSectionTest, StringTableBuilderPermSeveral) { |
| 76 | std::vector<IceString> Strings; |
| 77 | Strings.push_back("pop"); |
| 78 | Strings.push_back("lollipop"); |
| 79 | Strings.push_back("lipop"); |
| 80 | Strings.push_back("pops"); |
| 81 | Strings.push_back("unpop"); |
| 82 | Strings.push_back("popular"); |
| 83 | Strings.push_back("a"); |
| 84 | Strings.push_back("z"); |
| 85 | Strings.push_back("foo"); |
| 86 | Strings.push_back("bar"); |
| 87 | Strings.push_back(".text"); |
| 88 | Strings.push_back(".symtab"); |
| 89 | Strings.push_back(".strtab"); |
| 90 | Strings.push_back(".shstrtab"); |
| 91 | Strings.push_back("_start"); |
| 92 | const SizeT NumTests = 128; |
Jim Stichnoth | caef348 | 2015-04-09 11:19:38 -0700 | [diff] [blame] | 93 | const uint64_t RandomSeed = 12345; // arbitrary value for now |
| 94 | RandomNumberGenerator R(RandomSeed); |
| 95 | RandomNumberGeneratorWrapper RNG(R); |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 96 | for (SizeT i = 0; i < NumTests; ++i) { |
Jim Stichnoth | caef348 | 2015-04-09 11:19:38 -0700 | [diff] [blame] | 97 | RandomShuffle(Strings.begin(), Strings.end(), RNG); |
Jan Voung | 08c3bcd | 2014-12-01 17:55:16 -0800 | [diff] [blame] | 98 | ELFStringTableSection Strtab(".strtab", SHT_STRTAB, 0, 1, 0); |
| 99 | for (auto &S : Strings) { |
| 100 | Strtab.add(S); |
| 101 | } |
| 102 | Strtab.doLayout(); |
| 103 | CheckStringTablePermLayout(Strtab); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // Test that adding duplicate strings is fine. |
| 108 | TEST(IceELFSectionTest, StringTableBuilderDuplicates) { |
| 109 | ELFStringTableSection Strtab(".strtab", SHT_STRTAB, 0, 1, 0); |
| 110 | Strtab.add("unpop"); |
| 111 | Strtab.add("pop"); |
| 112 | Strtab.add("lollipop"); |
| 113 | Strtab.add("a"); |
| 114 | Strtab.add("popular"); |
| 115 | Strtab.add("pops"); |
| 116 | Strtab.add("lipop"); |
| 117 | Strtab.add(".strtab"); |
| 118 | Strtab.add(".shstrtab"); |
| 119 | Strtab.add(".symtab"); |
| 120 | |
| 121 | Strtab.add(".symtab"); |
| 122 | Strtab.add(".shstrtab"); |
| 123 | Strtab.add(".strtab"); |
| 124 | Strtab.add("lipop"); |
| 125 | Strtab.add("pops"); |
| 126 | Strtab.add("popular"); |
| 127 | Strtab.add("a"); |
| 128 | Strtab.add("lollipop"); |
| 129 | Strtab.add("pop"); |
| 130 | Strtab.add("unpop"); |
| 131 | |
| 132 | Strtab.doLayout(); |
| 133 | CheckStringTablePermLayout(Strtab); |
| 134 | } |
| 135 | |
| 136 | } // end of anonymous namespace |
| 137 | } // end of namespace Ice |