blob: fdc71095fccdf7d770aaee021f3274cc3cdb8744 [file] [log] [blame]
Shih-wei Liao5460a1f2012-03-16 22:41:16 -07001//===- FactoriesTest.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 <cstdlib>
10#include "FactoriesTest.h"
11#include <string>
12
13using namespace mcld;
14using namespace mcldtest;
15
Shih-wei Liao5460a1f2012-03-16 22:41:16 -070016// Constructor can do set-up work for all test here.
Stephen Hines37b74a32014-11-26 18:48:20 -080017FactoriesTest::FactoriesTest() {
18 m_pNodeAlloc = new NodeAlloc();
19 m_pFileAlloc = new FileAlloc();
Shih-wei Liao5460a1f2012-03-16 22:41:16 -070020}
21
22// Destructor can do clean-up work that doesn't throw exceptions here.
Stephen Hines37b74a32014-11-26 18:48:20 -080023FactoriesTest::~FactoriesTest() {
24 delete m_pNodeAlloc;
25 delete m_pFileAlloc;
Shih-wei Liao5460a1f2012-03-16 22:41:16 -070026}
27
28// SetUp() will be called immediately before each test.
Stephen Hines37b74a32014-11-26 18:48:20 -080029void FactoriesTest::SetUp() {
Shih-wei Liao5460a1f2012-03-16 22:41:16 -070030}
31
32// TearDown() will be called immediately after each test.
Stephen Hines37b74a32014-11-26 18:48:20 -080033void FactoriesTest::TearDown() {
Shih-wei Liao5460a1f2012-03-16 22:41:16 -070034}
35
36//==========================================================================//
37// Testcases
38//
Stephen Hines37b74a32014-11-26 18:48:20 -080039TEST_F(FactoriesTest, node_produce) {
40 NodeAlloc::NodeType* node = m_pNodeAlloc->produce();
41 ASSERT_EQ(1, m_pNodeAlloc->size());
42 ASSERT_FALSE(m_pNodeAlloc->empty());
43 node = m_pNodeAlloc->produce();
44 ASSERT_EQ(2, m_pNodeAlloc->size());
45 ASSERT_FALSE(m_pNodeAlloc->empty());
46 node = m_pNodeAlloc->produce();
47 ASSERT_EQ(3, m_pNodeAlloc->size());
48 ASSERT_FALSE(m_pNodeAlloc->empty());
Shih-wei Liao5460a1f2012-03-16 22:41:16 -070049}
50
Stephen Hines37b74a32014-11-26 18:48:20 -080051TEST_F(FactoriesTest, node_iterate) {
52 NodeAlloc::NodeType* node = 0;
53 for (int i = 0; i < 100; ++i) {
54 node = m_pNodeAlloc->produce();
55 node->data = (int*)malloc(sizeof(int));
56 *(node->data) = i;
57 }
Shih-wei Liao5460a1f2012-03-16 22:41:16 -070058
Stephen Hines37b74a32014-11-26 18:48:20 -080059 int counter = 0;
60 NodeAlloc::iterator data = m_pNodeAlloc->begin();
61 NodeAlloc::iterator dEnd = m_pNodeAlloc->end();
62 for (; data != dEnd; ++data) {
63 ASSERT_EQ(counter, *(*data).data);
64 free((*data).data);
65 (*data).data = 0;
66 ++counter;
67 }
Shih-wei Liao5460a1f2012-03-16 22:41:16 -070068}
69
Stephen Hines37b74a32014-11-26 18:48:20 -080070TEST_F(FactoriesTest, node_delegate_empty) {
71 NodeAlloc::NodeType* node = 0;
72 for (int i = 0; i < 100; ++i) {
73 node = m_pNodeAlloc->produce();
74 node->data = (int*)malloc(sizeof(int));
75 *(node->data) = i;
76 }
77 NodeAlloc* delegatee = new NodeAlloc();
78 m_pNodeAlloc->delegate(*delegatee);
79 ASSERT_EQ(100, m_pNodeAlloc->size());
80 int counter = 0;
81 NodeAlloc::iterator data = m_pNodeAlloc->begin();
82 NodeAlloc::iterator dEnd = m_pNodeAlloc->end();
83 for (; data != dEnd; ++data) {
84 ASSERT_EQ(counter, *(*data).data);
85 free((*data).data);
86 (*data).data = 0;
87 ++counter;
88 }
89 delete delegatee;
Shih-wei Liao5460a1f2012-03-16 22:41:16 -070090}
91
Stephen Hines37b74a32014-11-26 18:48:20 -080092TEST_F(FactoriesTest, node_empty_delegate) {
93 NodeAlloc::NodeType* node = 0;
94 NodeAlloc* delegatee = new NodeAlloc();
95 for (int i = 0; i < 100; ++i) {
96 node = delegatee->produce();
97 node->data = (int*)malloc(sizeof(int));
98 *(node->data) = i;
99 }
100 m_pNodeAlloc->delegate(*delegatee);
101 ASSERT_EQ(100, m_pNodeAlloc->size());
102 int counter = 0;
103 NodeAlloc::iterator data = m_pNodeAlloc->begin();
104 NodeAlloc::iterator dEnd = m_pNodeAlloc->end();
105 for (; data != dEnd; ++data) {
106 ASSERT_EQ(counter, *(*data).data);
107 free((*data).data);
108 (*data).data = 0;
109 ++counter;
110 }
111 ASSERT_EQ(0, delegatee->size());
112 ASSERT_TRUE(delegatee->empty());
113 delete delegatee;
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700114}
115
Stephen Hines37b74a32014-11-26 18:48:20 -0800116TEST_F(FactoriesTest, node_delegate) {
117 NodeAlloc::NodeType* node = 0;
118 NodeAlloc* delegatee = new NodeAlloc();
119 int counter = 0;
120 // produce agent
121 for (int i = 0; i < 100; ++i) {
122 node = m_pNodeAlloc->produce();
123 node->data = (int*)malloc(sizeof(int));
124 *(node->data) = counter;
125 ++counter;
126 }
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700127
Stephen Hines37b74a32014-11-26 18:48:20 -0800128 // produce delegatee
129 for (int i = 0; i < 100; ++i) {
130 node = delegatee->produce();
131 node->data = (int*)malloc(sizeof(int));
132 *(node->data) = counter;
133 ++counter;
134 }
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700135
Stephen Hines37b74a32014-11-26 18:48:20 -0800136 m_pNodeAlloc->delegate(*delegatee);
137 ASSERT_EQ(200, m_pNodeAlloc->size());
138 ASSERT_FALSE(m_pNodeAlloc->empty());
139 NodeAlloc::iterator data = m_pNodeAlloc->begin();
140 NodeAlloc::iterator dEnd = m_pNodeAlloc->end();
141 for (counter = 0; data != dEnd; ++data) {
142 ASSERT_EQ(counter, *(*data).data);
143 free((*data).data);
144 (*data).data = 0;
145 ++counter;
146 }
147 ASSERT_EQ(0, delegatee->size());
148 ASSERT_TRUE(delegatee->empty());
149 delete delegatee;
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700150}
151
Stephen Hines37b74a32014-11-26 18:48:20 -0800152TEST_F(FactoriesTest, node_delegate_self) {
153 NodeAlloc::NodeType* node = 0;
154 for (int i = 0; i < 100; ++i) {
155 node = m_pNodeAlloc->produce();
156 node->data = (int*)malloc(sizeof(int));
157 *(node->data) = i;
158 }
159 ASSERT_EQ(100, m_pNodeAlloc->size());
160 m_pNodeAlloc->delegate(*m_pNodeAlloc);
161 ASSERT_EQ(100, m_pNodeAlloc->size());
162 ASSERT_FALSE(m_pNodeAlloc->empty());
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700163}
164
Stephen Hines37b74a32014-11-26 18:48:20 -0800165TEST_F(FactoriesTest, file_produce) {
166 int counter = 0;
167 for (counter = 1; counter < 1000; ++counter) {
168 MCLDFile* file = m_pFileAlloc->produce();
169 ASSERT_EQ(counter, m_pFileAlloc->size());
170 ASSERT_FALSE(m_pFileAlloc->empty());
171 }
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700172}
173
Stephen Hines37b74a32014-11-26 18:48:20 -0800174TEST_F(FactoriesTest, file_produce_by_params) {
175 int counter = 0;
176 for (counter = 1; counter < 1000; ++counter) {
177 char name[100];
178 sprintf(name, "file %d", counter);
179 char path_name[100];
180 sprintf(path_name, "/proj/mtk%d", counter);
181 MCLDFile* file = m_pFileAlloc->produce(
182 string(name), sys::fs::Path(string(path_name)), MCLDFile::Archive);
183 ASSERT_EQ(counter, m_pFileAlloc->size());
184 ASSERT_FALSE(m_pFileAlloc->empty());
185 ASSERT_TRUE(file->isRecognized());
186 ASSERT_STREQ(name, file->name().data());
187 }
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700188}
189
Stephen Hines37b74a32014-11-26 18:48:20 -0800190TEST_F(FactoriesTest, file_iterate) {
191 int counter = 0;
192 for (counter = 1; counter < 1000; ++counter) {
193 char name[100];
194 sprintf(name, "file %d", counter);
195 char path_name[100];
196 sprintf(path_name, "/proj/mtk%d", counter);
197 MCLDFile* file = m_pFileAlloc->produce(
198 string(name), sys::fs::Path(string(path_name)), MCLDFile::Archive);
199 }
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700200
Stephen Hines37b74a32014-11-26 18:48:20 -0800201 ASSERT_EQ(counter - 1, m_pFileAlloc->size());
202 ASSERT_FALSE(m_pFileAlloc->empty());
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700203
Stephen Hines37b74a32014-11-26 18:48:20 -0800204 MCLDFileFactory::iterator file = m_pFileAlloc->begin();
205 MCLDFileFactory::iterator fEnd = m_pFileAlloc->end();
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700206
Stephen Hines37b74a32014-11-26 18:48:20 -0800207 while (file != fEnd) {
208 ASSERT_TRUE((*file).isRecognized());
209 ASSERT_FALSE((*file).name().empty());
210 ++file;
211 }
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700212}