Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
quadtree_test.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
39#include <gtest/gtest.h>
40#include <quadtree.H>
41// They define macros with the names of those of quadnode.H
42#include <rbNode.H>
43#include <Dijkstra.H>
44#include <random>
45#include <algorithm>
46#include <unordered_set>
47#include <sstream>
48
49// ============================================================================
50// Basic Functionality Tests
51// ============================================================================
52
53TEST(QuadTreeBasic, ConstructorAndEmpty)
54{
55 QuadTree tree(0, 100, 0, 100, 4);
56
57 EXPECT_NE(tree.get_root(), nullptr);
58 EXPECT_EQ(tree.get_max_num_points_per_node(), 4);
59}
60
61TEST(QuadTreeBasic, RejectsInvalidBoundsAndZeroCapacity)
62{
63 EXPECT_THROW((QuadTree(0, 0, 0, 100, 1)), std::domain_error);
64 EXPECT_THROW((QuadTree(100, 0, 0, 100, 1)), std::domain_error);
65 EXPECT_THROW((QuadTree(0, 100, 7, 7, 1)), std::domain_error);
66 EXPECT_THROW((QuadTree(0, 100, 100, 0, 1)), std::domain_error);
67 EXPECT_THROW((QuadTree(0, 100, 0, 100, 0)), std::domain_error);
68
69 QuadTree tree(0, 100, 0, 100, 4);
70 EXPECT_THROW(tree.set_max_num_points_per_node(0), std::domain_error);
71 EXPECT_EQ(tree.get_max_num_points_per_node(), 4u);
72}
73
74TEST(QuadTreeBasic, QuadNodeSetRegionRejectsInvalidBoundsWithoutMutation)
75{
76 QuadNode node(0, 10, 0, 10);
77 EXPECT_THROW(node.set_region(5, 5, 0, 10), std::domain_error);
78 EXPECT_EQ(node.get_min_x(), 0);
79 EXPECT_EQ(node.get_max_x(), 10);
80 EXPECT_EQ(node.get_min_y(), 0);
81 EXPECT_EQ(node.get_max_y(), 10);
82}
83
84TEST(QuadTreeBasic, InsertSinglePoint)
85{
86 QuadTree tree(0, 100, 0, 100, 4);
87
88 Point * inserted = tree.insert(Point(50, 50));
89
90 ASSERT_NE(inserted, nullptr);
91 EXPECT_EQ(inserted->get_x(), 50);
92 EXPECT_EQ(inserted->get_y(), 50);
93}
94
95TEST(QuadTreeBasic, InsertOutsideBounds)
96{
97 QuadTree tree(0, 100, 0, 100, 4);
98
99 Point * result1 = tree.insert(Point(-10, 50));
100 Point * result2 = tree.insert(Point(50, 150));
101 Point * result3 = tree.insert(Point(150, 50));
102 Point * result4 = tree.insert(Point(50, -10));
103
104 EXPECT_EQ(result1, nullptr);
105 EXPECT_EQ(result2, nullptr);
106 EXPECT_EQ(result3, nullptr);
107 EXPECT_EQ(result4, nullptr);
108}
109
110TEST(QuadTreeBasic, ContainsCheck)
111{
112 QuadTree tree(0, 100, 0, 100, 4);
113
114 tree.insert(Point(25, 25));
115 tree.insert(Point(75, 75));
116
117 EXPECT_TRUE(tree.contains(Point(25, 25)));
118 EXPECT_TRUE(tree.contains(Point(75, 75)));
119 EXPECT_TRUE(tree.contains(Point(50, 50))); // In bounds but not inserted
120 EXPECT_FALSE(tree.contains(Point(-10, 50))); // Out of bounds
121 EXPECT_FALSE(tree.contains(Point(150, 50))); // Out of bounds
122}
123
124TEST(QuadTreeBasic, SearchExistingPoint)
125{
126 QuadTree tree(0, 100, 0, 100, 4);
127
128 tree.insert(Point(30, 40));
129 tree.insert(Point(70, 60));
130
131 Point * found1 = tree.search(Point(30, 40));
132 Point * found2 = tree.search(Point(70, 60));
133
134 ASSERT_NE(found1, nullptr);
135 ASSERT_NE(found2, nullptr);
136 EXPECT_EQ(found1->get_x(), 30);
137 EXPECT_EQ(found1->get_y(), 40);
138 EXPECT_EQ(found2->get_x(), 70);
139 EXPECT_EQ(found2->get_y(), 60);
140}
141
142TEST(QuadTreeBasic, SearchNonExistingPoint)
143{
144 QuadTree tree(0, 100, 0, 100, 4);
145
146 tree.insert(Point(30, 40));
147
148 Point * found = tree.search(Point(50, 50));
149
150 EXPECT_EQ(found, nullptr);
151}
152
153TEST(QuadTreeBasic, SearchContainerNode)
154{
155 QuadTree tree(0, 100, 0, 100, 4);
156
157 tree.insert(Point(25, 25));
158
159 QuadNode * node = tree.search_container_node(Point(25, 25));
160
161 ASSERT_NE(node, nullptr);
162 EXPECT_TRUE(node->is_leaf());
163 EXPECT_NE(node->search_point(Point(25, 25)), nullptr);
164}
165
166TEST(QuadTreeBasic, RemoveSinglePoint)
167{
168 QuadTree tree(0, 100, 0, 100, 4);
169
170 tree.insert(Point(50, 50));
171 EXPECT_NE(tree.search(Point(50, 50)), nullptr);
172
173 tree.remove(Point(50, 50));
174 EXPECT_EQ(tree.search(Point(50, 50)), nullptr);
175}
176
177TEST(QuadTreeBasic, RemoveNonExistingPoint)
178{
179 QuadTree tree(0, 100, 0, 100, 4);
180
181 tree.insert(Point(50, 50));
182
183 // Should not crash or cause errors
184 tree.remove(Point(30, 30));
185
186 EXPECT_NE(tree.search(Point(50, 50)), nullptr);
187}
188
189TEST(QuadTreeBasic, RemoveOutsideBoundsIsANoOp)
190{
191 QuadTree tree(0, 100, 0, 100, 1);
192 tree.insert(Point(10, 10));
193 tree.insert(Point(90, 90));
194
195 EXPECT_NO_THROW(tree.remove(Point(-1, 50)));
196 EXPECT_NO_THROW(tree.remove(Point(50, 100)));
197 EXPECT_NE(tree.search(Point(10, 10)), nullptr);
198 EXPECT_NE(tree.search(Point(90, 90)), nullptr);
199}
200
201TEST(QuadTreeBasic, EmptyTree)
202{
203 QuadTree tree(0, 100, 0, 100, 4);
204
205 tree.insert(Point(25, 25));
206 tree.insert(Point(75, 75));
207 tree.insert(Point(50, 50));
208
209 tree.empty();
210
211 EXPECT_EQ(tree.search(Point(25, 25)), nullptr);
212 EXPECT_EQ(tree.search(Point(75, 75)), nullptr);
213 EXPECT_EQ(tree.search(Point(50, 50)), nullptr);
214}
215
216TEST(QuadTreeBasic, ClearEdgeCases)
217{
218 // Test 1: clear on newly constructed tree (no-op)
219 QuadTree tree(0, 100, 0, 100, 4);
220 tree.clear();
221 EXPECT_EQ(tree.search(Point(50, 50)), nullptr);
222
223 // Test 2: re-insertion after clear
224 tree.insert(Point(25, 25));
225 tree.clear();
226 EXPECT_EQ(tree.search(Point(25, 25)), nullptr);
227
228 tree.insert(Point(25, 25));
229 EXPECT_NE(tree.search(Point(25, 25)), nullptr);
230}
231
232TEST(QuadTreeBasic, ClearYieldsSameStateAsEmpty)
233{
234 QuadTree tree(0, 100, 0, 100, 4);
235 tree.insert(Point(25, 25));
236 tree.insert(Point(75, 75));
237
238 tree.clear();
239
240 EXPECT_EQ(tree.search(Point(25, 25)), nullptr);
241 EXPECT_EQ(tree.search(Point(75, 75)), nullptr);
242}
243
244// ============================================================================
245// Subdivision and Merging Tests
246// ============================================================================
247
248TEST(QuadTreeSubdivision, SingleLevelSplit)
249{
250 QuadTree tree(0, 100, 0, 100, 2); // Max 2 points per node
251
252 tree.insert(Point(25, 25));
253 tree.insert(Point(30, 30));
254
255 // Root should still be a leaf
256 EXPECT_TRUE(tree.get_root()->is_leaf());
257
258 // Third point should trigger split
259 tree.insert(Point(35, 35));
260
261 // Root should now be internal (Gray)
262 EXPECT_FALSE(tree.get_root()->is_leaf());
263 EXPECT_EQ(COLOR(tree.get_root()), QuadNode::Color::Gray);
264}
265
266TEST(QuadTreeSubdivision, MultiLevelSplit)
267{
268 QuadTree tree(0, 100, 0, 100, 2);
269
270 // Insert many points in one quadrant to force deep splits
271 for (int i = 1; i <= 10; ++i)
272 {
273 tree.insert(Point(10 + i, 10 + i));
274 }
275
276 // Verify root is internal
277 EXPECT_FALSE(tree.get_root()->is_leaf());
278
279 // Verify tree has multiple levels
280 QuadNode * nw = NW_CHILD(tree.get_root());
281 ASSERT_NE(nw, nullptr);
282
283 // At least one child should be further subdivided
284 bool has_deep_child = false;
285 if (not nw->is_leaf() || (NE_CHILD(tree.get_root()) != nullptr and not NE_CHILD(tree.get_root())->is_leaf()))
286 has_deep_child = true;
287
288 EXPECT_TRUE(has_deep_child);
289}
290
291TEST(QuadTreeSubdivision, AllQuadrantsPopulated)
292{
293 QuadTree tree(0, 100, 0, 100, 1);
294
295 // Insert one point in each quadrant
296 tree.insert(Point(25, 25)); // SW
297 tree.insert(Point(75, 25)); // SE
298 tree.insert(Point(25, 75)); // NW
299 tree.insert(Point(75, 75)); // NE
300
301 QuadNode * root = tree.get_root();
302 EXPECT_FALSE(root->is_leaf());
303
304 // All four children should exist and be leaves
305 ASSERT_NE(NW_CHILD(root), nullptr);
306 ASSERT_NE(NE_CHILD(root), nullptr);
307 ASSERT_NE(SW_CHILD(root), nullptr);
308 ASSERT_NE(SE_CHILD(root), nullptr);
309
310 EXPECT_TRUE(NW_CHILD(root)->is_leaf());
311 EXPECT_TRUE(NE_CHILD(root)->is_leaf());
312 EXPECT_TRUE(SW_CHILD(root)->is_leaf());
313 EXPECT_TRUE(SE_CHILD(root)->is_leaf());
314}
315
316TEST(QuadTreeMerging, RemovalTriggersJoin)
317{
318 QuadTree tree(0, 100, 0, 100, 2);
319
320 // Insert 3 points in DIFFERENT quadrants to trigger split
321 // Points must be in different quadrants to avoid nested splits
322 tree.insert(Point(25, 25)); // SW quadrant
323 tree.insert(Point(75, 25)); // SE quadrant
324 tree.insert(Point(25, 75)); // NW quadrant
325
326 EXPECT_FALSE(tree.get_root()->is_leaf());
327
328 // Remove one point to go below threshold
329 tree.remove(Point(25, 75));
330
331 // Root should merge back to leaf (only 2 points left, threshold is 2)
332 EXPECT_TRUE(tree.get_root()->is_leaf());
333}
334
335TEST(QuadTreeMerging, MultipleRemovalsGradualJoin)
336{
337 QuadTree tree(0, 100, 0, 100, 2);
338
339 // Insert many points
340 std::vector<Point> points = {
341 Point(10, 10), Point(15, 15), Point(20, 20),
342 Point(80, 80), Point(85, 85), Point(90, 90)
343 };
344
345 for (const auto & p : points)
346 tree.insert(p);
347
348 // Tree should be subdivided
349 EXPECT_FALSE(tree.get_root()->is_leaf());
350
351 // Remove points one by one
352 for (const auto & p : points)
353 {
354 tree.remove(p);
355 }
356
357 // After all removals, root should be a leaf again
358 EXPECT_TRUE(tree.get_root()->is_leaf());
359 EXPECT_EQ(COLOR(tree.get_root()), QuadNode::Color::White);
360}
361
362TEST(QuadTreeMerging, CollapsesEveryEligibleAncestor)
363{
364 QuadTree tree(0, 1024, 0, 1024, 2);
365 const Point p1(1, 1);
366 const Point p2(2, 2);
367 const Point p3(3, 3);
368 const Point far(900, 900);
369
370 tree.insert(p1);
371 tree.insert(p2);
372 tree.insert(p3);
373 ASSERT_FALSE(tree.get_root()->is_leaf());
374
375 tree.remove(p3);
376 ASSERT_TRUE(tree.get_root()->is_leaf());
377
378 tree.insert(far);
379 ASSERT_FALSE(tree.get_root()->is_leaf());
380 EXPECT_NO_THROW(tree.remove(far));
381
382 EXPECT_TRUE(tree.get_root()->is_leaf());
383 EXPECT_NE(tree.search(p1), nullptr);
384 EXPECT_NE(tree.search(p2), nullptr);
385}
386
387// ============================================================================
388// Copy Constructor and Assignment Tests
389// ============================================================================
390
391TEST(QuadTreeCopy, CopyConstructor)
392{
393 QuadTree tree1(0, 100, 0, 100, 3);
394
395 tree1.insert(Point(25, 25));
396 tree1.insert(Point(75, 75));
397 tree1.insert(Point(50, 50));
398
399 QuadTree tree2(tree1);
400
401 // Verify all points are in the copy
402 EXPECT_NE(tree2.search(Point(25, 25)), nullptr);
403 EXPECT_NE(tree2.search(Point(75, 75)), nullptr);
404 EXPECT_NE(tree2.search(Point(50, 50)), nullptr);
405
406 // Verify they are independent
407 tree1.insert(Point(10, 10));
408 EXPECT_EQ(tree2.search(Point(10, 10)), nullptr);
409}
410
411TEST(QuadTreeCopy, AssignmentOperator)
412{
413 QuadTree tree1(0, 100, 0, 100, 3);
414 tree1.insert(Point(25, 25));
415 tree1.insert(Point(75, 75));
416
417 QuadTree tree2(0, 200, 0, 200, 5);
418 tree2.insert(Point(150, 150));
419
420 tree2 = tree1;
421
422 // tree2 should now have tree1's data
423 EXPECT_NE(tree2.search(Point(25, 25)), nullptr);
424 EXPECT_NE(tree2.search(Point(75, 75)), nullptr);
425 EXPECT_EQ(tree2.search(Point(150, 150)), nullptr);
426
427 // Configuration should also be copied
428 EXPECT_EQ(tree2.get_max_num_points_per_node(), 3);
429}
430
431TEST(QuadTreeCopy, SelfAssignment)
432{
433 QuadTree tree(0, 100, 0, 100, 3);
434 tree.insert(Point(50, 50));
435
436 tree = tree;
437
438 // Should still work
439 EXPECT_NE(tree.search(Point(50, 50)), nullptr);
440}
441
442// ============================================================================
443// Stress Tests
444// ============================================================================
445
446TEST(QuadTreeStress, InsertManyPoints)
447{
448 QuadTree tree(0, 1000, 0, 1000, 4);
449
450 std::mt19937 gen(12345);
451 std::uniform_real_distribution<> dis(0, 1000);
452
453 const size_t num_points = 10000;
454 std::vector<Point> points;
455
456 for (size_t i = 0; i < num_points; ++i)
457 {
458 Point p(dis(gen), dis(gen));
459 points.push_back(p);
460 Point * inserted = tree.insert(p);
461 ASSERT_NE(inserted, nullptr);
462 }
463
464 // Verify all points can be found
465 for (const auto & p : points)
466 {
467 EXPECT_NE(tree.search(p), nullptr);
468 }
469}
470
471TEST(QuadTreeStress, InsertRemoveCycles)
472{
473 QuadTree tree(0, 100, 0, 100, 4);
474
475 std::mt19937 gen(54321);
476 std::uniform_real_distribution<> dis(0, 100);
477
478 const size_t cycles = 100;
479 const size_t points_per_cycle = 50;
480
481 for (size_t cycle = 0; cycle < cycles; ++cycle)
482 {
483 std::vector<Point> points;
484
485 // Insert points
486 for (size_t i = 0; i < points_per_cycle; ++i)
487 {
488 Point p(dis(gen), dis(gen));
489 points.push_back(p);
490 tree.insert(p);
491 }
492
493 // Remove half of them
494 for (size_t i = 0; i < points_per_cycle / 2; ++i)
495 {
496 tree.remove(points[i]);
497 }
498 }
499
500 // Tree should still be functional
501 Point * test = tree.insert(Point(50, 50));
502 EXPECT_NE(test, nullptr);
503}
504
505TEST(QuadTreeStress, DenseRegion)
506{
507 QuadTree tree(0, 100, 0, 100, 2);
508
509 // Insert many points in a small region
510 for (int x = 40; x <= 60; ++x)
511 {
512 for (int y = 40; y <= 60; ++y)
513 {
514 tree.insert(Point(x, y));
515 }
516 }
517
518 // Verify all can be found
519 for (int x = 40; x <= 60; ++x)
520 {
521 for (int y = 40; y <= 60; ++y)
522 {
523 EXPECT_NE(tree.search(Point(x, y)), nullptr);
524 }
525 }
526}
527
528// ============================================================================
529// Edge Cases
530// ============================================================================
531
532TEST(QuadTreeEdgeCases, BoundaryPoints)
533{
534 QuadTree tree(0, 100, 0, 100, 4);
535
536 // Test exact boundary points
537 Point * p1 = tree.insert(Point(0, 0));
538 Point * p2 = tree.insert(Point(0, 100)); // Upper bound, should fail
539 Point * p3 = tree.insert(Point(100, 0)); // Upper bound, should fail
540 Point * p4 = tree.insert(Point(100, 100)); // Upper bounds, should fail
541
542 EXPECT_NE(p1, nullptr);
543 EXPECT_EQ(p2, nullptr); // Upper bounds are exclusive
544 EXPECT_EQ(p3, nullptr);
545 EXPECT_EQ(p4, nullptr);
546}
547
548TEST(QuadTreeEdgeCases, MidpointCoordinates)
549{
550 QuadTree tree(0, 100, 0, 100, 1);
551
552 // Insert point exactly at midpoint
553 tree.insert(Point(50, 50));
554
555 // Insert more to trigger split
556 tree.insert(Point(25, 25));
557
558 // Midpoint should be in one of the quadrants
559 EXPECT_NE(tree.search(Point(50, 50)), nullptr);
560}
561
562TEST(QuadTreeEdgeCases, SinglePointCapacity)
563{
564 QuadTree tree(0, 100, 0, 100, 1);
565
566 tree.insert(Point(25, 25));
567 tree.insert(Point(30, 30));
568
569 // Should trigger immediate split
570 EXPECT_FALSE(tree.get_root()->is_leaf());
571}
572
573TEST(QuadTreeEdgeCases, DuplicatePointsTerminateAndRemainCounted)
574{
575 QuadTree tree(0, 100, 0, 100, 1);
576 const Point duplicate(25, 25);
577
578 for (size_t i = 0; i < 100; ++i)
579 ASSERT_NE(tree.insert(duplicate), nullptr);
580
581 EXPECT_TRUE(tree.get_root()->is_leaf());
582 EXPECT_EQ(tree.get_root()->get_num_points(), 100u);
583
584 for (size_t i = 0; i < 100; ++i)
585 tree.remove(duplicate);
586
587 EXPECT_EQ(tree.search(duplicate), nullptr);
588 EXPECT_EQ(COLOR(tree.get_root()), QuadNode::Color::White);
589}
590
591TEST(QuadTreeEdgeCases, VerySmallRegion)
592{
593 QuadTree tree(0, 1, 0, 1, 2);
594
595 tree.insert(Point(0.1, 0.1));
596 tree.insert(Point(0.9, 0.9));
597
598 EXPECT_NE(tree.search(Point(0.1, 0.1)), nullptr);
599 EXPECT_NE(tree.search(Point(0.9, 0.9)), nullptr);
600}
601
602TEST(QuadTreeEdgeCases, VeryLargeRegion)
603{
604 QuadTree tree(-1e9, 1e9, -1e9, 1e9, 4);
605
606 tree.insert(Point(0, 0));
607 tree.insert(Point(1e8, 1e8));
608 tree.insert(Point(-5e8, -5e8));
609
610 EXPECT_NE(tree.search(Point(0, 0)), nullptr);
611 EXPECT_NE(tree.search(Point(1e8, 1e8)), nullptr);
612 EXPECT_NE(tree.search(Point(-5e8, -5e8)), nullptr);
613}
614
615// ============================================================================
616// Traversal Tests
617// ============================================================================
618
619TEST(QuadTreeTraversal, ForEachNode)
620{
621 QuadTree tree(0, 100, 0, 100, 2);
622
623 for (int i = 0; i < 10; ++i)
624 {
625 tree.insert(Point(10 * i, 10 * i));
626 }
627
628 size_t node_count = 0;
629 tree.for_each([&node_count](QuadNode * node) {
630 ++node_count;
631 EXPECT_NE(node, nullptr);
632 });
633
634 EXPECT_GT(node_count, 0);
635}
636
637TEST(QuadTreeTraversal, CountLeaves)
638{
639 QuadTree tree(0, 100, 0, 100, 2);
640
641 tree.insert(Point(10, 10));
642 tree.insert(Point(20, 20));
643 tree.insert(Point(80, 80));
644 tree.insert(Point(90, 90));
645
646 size_t leaf_count = 0;
647 tree.for_each([&leaf_count](QuadNode * node) {
648 if (node->is_leaf())
649 ++leaf_count;
650 });
651
652 EXPECT_GT(leaf_count, 0);
653}
654
655// ============================================================================
656// Fuzz Tests
657// ============================================================================
658
659TEST(QuadTreeFuzz, RandomOperations)
660{
661 QuadTree tree(0, 1000, 0, 1000, 4);
662
663 std::mt19937 gen(99999);
664 // Use integer coordinates to avoid mpq_class precision issues with double conversion
665 std::uniform_int_distribution<int> coord_dis(0, 999);
666 std::uniform_int_distribution<> op_dis(0, 2);
667
668 // Store points directly to avoid string conversion precision issues
669 std::vector<Point> inserted_points;
670
671 for (int i = 0; i < 1000; ++i)
672 {
673 int op = op_dis(gen);
674
675 if (op == 0 || op == 1) // Insert
676 {
677 Point p(coord_dis(gen), coord_dis(gen));
678 Point * result = tree.insert(p);
679 if (result != nullptr)
680 inserted_points.push_back(p);
681 }
682 else if (op == 2 && not inserted_points.empty()) // Remove
683 {
684 // Pick a random inserted point
685 size_t idx = gen() % inserted_points.size();
686 Point to_remove = inserted_points[idx];
687
688 tree.remove(to_remove);
689 inserted_points.erase(inserted_points.begin() + idx);
690 }
691 }
692
693 // Verify consistency
694 for (const auto & p : inserted_points)
695 EXPECT_NE(tree.search(p), nullptr)
696 << "Point (" << p.get_x() << ", " << p.get_y() << ") should be in tree";
697}
698
699// ============================================================================
700// Macros
701// ============================================================================
702
703// rbNode.H gave COLOR another definition, which a QuadNode did not take,
704// and Dijkstra.H took PARENT away
705TEST(QuadNodeMacros, WorkAlongWithThoseOfOtherHeaders)
706{
707 QuadTree tree(0, 100, 0, 100, 1);
708 tree.insert(Point(10, 10));
709 tree.insert(Point(90, 90));
710
711 QuadNode * root = tree.get_root();
712 ASSERT_EQ(COLOR(root), QuadNode::Color::Gray);
713 for (QuadNode * child : {NW_CHILD(root), NE_CHILD(root),
715 EXPECT_EQ(PARENT(child), root);
716
717 RbNode<int> node(5);
718 EXPECT_EQ(COLOR(&node), RED);
719}
720
721// ============================================================================
722// Main
723// ============================================================================
724
725int main(int argc, char **argv)
726{
727 ::testing::InitGoogleTest(&argc, argv);
728 return RUN_ALL_TESTS();
729}
Dijkstra's shortest path algorithm.
int main()
Represents a point with rectangular coordinates in a 2D plane.
Definition point.H:221
const Geom_Number & get_x() const noexcept
Gets the x-coordinate value.
Definition point.H:448
const Geom_Number & get_y() const noexcept
Gets the y-coordinate value.
Definition point.H:457
Node for QuadTree spatial data structure.
Definition quadnode.H:95
const Geom_Number & get_min_y() const noexcept
Get minimum Y coordinate of this region.
Definition quadnode.H:494
const Geom_Number & get_max_y() const noexcept
Get maximum Y coordinate of this region.
Definition quadnode.H:497
const Geom_Number & get_min_x() const noexcept
Get minimum X coordinate of this region.
Definition quadnode.H:488
void set_region(const Geom_Number &_min_x, const Geom_Number &_max_x, const Geom_Number &_min_y, const Geom_Number &_max_y)
Set the region boundaries for this node.
Definition quadnode.H:346
Point * search_point(const Point &p) noexcept
Search for a point in this node.
Definition quadnode.H:518
bool is_leaf() const noexcept
Check if this node is a leaf (has no children).
Definition quadnode.H:385
size_t get_num_points() noexcept
Get total number of points in this subtree.
Definition quadnode.H:482
const Geom_Number & get_max_x() const noexcept
Get maximum X coordinate of this region.
Definition quadnode.H:491
QuadTree - Hierarchical spatial index for 2D points.
Definition quadtree.H:126
void empty(Node *&r) noexcept
Recursively delete all nodes.
Definition quadtree.H:300
void clear()
Alias for empty().
Definition quadtree.H:602
void for_each(Op &op)
Apply an operation to each node in the tree.
Definition quadtree.H:612
void remove(const Point &p)
Remove a point from the tree.
Definition quadtree.H:565
Point * search(const Point &p) noexcept
Search for a point in the tree.
Definition quadtree.H:524
Node * search_container_node(const Point &p) noexcept
Find the leaf node containing a point.
Definition quadtree.H:542
void set_max_num_points_per_node(const size_t &_max_num_points_per_node)
Set the maximum points per leaf node.
Definition quadtree.H:469
bool contains(const Point &p) const noexcept
Check if a point is within the tree's region.
Definition quadtree.H:487
Point * insert(Node *&r, const Point &p)
Recursive insert helper.
Definition quadtree.H:281
size_t get_max_num_points_per_node() const noexcept
Get the maximum points per leaf node.
Definition quadtree.H:477
Node * get_root() noexcept
Get the root node.
Definition quadtree.H:451
Declare RbNode type with 128-byte pool allocation.
Definition rbNode.H:116
#define TEST(name)
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Definition gmpfrxx.h:4138
static mpfr_t y
Definition mpfr_mul_d.c:3
and
Check uniqueness with explicit hash + equality functors.
static bool is_leaf(BinNode< std::string > *p) noexcept
Definition Huffman.H:109
#define PARENT(p)
Definition quadnode.H:53
#define COLOR(p)
Definition quadnode.H:59
#define SE_CHILD(p)
Definition quadnode.H:57
#define NE_CHILD(p)
Definition quadnode.H:55
#define NW_CHILD(p)
Definition quadnode.H:54
#define SW_CHILD(p)
Definition quadnode.H:56
QuadTree spatial data structure for efficient 2D point indexing.
Red-Black tree node definition and validation utilities.
#define RED
Red color constant (newly inserted nodes are red)
Definition rbNode.H:77
void test()
Definition test-comb.C:40