Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
bounded_cycle_index_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
44# include <gtest/gtest.h>
45
46# include "allocation_failure.H"
47
48# include <new>
49# include <utility>
50# include <vector>
51
52# include <Bounded_Cycle_Index.H>
53# include <tpl_graph.H>
54
55using namespace Aleph;
56
57namespace
58{
60 using Index = Bounded_Cycle_Index<Graph>;
61
62 // The complete digraph of n nodes
63 void complete(Graph & g, const int n)
64 {
66 for (int i = 0; i < n; ++i)
68 for (int i = 0; i < n; ++i)
69 for (int j = 0; j < n; ++j)
70 if (i != j)
71 g.insert_arc(nodes(i), nodes(j), 0);
72 }
73
74 // All that an index answers, to compare two of them: its arcs with
75 // their weights, their stamps and their cycles, its cycles with their
76 // sums, its clock, and what a query of all its arcs hands
77 struct Answers
78 {
79 std::vector<const void *> arcs;
80 std::vector<double> numbers;
81 std::vector<size_t> positions;
82
83 explicit Answers(Index & index)
84 {
86 for (size_t a = 0; a < index.num_arcs(); ++a)
87 {
88 arcs.push_back(index.arc(a));
89 numbers.push_back(index.weight(a));
90 numbers.push_back(double(index.stamp(a)));
91 positions.push_back(index.arc_number(index.arc(a)));
92 for (const uint32_t position : index.cycles_of(a))
93 positions.push_back(position);
94 all.append(a);
95 }
96 for (size_t position = 0; position < index.num_cycles(); ++position)
97 {
98 numbers.push_back(index.weight_sum(position));
99 for (const uint32_t a : index.cycle_arcs(position))
100 positions.push_back(a);
101 }
102 positions.push_back(index.num_cycles());
103 positions.push_back(index.longest_length());
104 positions.push_back(size_t(index.clock()));
105 positions.push_back(index.for_each_cycle_below(all, 1e300, Index::Max_Length,
106 [this] (const size_t position, const double sum)
107 {
108 positions.push_back(position);
109 numbers.push_back(sum);
110 }));
111 }
112
113 bool operator == (const Answers &) const = default;
114 };
115}
116
117// The tests need the operator new of allocation_failure.H
118struct BoundedCycleIndexAllocation : public ::testing::Test
119{
120 void SetUp() override
121 {
123 GTEST_SKIP() << "ThreadSanitizer replaces the global operator new";
124 }
125};
126
127// Whatever allocation fails while an index is built, its constructor
128// throws std::bad_alloc; what it had taken is freed, which the sanitizers
129// see
130TEST_F(BoundedCycleIndexAllocation, BuildingThrowsBadAllocWhateverAllocationFails)
131{
132 Graph g;
133 complete(g, 5);
134 size_t failures = 0;
135 for (size_t k = 1; k <= 400; ++k)
136 {
137 bool thrown = false;
138 size_t cycles = 0, refused = 0;
139 {
140 Fail_At the_kth(k);
141 try
142 {
143 const Index index(g, {2, 3, 4});
144 cycles = index.num_cycles();
145 }
146 catch (const std::bad_alloc &)
147 {
148 thrown = true;
149 }
150 refused = refused_allocations;
151 }
152 if (refused == 0) // the constructor asks for less than k allocations
153 {
154 EXPECT_FALSE(thrown);
155 EXPECT_EQ(cycles, 10u + 20u + 30u);
156 EXPECT_GT(failures, 10u);
157 return;
158 }
159 EXPECT_TRUE(thrown) << "allocation " << k;
160 EXPECT_EQ(cycles, 0u) << "allocation " << k;
161 ++failures;
162 }
163 ADD_FAILURE() << "an index asks for more than 400 allocations";
164}
165
166// A set of lengths is changed with an index built apart and swap(): if
167// there is no memory to build it the index in use keeps what it had, and
168// swap() does not ask for memory
169TEST_F(BoundedCycleIndexAllocation, AnIndexBuiltApartReplacesAnotherWithoutAllocating)
170{
171 Graph g;
172 complete(g, 5);
173 Index in_use(g, {3});
174 for (size_t a = 0; a < in_use.num_arcs(); ++a)
175 in_use.set_weight(a, double(a));
176
177 bool thrown = false;
178 {
179 No_Memory no_memory;
180 try
181 {
182 const Index next(g, {3, 4});
183 }
184 catch (const std::bad_alloc &)
185 {
186 thrown = true;
187 }
188 }
189 EXPECT_TRUE(thrown);
190 EXPECT_EQ(in_use.num_cycles(), 20u);
191 EXPECT_EQ(in_use.longest_length(), 3u);
192 for (size_t a = 0; a < in_use.num_arcs(); ++a)
193 EXPECT_EQ(in_use.weight(a), double(a));
194
195 Index next(g, {3, 4});
196 size_t asked = 0;
197 {
198 No_Memory no_memory;
199 in_use.swap(next);
200 asked = refused_allocations;
201 }
202 EXPECT_EQ(asked, 0u);
203 EXPECT_EQ(in_use.num_cycles(), 50u);
204 EXPECT_EQ(next.num_cycles(), 20u);
205 EXPECT_EQ(next.weight(7), 7.0);
206}
207
208// A change of weight and the queries ask for no memory
209TEST_F(BoundedCycleIndexAllocation, TheQueriesDoNotAllocate)
210{
211 Graph g;
212 complete(g, 5);
213 Index index(g, {2, 3, 4});
214 for (size_t a = 0; a < index.num_arcs(); ++a)
215 index.set_weight(a, a % 2 == 0 ? -1.0 : 0.5);
216 const Array<size_t> arcs = {0, 3, 7, 3};
217 Graph::Arc *first = index.arc(0);
218
219 size_t handed = 0, seen = 0, asked = 0;
220 double sum = 0;
221 {
222 No_Memory no_memory;
223 const auto count = [&handed] (size_t, double) { ++handed; };
224 seen += index.update(2, -1.0, 0.0, 4, count);
225 seen += index.for_each_cycle_below(arcs, 0.0, 4, count);
226 seen += index.for_each_cycle_below({1, 2}, 0.0, 4, count);
227 seen += index.for_each_cycle_below(0.0, 4, count);
228 index.set_weight(1, 0.25);
229 index.touch(1);
230 sum += index.weight_sum(0) + index.weight(1);
231 seen += index.is_stale(0, 0) + index.cycles_of(0).size() + index.cycle_arcs(0).size();
232 seen += index.arc_number(first) + index.length(0) + index.num_cycles_up_to(3);
233 asked = refused_allocations;
234 }
235 EXPECT_EQ(asked, 0u);
236 EXPECT_GT(handed, 0u);
237 EXPECT_GT(seen, 0u);
238 EXPECT_NE(sum, 0.0);
239}
240
241// An assignment copies apart and then exchanges: whatever allocation of
242// the copy fails, it throws std::bad_alloc and the index keeps all that
243// it had
244TEST_F(BoundedCycleIndexAllocation, AnAssignmentThatFailsLeavesTheIndexAsItWas)
245{
246 Graph g, h;
247 complete(g, 5);
248 complete(h, 3);
249 Index source(g, {2, 3, 4});
250 for (size_t a = 0; a < source.num_arcs(); ++a)
251 source.set_weight(a, double(a) - 7.5);
252 const Answers of_the_source(source);
253
254 size_t failures = 0;
255 for (size_t k = 1; k <= 400; ++k)
256 {
257 Index index(h, {2, 3});
258 index.set_weight(0, -1);
259 index.set_weight(3, 0.25);
260 const Answers before(index);
261 bool thrown = false;
262 size_t refused = 0;
263 {
264 Fail_At the_kth(k);
265 try
266 {
267 index = source;
268 }
269 catch (const std::bad_alloc &)
270 {
271 thrown = true;
272 }
273 refused = refused_allocations;
274 }
275 if (refused == 0) // the assignment asks for less than k allocations
276 {
277 EXPECT_FALSE(thrown);
278 EXPECT_TRUE(Answers(index) == of_the_source);
279 EXPECT_GT(failures, 10u);
280 return;
281 }
282 EXPECT_TRUE(thrown) << "allocation " << k;
283 EXPECT_TRUE(Answers(index) == before) << "allocation " << k;
284 ++failures;
285 }
286 ADD_FAILURE() << "an assignment asks for more than 400 allocations";
287}
288
289// Moving an index, by construction or by assignment, asks for no memory
290TEST_F(BoundedCycleIndexAllocation, MovingAnIndexDoesNotAllocate)
291{
292 Graph g, h;
293 complete(g, 5);
294 complete(h, 3);
295 Index index(g, {2, 3, 4});
296 for (size_t a = 0; a < index.num_arcs(); ++a)
297 index.set_weight(a, a % 2 == 0 ? -1.0 : 0.5);
298 Index other(h, {2});
299 const Answers before(index);
300
301 size_t asked = 0, left = 0;
302 {
303 No_Memory no_memory;
304 Index taken(std::move(index));
305 other = std::move(taken);
306 left = index.num_arcs() + taken.num_arcs() + index.num_cycles() + taken.num_cycles();
307 asked = refused_allocations;
308 }
309 EXPECT_EQ(asked, 0u);
310 EXPECT_EQ(left, 0u);
311 EXPECT_TRUE(Answers(other) == before);
312}
Index of the simple cycles of bounded length of a digraph, with a weight for each arc and a filter by...
A global operator new that a test can make fail.
#define ALEPH_ALLOCATION_FAILURE_UNDER_TSAN
TEST_F(BoundedCycleIndexAllocation, BuildingThrowsBadAllocWhateverAllocationFails)
long double h
Definition btreepic.C:154
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
Index of the simple cycles of bounded length of a digraph.
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:4377
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Definition tpl_graph.H:535
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
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
DynArray< Graph::Arc * > arcs
Definition graphpic.C:408
Main namespace for Aleph-w library functions.
bool operator==(const DynList< T > &l1, const DynList< T > &l2)
Equality operator for DynList.
bool all(Container &container, Operation &operation)
Return true if all elements satisfy a predicate.
void next()
Advance all underlying iterators (bounds-checked).
Definition ah-zip.H:236
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Arc of graph implemented with double-linked adjacency lists.
Definition tpl_graph.H:231
static int * k
Generic graph and digraph implementations.