Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 "IceDefs.h" | |
| 13 #include "IceELFSection.h" | |
| 14 #include "gtest/gtest.h" | |
|
Jim Stichnoth
2014/11/21 21:32:23
Should this include go before the Ice*.h includes?
jvoung (off chromium)
2014/11/24 21:35:47
Done.
| |
| 15 | |
| 16 namespace Ice { | |
| 17 namespace { | |
| 18 | |
| 19 // Test string table layout for various permutations. Test that pop, | |
| 20 // lollipop, and lipop are able to share data, while the other strings do not. | |
|
jvoung (off chromium)
2014/11/21 00:11:55
Not sure how you guys feel about adding another ty
| |
| 21 void CheckStringTablePermLayout(const ELFStringTableSection &Strtab) { | |
| 22 size_t pop_index = Strtab.getIndex("pop"); | |
| 23 size_t pop_size = IceString("pop").size(); | |
| 24 size_t lollipop_index = Strtab.getIndex("lollipop"); | |
| 25 size_t lollipop_size = IceString("lollipop").size(); | |
| 26 size_t lipop_index = Strtab.getIndex("lipop"); | |
| 27 size_t lipop_size = IceString("lipop").size(); | |
| 28 size_t pops_index = Strtab.getIndex("pops"); | |
| 29 size_t pops_size = IceString("pops").size(); | |
| 30 size_t unpop_index = Strtab.getIndex("unpop"); | |
| 31 size_t unpop_size = IceString("unpop").size(); | |
| 32 size_t popular_index = Strtab.getIndex("popular"); | |
| 33 size_t popular_size = IceString("popular").size(); | |
| 34 size_t strtab_index = Strtab.getIndex(".strtab"); | |
| 35 size_t strtab_size = IceString(".strtab").size(); | |
| 36 size_t shstrtab_index = Strtab.getIndex(".shstrtab"); | |
| 37 size_t shstrtab_size = IceString(".shstrtab").size(); | |
| 38 size_t symtab_index = Strtab.getIndex(".symtab"); | |
| 39 size_t symtab_size = IceString(".symtab").size(); | |
| 40 | |
| 41 // Check that some sharing exists. | |
| 42 EXPECT_EQ(pop_index, lollipop_index + (lollipop_size - pop_size)); | |
| 43 EXPECT_EQ(lipop_index, lollipop_index + (lollipop_size - lipop_size)); | |
| 44 | |
| 45 // Check that .strtab does not share with .shstrtab (the dot throws it off). | |
| 46 EXPECT_NE(strtab_index, shstrtab_index + (shstrtab_size - strtab_size)); | |
| 47 | |
| 48 // Check contents make sense. | |
| 49 EXPECT_EQ(Strtab.getSectionData().slice(pop_index, pop_index + pop_size), | |
| 50 llvm::StringRef("pop")); | |
| 51 EXPECT_EQ(Strtab.getSectionData().slice(lollipop_index, | |
| 52 lollipop_index + lollipop_size), | |
| 53 llvm::StringRef("lollipop")); | |
| 54 EXPECT_EQ(Strtab.getSectionData().slice(pops_index, | |
| 55 pops_index + pops_size), | |
| 56 llvm::StringRef("pops")); | |
| 57 EXPECT_EQ(Strtab.getSectionData().slice(unpop_index, | |
| 58 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(Strtab.getSectionData().slice(strtab_index, | |
| 64 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(Strtab.getSectionData().slice(symtab_index, | |
| 70 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; | |
| 93 for (SizeT i = 0; i < NumTests; ++i) { | |
| 94 std::random_shuffle(Strings.begin(), Strings.end()); | |
| 95 ELFStringTableSection Strtab(".strtab", SHT_STRTAB, 0, 1, 0); | |
| 96 for (auto &S : Strings) { | |
| 97 Strtab.add(S); | |
| 98 } | |
| 99 Strtab.doLayout(); | |
| 100 CheckStringTablePermLayout(Strtab); | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 // Test that adding duplicate strings is fine. | |
| 105 TEST(IceELFSectionTest, StringTableBuilderDuplicates) { | |
| 106 ELFStringTableSection Strtab(".strtab", SHT_STRTAB, 0, 1, 0); | |
| 107 Strtab.add("unpop"); | |
| 108 Strtab.add("pop"); | |
| 109 Strtab.add("lollipop"); | |
| 110 Strtab.add("a"); | |
| 111 Strtab.add("popular"); | |
| 112 Strtab.add("pops"); | |
| 113 Strtab.add("lipop"); | |
| 114 Strtab.add(".strtab"); | |
| 115 Strtab.add(".shstrtab"); | |
| 116 Strtab.add(".symtab"); | |
| 117 | |
| 118 Strtab.add(".symtab"); | |
| 119 Strtab.add(".shstrtab"); | |
| 120 Strtab.add(".strtab"); | |
| 121 Strtab.add("lipop"); | |
| 122 Strtab.add("pops"); | |
| 123 Strtab.add("popular"); | |
| 124 Strtab.add("a"); | |
| 125 Strtab.add("lollipop"); | |
| 126 Strtab.add("pop"); | |
| 127 Strtab.add("unpop"); | |
| 128 | |
| 129 Strtab.doLayout(); | |
| 130 CheckStringTablePermLayout(Strtab); | |
| 131 } | |
| 132 | |
| 133 } // end of anonymous namespace | |
| 134 } // end of namespace Ice | |
| OLD | NEW |