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