summaryrefslogtreecommitdiff
path: root/unittests/NamePoolTest.cpp
blob: 614ee6a4f1a20289b578d6a1321a981e946b03c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
//===- NamePoolTest.cpp ---------------------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "NamePoolTest.h"
#include <mcld/LD/NamePool.h>
#include <mcld/LD/Resolver.h>
#include <mcld/LD/StaticResolver.h>
#include <mcld/LD/ResolveInfo.h>
#include <mcld/LD/LDSymbol.h>
#include <llvm/ADT/StringRef.h>
#include <string>
#include <cstdio>

using namespace mcld;
using namespace mcldtest;


// Constructor can do set-up work for all test here.
NamePoolTest::NamePoolTest()
{
  // create testee. modify it if need
  StaticResolver resolver;
  m_pTestee = new NamePool(resolver, 10);
}

// Destructor can do clean-up work that doesn't throw exceptions here.
NamePoolTest::~NamePoolTest()
{
  delete m_pTestee;
}

// SetUp() will be called immediately before each test.
void NamePoolTest::SetUp()
{
}

// TearDown() will be called immediately after each test.
void NamePoolTest::TearDown()
{
}

//==========================================================================//
// Testcases
//


TEST_F( NamePoolTest, insertString ) {
  const char *s1 = "Hello MCLinker";
  llvm::StringRef result1 = m_pTestee->insertString(s1);
  EXPECT_NE(s1, result1.data());
  EXPECT_STREQ(s1, result1.data());
}

TEST_F( NamePoolTest, insertSameString ) {
  const char *s1 = "Hello MCLinker";
  std::string s2(s1);
  llvm::StringRef result1 = m_pTestee->insertString(s1);
  llvm::StringRef result2 = m_pTestee->insertString(s2.c_str());
  EXPECT_STREQ(s1, result1.data());
  EXPECT_STREQ(s2.c_str(), result2.data());
  EXPECT_EQ(result1.data(), result2.data());
}

TEST_F( NamePoolTest, insert_local_defined_Symbol ) {
  const char *name = "Hello MCLinker";
  bool isDyn = false;
  ResolveInfo::Type type = ResolveInfo::Function;
  ResolveInfo::Desc desc = ResolveInfo::Define;
  ResolveInfo::Binding binding = ResolveInfo::Local;
  uint64_t value = 0;
  uint64_t size = 0;
  ResolveInfo::Visibility other = ResolveInfo::Default;
  Resolver::Result result1;
  m_pTestee->insertSymbol(name,
                          isDyn,
                          type,
                          desc,
                          binding,
                          size,
                          other,
                          NULL,
                          result1);

  EXPECT_NE(name, result1.info->name());
  EXPECT_STREQ(name, result1.info->name());
  EXPECT_EQ(isDyn, result1.info->isDyn());
  EXPECT_EQ(type, result1.info->type());
  EXPECT_EQ(desc, result1.info->desc());
  EXPECT_EQ(binding, result1.info->binding());
  EXPECT_EQ(size, result1.info->size());
  EXPECT_EQ(other, result1.info->visibility());

  Resolver::Result result2;
  m_pTestee->insertSymbol(name,
                          isDyn,
                          type,
                          desc,
                          binding,
                          size,
                          other,
                          NULL,
                          result2);

  EXPECT_NE(name, result1.info->name());
  EXPECT_STREQ(name, result1.info->name());
  EXPECT_EQ(isDyn, result1.info->isDyn());
  EXPECT_EQ(type, result1.info->type());
  EXPECT_EQ(desc, result1.info->desc());
  EXPECT_EQ(binding, result1.info->binding());
  EXPECT_EQ(size, result1.info->size());
  EXPECT_EQ(other, result1.info->visibility());

  EXPECT_NE(result1.existent, result2.existent);
}

