-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
364 lines (224 loc) · 8.4 KB
/
test.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#include "include/test.h"
#include "include/tree23.h"
//--#include "include/debug-printer.h"
#include <iostream>
#include <iterator>
#include <algorithm>
using namespace std;
void run_test(const vector<int>& test_case, void (*f)(const std::vector<int>&))
{
cout << "Input vector<int> to next case is: \n";
copy(test_case.begin(), test_case.end(), ostream_iterator<int>(cout, ", "));
cout << endl << flush;
try {
(*f)(test_case);
} catch(...) {
cout << "An uncaught exception occured:\n" << endl;
}
cout << "\n================================" << endl;
}
void run_tests(const std::vector<std::vector<int>>& other_cases, const std::vector<int>& base_case, void (*f)(const std::vector<int>&))
{
for( const auto& append_vec : other_cases) {
vector<int> test_case {base_case};
auto key_break = test_case.size();
copy(append_vec.begin(), append_vec.end(), back_inserter(test_case));
random_shuffle(test_case.begin(), test_case.end());
cout << "Input vector<int> to next case is: \n";
copy(test_case.begin(), test_case.end(), ostream_iterator<int>(cout, ", "));
cout << endl << flush;
try {
(*f)(test_case);
} catch(...) {
cout << "An uncaught exception occured:\n" << endl;
}
cout << "\n================================" << endl;
}
}
tree23<int, int> insert_vec_into_tree(const vector<int>& test_case)
{
tree23<int, int> tree;
for (auto& key : test_case) {
cout << "Inserting: " << key << std::endl;
tree.insert(key, key); // debug start
cout << "Tree after insert of " << key;
tree.printlevelOrder(cout); // debug end
cout << std::endl;
}
return tree;
}
void test_insert(const vector<int>& test_case)
{
tree23<int, int> tree;
int i = 0;
tree = insert_vec_into_tree(test_case);
print_tree(tree); // or to print debug info change to: debug_print_tree(tree);
cout << flush << "\nTesting find(v) of all keys v inserted so for. First, reprinting the tree.\n" << flush;
print_tree(tree);
for (auto key : test_case) {
string result_msg = tree.find(key) ? " found " : " not found ";
cout << "Key " << key << " was " << result_msg << endl;
}
return;
}
void test_remove(const std::vector<int>& test_case)
{
tree23<int, int> tree(insert_vec_into_tree(test_case));
vector<int> removal_vec{test_case}; // make copy of test_case vector
random_shuffle(removal_vec.begin(), removal_vec.end()); // and scramble it.
cout << "Printing the tree before removals start.\n";
// print_tree(tree);
tree.printlevelOrder(cout);
for(auto& key : removal_vec) {
cout << "\nRemoving key: " << key << endl;
tree.remove(key);
cout << "\nPrinting tree in order after removal: \n";
print_tree(tree);
}
cout << flush << "\nTesting find(v) of all keys v removed so for. First, reprinting the tree.\n" << flush;
print_tree(tree);
for (auto& key : removal_vec) {
string result_msg = tree.find(key) ? " found " : " not found ";
cout << "Key " << key << " was " << result_msg << endl;
}
}
void test_copy_ctor(const std::vector<int>& input)
{
tree23<int, int> tree{ insert_vec_into_tree(input) };
tree23<int, int> tree_copy{tree};
auto lambda_closure = [](const pair<int, int>& pr){ cout << pr.first << ", "; };
cout << "input tree first followed by its copy" << endl;
tree.inOrderTraverse(lambda_closure);
cout << "\n";
}
void test_const_iterator_methods(const std::vector<int>& input)
{
const tree23<int, int> tree{ insert_vec_into_tree(input)};
tree23<int, int>::const_iterator citer{ tree.begin() };
tree23<int, int>::const_iterator citerb{ tree.begin() };
cout << "Testing const_iterator comparison operators == and !=.\n" << endl << flush;
if (citer == citerb) {
cout << "citer == citerb\n" << endl << flush;
}
if (citer != citerb) {
cout << "citer != citerb\n" << endl << flush;
}
cout << "Testing const_iterator move constructor:\ntree23<int, int>::const_iterator citer2 {std::move(citer)};" << endl << flush;
tree23<int, int>::const_iterator citer2 {std::move(citer)};
cout << "Testing const_iterator move constructor:\ntree23<int, int>::const_iterator citer3 = citer2;" << endl << flush;
tree23<int, int>::const_iterator citer3 = citer2; // Invokes the copy constructor.
auto key_value1 = *--citer3;
/*
const tree23<int, int>::KeyValue& key_value2 = *++citer3;
const tree23<int, int>::KeyValue& key_value3 = *++citer3;
*/
const pair<const int, int>& key_value2 = *++citer3;
const pair<const int, int>& key_value3 = *++citer3;
int debug = 10;
++debug;
}
void test_nonconst_iterator(const std::vector<int>& input)
{
tree23<int, int> tree = insert_vec_into_tree(input);
tree23<int, int> tree_copy{tree};
auto lambda_closure = [](const pair<const int, int>& key_value ){ cout << key_value.first << ", "; };
cout << "input tree first followed by its copy" << endl;
tree.inOrderTraverse(lambda_closure);
cout << "\n";
cout << "Printing with non-const iterator\n";
using nonconst_tree23 = tree23<int, int>;
tree23<int, int> nonconst_tree = const_cast<nonconst_tree23&>(tree);
tree23<int, int>::iterator iter = nonconst_tree.begin();
for (; iter != nonconst_tree.end(); ++iter) {
//--cout << (*iter).key << ", " << endl << flush;
cout << (*iter).first << ", " << endl << flush;
}
cout << endl;
}
void test_forward_iterator(const std::vector<int>& test_case)
{
tree23<int, int> tree;
int i = 0;
for (auto& key : test_case) {
cout << "Inserting: " << key << std::endl;
tree.insert(key, key);
}
tree.printlevelOrder(cout);
cout << "\n\nIn order print of tree:\n";
auto lambda_closure = [](const pair<const int, int>& key_value ){ cout << key_value.first << ", "; };
tree.inOrderTraverse(lambda_closure);
cout << flush << "\nPrinting tree with iterator:\n";
try {
print_with_iterator(tree);
} catch (std::exception& e) {
cout << "\nException: " << e.what() << endl;
}
cout << flush;
return;
}
void print_with_iterator(const tree23<int, int>& tree)
{
for (const auto& pr : tree)
cout << pr.first << ", " << flush;
}
void test_backward_iterator(const std::vector<int>& input)
{
tree23<int, int> tree;
int i = 0;
for (auto& key : input) {
cout << "Inserting: " << key << std::endl;
tree.insert(key, key);
}
tree.printlevelOrder(cout);
cout << "\n\nPrinting tree with iterator:\n";
print_with_iterator(tree);
cout << flush << "\nPrinting tree with reverse iterator:\n";
try {
print_with_reverse_iterator(tree);
} catch (std::exception& e) {
cout << "\nException: " << e.what() << endl;
}
cout << flush;
return;
}
void print_with_reverse_iterator(const tree23<int, int>& tree)
{
for (auto it = tree.rbegin(); it != tree.rend(); ++it)
cout << (*it).first << ", " << flush;
}
void test_reverse_iterators(const vector<int>& input)
{
tree23<int, int> tree{ insert_vec_into_tree(input) };
print_with_const_reverse_iterator(tree);
}
void print_with_const_reverse_iterator(const tree23<int, int>& tree)
{
tree23<int, int>::const_reverse_iterator riter = tree.rbegin();
tree23<int, int>::const_reverse_iterator riter_end = tree.rend();
for(; riter != riter_end; ++riter) {
const tree23<int, int>::value_type& key_value = *riter;
cout << key_value.first << ", " << flush;
}
}
void print_tree(const tree23<int, int> & tree)
{
cout << endl;
for (auto &[key, value] : tree) {
cout << "{ " << key << ", " << value << "}, " << std::flush;
}
cout << endl << flush;
}
/*
void debug_print_tree(const tree23<int, int> & tree)
{
tree.printlevelOrder(cout);
DebugPrinter<tree23<int,int>> debug_printer(tree, cout);
cout << "\nLevel order debug print of tree: \n" << flush;
tree.levelOrderTraverse(debug_printer);
cout << endl;
cout << "\n\nIn order print of tree: ";
auto lambda_closure = [](const tree23<int, int>::value_type& key_value ){ cout << key_value.first << ' '; };
tree.inOrderTraverse(lambda_closure);
cout << endl << flush;
}
*/