/
main.cpp
293 lines (240 loc) · 6.57 KB
/
main.cpp
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Simple tests.
*/
#include <iostream>
#include <cstdarg>
#include <sstream>
#define DEBUG
#include "cmybinarytree.h"
#include "ilogger.h"
#include "celapsedtime.h"
using namespace std;
class CLogger: public ILogger
{
public:
CLogger()
{}
void print(const char a_acArg[] ...)
{
std::string _strOutput; const char* _pcOpt;
va_list _pArgument;
va_start(_pArgument,a_acArg);
for(_pcOpt = a_acArg;*_pcOpt != '\0';_pcOpt++)
{
while( *_pcOpt != '%' )
{
_strOutput += *_pcOpt;
_pcOpt++;
}
_pcOpt++;
switch(*_pcOpt)
{
case 'i':
case 'I':
{
std::ostringstream s;
s<<va_arg(_pArgument,int);
_strOutput.append(s.str());
}
break;
case 'd':
case 'D':
{
std::ostringstream s;
s<<va_arg(_pArgument,double);
_strOutput.append(s.str());
}
break;
case 's':
case 'S':
{
_strOutput.append(va_arg(_pArgument,char*));
}
break;
}
}
cout<<_strOutput<<endl;
va_end(_pArgument);
}
};
CLogger g_oCLogger = CLogger();
void printRes(bool a_fRes, const char* a_cName)
{
using namespace std;
cout<<"Test case: "<<a_cName<<" : "<<(a_fRes==true?"PASSED":"FAILED")<<endl;
}
#define PRINT_RES(a) printRes(a,__func__)
class CTest
{
public:
CTest(int a):member(a){}
int member;
friend bool operator >(const CTest &lvalue, const CTest &rvalue)
{
return (lvalue.member > rvalue.member);
}
friend bool operator <(const CTest &lvalue, const CTest &rvalue)
{
return (lvalue.member < rvalue.member);
}
friend bool operator <=(const CTest &lvalue, const CTest &rvalue)
{
return (lvalue.member <= rvalue.member);
}
friend bool operator ==(const CTest &lvalue, const CTest &rvalue)
{
return (lvalue.member == rvalue.member);
}
static void printTest(CTest var)
{
std::cout<<"Wartosc: "<<var.member<<std::endl;
}
};
const int g_iCount = 10000;
bool Test_1_adding_finding_items_I();
bool Test_2_adding_finding_items_R();
bool Test_3_removing_items();
int main()
{
bool fResult = true;
fResult &= Test_1_adding_finding_items_I();
fResult &= Test_2_adding_finding_items_R();
fResult &= Test_3_removing_items();
cout<<"Test result: "<<(fResult==true?"PASSED":"FAILED")<<endl;
return 0;
}
bool Test_1_adding_finding_items_I()
{
CElapsedTime oETime = CElapsedTime(&g_oCLogger);
CMyBinaryTree<CTest> tree; // tree
const int liczbaElementow = g_iCount;
bool fResult = true;
for(int i=0; i<liczbaElementow;i++)
{
CTest ob(i);
fResult &= tree.push(ob, METHOD_ITERATION);
}
for(int i=0;i<liczbaElementow;i++)
{
CTest ob(i);
fResult &= tree.find(ob, METHOD_ITERATION);
}
fResult &= (tree.getItemsNumber() == liczbaElementow);
PRINT_RES(fResult);
return fResult;
}
bool Test_2_adding_finding_items_R()
{
CElapsedTime oETime = CElapsedTime(&g_oCLogger);
CMyBinaryTree<CTest> tree;
const int liczbaElementow = g_iCount;
bool fResult = true;
for(int i=0; i<liczbaElementow;i++)
{
CTest ob(i);
fResult &= tree.push(ob, METHOD_RECURSION);
}
for(int i=0;i<liczbaElementow;i++)
{
CTest ob(i);
fResult &= tree.find(ob, METHOD_RECURSION);
}
fResult &= (tree.getItemsNumber() == liczbaElementow);
PRINT_RES(fResult);
return fResult;
}
bool Test_removing_items_no_right_child();
bool Test_removing_items_no_left_child();
bool Test_removing_items_right_and_left();
bool Test_removing_items_foreign_item();
bool Test_removing_last_item();
bool Test_3_removing_items()
{
CElapsedTime oETime = CElapsedTime(&g_oCLogger);
#define DEBUG
bool fResult = true;
fResult &= Test_removing_items_no_right_child();
fResult &= Test_removing_items_no_left_child();
fResult &= Test_removing_items_right_and_left();
fResult &= Test_removing_items_foreign_item();
fResult &= Test_removing_last_item();
PRINT_RES(fResult);
return fResult;
}
bool Test_removing_items_no_right_child()
{
CMyBinaryTree<CTest> tree;
const int liczbaElementow = 5;
bool fResult = true;
for(int i=0; i<liczbaElementow;i++)
{
CTest ob(i);
fResult &= tree.push(ob, METHOD_RECURSION);
}
// remove last item
fResult &= tree.remove(CTest(liczbaElementow - 2));
PRINT_RES(fResult);
return fResult;
}
bool Test_removing_items_no_left_child()
{
bool fResult = true;
// build the tree
CMyBinaryTree<CTest> tree;
CTest ob5(5); CTest ob4(4); CTest ob3(3); CTest ob6(6);
tree.push(ob5);
tree.push(ob4);
tree.push(ob3);
tree.push(ob6);
fResult &= tree.remove(ob4);
PRINT_RES(fResult);
return fResult;
}
bool Test_removing_items_right_and_left()
{
bool fResult = true;
CMyBinaryTree<CTest> tree3;
CTest ob1(1); CTest ob2(2); CTest ob10(10); CTest ob12(12);
CTest ob8(8); CTest ob6(6); CTest ob9(9); CTest ob5(5);
tree3.push(ob1);
tree3.push(ob2);
tree3.push(ob10);
tree3.push(ob8);
tree3.push(ob12);
tree3.push(ob6);
tree3.push(ob9);
tree3.push(ob5);
PRINT_RES(fResult);
return fResult;
}
bool Test_removing_items_foreign_item()
{
CMyBinaryTree<CTest> tree;
const int liczbaElementow = 5;
bool fResult = true;
for(int i=0; i<liczbaElementow;i++)
{
CTest ob(i);
fResult &= tree.push(ob);
}
for(int i=0; i<liczbaElementow-2;i++)
fResult &= tree.remove(CTest(liczbaElementow + 10));
PRINT_RES((fResult == false));
return (fResult == false);
}
bool Test_removing_last_item()
{
CMyBinaryTree<CTest> tree;
const int liczbaElementow = g_iCount;
bool fResult = true;
for(int i=0; i<liczbaElementow;i++)
{
CTest ob(i);
fResult &= tree.push(ob);
}
// remove last item
for(int i=0; i<liczbaElementow;i++)
fResult &= tree.remove(CTest(liczbaElementow -i-1));
PRINT_RES(fResult);
return fResult;
}