summaryrefslogtreecommitdiff
path: root/unittests/FactoriesTest.cpp
blob: fdc71095fccdf7d770aaee021f3274cc3cdb8744 (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
//===- FactoriesTest.cpp --------------------------------------------------===//
//
//                     The MCLinker Project
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include <cstdlib>
#include "FactoriesTest.h"
#include <string>

using namespace mcld;
using namespace mcldtest;

// Constructor can do set-up work for all test here.
FactoriesTest::FactoriesTest() {
  m_pNodeAlloc = new NodeAlloc();
  m_pFileAlloc = new FileAlloc();
}

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

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

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

//==========================================================================//
// Testcases
//
TEST_F(FactoriesTest, node_produce) {
  NodeAlloc::NodeType* node = m_pNodeAlloc->produce();
  ASSERT_EQ(1, m_pNodeAlloc->size());
  ASSERT_FALSE(m_pNodeAlloc->empty());
  node = m_pNodeAlloc->produce();
  ASSERT_EQ(2, m_pNodeAlloc->size());
  ASSERT_FALSE(m_pNodeAlloc->empty());
  node = m_pNodeAlloc->produce();
  ASSERT_EQ(3, m_pNodeAlloc->size());
  ASSERT_FALSE(m_pNodeAlloc->empty());
}

TEST_F(FactoriesTest, node_iterate) {
  NodeAlloc::NodeType* node = 0;
  for (int i = 0; i < 100; ++i) {
    node = m_pNodeAlloc->produce();
    node->data = (int*)malloc(sizeof(int));
    *(node->data) = i;
  }

  int counter = 0;
  NodeAlloc::iterator data = m_pNodeAlloc->begin();
  NodeAlloc::iterator dEnd = m_pNodeAlloc->end();
  for (; data != dEnd; ++data) {
    ASSERT_EQ(counter, *(*data).data);
    free((*data).data);
    (*data).data = 0;
    ++counter;
  }
}

TEST_F(FactoriesTest, node_delegate_empty) {
  NodeAlloc::NodeType* node = 0;
  for (int i = 0; i < 100; ++i) {
    node = m_pNodeAlloc->produce();
    node->data = (int*)malloc(sizeof(int));
    *(node->data) = i;
  }
  NodeAlloc* delegatee = new NodeAlloc();
  m_pNodeAlloc->delegate(*delegatee);
  ASSERT_EQ(100, m_pNodeAlloc->size());
  int counter = 0;
  NodeAlloc::iterator data = m_pNodeAlloc->begin();
  NodeAlloc::iterator dEnd = m_pNodeAlloc->end();
  for (; data != dEnd; ++data) {
    ASSERT_EQ(counter, *(*data).data);
    free((*data).data);
    (*data).data = 0;
    ++counter;
  }
  delete delegatee;
}

TEST_F(FactoriesTest, node_empty_delegate) {
  NodeAlloc::NodeType* node = 0;
  NodeAlloc* delegatee = new NodeAlloc();
  for (int i = 0; i < 100; ++i) {
    node = delegatee->produce();
    node->data = (int*)malloc(sizeof(int));
    *(node->data) = i;
  }
  m_pNodeAlloc->delegate(*delegatee);
  ASSERT_EQ(100, m_pNodeAlloc->size());
  int counter = 0;
  NodeAlloc::iterator data = m_pNodeAlloc->begin();
  NodeAlloc::iterator dEnd = m_pNodeAlloc->end();
  for (; data != dEnd; ++data) {
    ASSERT_EQ(counter, *(*data).data);
    free((*data).data);
    (*data).data = 0;
    ++counter;
  }
  ASSERT_EQ(0, delegatee->size());
  ASSERT_TRUE(delegatee->empty());
  delete delegatee;
}

TEST_F(FactoriesTest, node_delegate) {
  NodeAlloc::NodeType* node = 0;
  NodeAlloc* delegatee = new NodeAlloc();
  int counter = 0;
  // produce agent
  for (int i = 0; i < 100; ++i) {
    node = m_pNodeAlloc->produce();
    node->data = (int*)malloc(sizeof(int));
    *(node->data) = counter;
    ++counter;
  }

  // produce delegatee
  for (int i = 0; i < 100; ++i) {
    node = delegatee->produce();
    node->data = (int*)malloc(sizeof(int));
    *(node->data) = counter;
    ++counter;
  }

  m_pNodeAlloc->delegate(*delegatee);
  ASSERT_EQ(200, m_pNodeAlloc->size());
  ASSERT_FALSE(m_pNodeAlloc->empty());
  NodeAlloc::iterator data = m_pNodeAlloc->begin();
  NodeAlloc::iterator dEnd = m_pNodeAlloc->end();
  for (counter = 0; data != dEnd; ++data) {
    ASSERT_EQ(counter, *(*data).data);
    free((*data).data);
    (*data).data = 0;
    ++counter;
  }
  ASSERT_EQ(0, delegatee->size());
  ASSERT_TRUE(delegatee->empty());
  delete delegatee;
}

TEST_F(FactoriesTest, node_delegate_self) {
  NodeAlloc::NodeType* node = 0;
  for (int i = 0; i < 100; ++i) {
    node = m_pNodeAlloc->produce();
    node->data = (int*)malloc(sizeof(int));
    *(node->data) = i;
  }
  ASSERT_EQ(100, m_pNodeAlloc->size());
  m_pNodeAlloc->delegate(*m_pNodeAlloc);
  ASSERT_EQ(100, m_pNodeAlloc->size());
  ASSERT_FALSE(m_pNodeAlloc->empty());
}

TEST_F(FactoriesTest, file_produce) {
  int counter = 0;
  for (counter = 1; counter < 1000; ++counter) {
    MCLDFile* file = m_pFileAlloc->produce();
    ASSERT_EQ(counter, m_pFileAlloc->size());
    ASSERT_FALSE(m_pFileAlloc->empty());
  }
}

TEST_F(FactoriesTest, file_produce_by_params) {
  int counter = 0;
  for (counter = 1; counter < 1000; ++counter) {
    char name[100];
    sprintf(name, "file %d", counter);
    char path_name[100];
    sprintf(path_name, "/proj/mtk%d", counter);
    MCLDFile* file = m_pFileAlloc->produce(
        string(name), sys::fs::Path(string(path_name)), MCLDFile::Archive);
    ASSERT_EQ(counter, m_pFileAlloc->size());
    ASSERT_FALSE(m_pFileAlloc->empty());
    ASSERT_TRUE(file->isRecognized());
    ASSERT_STREQ(name, file->name().data());
  }
}

TEST_F(FactoriesTest, file_iterate) {
  int counter = 0;
  for (counter = 1; counter < 1000; ++counter) {
    char name[100];
    sprintf(name, "file %d", counter);
    char path_name[100];
    sprintf(path_name, "/proj/mtk%d", counter);
    MCLDFile* file = m_pFileAlloc->produce(
        string(name), sys::fs::Path(string(path_name)), MCLDFile::Archive);
  }

  ASSERT_EQ(counter - 1, m_pFileAlloc->size());
  ASSERT_FALSE(m_pFileAlloc->empty());

  MCLDFileFactory::iterator file = m_pFileAlloc->begin();
  MCLDFileFactory::iterator fEnd = m_pFileAlloc->end();

  while (file != fEnd) {
    ASSERT_TRUE((*file).isRecognized());
    ASSERT_FALSE((*file).name().empty());
    ++file;
  }
}