41#include <gtest/gtest.h>
46using namespace testing;
55 for (
size_t i = 0; i < arr.
size(); ++i)
56 out.push_back(arr[i]);
61 std::vector<typename Tree::key_type>
to_vector(
const Tree & tree)
67 void expect_matches_reference(
const Tree & tree,
const std::set<int> & ref)
69 const std::vector<int> tree_keys =
to_vector(tree);
70 const std::vector<int> ref_keys(ref.begin(), ref.end());
71 const auto min_key = tree.min_key();
72 const auto max_key = tree.max_key();
73 EXPECT_EQ(tree_keys, ref_keys);
74 EXPECT_EQ(tree.size(), ref.size());
75 EXPECT_TRUE(tree.verify());
79 EXPECT_FALSE(min_key.has_value());
80 EXPECT_FALSE(max_key.has_value());
84 ASSERT_TRUE(min_key.has_value());
85 ASSERT_TRUE(max_key.has_value());
86 EXPECT_EQ(*min_key, *ref.begin());
87 EXPECT_EQ(*max_key, *ref.rbegin());
91 void expect_optional_eq(
const std::optional<int> & got,
110 EXPECT_TRUE(tree.
empty());
111 EXPECT_TRUE(tree.is_empty());
112 EXPECT_EQ(tree.size(), 0);
113 EXPECT_EQ(tree.height(), 0);
115 EXPECT_FALSE(tree.min_key().has_value());
116 EXPECT_FALSE(tree.max_key().has_value());
117 EXPECT_FALSE(tree.lower_bound(10).has_value());
118 EXPECT_FALSE(tree.upper_bound(10).has_value());
119 EXPECT_TRUE(tree.verify());
124 Tree from_init = {40, 10, 90, 10, 20, 70, 40};
125 std::vector<int> src = {8, 3, 5, 8, 2, 9, 1};
126 Tree from_range(src.begin(), src.end());
128 EXPECT_EQ(
to_vector(from_init), (std::vector<int>{10, 20, 40, 70, 90}));
129 EXPECT_EQ(
to_vector(from_range), (std::vector<int>{1, 2, 3, 5, 8, 9}));
130 EXPECT_TRUE(from_init.verify());
131 EXPECT_TRUE(from_range.verify());
137 for (
int value : {40, 10, 90, 20, 70, 60, 30, 50, 80})
140 EXPECT_FALSE(tree.
insert(70));
142 EXPECT_TRUE(tree.
search(80));
145 EXPECT_EQ(
to_vector(tree), (std::vector<int>{10, 20, 30, 40, 50, 60, 70, 80, 90}));
146 EXPECT_EQ(tree.height(), 2);
148 expect_optional_eq(tree.lower_bound(55), 60);
149 expect_optional_eq(tree.lower_bound(60), 60);
150 expect_optional_eq(tree.lower_bound(91), std::nullopt);
152 expect_optional_eq(tree.upper_bound(60), 70);
153 expect_optional_eq(tree.upper_bound(89), 90);
154 expect_optional_eq(tree.upper_bound(90), std::nullopt);
156 EXPECT_TRUE(tree.verify());
164 for (
int i = 1; i <= 40; ++i)
170 for (
int value : {1, 2, 3, 7, 8, 9, 16, 17, 18, 25, 26, 27, 40, 31, 24, 12})
175 expect_matches_reference(tree, ref);
178 EXPECT_FALSE(tree.
remove(999));
179 expect_matches_reference(tree, ref);
184 Tree tree = {50, 20, 70, 10, 30, 60, 80};
192 EXPECT_TRUE(
copy.verify());
193 EXPECT_TRUE(assigned.verify());
197 move_assigned = std::move(assigned);
201 EXPECT_TRUE(moved.verify());
202 EXPECT_TRUE(move_assigned.verify());
209 std::mt19937
rng(0xBADA55);
210 std::uniform_int_distribution<int> op_dist(0, 5);
211 std::uniform_int_distribution<int> value_dist(-50, 150);
213 for (
size_t step = 0; step < 1200; ++step)
218 switch (op_dist(
rng))
235 const auto it = ref.lower_bound(
value);
237 it == ref.end() ? std::nullopt : std::optional<int>(*it);
244 const auto it = ref.upper_bound(
value);
246 it == ref.end() ? std::nullopt : std::optional<int>(*it);
252 expect_matches_reference(tree, ref);
TEST_F(BTreeTest, EmptyTreeReportsNoKeys)
size_t size_t int32_t value
size_t size_t int32_t * out
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
Generic B-Tree with configurable minimum degree.
Minimal std::expected-style result type for C++20.
constexpr bool has_value() const noexcept
Check if a value is held.
QuadTree - Hierarchical spatial index for 2D points.
void empty(Node *&r) noexcept
Recursively delete all nodes.
void remove(const Point &p)
Remove a point from the tree.
Point * search(const Point &p) noexcept
Search for a point in the tree.
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.
Main namespace for Aleph-w library functions.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
std::vector< typename C::Item_Type > to_vector(const C &c)
Convert a container to a std::vector.
B-Tree implementation with configurable minimum degree.