39#include <gtest/gtest.h>
46#include <unordered_set>
53TEST(QuadTreeBasic, ConstructorAndEmpty)
61TEST(QuadTreeBasic, RejectsInvalidBoundsAndZeroCapacity)
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);
74TEST(QuadTreeBasic, QuadNodeSetRegionRejectsInvalidBoundsWithoutMutation)
77 EXPECT_THROW(node.
set_region(5, 5, 0, 10), std::domain_error);
84TEST(QuadTreeBasic, InsertSinglePoint)
90 ASSERT_NE(inserted,
nullptr);
91 EXPECT_EQ(inserted->
get_x(), 50);
92 EXPECT_EQ(inserted->
get_y(), 50);
95TEST(QuadTreeBasic, InsertOutsideBounds)
104 EXPECT_EQ(result1,
nullptr);
105 EXPECT_EQ(result2,
nullptr);
106 EXPECT_EQ(result3,
nullptr);
107 EXPECT_EQ(result4,
nullptr);
110TEST(QuadTreeBasic, ContainsCheck)
124TEST(QuadTreeBasic, SearchExistingPoint)
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);
142TEST(QuadTreeBasic, SearchNonExistingPoint)
150 EXPECT_EQ(found,
nullptr);
153TEST(QuadTreeBasic, SearchContainerNode)
161 ASSERT_NE(node,
nullptr);
166TEST(QuadTreeBasic, RemoveSinglePoint)
177TEST(QuadTreeBasic, RemoveNonExistingPoint)
189TEST(QuadTreeBasic, RemoveOutsideBoundsIsANoOp)
216TEST(QuadTreeBasic, ClearEdgeCases)
232TEST(QuadTreeBasic, ClearYieldsSameStateAsEmpty)
248TEST(QuadTreeSubdivision, SingleLevelSplit)
266TEST(QuadTreeSubdivision, MultiLevelSplit)
271 for (
int i = 1; i <= 10; ++i)
281 ASSERT_NE(nw,
nullptr);
284 bool has_deep_child =
false;
286 has_deep_child =
true;
288 EXPECT_TRUE(has_deep_child);
291TEST(QuadTreeSubdivision, AllQuadrantsPopulated)
302 EXPECT_FALSE(
root->is_leaf());
316TEST(QuadTreeMerging, RemovalTriggersJoin)
335TEST(QuadTreeMerging, MultipleRemovalsGradualJoin)
340 std::vector<Point> points = {
345 for (
const auto & p : points)
352 for (
const auto & p : points)
362TEST(QuadTreeMerging, CollapsesEveryEligibleAncestor)
365 const Point p1(1, 1);
366 const Point p2(2, 2);
367 const Point p3(3, 3);
368 const Point far(900, 900);
380 EXPECT_NO_THROW(tree.
remove(far));
383 EXPECT_NE(tree.
search(p1),
nullptr);
384 EXPECT_NE(tree.
search(p2),
nullptr);
391TEST(QuadTreeCopy, CopyConstructor)
411TEST(QuadTreeCopy, AssignmentOperator)
431TEST(QuadTreeCopy, SelfAssignment)
446TEST(QuadTreeStress, InsertManyPoints)
450 std::mt19937 gen(12345);
451 std::uniform_real_distribution<> dis(0, 1000);
453 const size_t num_points = 10000;
454 std::vector<Point> points;
456 for (
size_t i = 0; i < num_points; ++i)
458 Point p(dis(gen), dis(gen));
461 ASSERT_NE(inserted,
nullptr);
465 for (
const auto & p : points)
467 EXPECT_NE(tree.
search(p),
nullptr);
471TEST(QuadTreeStress, InsertRemoveCycles)
475 std::mt19937 gen(54321);
476 std::uniform_real_distribution<> dis(0, 100);
478 const size_t cycles = 100;
479 const size_t points_per_cycle = 50;
481 for (
size_t cycle = 0; cycle < cycles; ++cycle)
483 std::vector<Point> points;
486 for (
size_t i = 0; i < points_per_cycle; ++i)
488 Point p(dis(gen), dis(gen));
494 for (
size_t i = 0; i < points_per_cycle / 2; ++i)
502 EXPECT_NE(
test,
nullptr);
505TEST(QuadTreeStress, DenseRegion)
510 for (
int x = 40; x <= 60; ++x)
512 for (
int y = 40;
y <= 60; ++
y)
519 for (
int x = 40; x <= 60; ++x)
521 for (
int y = 40;
y <= 60; ++
y)
532TEST(QuadTreeEdgeCases, BoundaryPoints)
542 EXPECT_NE(p1,
nullptr);
543 EXPECT_EQ(p2,
nullptr);
544 EXPECT_EQ(p3,
nullptr);
545 EXPECT_EQ(p4,
nullptr);
548TEST(QuadTreeEdgeCases, MidpointCoordinates)
562TEST(QuadTreeEdgeCases, SinglePointCapacity)
573TEST(QuadTreeEdgeCases, DuplicatePointsTerminateAndRemainCounted)
576 const Point duplicate(25, 25);
578 for (
size_t i = 0; i < 100; ++i)
579 ASSERT_NE(tree.
insert(duplicate),
nullptr);
584 for (
size_t i = 0; i < 100; ++i)
587 EXPECT_EQ(tree.
search(duplicate),
nullptr);
591TEST(QuadTreeEdgeCases, VerySmallRegion)
602TEST(QuadTreeEdgeCases, VeryLargeRegion)
604 QuadTree tree(-1e9, 1e9, -1e9, 1e9, 4);
612 EXPECT_NE(tree.
search(
Point(-5e8, -5e8)),
nullptr);
619TEST(QuadTreeTraversal, ForEachNode)
623 for (
int i = 0; i < 10; ++i)
628 size_t node_count = 0;
631 EXPECT_NE(node,
nullptr);
634 EXPECT_GT(node_count, 0);
637TEST(QuadTreeTraversal, CountLeaves)
646 size_t leaf_count = 0;
652 EXPECT_GT(leaf_count, 0);
659TEST(QuadTreeFuzz, RandomOperations)
663 std::mt19937 gen(99999);
665 std::uniform_int_distribution<int> coord_dis(0, 999);
666 std::uniform_int_distribution<> op_dis(0, 2);
669 std::vector<Point> inserted_points;
671 for (
int i = 0; i < 1000; ++i)
673 int op = op_dis(gen);
675 if (op == 0 || op == 1)
677 Point p(coord_dis(gen), coord_dis(gen));
679 if (result !=
nullptr)
680 inserted_points.push_back(p);
682 else if (op == 2 && not inserted_points.empty())
685 size_t idx = gen() % inserted_points.size();
686 Point to_remove = inserted_points[idx];
689 inserted_points.erase(inserted_points.begin() + idx);
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";
705TEST(QuadNodeMacros, WorkAlongWithThoseOfOtherHeaders)
712 ASSERT_EQ(
COLOR(
root), QuadNode::Color::Gray);
727 ::testing::InitGoogleTest(&argc, argv);
728 return RUN_ALL_TESTS();
Dijkstra's shortest path algorithm.
Represents a point with rectangular coordinates in a 2D plane.
const Geom_Number & get_x() const noexcept
Gets the x-coordinate value.
const Geom_Number & get_y() const noexcept
Gets the y-coordinate value.
Node for QuadTree spatial data structure.
const Geom_Number & get_min_y() const noexcept
Get minimum Y coordinate of this region.
const Geom_Number & get_max_y() const noexcept
Get maximum Y coordinate of this region.
const Geom_Number & get_min_x() const noexcept
Get minimum X coordinate of this region.
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.
Point * search_point(const Point &p) noexcept
Search for a point in this node.
bool is_leaf() const noexcept
Check if this node is a leaf (has no children).
size_t get_num_points() noexcept
Get total number of points in this subtree.
const Geom_Number & get_max_x() const noexcept
Get maximum X coordinate of this region.
QuadTree - Hierarchical spatial index for 2D points.
void empty(Node *&r) noexcept
Recursively delete all nodes.
void clear()
Alias for empty().
void for_each(Op &op)
Apply an operation to each node in the tree.
void remove(const Point &p)
Remove a point from the tree.
Point * search(const Point &p) noexcept
Search for a point in the tree.
Node * search_container_node(const Point &p) noexcept
Find the leaf node containing a point.
void set_max_num_points_per_node(const size_t &_max_num_points_per_node)
Set the maximum points per leaf node.
bool contains(const Point &p) const noexcept
Check if a point is within the tree's region.
Point * insert(Node *&r, const Point &p)
Recursive insert helper.
size_t get_max_num_points_per_node() const noexcept
Get the maximum points per leaf node.
Node * get_root() noexcept
Get the root node.
Declare RbNode type with 128-byte pool allocation.
__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)
and
Check uniqueness with explicit hash + equality functors.
static bool is_leaf(BinNode< std::string > *p) noexcept
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)