Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
graph_allocation_failure_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
42#include <gtest/gtest.h>
43
44#include "allocation_failure.H"
45
46#include <algorithm>
47#include <new>
48#include <vector>
49
50#include <tpl_agraph.H>
51#include <tpl_cut_nodes.H>
52#include <tpl_graph.H>
53#include <tpl_graph_utils.H>
54#include <tpl_net.H>
55#include <tpl_sgraph.H>
56
57using namespace Aleph;
58
59namespace
60{
61 // The tests need the operator new of allocation_failure.H
62 template <class G>
63 struct GraphWithoutMemory : public ::testing::Test
64 {
65 void SetUp() override
66 {
68 GTEST_SKIP() << "ThreadSanitizer replaces the global operator new";
69 }
70 };
71
72 using Graph_Kinds =
73 ::testing::Types<List_Graph<Graph_Node<int>, Graph_Arc<int>>,
79
80 // The arcs of g, and of each node, are the expected ones; the walk over
81 // the adjacency of the nodes reads each arc, so a freed one is seen
82 template <class G>
83 void expect_arcs(const G & g, const std::vector<typename G::Arc *> & arcs)
84 {
85 ASSERT_EQ(g.get_num_arcs(), arcs.size());
86 size_t in_graph = 0;
87 for (typename G::Arc_Iterator it(g); it.has_curr(); it.next_ne())
88 {
89 auto a = it.get_curr_ne();
90 EXPECT_NE(std::find(arcs.begin(), arcs.end(), a), arcs.end());
91 ++in_graph;
92 }
93 EXPECT_EQ(in_graph, arcs.size());
94
95 for (typename G::Node_Iterator it(g); it.has_curr(); it.next_ne())
96 {
97 auto p = it.get_curr_ne();
98 size_t adjacent = 0, sum = 0;
99 for (typename G::Node_Arc_Iterator j(p); j.has_curr(); j.next_ne())
100 {
101 auto a = j.get_curr_ne();
102 EXPECT_NE(std::find(arcs.begin(), arcs.end(), a), arcs.end());
103 sum += size_t(a->get_info());
104 ++adjacent;
105 }
106 EXPECT_EQ(adjacent, g.get_num_arcs(p));
107 EXPECT_GE(sum, adjacent);
108 }
109 }
110}
111
112TYPED_TEST_SUITE(GraphWithoutMemory, Graph_Kinds);
113
114// List_SGraph::insert_arc() put the arc in the list of its source, then in
115// that of its target and in the set of arcs; when one of them could not
116// take it, the others kept it, and the arc, freed when insert_arc() threw,
117// was left in the graph
118TYPED_TEST(GraphWithoutMemory, InsertArcLeavesTheGraphAsItWas)
119{
120 using G = TypeParam;
121 for (bool loop : {false, true})
122 for (size_t k = 1; ; ++k)
123 {
124 SCOPED_TRACE(testing::Message() << "allocation " << k
125 << (loop ? ", a loop" : ""));
126 G g;
127 auto a = g.insert_node(1);
128 auto b = g.insert_node(2);
129 std::vector<typename G::Arc *> arcs = {g.insert_arc(a, b, 1),
130 g.insert_arc(b, a, 2)};
131
132 typename G::Arc * arc = nullptr;
133 bool thrown = false;
134 {
135 Fail_At failing(k);
136 try
137 {
138 arc = g.insert_arc(b, loop ? b : a, 3);
139 }
140 catch (const std::bad_alloc &)
141 {
142 thrown = true;
143 }
144 }
145
146 if (not thrown)
147 {
148 arcs.push_back(arc);
149 expect_arcs(g, arcs);
150 break;
151 }
152 expect_arcs(g, arcs);
153
154 // and it takes the arc with memory
155 arcs.push_back(g.insert_arc(b, loop ? b : a, 3));
156 expect_arcs(g, arcs);
157 }
158}
159
160// remove_node() was noexcept, though it makes a list of the arcs of the
161// node, so the program ended without memory for it; that of List_SGraph
162// made the list while it disconnected the arcs, so it would have left some
163// out of the graph and still in its set of arcs
164TYPED_TEST(GraphWithoutMemory, RemoveNodeLeavesTheGraphAsItWasOrRemovesIt)
165{
166 using G = TypeParam;
167 for (size_t k = 1; ; ++k)
168 {
169 SCOPED_TRACE(testing::Message() << "allocation " << k);
170 G g;
171 auto a = g.insert_node(1);
172 auto b = g.insert_node(2);
173 auto c = g.insert_node(3);
174 std::vector<typename G::Arc *> arcs = {g.insert_arc(a, b, 1),
175 g.insert_arc(b, c, 2),
176 g.insert_arc(c, b, 3),
177 g.insert_arc(c, a, 4),
178 g.insert_arc(b, b, 5)};
179 bool thrown = false;
180 {
181 Fail_At failing(k);
182 try
183 {
184 g.remove_node(b);
185 }
186 catch (const std::bad_alloc &)
187 {
188 thrown = true;
189 }
190 }
191
192 if (thrown)
193 {
194 EXPECT_EQ(g.get_num_nodes(), 3u);
195 expect_arcs(g, arcs);
196 continue;
197 }
198
199 EXPECT_EQ(g.get_num_nodes(), 2u);
200 expect_arcs(g, {arcs[3]});
201 break;
202 }
203}
204
205namespace
206{
208
209 // The sets of sources and of sinks of net are those of its arcs
210 void expect_sources_and_sinks(const Net & net)
211 {
212 for (Net::Node_Iterator it(net); it.has_curr(); it.next_ne())
213 {
214 auto p = it.get_curr_ne();
215 EXPECT_EQ(net.is_source(p), net.get_in_degree(p) == 0);
216 EXPECT_EQ(net.is_sink(p), net.get_out_degree(p) == 0);
217 }
218 }
219
220 struct NetWithoutMemory : public ::testing::Test
221 {
222 void SetUp() override
223 {
225 GTEST_SKIP() << "ThreadSanitizer replaces the global operator new";
226 }
227 };
228}
229
230// remove_arc() and disconnect_arc() put in the set of sinks the source of
231// the arc after the arc was gone, so without memory for it the network had
232// a sink that was not in its set; disconnect_arc() was noexcept, too
233TEST_F(NetWithoutMemory, ArcsGoOnlyIfTheSetsOfSourcesAndSinksTakeTheirEnds)
234{
235 for (int how = 0; how < 3; ++how) // remove_arc(), disconnect_arc(), remove_node()
236 for (size_t k = 1; ; ++k)
237 {
238 SCOPED_TRACE(testing::Message() << "way " << how << ", allocation " << k);
239 Net net;
240 auto s = net.insert_node(0);
241 auto a = net.insert_node(1);
242 auto t = net.insert_node(2);
243 auto sa = net.insert_arc(s, a, 3, 0);
244 net.insert_arc(a, t, 2, 0);
245 net.insert_arc(s, t, 1, 0);
246 bool thrown = false;
247 {
248 Fail_At failing(k);
249 try
250 {
251 if (how == 0)
252 net.remove_arc(sa);
253 else if (how == 1)
254 net.disconnect_arc(sa);
255 else
256 net.remove_node(a);
257 }
258 catch (const std::bad_alloc &)
259 {
260 thrown = true;
261 }
262 }
263 expect_sources_and_sinks(net);
264 if (how == 1 and not thrown)
265 net.connect_arc(sa);
266 if (not thrown)
267 break;
268 if (how < 2)
269 EXPECT_EQ(net.get_num_arcs(), 3u);
270 else // the arcs of a that went are gone, and a is still there
271 EXPECT_EQ(net.get_num_nodes(), 3u);
272 }
273}
274
275// clear_graph() was noexcept, though a network puts in its sets of sources
276// and sinks the ends of the arcs that it removes, so that without memory
277// for them the program ended
278TEST_F(NetWithoutMemory, ClearGraphThrowsAndLeavesTheNetworkRight)
279{
280 for (size_t k = 1; ; ++k)
281 {
282 SCOPED_TRACE(testing::Message() << "allocation " << k);
283 Net net;
284 auto s = net.insert_node(0);
285 auto a = net.insert_node(1);
286 auto t = net.insert_node(2);
287 net.insert_arc(s, a, 3, 0);
288 net.insert_arc(a, t, 2, 0);
289 net.insert_arc(s, t, 1, 0);
290 bool thrown = false;
291 {
292 Fail_At failing(k);
293 try
294 {
295 clear_graph(net);
296 }
297 catch (const std::bad_alloc &)
298 {
299 thrown = true;
300 }
301 }
302 expect_sources_and_sinks(net);
303 if (not thrown)
304 {
305 EXPECT_EQ(net.get_num_arcs(), 0u);
306 EXPECT_EQ(net.get_num_nodes(), 0u);
307 break;
308 }
309 EXPECT_EQ(net.get_num_nodes(), 3u); // the nodes go after the arcs
310 clear_graph(net);
311 EXPECT_EQ(net.get_num_nodes(), 0u);
312 }
313}
314
315// The copy assignment emptied the graph before it copied the other, so
316// without memory for the copy the graph lost what it had
317TYPED_TEST(GraphWithoutMemory, CopyAssignmentLeavesTheGraphAsItWas)
318{
319 using G = TypeParam;
320 G other;
321 auto x = other.insert_node(10);
322 auto y = other.insert_node(20);
323 other.insert_arc(x, y, 7);
324 other.insert_arc(y, y, 8);
325
326 int mark = 0;
327 for (size_t k = 1; ; ++k)
328 {
329 SCOPED_TRACE(testing::Message() << "allocation " << k);
330 G g;
331 auto a = g.insert_node(1);
332 auto b = g.insert_node(2);
333 std::vector<typename G::Arc *> arcs = {g.insert_arc(a, b, 1)};
334 g.get_cookie() = &mark;
335
336 bool thrown = false;
337 {
338 Fail_At failing(k);
339 try
340 {
341 g = other;
342 }
343 catch (const std::bad_alloc &)
344 {
345 thrown = true;
346 }
347 }
348
349 EXPECT_EQ(g.get_cookie(), &mark);
350 EXPECT_EQ(g.is_digraph(), other.is_digraph());
351 if (thrown)
352 {
353 EXPECT_EQ(g.get_num_nodes(), 2u);
354 expect_arcs(g, arcs);
355 continue;
356 }
357
358 EXPECT_EQ(g.get_num_nodes(), 2u);
359 EXPECT_EQ(g.get_num_arcs(), 2u);
360 int sum = 0;
361 for (typename G::Arc_Iterator it(g); it.has_curr(); it.next_ne())
362 sum += it.get_curr_ne()->get_info();
363 EXPECT_EQ(sum, 15);
364 break;
365 }
366}
367
368// List_SGraph::insert_node() counted the node before it put it in its set
369// of nodes, so without memory for the set the graph counted a node that it
370// did not have, and the node was lost
371TYPED_TEST(GraphWithoutMemory, InsertNodeLeavesTheGraphAsItWas)
372{
373 using G = TypeParam;
374 for (size_t k = 1; ; ++k)
375 {
376 SCOPED_TRACE(testing::Message() << "allocation " << k);
377 G g;
378 g.insert_node(1);
379 bool thrown = false;
380 {
381 Fail_At failing(k);
382 try
383 {
384 g.insert_node(2);
385 }
386 catch (const std::bad_alloc &)
387 {
388 thrown = true;
389 }
390 }
391
392 size_t nodes = 0;
393 for (typename G::Node_Iterator it(g); it.has_curr(); it.next_ne())
394 ++nodes;
395 EXPECT_EQ(nodes, g.get_num_nodes());
396 if (not thrown)
397 {
398 EXPECT_EQ(nodes, 2u);
399 break;
400 }
401 EXPECT_EQ(nodes, 1u);
402 }
403}
404
405namespace
406{
407 // The tests need the operator new of allocation_failure.H
408 template <class G>
409 struct CopiesWithoutMemory : public ::testing::Test
410 {
411 void SetUp() override
412 {
414 GTEST_SKIP() << "ThreadSanitizer replaces the global operator new";
415 }
416 };
417
418 using Undirected_Kinds =
419 ::testing::Types<List_Graph<Graph_Node<int>, Graph_Arc<int>>,
422
423 // Two triangles joined at the node 2, the second one with a tail: the
424 // cut nodes are 2 and 4
425 template <class G>
426 std::vector<typename G::Node *> bow(G & g)
427 {
428 std::vector<typename G::Node *> v;
429 for (int i = 0; i < 6; ++i)
430 v.push_back(g.insert_node(i));
431
432 const int arcs[][2] = {{0, 1}, {1, 2}, {2, 0}, {2, 3}, {3, 4}, {4, 2}, {4, 5}};
433 int k = 1;
434 for (const auto & a : arcs)
435 g.insert_arc(v[a[0]], v[a[1]], k++);
436 return v;
437 }
438
439 // Runs copy failing at its allocations, one after the other, until it
440 // does not throw
441 template <class Copy>
442 void fail_each_allocation(Copy copy)
443 {
444 for (size_t k = 1; ; ++k)
445 {
446 SCOPED_TRACE(testing::Message() << "allocation " << k);
447 bool thrown = false;
448 {
449 Fail_At failing(k);
450 try
451 {
452 copy();
453 }
454 catch (const std::bad_alloc &)
455 {
456 thrown = true;
457 }
458 }
459 if (not thrown)
460 return;
461 }
462 }
463}
464
465TYPED_TEST_SUITE(CopiesWithoutMemory, Undirected_Kinds);
466
467// The copies kept each new node in a unique_ptr while they inserted it,
468// but List_SGraph::insert_node() deletes the node that it cannot take, so
469// that the node was deleted twice; Array_Graph::insert_node() did not
470// delete it, so that the nodes that insert_node(info) made were lost.
471// AddressSanitizer stops at a node deleted twice, and LeakSanitizer
472// reports a lost one
473TYPED_TEST(CopiesWithoutMemory, EachNodeIsDeletedOnce)
474{
475 using G = TypeParam;
476 G g;
477 auto v = bow(g);
478 auto cuts = compute_cut_nodes(g, v[0]);
479 ASSERT_EQ(cuts.size(), 2u);
480 paint_subgraphs(g, cuts);
481 const long color = NODE_COUNTER(v[0]);
482
483 fail_each_allocation([&] { G tree = find_breadth_first_spanning_tree(g, v[0]); });
484 fail_each_allocation([&] { G block = map_subgraph(g, color); });
485 fail_each_allocation([&] { auto copies = map_cut_graph(g, cuts); });
486
489 cc(v[0], cut_list);
490 cc.paint_subgraphs();
491 fail_each_allocation([&]
492 {
493 G block;
494 cc.map_subgraph(block, NODE_COUNTER(v[0]));
495 });
496 fail_each_allocation([&]
497 {
498 G cut_graph;
500 cc.map_cut_graph(cut_graph, cross);
501 });
502
503 fail_each_allocation([&] { (void) g.insert_node(7); });
504}
A global operator new that a test can make fail.
#define ALEPH_ALLOCATION_FAILURE_UNDER_TSAN
Computation of cut nodes (articulation points) of a graph.
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:4377
Dynamic doubly linked list with O(1) size and bidirectional access.
Arc for graphs implemented with simple adjacency lists.
Definition tpl_sgraph.H:197
Iterator on the arcs of a graph.
Definition tpl_graph.H:854
Graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:439
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
virtual void remove_node(Node *node)
Remove a node from the graph and free its memory.
Definition tpl_graph.H:557
_Graph_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
Graph class implemented with singly-linked adjacency lists.
Definition tpl_sgraph.H:274
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
bool is_digraph() const noexcept
Return true if the graph this is directed.
Definition graph-dry.H:937
void *& get_cookie() noexcept
Return a modifiable reference to graph's cookie.
Definition graph-dry.H:931
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
Definition graph-dry.H:1064
TEST_F(NetWithoutMemory, ArcsGoOnlyIfTheSetsOfSourcesAndSinksTakeTheirEnds)
TYPED_TEST_SUITE(GraphWithoutMemory, Graph_Kinds)
TYPED_TEST(GraphWithoutMemory, InsertArcLeavesTheGraphAsItWas)
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
DynArray< Graph::Arc * > arcs
Definition graphpic.C:408
#define NODE_COUNTER(p)
Get the counter of a node.
GT find_breadth_first_spanning_tree(GT &g, typename GT::Node *gp)
Build a breadth-first spanning tree (mapped to the original graph).
long paint_subgraphs(const GT &g, const DynList< typename GT::Node * > &cut_node_list)
Paint connected blocks around articulation points.
DynList< typename GT::Node * > compute_cut_nodes(const GT &g, typename GT::Node *start)
Compute articulation points (cut vertices) of an undirected graph.
void clear_graph(GT &g)
Clean a graph: all its nodes and arcs are removed and freed.
Definition tpl_graph.H:4776
std::tuple< GT, DynList< typename GT::Arc * > > map_cut_graph(const GT &g, const DynList< typename GT::Node * > &cut_node_list)
Extract the cut graph and cross-arc list.
GT map_subgraph(const GT &g, const long color)
Extract a mapped subgraph containing a given color.
static mpfr_t y
Definition mpfr_mul_d.c:3
Main namespace for Aleph-w library functions.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
and
Check uniqueness with explicit hash + equality functors.
static long & color(typename GT::Node *p)
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
::testing::Types< Aleph::List_Graph< Aleph::Graph_Node< int >, Aleph::Graph_Arc< int > >, Aleph::List_SGraph< Aleph::Graph_Snode< int >, Aleph::Graph_Sarc< int > >, Aleph::Array_Graph< Aleph::Graph_Anode< int >, Aleph::Graph_Aarc< int > > > Undirected_Kinds
The kinds of graph that are not directed, with int infos.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:231
Iterator on all arcs of a graph.
Definition tpl_graph.H:941
Arc of a flow network implemented with adjacency lists.
Definition tpl_net.H:160
Flow network implemented with adjacency lists.
Definition tpl_net.H:311
Node * insert_node(const Node_Type &node_info)
Insert a new node by copying node_info.
Definition tpl_net.H:750
Arc * connect_arc(Arc *arc)
Connect a previously disconnected arc.
Definition tpl_net.H:832
void remove_arc(Arc *arc) override
Remove arc arc from the network.
Definition tpl_net.H:906
virtual void disconnect_arc(Arc *arc)
Disconnect arc arc from the graph without deleting it.
Definition tpl_net.H:921
size_t get_out_degree(Node *p) const noexcept
Return the out-degree of p (number of outgoing arcs).
Definition tpl_net.H:393
bool is_source(Node *node) const noexcept
Return true if node is a source.
Definition tpl_net.H:417
Arc * insert_arc(Node *src_node, Node *tgt_node, const Flow_Type &cap, const Flow_Type &flow, const typename Arc::Arc_Type &arc_info=Arc_Type())
Insert a capacitated arc with an initial flow.
Definition tpl_net.H:799
void remove_node(Node *p) override
Remove node p and all its arcs from the network.
Definition tpl_net.H:933
bool is_sink(Node *node) const noexcept
Return true if node is a sink.
Definition tpl_net.H:423
size_t get_in_degree(Node *p) const noexcept
Return the in-degree of p (number of incoming arcs).
Definition tpl_net.H:387
static int * k
Array-based graph implementation.
Articulation points (cut nodes), bridges, and biconnected components.
Generic graph and digraph implementations.
Utility algorithms and operations for graphs.
Network flow graph structures.
Simple graph implementation with adjacency lists.