1 //===- implTest.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/MC/SymbolCategory.h"
10 #include "mcld/LD/ResolveInfo.h"
11 #include "mcld/LD/LDSymbol.h"
12 #include <iostream>
13 #include "SymbolCategoryTest.h"
14
15 using namespace std;
16 using namespace mcld;
17 using namespace mcldtest;
18
19 // Constructor can do set-up work for all test here.
SymbolCategoryTest()20 SymbolCategoryTest::SymbolCategoryTest() {
21 // create testee. modify it if need
22 m_pTestee = new SymbolCategory();
23 }
24
25 // Destructor can do clean-up work that doesn't throw exceptions here.
~SymbolCategoryTest()26 SymbolCategoryTest::~SymbolCategoryTest() {
27 delete m_pTestee;
28 }
29
30 // SetUp() will be called immediately before each test.
SetUp()31 void SymbolCategoryTest::SetUp() {
32 }
33
34 // TearDown() will be called immediately after each test.
TearDown()35 void SymbolCategoryTest::TearDown() {
36 }
37
38 //==========================================================================//
39 // Testcases
40 //
41
TEST_F(SymbolCategoryTest,upward_test)42 TEST_F(SymbolCategoryTest, upward_test) {
43 ResolveInfo* a = ResolveInfo::Create("a");
44 ResolveInfo* b = ResolveInfo::Create("b");
45 ResolveInfo* c = ResolveInfo::Create("c");
46 ResolveInfo* d = ResolveInfo::Create("d");
47 ResolveInfo* e = ResolveInfo::Create("e");
48 e->setBinding(ResolveInfo::Global);
49 d->setBinding(ResolveInfo::Weak);
50 c->setDesc(ResolveInfo::Common);
51 c->setBinding(ResolveInfo::Global);
52 b->setBinding(ResolveInfo::Local);
53 a->setType(ResolveInfo::File);
54
55 LDSymbol* aa = LDSymbol::Create(*a);
56 LDSymbol* bb = LDSymbol::Create(*b);
57 LDSymbol* cc = LDSymbol::Create(*c);
58 LDSymbol* dd = LDSymbol::Create(*d);
59 LDSymbol* ee = LDSymbol::Create(*e);
60
61 m_pTestee->add(*ee);
62 m_pTestee->add(*dd);
63 m_pTestee->add(*cc);
64 m_pTestee->add(*bb);
65 m_pTestee->add(*aa);
66
67 SymbolCategory::iterator sym = m_pTestee->begin();
68 ASSERT_STREQ("a", (*sym)->name());
69 ++sym;
70 ASSERT_STREQ("b", (*sym)->name());
71 ++sym;
72 ASSERT_STREQ("c", (*sym)->name());
73 ++sym;
74 ASSERT_STREQ("d", (*sym)->name());
75 ++sym;
76 ASSERT_STREQ("e", (*sym)->name());
77
78 ASSERT_TRUE(1 == m_pTestee->numOfLocals());
79 ASSERT_TRUE(1 == m_pTestee->numOfCommons());
80 ASSERT_TRUE(2 == m_pTestee->numOfDynamics());
81 ASSERT_TRUE(5 == m_pTestee->numOfSymbols());
82 }
83
TEST_F(SymbolCategoryTest,change_local_to_dynamic)84 TEST_F(SymbolCategoryTest, change_local_to_dynamic) {
85 ResolveInfo* a = ResolveInfo::Create("a");
86 ResolveInfo* b = ResolveInfo::Create("b");
87 ResolveInfo* c = ResolveInfo::Create("c");
88 ResolveInfo* d = ResolveInfo::Create("d");
89 ResolveInfo* e = ResolveInfo::Create("e");
90
91 a->setBinding(ResolveInfo::Local);
92 b->setBinding(ResolveInfo::Local);
93 c->setBinding(ResolveInfo::Local);
94 d->setDesc(ResolveInfo::Common);
95 d->setBinding(ResolveInfo::Global);
96 e->setBinding(ResolveInfo::Global);
97
98 LDSymbol* aa = LDSymbol::Create(*a);
99 LDSymbol* bb = LDSymbol::Create(*b);
100 LDSymbol* cc = LDSymbol::Create(*c);
101 LDSymbol* dd = LDSymbol::Create(*d);
102 LDSymbol* ee = LDSymbol::Create(*e);
103
104 a->setSymPtr(aa);
105 b->setSymPtr(bb);
106 c->setSymPtr(cc);
107 d->setSymPtr(dd);
108 e->setSymPtr(ee);
109
110 m_pTestee->add(*ee);
111 m_pTestee->add(*dd);
112 m_pTestee->add(*cc);
113 m_pTestee->add(*bb);
114 m_pTestee->add(*aa);
115
116 SymbolCategory::iterator sym = m_pTestee->begin();
117 ASSERT_STREQ("c", (*sym)->name());
118 ++sym;
119 ASSERT_STREQ("b", (*sym)->name());
120 ++sym;
121 ASSERT_STREQ("a", (*sym)->name());
122 ++sym;
123 ASSERT_STREQ("d", (*sym)->name());
124 ++sym;
125 ASSERT_STREQ("e", (*sym)->name());
126
127 m_pTestee->changeToDynamic(*bb);
128
129 sym = m_pTestee->begin();
130 ASSERT_STREQ("c", (*sym)->name());
131 ++sym;
132 ASSERT_STREQ("a", (*sym)->name());
133 ++sym;
134 ASSERT_STREQ("b", (*sym)->name());
135 ++sym;
136 ASSERT_STREQ("d", (*sym)->name());
137 ++sym;
138 ASSERT_STREQ("e", (*sym)->name());
139 }
140