Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
random_generators_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
45#include <gtest/gtest.h>
46#include <Karger.H>
47#include <ah-random.H>
48#include <bloom-filter.H>
50#include <hash-fct.H>
51#include <random_graph.H>
52#include <random_net.H>
53#include <random_tree.H>
54#include <reservoir-sampling.H>
55#include <tpl_graph.H>
56#include <tpl_net.H>
57#include <tpl_sgraph.H>
58
59#include <algorithm>
60#include <cstdint>
61#include <cstdio>
62#include <limits>
63#include <stdexcept>
64#include <string>
65#include <tuple>
66#include <type_traits>
67#include <unordered_map>
68#include <utility>
69#include <vector>
70
71using namespace Aleph;
72
73namespace
74{
79
80 void num(std::string & out, const unsigned long long x)
81 {
82 out += ' ';
83 out += std::to_string(x);
84 }
85
86 void real(std::string & out, const double x)
87 {
88 char buf[40];
89 std::snprintf(buf, sizeof buf, " %.6g", x);
90 out += buf;
91 }
92
93 // The nodes are numbered in the order of the graph; then each arc is
94 // written by the numbers of its nodes, in the order of the graph too, with
95 // what arc_info writes of it
96 template <class GT, class Node_Info, class Arc_Info>
97 std::string describe(const GT & g, Node_Info node_info, Arc_Info arc_info)
98 {
99 std::string out;
100 std::unordered_map<const void *, size_t> number;
101 num(out, g.get_num_nodes());
102 num(out, g.get_num_arcs());
103 for (typename GT::Node_Iterator it(g); it.has_curr(); it.next_ne())
104 {
105 auto p = it.get_curr();
106 number.emplace(p, number.size());
107 node_info(out, p);
108 }
109 out += " |";
110 for (typename GT::Arc_Iterator it(g); it.has_curr(); it.next_ne())
111 {
112 auto a = it.get_curr();
113 num(out, number.at(g.get_src_node(a)));
114 num(out, number.at(g.get_tgt_node(a)));
115 arc_info(out, a);
116 }
117 return out;
118 }
119
120 template <class GT>
121 std::string describe(const GT & g)
122 {
123 return describe(g, [] (std::string &, auto) {}, [] (std::string &, auto) {});
124 }
125
126 // The nodes and the arcs of a List_SGraph go in the order of their
127 // addresses, which changes from a run to another: the nodes are written by
128 // their positions, which are distinct, in their order, and then each arc by
129 // the positions of its ends, the least first, in their order, with its
130 // weight
131 std::string describe_euclidian(const EGraph & g)
132 {
133 using Point = std::pair<long, long>;
134 std::vector<Point> nodes;
135 for (EGraph::Node_Iterator it(g); it.has_curr(); it.next_ne())
136 nodes.emplace_back(it.get_curr()->get_info().x, it.get_curr()->get_info().y);
137 std::sort(nodes.begin(), nodes.end());
138
139 std::vector<std::tuple<Point, Point, double>> arcs;
140 for (EGraph::Arc_Iterator it(g); it.has_curr(); it.next_ne())
141 {
142 auto a = it.get_curr();
143 const My_P & p = g.get_src_node(a)->get_info();
144 const My_P & q = g.get_tgt_node(a)->get_info();
145 Point u(p.x, p.y), v(q.x, q.y);
146 if (v < u)
147 std::swap(u, v);
148 arcs.emplace_back(u, v, a->get_info());
149 }
150 std::sort(arcs.begin(), arcs.end());
151
152 std::string out;
153 num(out, g.get_num_nodes());
154 num(out, g.get_num_arcs());
155 for (const auto & [x, y] : nodes)
156 {
157 num(out, x);
158 num(out, y);
159 }
160 out += " |";
161 for (const auto & [u, v, w] : arcs)
162 {
163 num(out, u.first);
164 num(out, u.second);
165 num(out, v.first);
166 num(out, v.second);
167 real(out, w);
168 }
169 return out;
170 }
171
172 void tree_pre(Tree_Node<int> * p, std::string & out)
173 {
174 size_t children = 0;
175 for (auto c = p->get_left_child(); c != nullptr; c = c->get_right_sibling())
176 ++children;
177 num(out, children);
178 for (auto c = p->get_left_child(); c != nullptr; c = c->get_right_sibling())
179 tree_pre(c, out);
180 }
181
182 // Two cliques of six nodes, joined by three arcs, with three more arcs
183 // inside the first one
184 UGraph karger_graph()
185 {
186 UGraph g;
187 std::vector<UGraph::Node *> n;
188 for (int i = 0; i < 12; ++i)
189 n.push_back(g.insert_node(i));
190 for (int c = 0; c < 12; c += 6)
191 for (int i = c; i < c + 6; ++i)
192 for (int j = i + 1; j < c + 6; ++j)
193 g.insert_arc(n[i], n[j]);
194 g.insert_arc(n[0], n[6]);
195 g.insert_arc(n[2], n[9]);
196 g.insert_arc(n[5], n[11]);
197 g.insert_arc(n[1], n[3]);
198 g.insert_arc(n[2], n[4]);
199 g.insert_arc(n[0], n[5]);
200 return g;
201 }
202
203 // A cut: its size, and the numbers of the nodes of each side and of the
204 // arcs that cross it, in the order of their lists
205 std::string describe_cut(UGraph & g, const size_t size,
206 const DynList<UGraph::Node *> & vs,
207 const DynList<UGraph::Node *> & vt,
208 const DynList<UGraph::Arc *> & cut)
209 {
210 std::unordered_map<const void *, size_t> number;
211 for (UGraph::Node_Iterator it(g); it.has_curr(); it.next_ne())
212 number.emplace(it.get_curr(), number.size());
213 std::unordered_map<const void *, size_t> arc_number;
214 for (UGraph::Arc_Iterator it(g); it.has_curr(); it.next_ne())
215 arc_number.emplace(it.get_curr(), arc_number.size());
216
217 std::string out;
218 num(out, size);
219 out += " S";
220 for (auto it = vs.get_it(); it.has_curr(); it.next_ne())
221 num(out, number.at(it.get_curr()));
222 out += " T";
223 for (auto it = vt.get_it(); it.has_curr(); it.next_ne())
224 num(out, number.at(it.get_curr()));
225 out += " C";
226 for (auto it = cut.get_it(); it.has_curr(); it.next_ne())
227 num(out, arc_number.at(it.get_curr()));
228 return out;
229 }
230
231 std::uint64_t fnv1a(const std::string & s)
232 {
233 std::uint64_t h = 0xcbf29ce484222325ULL;
234 for (const unsigned char c : s)
235 {
236 h ^= c;
237 h *= 0x100000001b3ULL;
238 }
239 return h;
240 }
241
242 // The lines of output for a seed: the seed, and then each generator
243 std::vector<std::string> lines_for(const unsigned long s)
244 {
245 std::vector<std::string> lines;
246 std::string out = "== seed";
247 num(out, s);
248 lines.push_back(out);
249
250 {
252 lines.push_back("graph sparse:" + describe(gen(30, size_t(60))));
253 lines.push_back("graph p:" + describe(gen(25, 0.2)));
254 lines.push_back("graph sparse, not connected:" + describe(gen(30, size_t(20), false)));
255 lines.push_back("graph eulerian:" + describe(gen.eulerian(20, size_t(40))));
256 lines.push_back("graph eulerian p:" + describe(gen.eulerian(16, 0.3)));
257 lines.push_back("graph hamiltonian:" + describe(gen.sufficient_hamiltonian(14, 0.3)));
258 }
259 {
261 lines.push_back("digraph sparse:" + describe(gen(30, size_t(70))));
262 lines.push_back("digraph p:" + describe(gen(20, 0.15)));
263 lines.push_back("digraph eulerian:" + describe(gen.eulerian(16, size_t(40))));
264 lines.push_back("digraph eulerian p:" + describe(gen.eulerian(12, 0.3)));
265 lines.push_back("digraph hamiltonian:" + describe(gen.sufficient_hamiltonian(12, 0.3)));
266 }
267 {
268 Random_Network_Flow<Net> gen(static_cast<unsigned int>(s));
269 Net * net = gen(4, 6);
270 lines.push_back("network:" + describe(*net, [] (std::string &, auto) {},
271 [] (std::string & o, auto a) { real(o, a->cap); }));
272 delete net;
273 net = gen(3, 5, 0.3, 50, 10, 0.5, 0.1);
274 lines.push_back("network 2:" + describe(*net, [] (std::string &, auto) {},
275 [] (std::string & o, auto a) { real(o, a->cap); }));
276 delete net;
277 }
278 {
279 RandTree<int> gen(s);
280 for (const size_t n : {1, 2, 9, 60})
281 {
282 Tree_Node<int> * t = gen(n);
283 out = "tree " + std::to_string(n) + ":";
284 tree_pre(t, out);
285 lines.push_back(out);
286 destroy_tree(t);
287 }
288 }
289 {
290 UGraph g = karger_graph();
291 Karger_Min_Cut<UGraph> karger(s);
294 size_t size = karger(g, vs, vt, cut, 3);
295 out = "karger:" + describe_cut(g, size, vs, vt, cut);
296 vs.empty();
297 vt.empty();
298 cut.empty();
299 size = karger.fast(g, vs, vt, cut, 2);
300 out += " fast:" + describe_cut(g, size, vs, vt, cut);
301 num(out, karger.compute_min_cut_size(g, 4));
302 // s + 1 modulo the maximum of the generator, as reseed() takes it, so
303 // that it is the same where unsigned long has 32 bits, and s + 1 would
304 // wrap to 0
305 karger.reseed(static_cast<unsigned long>((s + 1ULL) % Gsl_Compatible_Rng::max()));
306 num(out, karger.compute_min_cut_size(g, 1));
307 lines.push_back(out);
308 }
309 {
310 Bloom_Filter<int> f(1024, 7, dft_seed_hash_ptr_fct<int>, s);
311 const DynList<size_t> seeds = f.hash_seeds();
312 out = "bloom:";
313 for (auto it = seeds.get_it(); it.has_curr(); it.next_ne())
314 num(out, it.get_curr());
315 lines.push_back(out);
316 }
317 {
318 Reservoir_Sampler<int> sampler(10, s);
319 for (int i = 0; i < 1000; ++i)
320 sampler.update(i);
321 out = "reservoir:";
322 for (const int x : sampler.get_sample())
323 num(out, x);
324 sampler.clear();
325 for (int i = 0; i < 300; ++i)
326 sampler.update(i);
327 out += " |";
328 for (const int x : sampler.get_sample())
329 num(out, x);
330 std::vector<int> v(500);
331 for (int i = 0; i < 500; ++i)
332 v[i] = 3 * i;
333 out += " |";
334 for (const int x : reservoir_sample(v.begin(), v.end(), 7, s))
335 num(out, x);
336 lines.push_back(out);
337 }
338 {
339 const EGraph g = gen_random_euclidian_graph<EGraph>(25, 50, 40, 30,
340 static_cast<unsigned int>(s));
341 lines.push_back("euclidian:" + describe_euclidian(g));
342 }
343 {
344 init_jsw(static_cast<std::uint32_t>(s));
345 out = "jsw:";
346 for (int b = 0; b < 256; ++b)
347 {
348 const unsigned char c = static_cast<unsigned char>(b);
349 num(out, jsw_hash(&c, 1));
350 }
351 num(out, jsw_hash("Aleph-w"));
352 lines.push_back(out);
354 }
355 return lines;
356 }
357} // namespace
358
359TEST(RandomGenerators, WhatTheyMadeWithGsl)
360{
361 // The hashes of the lines that the generators gave with GSL. For each
362 // seed: the line of the seed; the graphs, sparse, of a probability,
363 // sparse and not connected, Eulerian, Eulerian of a probability and
364 // Hamiltonian; the digraphs, all of these but the one not connected; two
365 // networks; the trees of 1, 2, 9 and 60 nodes; the cuts of Karger; the
366 // seeds of a Bloom filter; the samples of a reservoir; a Euclidean graph;
367 // and the table of jsw_hash()
368 static const std::uint64_t golden[] = {
369 // seed 0
370 0xbf6aeb12f149fed4ULL, 0x6f511526ebaf569dULL, 0x4deba50a2eb8f7bfULL,
371 0x381da76af356cab3ULL, 0x7d7424f2d8d0b8e1ULL, 0xc50c17f5f47b752bULL,
372 0x1f0f26144038e7afULL, 0xb31eb3736eb3f411ULL, 0x6e272aeee69e1748ULL,
373 0x8003699f11cf9091ULL, 0xdd2be74b369a4f2cULL, 0xe4856f3b289dccc4ULL,
374 0x879b4e00418acd95ULL, 0x604a5935b83cf980ULL, 0x869a8495dc14f410ULL,
375 0x1fd3b68b81e0134cULL, 0x7deb970b96ba0f48ULL, 0xc021367a91012c46ULL,
376 0x43a0c494702d1e9dULL, 0x3d624cbf1b43cc29ULL, 0x023cff92af896d37ULL,
377 0xcc42b61139e1f130ULL, 0xc231bc7683da6e00ULL,
378 // seed 1
379 0xbf6aec12f14a0087ULL, 0x56d0bbcf5af437ceULL, 0x3ab41b2c09e37704ULL,
380 0x573c642fbf2f41e0ULL, 0x070de05da19320c0ULL, 0x264d306783592285ULL,
381 0x6cedd2e2e32a2cb3ULL, 0xdc9175f4ec78fecdULL, 0x54041d6fd6ca1e58ULL,
382 0x4a3a05c605ca3a0eULL, 0x52525b04fd596d26ULL, 0x16797da1dd32516eULL,
383 0x03ca174932a6cec9ULL, 0x8a6f04069cf466d3ULL, 0x869a8495dc14f410ULL,
384 0x1fd3b68b81e0134cULL, 0x6e42745bebcf360aULL, 0xf94ee3ba7def82ccULL,
385 0x961132833c4dbbfbULL, 0x08460e090b658b25ULL, 0xfc3619b02331dd7eULL,
386 0xc8efa1228c3f4cfbULL, 0xd011d4ff65e89969ULL,
387 // seed 12345
388 0x118e2e24d31a4c4fULL, 0x47a3a754887a996bULL, 0x54361a13cda8abb7ULL,
389 0x175dd2f949705fd8ULL, 0x87fdc70c5167a565ULL, 0x6faffbcd27687fedULL,
390 0xcce4ecf6e78fe225ULL, 0x3965f88a369d62a9ULL, 0x3435461ab9f4aa48ULL,
391 0xa5ec352947df983cULL, 0x755a5e0527a471bfULL, 0x53b99ddd4a1ef6b0ULL,
392 0x4dc48e12ede388d7ULL, 0xdcb58dc54b6f1424ULL, 0x869a8495dc14f410ULL,
393 0x1fd3b68b81e0134cULL, 0x01436d933b157c12ULL, 0xaa77eac337f88546ULL,
394 0x5b422700949e4ed5ULL, 0x2e3c29003279ca09ULL, 0xa38ea345c62a0ffcULL,
395 0xdf00ab511e4a950fULL, 0x7e1acb6a3066d4d8ULL,
396 // seed 4294967294
397 0x0d24555385f3f048ULL, 0x126d1c47f46d7d7bULL, 0x6ba1fbd423966727ULL,
398 0x4eb4a207cb87715dULL, 0x21656c559ce34bbdULL, 0x594ba60185f79232ULL,
399 0x62fec0c48f08de63ULL, 0x691ac06dd47e445aULL, 0x83f445956ca9ed11ULL,
400 0x2745874268d4fa70ULL, 0x902e749f5766405dULL, 0x5693c2ea7bab028bULL,
401 0xb136634f8e41d264ULL, 0x296a197a405ebf8eULL, 0x869a8495dc14f410ULL,
402 0x1fd3b68b81e0134cULL, 0x0ae4cc656280e88cULL, 0xc5223f7e67c1b05aULL,
403 0x791ea0482fe7868fULL, 0x1cbed8d3f9ac9a59ULL, 0xec3276c7d3d4d486ULL,
404 0x21bc3e31556dc712ULL, 0x11b74001e3c3be38ULL,
405 // seed 4294967295
406 0x0d24565385f3f1fbULL, 0x6f511526ebaf569dULL, 0x4deba50a2eb8f7bfULL,
407 0x381da76af356cab3ULL, 0x7d7424f2d8d0b8e1ULL, 0xc50c17f5f47b752bULL,
408 0x1f0f26144038e7afULL, 0xb31eb3736eb3f411ULL, 0x6e272aeee69e1748ULL,
409 0x8003699f11cf9091ULL, 0xdd2be74b369a4f2cULL, 0xe4856f3b289dccc4ULL,
410 0x879b4e00418acd95ULL, 0x604a5935b83cf980ULL, 0x869a8495dc14f410ULL,
411 0x1fd3b68b81e0134cULL, 0x7deb970b96ba0f48ULL, 0xc021367a91012c46ULL,
412 0x43a0c494702d1e9dULL, 0x3d624cbf1b43cc29ULL, 0x6ba91bf1c4cdde62ULL,
413 0xcc42b61139e1f130ULL, 0xc231bc7683da6e00ULL,
414 // seed 4294967301
415 0x042cb55380c9916dULL, 0xbb3a9f9aadeda92fULL, 0x72d4029847eef99cULL,
416 0x0072c6a658d68841ULL, 0x342bea24d3ed1ff0ULL, 0x4db7bd2097f438f1ULL,
417 0xafa2dc2d2911b783ULL, 0x16c2089df454e958ULL, 0x582699ac9686606bULL,
418 0x6d8d2143b2a23eaeULL, 0x05a6eb57a52c50f8ULL, 0x885da9241efdf674ULL,
419 0x559d5766aa658ddeULL, 0xad574524c68180abULL, 0x869a8495dc14f410ULL,
420 0x1fd3b68b81e0134cULL, 0xab1cf6530f406d02ULL, 0xdfacce1ca0dc16e0ULL,
421 0x1dbaf9068def4d74ULL, 0xfe5efd0c084ef1d7ULL, 0xe3914214f007e432ULL,
422 0xcd587691155ba501ULL, 0x7b22f892cc2c2d90ULL,
423 };
424
425 // The table of jsw_hash() is of long: where long has 32 bits and size_t
426 // 64, as on Windows, its numbers above 2^31 - 1 are negative, and the
427 // hashes of the line of the table, for the seeds below 2^32, are these,
428 // which GSL gave there too. Where size_t has 32 bits too, jsw_hash()
429 // gives other hashes, which the test does not have: it does not compare
430 // the line there
431 static const std::uint64_t golden_jsw_long32[] = {
432 0x3ff9424109e6654dULL, 0x49f945dd5ec91e54ULL, 0x3521995e48d2e088ULL,
433 0x575c8e62a68e5ce6ULL, 0x3ff9424109e6654dULL,
434 };
435
436 // The last seed, above 32 bits, exists only where unsigned long has 64:
437 // where it has 32, its lines are skipped
438 const unsigned long long seeds[] = {0, 1, 12345, 0xfffffffeULL, 0xffffffffULL,
439 0x100000005ULL};
440 const size_t lines_per_seed = 23;
441 size_t i = 0;
442 for (size_t k = 0; k < std::size(seeds); ++k)
443 {
444 const unsigned long long s = seeds[k];
445 if (s > std::numeric_limits<unsigned long>::max())
446 {
447 i += lines_per_seed;
448 continue;
449 }
450
451 const std::vector<std::string> lines = lines_for(static_cast<unsigned long>(s));
452 ASSERT_EQ(lines.size(), lines_per_seed);
453 for (size_t j = 0; j < lines_per_seed; ++j, ++i)
454 {
455 ASSERT_LT(i, std::size(golden));
456 std::uint64_t expected = golden[i];
457 if (sizeof(long) == 4 and j == lines_per_seed - 1) // the table of jsw_hash()
458 {
459 if (sizeof(std::size_t) != 8)
460 continue;
461 ASSERT_LT(k, std::size(golden_jsw_long32));
462 expected = golden_jsw_long32[k];
463 }
464 EXPECT_EQ(fnv1a(lines[j]), expected) << lines[j];
465 }
466 }
467 EXPECT_EQ(i, std::size(golden));
468}
469
470TEST(RandomGenerators, TheGeneratorsAreNotCopied)
471{
472 // A copy of a RandTree compiled, and both freed the same generator
473 EXPECT_FALSE(std::is_copy_constructible_v<RandTree<int>>);
474 EXPECT_FALSE(std::is_copy_assignable_v<RandTree<int>>);
475
476 // The others keep what they had: a network generator is neither copied
477 // nor moved, Karger and a reservoir are moved
478 EXPECT_FALSE(std::is_copy_constructible_v<Random_Network_Flow<Net>>);
479 EXPECT_FALSE(std::is_move_constructible_v<Random_Network_Flow<Net>>);
480 EXPECT_FALSE(std::is_copy_constructible_v<Karger_Min_Cut<UGraph>>);
481 EXPECT_TRUE(std::is_nothrow_move_constructible_v<Karger_Min_Cut<UGraph>>);
482 EXPECT_TRUE(std::is_move_assignable_v<Karger_Min_Cut<UGraph>>);
483 EXPECT_FALSE(std::is_copy_constructible_v<Reservoir_Sampler<int>>);
484 EXPECT_TRUE(std::is_move_constructible_v<Reservoir_Sampler<int>>);
485 EXPECT_FALSE((std::is_copy_constructible_v<Random_Graph<UGraph>>));
486}
487
488namespace
489{
490 // An arc filter whose moves throw while it is armed
491 struct Move_Throwing_Filter
492 {
493 static inline bool armed = false;
494
495 Move_Throwing_Filter() = default;
496
497 Move_Throwing_Filter(const Move_Throwing_Filter &) = default;
498
499 Move_Throwing_Filter(Move_Throwing_Filter &&)
500 {
501 if (armed)
502 throw std::runtime_error("the move of the filter");
503 }
504
505 Move_Throwing_Filter & operator = (const Move_Throwing_Filter &) = default;
506
507 Move_Throwing_Filter & operator = (Move_Throwing_Filter &&)
508 {
509 if (armed)
510 throw std::runtime_error("the move of the filter");
511 return *this;
512 }
513
514 bool operator () (UGraph::Arc *) const noexcept { return true; }
515 };
516} // namespace
517
518TEST(RandomGenerators, AKargerKeepsItsGeneratorWhenTheMoveOfItsFilterThrows)
519{
521 UGraph g = karger_graph();
522 Karger k(7);
523 Karger other(8);
524 Move_Throwing_Filter::armed = true;
525 EXPECT_THROW((void) Karger(std::move(k)), std::runtime_error);
526 EXPECT_THROW(other = std::move(k), std::runtime_error);
527 Move_Throwing_Filter::armed = false;
528
529 // Each one makes the cuts of a new solver of its seed
530 const std::pair<Karger *, unsigned long> cases[] = {{&k, 7}, {&other, 8}};
531 for (const auto & [karger, seed] : cases)
532 {
533 Karger twin(seed);
534 DynList<UGraph::Node *> vs1, vt1, vs2, vt2;
535 DynList<UGraph::Arc *> cut1, cut2;
536 const size_t c1 = (*karger)(g, vs1, vt1, cut1, 5);
537 const size_t c2 = twin(g, vs2, vt2, cut2, 5);
538 EXPECT_EQ(describe_cut(g, c1, vs1, vt1, cut1), describe_cut(g, c2, vs2, vt2, cut2))
539 << "seed " << seed;
540 }
541}
542
543TEST(RandomGenerators, TheEuclideanGeneratorClearsItsGenerator)
544{
545 (void) gen_random_euclidian_graph<EGraph>(10, 20, 10, 10, 3u);
546 EXPECT_EQ(rand_gen, nullptr);
547
548 // No nodes: the random graph throws, after rand_gen is set
549 EXPECT_THROW((void) gen_random_euclidian_graph<EGraph>(0, 1, 10, 10, 3u), std::domain_error);
550 EXPECT_EQ(rand_gen, nullptr);
551}
552
553TEST(RandomGenerators, AReservoirRefusesTheElementsThatItsGeneratorCannotPlace)
554{
555 // The element after n seen is drawn among n + 1 positions, and the
556 // generator draws among 2^32 - 1 at most
557 Reservoir_Sampler<int> s(3, 42);
558 for (int i = 0; i < 3; ++i)
559 s.update(i);
560 s.set_n_seen_for_testing(static_cast<size_t>(s.rng_range() - 2));
561 EXPECT_NO_THROW(s.update(7));
562 EXPECT_EQ(s.total_seen(), static_cast<size_t>(s.rng_range() - 1));
563 EXPECT_THROW(s.update(8), std::runtime_error);
564}
565
566#if ALEPH_WITH_GSL
567TEST(RandomGenerators, TheSeedsOfABloomFilterAreTheNumbersOfGsl)
568{
569 for (const unsigned long s : {0UL, 7UL, 0xfffffffeUL, 0xffffffffUL})
570 {
571 gsl_rng * r = gsl_rng_alloc(gsl_rng_mt19937);
572 gsl_rng_set(r, s % gsl_rng_max(r));
573 Bloom_Filter<int> f(512, 9, dft_seed_hash_ptr_fct<int>, s);
574 const DynList<size_t> seeds = f.hash_seeds();
575 ASSERT_EQ(seeds.size(), 9u);
576 for (auto it = seeds.get_it(); it.has_curr(); it.next_ne())
577 EXPECT_EQ(it.get_curr(), gsl_rng_get(r)) << "seed " << s;
578 gsl_rng_free(r);
579 }
580}
581
582TEST(RandomGenerators, TheTableOfJswHashHasTheNumbersOfGsl)
583{
584 // For a key of one byte b, jsw_hash() gives its initial value, 16777551,
585 // rotated by one bit, xor the entry b of its table, which keeps a number
586 // of GSL as a long
587 const size_t rotated = (size_t(16777551) << 1) | (size_t(16777551) >> 31);
588 for (const std::uint32_t s : {0u, 1u, 52679987u, 0xfffffffeu, 0xffffffffu})
589 {
590 init_jsw(s);
591 gsl_rng * r = gsl_rng_alloc(gsl_rng_mt19937);
592 gsl_rng_set(r, s % gsl_rng_max(r));
593 for (int b = 0; b < 256; ++b)
594 {
595 const unsigned char c = static_cast<unsigned char>(b);
596 const long entry = static_cast<long>(gsl_rng_get(r));
597 EXPECT_EQ(jsw_hash(&c, 1), rotated ^ static_cast<size_t>(entry))
598 << "seed " << s << ", byte " << b;
599 }
600 gsl_rng_free(r);
601 }
603}
604
605TEST(RandomGenerators, TheSampleOfAReservoirIsThatOfGsl)
606{
607 // Algorithm R, with the positions of gsl_rng_uniform_int(), seeded as the
608 // sampler is seeded
609 for (const unsigned long s : {0UL, 3UL, 0xffffffffUL})
610 {
611 gsl_rng * r = gsl_rng_alloc(gsl_rng_mt19937);
612 gsl_rng_set(r, s);
613 std::vector<int> expected;
614 for (int i = 0; i < 2000; ++i)
615 if (i < 16)
616 expected.push_back(i);
617 else if (const unsigned long j = gsl_rng_uniform_int(r, i + 1); j < 16)
618 expected[j] = i;
619 gsl_rng_free(r);
620
621 Reservoir_Sampler<int> sampler(16, s);
622 for (int i = 0; i < 2000; ++i)
623 sampler.update(i);
624 std::vector<int> got;
625 for (const int x : sampler.get_sample())
626 got.push_back(x);
627 EXPECT_EQ(got, expected) << "seed " << s;
628 }
629}
630#else
631// Without GSL (ALEPH_WITH_GSL is 0), the cases that compare with it
632// report SKIPPED instead of disappearing from the run, as
633// docs/ci_sanitizer_policy.md asks of a case that a configuration
634// cannot run; the hashes of the other cases stand for GSL
635TEST(RandomGenerators, TheSeedsOfABloomFilterAreTheNumbersOfGsl)
636{
637 GTEST_SKIP() << "without GSL (ALEPH_WITH_GSL is 0)";
638}
639
640TEST(RandomGenerators, TheTableOfJswHashHasTheNumbersOfGsl)
641{
642 GTEST_SKIP() << "without GSL (ALEPH_WITH_GSL is 0)";
643}
644
645TEST(RandomGenerators, TheSampleOfAReservoirIsThatOfGsl)
646{
647 GTEST_SKIP() << "without GSL (ALEPH_WITH_GSL is 0)";
648}
649#endif // ALEPH_WITH_GSL
Karger's randomized min-cut algorithm.
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
string Node_Info
Probabilistic set membership with Bloom filters.
long double h
Definition btreepic.C:154
long double w
Definition btreepic.C:153
size_t size_t int32_t * out
Definition ca-c-api.h:120
Bloom filter implementation.
Generic directed graph (digraph) wrapper template.
Definition graph-dry.H:4377
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
size_t size() const noexcept
Return the number of items of the list, in constant time.
Definition htlist.H:1264
void empty() noexcept
empty the list
Definition htlist.H:1576
Arc for graphs implemented with simple adjacency lists.
Definition tpl_sgraph.H:197
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^32 - 1.
Definition ah-random.H:259
Karger's randomized minimum cut algorithm.
Definition Karger.H:165
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
_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
Represents a point with rectangular coordinates in a 2D plane.
Definition point.H:221
Random directed graph (digraph) generator.
Random undirected graph generator.
Incremental reservoir sampler (Algorithm R).
void update(const T &val)
Process a new element from the stream.
uint64_t rng_range() const noexcept
RNG output range (max - min + 1).
void set_n_seen_for_testing(const size_t n)
Forcibly set the internal stream counter (for testing only).
size_t total_seen() const noexcept
Total elements processed so far.
Forward declaration used by CRTP helpers before the full node definition.
Tree_Node * get_left_child() const noexcept
Returns the leftmost child of this.
Minimal std::expected-style result type for C++20.
constexpr size_t get_num_nodes() const noexcept
Return the total of nodes of graph.
Definition graph-dry.H:975
Node * get_src_node(Arc *arc) const noexcept
Return the source node of arc (only for directed graphs)
Definition graph-dry.H:1017
constexpr size_t get_num_arcs() const noexcept
Return the number of arcs of the graph.
Definition graph-dry.H:1064
Node * get_tgt_node(Arc *arc) const noexcept
Return the target node of arc (only for directed graphs)
Definition graph-dry.H:1023
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
Generator for uniformly random trees.
#define TEST(name)
Common utilities for Euclidean graphs.
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
DynArray< Graph::Arc * > arcs
Definition graphpic.C:408
size_t jsw_hash(const void *key, size_t len) noexcept
JSW hash (Julienne Walker)
Definition hash-fct.C:274
void init_jsw() noexcept
Initializes the randomized lookup table used by jsw_hash().
Definition hash-fct.C:246
void destroy_tree(Node *root)
Destroys (frees memory) the tree whose root is root.
Standard hash functions for Aleph types.
static mpfr_t y
Definition mpfr_mul_d.c:3
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
auto reservoir_sample(Itor beg, Sent end, size_t k, unsigned long seed=static_cast< unsigned long >(std::chrono::high_resolution_clock::now().time_since_epoch().count()))
Convenience function to sample k elements from a range.
and
Check uniqueness with explicit hash + equality functors.
const unsigned Default_Hash_Seed
Definition hash-fct.C:46
Gsl_Compatible_Rng * rand_gen
Internal RNG handle used by the helper functors in this header.
int num(Node *p)
The number of a node of a built graph.
Random graph generation utilities.
Random network flow graph generation.
Random tree generation.
Reservoir sampling for random selection from data streams.
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
Simple integer point used as node payload by the helper functors in this header.
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
ValueArg< size_t > seed
Definition testHash.C:53
static int * k
Gsl_Compatible_Rng r
Generic graph and digraph implementations.
Network flow graph structures.
Simple graph implementation with adjacency lists.