Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
fibonacci_heap_allocation_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
43#include <gtest/gtest.h>
44
45#include "allocation_failure.H"
46
47#include <cstdlib>
48#include <new>
49#include <numeric>
50#include <utility>
51#include <vector>
52
53#include <tpl_fibonacci_heap.H>
54#include <tpl_graph.H>
55#include <archeap.H>
56
57using namespace Aleph;
58
59// The tests need the operator new of allocation_failure.H
60struct FibonacciHeapWithoutMemory : public ::testing::Test
61{
62 void SetUp() override
63 {
65 GTEST_SKIP() << "ThreadSanitizer replaces the global operator new";
66 }
67};
68
69// The constructors reserved the room of consolidate(), and they were
70// noexcept: without memory, they ended the program
71TEST_F(FibonacciHeapWithoutMemory, AHeapIsBuiltWithoutMemory)
72{
73 using Heap = Fibonacci_Heap<int>;
74 size_t refused = 0;
75 {
76 No_Memory no_memory;
77 Heap heap;
78 Heap other(std::in_place);
79 refused = refused_allocations;
80 }
81 EXPECT_EQ(refused, 0u);
82
83 Heap heap;
84 for (int key : {4, 1, 3})
85 (void) heap.insert(key);
86 EXPECT_EQ(heap.extract_min(), 1);
87 EXPECT_EQ(heap.extract_min(), 3);
88 EXPECT_EQ(heap.extract_min(), 4);
89}
90
91namespace
92{
94 using GNode = Graph::Node;
95 using GArc = Graph::Arc;
96
97 struct Arc_Weight
98 {
99 int operator()(GArc * a) const noexcept { return a->get_info(); }
100 };
101
102 // The handle of a node is in its cookie, so that reaching it allocates
103 // nothing
104 struct Cookie_Access
105 {
106 void *& operator()(GNode * p) const noexcept { return NODE_COOKIE(p); }
107 };
108
110} // namespace
111
112// get_min_arc() cleared the handle of the node of the arc before
113// extract_min(), which without memory throws and leaves the heap as it
114// was: the node stayed in the heap without its handle, and putting an arc
115// to it again put a second node in the heap
116TEST_F(FibonacciHeapWithoutMemory, AnArcHeapKeepsItsHandlesWithoutMemory)
117{
118 Graph g;
119 GNode * a = g.insert_node(0);
120 GNode * b = g.insert_node(1);
121 GNode * c = g.insert_node(2);
122 GNode * d = g.insert_node(3);
123 for (GNode * p : {a, b, c, d})
124 NODE_COOKIE(p) = nullptr;
125
126 Arc_Heap heap; // new, so that it lacks the room of consolidate()
127 heap.put_arc(g.insert_arc(a, b, 5), b);
128 heap.put_arc(g.insert_arc(a, c, 3), c);
129 heap.put_arc(g.insert_arc(a, d, 4), d);
130
131 bool threw = false;
132 size_t refused = 0;
133 {
134 No_Memory no_memory;
135 try
136 {
137 (void) heap.get_min_arc(); // that of c
138 }
139 catch (const std::bad_alloc &)
140 {
141 threw = true;
142 }
143 refused = refused_allocations;
144 }
145 ASSERT_TRUE(threw);
146 EXPECT_GT(refused, 0u);
147 EXPECT_NE(NODE_COOKIE(c), nullptr); // c keeps its handle
148
149 // a heavier arc to c changes nothing
150 heap.put_arc(g.insert_arc(b, c, 6), c);
151 std::vector<int> weights;
152 while (not heap.is_empty())
153 weights.push_back(heap.get_min_arc()->get_info());
154 EXPECT_EQ(weights, (std::vector<int>{3, 4, 5}));
155 for (GNode * p : {a, b, c, d})
156 EXPECT_EQ(NODE_COOKIE(p), nullptr);
157}
158
159// consolidate() kept the roots in a vector that it allocated on each call,
160// once extract_min() or delete_node() had taken their node out of the heap
161TEST_F(FibonacciHeapWithoutMemory, ExtractAndDeleteAllocateNothing)
162{
163 using Heap = Fibonacci_Heap<int>;
164 Heap heap;
165 std::vector<Heap::Node *> by_key(1000);
166 for (int k = 0; k < 1000; ++k)
167 {
168 const int key = k * 7919 % 1000; // every key from 0 to 999
169 by_key[key] = heap.insert(key);
170 }
171 ASSERT_EQ(heap.extract_min(), 0); // the roots end in trees
172
173 std::vector<int> extracted;
174 extracted.reserve(400);
175 size_t refused = 0;
176 {
177 No_Memory no_memory;
178 for (int i = 0; i < 400; ++i)
179 extracted.push_back(heap.extract_min());
180 for (int key = 900; key < 1000; ++key)
181 heap.delete_node(by_key[key]);
182 refused = refused_allocations;
183 }
184
185 EXPECT_EQ(refused, 0u);
186 std::vector<int> expected(400);
187 std::iota(expected.begin(), expected.end(), 1);
188 EXPECT_EQ(extracted, expected);
189 ASSERT_EQ(heap.size(), 499u);
190 for (int key = 401; key < 900; ++key)
191 ASSERT_EQ(heap.extract_min(), key);
192 EXPECT_TRUE(heap.is_empty());
193}
194
195// A heap that was moved from lacks the room of consolidate(), which
196// extract_min() and delete_node() allocate before they change the heap
197TEST_F(FibonacciHeapWithoutMemory, AHeapMovedFromIsAsItWasWithoutMemory)
198{
199 using Heap = Fibonacci_Heap<int>;
200 for (bool by_assignment : {false, true})
201 {
202 SCOPED_TRACE(by_assignment ? "move assignment" : "move construction");
203 Heap heap;
204 (void) heap.insert(1);
205 Heap other;
206 if (by_assignment)
207 other = std::move(heap);
208 else
209 {
210 Heap moved(std::move(heap));
211 other.swap(moved);
212 }
213
214 Heap::Node * eight = nullptr;
215 for (int key : {5, 3, 8, 6})
216 {
217 Heap::Node * p = heap.insert(key);
218 if (key == 8)
219 eight = p;
220 }
221
222 bool extract_threw = false;
223 bool delete_threw = false;
224 size_t refused = 0;
225 {
226 No_Memory no_memory;
227 try
228 {
229 (void) heap.extract_min();
230 }
231 catch (const std::bad_alloc &)
232 {
233 extract_threw = true;
234 }
235 try
236 {
237 heap.delete_node(eight);
238 }
239 catch (const std::bad_alloc &)
240 {
241 delete_threw = true;
242 }
243 refused = refused_allocations;
244 }
245
246 EXPECT_GT(refused, 0u); // it had no room
247 EXPECT_TRUE(extract_threw);
248 EXPECT_TRUE(delete_threw);
249 ASSERT_EQ(heap.size(), 4u);
250 EXPECT_EQ(heap.extract_min(), 3);
251 heap.delete_node(eight);
252 EXPECT_EQ(heap.extract_min(), 5);
253 EXPECT_EQ(heap.extract_min(), 6);
254 EXPECT_TRUE(heap.is_empty());
255 EXPECT_EQ(other.extract_min(), 1);
256 }
257}
A global operator new that a test can make fail.
#define ALEPH_ALLOCATION_FAILURE_UNDER_TSAN
Arc heap for graph algorithms.
Implementation of a Fibonacci Heap priority queue.
Node * insert(const T &val)
Inserts a new element (copy).
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
Node Node
The graph type.
Definition tpl_graph.H:443
Arc Arc
The node class type.
Definition tpl_graph.H:444
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
Definition tpl_graph.H:618
Minimal std::expected-style result type for C++20.
ArcInfo & get_info() noexcept
Return a modifiable reference to the arc data.
Definition graph-dry.H:854
TEST_F(FibonacciHeapWithoutMemory, AHeapIsBuiltWithoutMemory)
Grafo::Arc GArc
Definition floyd.cc:59
Grafo::Node GNode
Definition floyd.cc:58
#define NODE_COOKIE(p)
Return the node cookie
Main namespace for Aleph-w library functions.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:231
static int * k
Fibonacci Heap implementation.
Generic graph and digraph implementations.