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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
|
/*************************************************************************
*
* Copyright (c) 2010 Kohei Yoshida
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
************************************************************************/
#include "test_global.hpp" // This must be the first header to be included.
#include "mdds/point_quad_tree.hpp"
#include <algorithm>
#include <memory>
#include <vector>
#include <sstream>
#include <boost/cstdint.hpp>
using namespace std;
using namespace mdds;
using ::boost::uint16_t;
struct data_printer
{
void operator()(const string* p)
{
cout << *p << " ";
}
};
template<typename _DbType>
struct search_result_printer
{
void operator()(const pair<const typename _DbType::point, const typename _DbType::value_type>& r) const
{
cout << " (x=" << r.first.x << ", y=" << r.first.y << ", value='" << *r.second << "')" << endl;
}
};
void pqt_test_basic()
{
stack_printer __stack_printer__("::pqt_test");
typedef point_quad_tree<uint16_t, const string*> db_type;
db_type db;
string A("A");
string B("B");
string C("C");
string D("D");
string E("E");
string F("F");
string G("G");
string H("H");
string I("I");
string J("J");
string K("K");
string L("L");
string M("M");
string N("N");
db.insert(25, 32, &A);
db.insert(5, 45, &B);
db.insert(52, 10, &C);
db.insert(80, 5, &D);
db.insert(40, 50, &E);
db.insert(10, 10, &F);
db.insert(20, 20, &G);
db.insert(80, 80, &H);
db.insert(58, 46, &I);
db.insert(36, 55, &J);
db.insert(26, 52, &K);
db.insert(38, 68, &L);
db.insert(39, 78, &M);
db.insert(72, 52, &N);
assert(db.size() == 14);
cout << "node count = " << get_node_instance_count() << endl;
db.dump_tree_svg("./obj/test.svg");
{
db_type::data_array_type result;
db.search_region(10, 10, 60, 20, result);
cout << "search region: (10, 10, 60, 20)" << endl;
cout << "result: ";
for_each(result.begin(), result.end(), data_printer());
cout << endl;
result.clear();
db.search_region(10, 10, 61, 61, result);
cout << "search region: (10, 10, 61, 61)" << endl;
cout << "result: ";
for_each(result.begin(), result.end(), data_printer());
cout << endl;
}
db_type::search_results result = db.search_region(10, 10, 60, 20);
db_type::search_results::const_iterator itr = result.begin(), itr_end = result.end();
cout << "result: " << endl;
for_each(result.begin(), result.end(), search_result_printer<db_type>());
result = db.search_region(10, 10, 61, 61);
itr = result.begin(), itr_end = result.end();
cout << "result: " << endl;
for_each(result.begin(), result.end(), search_result_printer<db_type>());
db.remove(20, 20);
db.remove(40, 50);
assert(db.size() == 12);
db.dump_tree_svg("./obj/test-remove.svg");
db.clear();
assert(db.empty());
assert(db.size() == 0);
}
void pqt_test_insertion_removal()
{
stack_printer __stack_printer__("::pqt_test_insertion_removal");
typedef point_quad_tree<int32_t, const string*> db_type;
db_type db;
// Check its empty-ness...
assert(db.empty());
assert(db.size() == 0);
// Create all data instances.
vector<unique_ptr<string>> data_store;
data_store.reserve(100);
for (size_t i = 0; i < 100; ++i)
{
ostringstream os;
os << "0x" << hex << i;
data_store.emplace_back(new string(os.str()));
}
vector<db_type::node_data> expected;
// Insert data one by one, and verify each insertion.
for (int32_t i = 0; i < 10; ++i)
{
for (int32_t j = 0; j < 10; ++j)
{
int32_t x = i * 10 + 1, y = j * 10 + 1;
size_t index = i * 10 + j;
const string* data_ptr = data_store[index].get();
cout << "inserting '" << *data_ptr << "' at (" << x << "," << y << ")" << endl;
db.insert(x, y, data_ptr);
expected.push_back(db_type::node_data(x, y, data_ptr));
vector<db_type::node_data> stored_data;
db.get_all_stored_data(stored_data);
assert(stored_data.size() == (index + 1));
assert(db.size() == (index + 1));
assert(!db.empty());
bool success = db.verify_data(expected);
assert(success);
}
}
db.dump_tree_svg("./obj/pqt_test_insertion.svg");
// Remove data one by one, and check the size after each removal.
size_t node_count = 100;
for (int32_t i = 0; i < 10; ++i)
{
for (int32_t j = 0; j < 10; ++j)
{
int32_t x = i * 10 + 1, y = j * 10 + 1;
db.remove(x, y);
size_t n = db.size();
cout << "removing node at (" << x << "," << y << ") "
<< "size after removal: " << n << endl;
--node_count;
assert(node_count == n);
}
}
}
void pqt_test_remove_root()
{
stack_printer __stack_printer__("::pqt_test_remove_root");
typedef point_quad_tree<int32_t, const string*> db_type;
string O("O");
string NW("NW");
string NE("NE");
string SW("SW");
string SE("SE");
db_type db;
// Insert all data and verify their storage.
db.insert(10, 10, &O);
db.insert(20, 0, &NE);
db.insert(0, 0, &NW);
db.insert(20, 20, &SE);
db.insert(0, 20, &SW);
db.dump_tree_svg("./obj/pqt_test_remove_root-1.svg");
vector<db_type::node_data> expected;
expected.push_back(db_type::node_data(10, 10, &O));
expected.push_back(db_type::node_data(20, 0, &NE));
expected.push_back(db_type::node_data(0, 0, &NW));
expected.push_back(db_type::node_data(20, 20, &SE));
expected.push_back(db_type::node_data(0, 20, &SW));
bool success = db.verify_data(expected);
assert(success);
assert(db.size() == 5);
// Now, remove the root node.
db.remove(10, 10);
db.dump_tree_svg("./obj/pqt_test_remove_root-2.svg");
expected.clear();
expected.push_back(db_type::node_data(20, 0, &NE));
expected.push_back(db_type::node_data(0, 0, &NW));
expected.push_back(db_type::node_data(20, 20, &SE));
expected.push_back(db_type::node_data(0, 20, &SW));
success = db.verify_data(expected);
assert(success);
assert(db.size() == 4);
}
void pqt_test_equality()
{
stack_printer __stack_printer__("::pqt_test_equality");
typedef point_quad_tree<int32_t, const string*> db_type;
db_type db1, db2;
string A("A");
string B("B");
string C("C");
string D("D");
string E("E");
string F("F");
assert(db1 == db2); // both are empty.
db1.insert(0, 0, &A);
db2.insert(0, 0, &A);
assert(db1 == db2);
db1.remove(0, 0);
assert(db1 != db2);
db1.insert(0, 0, &B);
assert(db1 != db2);
db2.insert(0, 0, &B); // B overwrites A.
assert(db1 == db2); // Both should have B at (0,0).
db1.insert(1, 1, &C);
db2.insert(2, 2, &C);
assert(db1 != db2);
db1.insert(2, 2, &C);
db2.insert(1, 1, &C);
assert(db1 == db2);
// Inserting data in different orders should make no difference in equality.
db1.insert(1, 3, &D);
db1.insert(1, 4, &E);
db1.insert(1, 5, &F);
db2.insert(1, 5, &F);
db2.insert(1, 4, &E);
db2.insert(1, 3, &D);
assert(db1 == db2);
db1.remove(1, 4);
db2.remove(1, 4);
assert(db1 == db2);
// Make them empty again.
db1.clear();
db2.clear();
assert(db1 == db2);
}
void pqt_test_assignment()
{
stack_printer __stack_printer__("::pqt_test_assignment");
typedef point_quad_tree<int32_t, const string*> db_type;
db_type db1, db2;
string A("A");
string B("B");
string C("C");
string D("D");
string E("E");
string F("F");
db1.insert(0, 10, &A);
db1.insert(2, 5, &B);
db1.insert(-10, 2, &C);
db1.insert(5, 7, &D);
vector<db_type::node_data> expected;
expected.push_back(db_type::node_data(0, 10, &A));
expected.push_back(db_type::node_data(2, 5, &B));
expected.push_back(db_type::node_data(-10, 2, &C));
expected.push_back(db_type::node_data(5, 7, &D));
bool success = db1.verify_data(expected);
assert(success);
db2 = db1;
success = db2.verify_data(expected);
assert(success);
success = db1.verify_data(expected);
assert(success);
db2.insert(12, 45, &E);
db2.insert(20, 42, &F);
success = db2.verify_data(expected); // This should fail.
assert(!success);
db2 = db1; // Assign once again.
success = db2.verify_data(expected); // This now should succeed.
assert(success);
}
void pqt_test_swap()
{
stack_printer __stack_printer__("::pqt_test_swap");
typedef point_quad_tree<int32_t, const string*> db_type;
db_type db1, db2;
string A("A");
string B("B");
string C("C");
string D("D");
string E("E");
string F("F");
db1.insert(0, 10, &A);
db1.insert(2, 5, &B);
db1.insert(-10, 2, &C);
db1.insert(5, 7, &D);
vector<db_type::node_data> expected;
expected.push_back(db_type::node_data(0, 10, &A));
expected.push_back(db_type::node_data(2, 5, &B));
expected.push_back(db_type::node_data(-10, 2, &C));
expected.push_back(db_type::node_data(5, 7, &D));
bool success = db1.verify_data(expected);
assert(success);
assert(db2.empty());
db1.swap(db2);
assert(db1.empty());
assert(!db2.empty());
success = db2.verify_data(expected);
assert(success);
}
template<typename _DbType>
bool verify_find(
const _DbType& db, typename _DbType::key_type x, typename _DbType::key_type y,
const typename _DbType::value_type data)
{
try
{
typename _DbType::value_type found = db.find(x, y);
cout << "found at (" << x << "," << y << "): " << found << endl;
if (found == data)
return true;
}
catch (const typename _DbType::data_not_found&)
{
cout << "nothing found at (" << x << "," << y << ")" << endl;
}
return false;
}
void pqt_test_find()
{
stack_printer __stack_printer__("::pqt_test_find");
typedef point_quad_tree<int32_t, const string*> db_type;
db_type db;
string A("A");
string B("B");
string C("C");
string D("D");
string E("E");
string F("F");
db.insert(92, 27, &A);
db.insert(53, 26, &B);
db.insert(69, 18, &C);
db.insert(0, 78, &D);
db.insert(17, 7, &E);
db.insert(91, 88, &F);
assert(db.size() == 6);
db.dump_tree_svg("obj/pqt_test_find.svg");
bool check;
check = verify_find(db, 92, 27, &A);
assert(check);
check = verify_find(db, 53, 26, &B);
assert(check);
check = verify_find(db, 69, 18, &C);
assert(check);
check = verify_find(db, 0, 78, &D);
assert(check);
check = verify_find(db, 17, 7, &E);
assert(check);
check = verify_find(db, 91, 88, &F);
assert(check);
// Check for non-existent data.
check = verify_find(db, 34, 86, &A);
assert(!check);
check = verify_find(db, -1, 7, &A);
assert(!check);
check = verify_find(db, 91, 27, &A);
assert(!check);
}
void pqt_test_node_access()
{
stack_printer __stack_printer__("::pqt_test_node_access");
typedef point_quad_tree<int32_t, const string*> db_type;
db_type db;
db_type::node_access nac = db.get_node_access();
assert(!nac);
string A("A");
string B("B");
string C("C");
string D("D");
string E("E");
string F("F");
db.insert(92, 27, &A);
db.insert(53, 26, &B);
db.insert(69, 18, &C);
db.insert(0, 78, &D);
db.insert(17, 7, &E);
db.insert(91, 88, &F);
assert(db.size() == 6);
nac = db.get_node_access();
// Test root node.
assert(nac);
assert(nac.x() == 92);
assert(nac.y() == 27);
assert(nac.data() == &A);
bool success = db.verify_node_iterator(nac);
assert(success);
}
int main()
{
try
{
pqt_test_basic();
pqt_test_insertion_removal();
pqt_test_remove_root();
pqt_test_equality();
pqt_test_assignment();
pqt_test_swap();
pqt_test_find();
pqt_test_node_access();
assert(get_node_instance_count() == 0);
}
catch (const std::exception& e)
{
cout << "Test failed: " << e.what() << endl;
return EXIT_FAILURE;
}
cout << "Test finished successfully!" << endl;
return EXIT_SUCCESS;
}
|