blob: a4ef558a5274b8ea07e0349bc17ae32bca19413e [file] [log] [blame]
Shih-wei Liao5460a1f2012-03-16 22:41:16 -07001//===- StrSymPoolTest.cpp -------------------------------------------------===//
2//
3// The MCLinker Project
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#include "mcld/LD/StrSymPool.h"
10#include "StrSymPoolTest.h"
11#include <string>
12#include <cstdio>
13
14using namespace mcld;
15using namespace mcldtest;
16
17
18// Constructor can do set-up work for all test here.
19StrSymPoolTest::StrSymPoolTest()
20{
21 // create testee. modify it if need
22 Resolver resolver;
23 m_pTestee = new StrSymPool(1, 1, resolver);
24}
25
26// Destructor can do clean-up work that doesn't throw exceptions here.
27StrSymPoolTest::~StrSymPoolTest()
28{
29 delete m_pTestee;
30}
31
32// SetUp() will be called immediately before each test.
33void StrSymPoolTest::SetUp()
34{
35}
36
37// TearDown() will be called immediately after each test.
38void StrSymPoolTest::TearDown()
39{
40}
41
42//==========================================================================//
43// Testcases
44//
45
46
47TEST_F( StrSymPoolTest, insertString ) {
48 const char *s1 = "Hello MCLinker";
49 const char *result1 = m_pTestee->insertString(s1);
50 EXPECT_NE(s1, result1);
51 EXPECT_STREQ(s1, result1);
52}
53
54TEST_F( StrSymPoolTest, insertSameString ) {
55 const char *s1 = "Hello MCLinker";
56 std::string s2(s1);
57 const char *result1 = m_pTestee->insertString(s1);
58 const char *result2 = m_pTestee->insertString(s2.c_str());
59 EXPECT_STREQ(s1, result1);
60 EXPECT_STREQ(s2.c_str(), result2);
61 EXPECT_EQ(result1, result2);
62}
63
64TEST_F( StrSymPoolTest, insert_local_defined_Symbol ) {
65 const char *name = "Hello MCLinker";
66 bool isDyn = false;
67 LDSymbol::Type type = LDSymbol::Defined;
68 LDSymbol::Binding binding = LDSymbol::Local;
69 const llvm::MCSectionData *section = 0;
70 uint64_t value = 0;
71 uint64_t size = 0;
72 uint8_t other = 0;
73
74 LDSymbol *sym = m_pTestee->insertSymbol(name,
75 isDyn,
76 type,
77 binding,
78 section,
79 value,
80 size,
81 other);
82 EXPECT_NE(name, sym->name());
83 EXPECT_STREQ(name, sym->name());
84 EXPECT_EQ(isDyn, sym->isDyn());
85 EXPECT_EQ(type, sym->type());
86 EXPECT_EQ(binding, sym->binding());
87 EXPECT_EQ(section, sym->section());
88 EXPECT_EQ(value, sym->value());
89 EXPECT_EQ(size, sym->size());
90 EXPECT_EQ(other, sym->other());
91
92 LDSymbol *sym2 = m_pTestee->insertSymbol(name,
93 isDyn,
94 type,
95 binding,
96 section,
97 value,
98 size,
99 other);
100 EXPECT_NE(name, sym2->name());
101 EXPECT_STREQ(name, sym2->name());
102 EXPECT_EQ(isDyn, sym2->isDyn());
103 EXPECT_EQ(type, sym2->type());
104 EXPECT_EQ(binding, sym2->binding());
105 EXPECT_EQ(section, sym2->section());
106 EXPECT_EQ(value, sym2->value());
107 EXPECT_EQ(size, sym2->size());
108 EXPECT_EQ(other, sym2->other());
109
110
111 EXPECT_NE(sym, sym2);
112}
113
114TEST_F( StrSymPoolTest, insert_global_reference_Symbol ) {
115 const char *name = "Hello MCLinker";
116 bool isDyn = false;
117 LDSymbol::Type type = LDSymbol::Reference;
118 LDSymbol::Binding binding = LDSymbol::Global;
119 const llvm::MCSectionData *section = 0;
120 uint64_t value = 0;
121 uint64_t size = 0;
122 uint8_t other = 0;
123
124 LDSymbol *sym = m_pTestee->insertSymbol(name,
125 isDyn,
126 type,
127 binding,
128 section,
129 value,
130 size,
131 other);
132 EXPECT_NE(name, sym->name());
133 EXPECT_STREQ(name, sym->name());
134 EXPECT_EQ(isDyn, sym->isDyn());
135 EXPECT_EQ(type, sym->type());
136 EXPECT_EQ(binding, sym->binding());
137 EXPECT_EQ(section, sym->section());
138 EXPECT_EQ(value, sym->value());
139 EXPECT_EQ(size, sym->size());
140 EXPECT_EQ(other, sym->other());
141
142
143 LDSymbol *sym2 = m_pTestee->insertSymbol(name,
144 isDyn,
145 type,
146 binding,
147 section,
148 value,
149 size,
150 other);
151
152
153 EXPECT_EQ(sym, sym2);
154
155
156 LDSymbol *sym3 = m_pTestee->insertSymbol("Different symbol",
157 isDyn,
158 type,
159 binding,
160 section,
161 value,
162 size,
163 other);
164
165 EXPECT_NE(sym, sym3);
166}
167
168
169TEST_F( StrSymPoolTest, insertSymbol_after_insert_same_string ) {
170 const char *name = "Hello MCLinker";
171 bool isDyn = false;
172 LDSymbol::Type type = LDSymbol::Defined;
173 LDSymbol::Binding binding = LDSymbol::Global;
174 const llvm::MCSectionData *section = 0;
175 uint64_t value = 0;
176 uint64_t size = 0;
177 uint8_t other = 0;
178
179 const char *result1 = m_pTestee->insertString(name);
180 LDSymbol *sym = m_pTestee->insertSymbol(name,
181 isDyn,
182 type,
183 binding,
184 section,
185 value,
186 size,
187 other);
188
189 EXPECT_STREQ(name, sym->name());
190 EXPECT_EQ(result1, sym->name());
191
192 char s[16];
193 strcpy(s, result1);
194 const char *result2 = m_pTestee->insertString(result1);
195 const char *result3 = m_pTestee->insertString(s);
196
197 EXPECT_EQ(result1, result2);
198 EXPECT_EQ(result1, result3);
199}
200
201
202TEST_F( StrSymPoolTest, insert_16384_weak_reference_symbols ) {
203 char name[16];
204 bool isDyn = false;
205 LDSymbol::Type type = LDSymbol::Reference;
206 LDSymbol::Binding binding = LDSymbol::Weak;
207 const llvm::MCSectionData *section = 0;
208 uint64_t value = 0;
209 uint64_t size = 0;
210 uint8_t other = 0;
211 strcpy(name, "Hello MCLinker");
212 LDSymbol *syms[128][128];
213 for(int i=0; i<128 ;++i) {
214 name[0] = i;
215 for(int j=0; j<128 ;++j) {
216 name[1] = j;
217 syms[i][j] = m_pTestee->insertSymbol(name,
218 isDyn,
219 type,
220 binding,
221 section,
222 value,
223 size,
224 other);
225
226 ASSERT_STREQ(name, syms[i][j]->name());
227 }
228 }
229 for(int i=127; i>=0 ;--i) {
230 name[0] = i;
231 for(int j=0; j<128 ;++j) {
232 name[1] = j;
233 LDSymbol *sym = m_pTestee->insertSymbol(name,
234 isDyn,
235 type,
236 binding,
237 section,
238 value,
239 size,
240 other);
241 ASSERT_EQ(sym, syms[i][j]);
242 }
243 }
244 for(int i=0; i<128 ;++i) {
245 name[0] = i;
246 for(int j=0; j<128 ;++j) {
247 name[1] = j;
248 LDSymbol *sym = m_pTestee->insertSymbol(name,
249 isDyn,
250 type,
251 binding,
252 section,
253 value,
254 size,
255 other);
256 ASSERT_EQ(sym, syms[i][j]);
257 }
258 }
259}