TEST_F( NamePoolTest, insert_global_reference_Symbol ) {
  const char *name = "Hello MCLinker";
  bool isDyn = false;
  ResolveInfo::Type type = ResolveInfo::NoType;
  ResolveInfo::Desc desc = ResolveInfo::Undefined;
  ResolveInfo::Binding binding = ResolveInfo::Global;
  uint64_t size = 0;
  ResolveInfo::Visibility other = ResolveInfo::Default;
  Resolver::Result result1;
  m_pTestee->insertSymbol(name,
                          isDyn,
                          type,
                          desc,
                          binding,
                          size,
                          other,
                          NULL,
                          result1);

  EXPECT_NE(name, result1.info->name());
  EXPECT_STREQ(name, result1.info->name());
  EXPECT_EQ(isDyn, result1.info->isDyn());
  EXPECT_EQ(type, result1.info->type());
  EXPECT_EQ(desc, result1.info->desc());
  EXPECT_EQ(binding, result1.info->binding());
  EXPECT_EQ(size, result1.info->size());
  EXPECT_EQ(other, result1.info->visibility());

  Resolver::Result result2;
  m_pTestee->insertSymbol(name,
                          isDyn,
                          type,
                          desc,
                          binding,
                          size,
                          other,
                          NULL,
                          result2);

  EXPECT_EQ(result1.info, result2.info);

  Resolver::Result result3;
  m_pTestee->insertSymbol("Different Symbol",
                          isDyn,
                          type,
                          desc,
                          binding,
                          size,
                          other,
                          NULL,
                          result3);


  EXPECT_NE(result1.info, result3.info);
}


TEST_F( NamePoolTest, insertSymbol_after_insert_same_string ) {
  const char *name = "Hello MCLinker";
  bool isDyn = false;
  LDSymbol::Type type = LDSymbol::Defined;
  LDSymbol::Binding binding = LDSymbol::Global;
  const llvm::MCSectionData *section = 0;
  uint64_t value = 0;
  uint64_t size = 0;
  uint8_t other = 0;

  const char *result1 =  m_pTestee->insertString(name);
  LDSymbol *sym =  m_pTestee->insertSymbol(name,
                                           isDyn,
                                           type,
                                           binding,
                                           section,
                                           value,
                                           size,
                                           other);

  EXPECT_STREQ(name, sym->name());
  EXPECT_EQ(result1, sym->name());

  char s[16];
  strcpy(s, result1);
  const char *result2 = m_pTestee->insertString(result1);
  const char *result3 = m_pTestee->insertString(s);

  EXPECT_EQ(result1, result2);
  EXPECT_EQ(result1, result3);
}


TEST_F( NamePoolTest, insert_16384_weak_reference_symbols ) {
  char name[16];
  bool isDyn = false;
  LDSymbol::Type type = LDSymbol::Reference;
  LDSymbol::Binding binding = LDSymbol::Weak;
  const llvm::MCSectionData *section = 0;
  uint64_t value = 0;
  uint64_t size = 0;
  uint8_t other = 0;
  strcpy(name, "Hello MCLinker");
  LDSymbol *syms[128][128];
  for(int i=0; i<128 ;++i) {
    name[0] = i;
    for(int j=0; j<128 ;++j) {
      name[1] = j;
      syms[i][j] =  m_pTestee->insertSymbol(name,
                                            isDyn,
                                            type,
                                            binding,
                                            section,
                                            value,
                                            size,
                                            other);

      ASSERT_STREQ(name, syms[i][j]->name());
    }
  }
  for(int i=127; i>=0 ;--i) {
    name[0] = i;
    for(int j=0; j<128 ;++j) {
      name[1] = j;
      LDSymbol *sym =  m_pTestee->insertSymbol(name,
                                               isDyn,
                                               type,
                                               binding,
                                               section,
                                               value,
                                               size,
                                               other);
      ASSERT_EQ(sym, syms[i][j]);
    }
  }
  for(int i=0; i<128 ;++i) {
    name[0] = i;
    for(int j=0; j<128 ;++j) {
      name[1] = j;
      LDSymbol *sym =  m_pTestee->insertSymbol(name,
                                               isDyn,
                                               type,
                                               binding,
                                               section,
                                               value,
                                               size,
                                               other);
      ASSERT_EQ(sym, syms[i][j]);
    }
  }
}