Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
bplus_allocation_failure_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11
12 Permission is hereby granted, free of charge, to any person obtaining a copy
13 of this software and associated documentation files (the "Software"), to deal
14 in the Software without restriction, including without limitation the rights
15 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16 copies of the Software, and to permit persons to whom the Software is
17 furnished to do so, subject to the following conditions:
18
19 The above copyright notice and this permission notice shall be included in all
20 copies or substantial portions of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 SOFTWARE.
29*/
30
44#include <gtest/gtest.h>
45
46#include "allocation_failure.H"
47
48#include <new>
49#include <string>
50
51#include <tpl_bplus_tree.H>
52
53using namespace Aleph;
54
55// The tests need the operator new of allocation_failure.H
56struct BPlusTreeWithoutMemory : public ::testing::Test
57{
58 void SetUp() override
59 {
61 GTEST_SKIP() << "ThreadSanitizer replaces the global operator new";
62 }
63};
64
65namespace
66{
67 template <typename Key> Key key_of(long i);
68
69 template <> int key_of<int>(const long i)
70 {
71 return static_cast<int>((i * 7919) % 100003);
72 }
73
74 // long enough for its copies to allocate too
75 template <> std::string key_of<std::string>(const long i)
76 {
77 return "a key long enough to allocate " + std::to_string((i * 7919) % 100003);
78 }
79
80 // Fail each allocation of the insertion of one more key into a tree of
81 // n_keys keys; return the number of allocations that failed
82 template <typename Key, size_t Degree>
83 size_t fail_each_allocation_of_an_insert(const long n_keys)
84 {
86 size_t failures = 0;
87 for (size_t n = 1;; ++n)
88 {
89 Tree tree;
90 for (long i = 0; i < n_keys; ++i)
91 EXPECT_TRUE(tree.insert(key_of<Key>(i)));
92
93 bool threw = false;
94 {
95 Fail_At fail(n);
96 try
97 {
98 (void) tree.insert(key_of<Key>(n_keys));
99 }
100 catch (const std::bad_alloc &)
101 {
102 threw = true;
103 }
104 }
105 if (not threw)
106 return failures;
107 ++failures;
108
109 EXPECT_TRUE(tree.verify()) << n_keys << " keys, allocation " << n;
110 EXPECT_EQ(tree.size(), static_cast<size_t>(n_keys)) << "allocation " << n;
111 EXPECT_EQ(tree.keys().size(), static_cast<size_t>(n_keys)) << "allocation " << n;
112 for (long i = 0; i < n_keys; ++i)
113 EXPECT_TRUE(tree.contains(key_of<Key>(i))) << "key " << i << ", allocation " << n;
114 EXPECT_TRUE(tree.insert(key_of<Key>(n_keys))) << "allocation " << n;
115 EXPECT_TRUE(tree.verify()) << "allocation " << n;
116 }
117 }
118}
119
120// The root is full: the insertion splits it
122{
123 EXPECT_GT((fail_each_allocation_of_an_insert<int, 16>(31)), 0u);
124 EXPECT_GT((fail_each_allocation_of_an_insert<int, 2>(3)), 0u);
125}
126
127// A leaf or an internal node below the root is split
128TEST_F(BPlusTreeWithoutMemory, SplitsBelowTheRoot)
129{
130 for (const long n : {7L, 100L, 500L})
131 (void) fail_each_allocation_of_an_insert<int, 2>(n);
132}
133
134// The copies of the keys allocate too
135TEST_F(BPlusTreeWithoutMemory, KeysWhoseCopiesAllocate)
136{
137 for (const long n : {31L, 300L})
138 (void) fail_each_allocation_of_an_insert<std::string, 16>(n);
139 for (const long n : {3L, 20L, 200L})
140 (void) fail_each_allocation_of_an_insert<std::string, 2>(n);
141}
A global operator new that a test can make fail.
#define ALEPH_ALLOCATION_FAILURE_UNDER_TSAN
TEST_F(BPlusTreeWithoutMemory, SplitOfAFullRoot)
Generic B+ Tree with configurable minimum degree.
QuadTree - Hierarchical spatial index for 2D points.
Definition quadtree.H:126
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
Main namespace for Aleph-w library functions.
B+ Tree implementation with linked leaves and configurable degree.