44# include <gtest/gtest.h>
63 void complete(
Graph & g,
const int n)
66 for (
int i = 0; i < n; ++i)
68 for (
int i = 0; i < n; ++i)
69 for (
int j = 0; j < n; ++j)
79 std::vector<const void *>
arcs;
80 std::vector<double> numbers;
81 std::vector<size_t> positions;
83 explicit Answers(Index & index)
86 for (
size_t a = 0; a < index.num_arcs(); ++a)
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);
96 for (
size_t position = 0; position < index.num_cycles(); ++position)
98 numbers.push_back(index.weight_sum(position));
99 for (
const uint32_t a : index.cycle_arcs(position))
100 positions.push_back(a);
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)
108 positions.push_back(position);
109 numbers.push_back(sum);
113 bool operator == (
const Answers &)
const =
default;
123 GTEST_SKIP() <<
"ThreadSanitizer replaces the global operator new";
135 for (
size_t k = 1;
k <= 400; ++
k)
138 size_t cycles = 0, refused = 0;
143 const Index index(g, {2, 3, 4});
144 cycles = index.num_cycles();
146 catch (
const std::bad_alloc &)
150 refused = refused_allocations;
154 EXPECT_FALSE(thrown);
155 EXPECT_EQ(cycles, 10u + 20u + 30u);
156 EXPECT_GT(failures, 10u);
159 EXPECT_TRUE(thrown) <<
"allocation " <<
k;
160 EXPECT_EQ(cycles, 0u) <<
"allocation " <<
k;
163 ADD_FAILURE() <<
"an index asks for more than 400 allocations";
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));
182 const Index
next(g, {3, 4});
184 catch (
const std::bad_alloc &)
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));
195 Index
next(g, {3, 4});
200 asked = refused_allocations;
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);
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);
219 size_t handed = 0, seen = 0, asked = 0;
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);
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;
235 EXPECT_EQ(asked, 0u);
236 EXPECT_GT(handed, 0u);
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);
255 for (
size_t k = 1;
k <= 400; ++
k)
257 Index index(
h, {2, 3});
258 index.set_weight(0, -1);
259 index.set_weight(3, 0.25);
260 const Answers before(index);
269 catch (
const std::bad_alloc &)
273 refused = refused_allocations;
277 EXPECT_FALSE(thrown);
278 EXPECT_TRUE(Answers(index) == of_the_source);
279 EXPECT_GT(failures, 10u);
282 EXPECT_TRUE(thrown) <<
"allocation " <<
k;
283 EXPECT_TRUE(Answers(index) == before) <<
"allocation " <<
k;
286 ADD_FAILURE() <<
"an assignment asks for more than 400 allocations";
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);
299 const Answers before(index);
301 size_t asked = 0, left = 0;
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;
309 EXPECT_EQ(asked, 0u);
311 EXPECT_TRUE(Answers(other) == before);
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)
Simple dynamic array with automatic resizing and functional operations.
T & append(const T &data)
Append a copy of data
Index of the simple cycles of bounded length of a digraph.
Generic directed graph (digraph) wrapper template.
virtual Node * insert_node(Node *node) noexcept
Insertion of a node already allocated.
Arc Arc
The node class type.
Arc * insert_arc(Node *src_node, Node *tgt_node, void *a)
DynArray< Graph::Node * > nodes
DynArray< Graph::Arc * > arcs
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).
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Arc of graph implemented with double-linked adjacency lists.
Generic graph and digraph implementations.