53#include <gtest/gtest.h>
63#if defined(_MSC_VER) && !defined(__clang__)
64# define TEST_SKIP_LIST 0
66# define TEST_SKIP_LIST 1
84#if defined(__SANITIZE_ADDRESS__) || defined(__SANITIZE_THREAD__)
85# define TEST_UNDER_A_SANITIZER 1
86#elif defined(__has_feature)
87# if __has_feature(address_sanitizer) || __has_feature(thread_sanitizer) || \
88 __has_feature(memory_sanitizer)
89# define TEST_UNDER_A_SANITIZER 1
92#ifndef TEST_UNDER_A_SANITIZER
93# define TEST_UNDER_A_SANITIZER 0
96#define TEST_SEES_THE_HEAP 0
97#if defined(__GLIBC__) && !TEST_UNDER_A_SANITIZER
98# if __GLIBC_PREREQ(2, 33)
100# undef TEST_SEES_THE_HEAP
101# define TEST_SEES_THE_HEAP 1
108# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
109#elif defined(_MSC_VER)
110# pragma warning(disable: 4996)
113using namespace Aleph;
120 return std::numeric_limits<int>::max();
127 std::vector<int>
keys(
const int n)
129 std::vector<int>
k(n);
130 for (
int i = 0; i < n; ++i)
131 k[i] = (i * 37 + 11) % n;
135 void add(std::string &
out,
const char * fmt,
unsigned long a)
138 std::snprintf(buf,
sizeof buf, fmt, a);
142 template <
bool Prio,
class Node>
143 void pre(
Node * p, std::string &
out)
145 if (p == Node::NullPtr)
147 add(
out,
" %lu",
static_cast<unsigned long>(
KEY(p)));
149 add(
out,
"/%lu",
static_cast<unsigned long>(
PRIO(p)));
159 std::snprintf(buf,
sizeof buf,
" [%d,%d]/%lu",
KEY(p).
low,
KEY(p).high,
160 static_cast<unsigned long>(
PRIO(p)));
166 std::uint64_t fnv1a(
const std::string & s)
168 std::uint64_t
h = 0xcbf29ce484222325ULL;
169 for (
const unsigned char c : s)
172 h *= 0x100000001b3ULL;
178 std::vector<std::string> lines_for(
const unsigned long s)
180 std::vector<std::string> lines;
181 std::string
out =
"== seed ";
183 lines.push_back(
out);
187 for (
int k :
keys(40))
190 pre<true>(t.getRoot(),
out);
191 add(
out,
" next %lu", t.rng().get());
192 lines.push_back(
out);
197 for (
int k :
keys(40))
200 pre<true>(t.getRoot(),
out);
201 lines.push_back(
out);
206 for (
int k :
keys(60))
208 for (
int k = 0;
k < 60;
k += 7)
211 pre<false>(t.getRoot(),
out);
212 add(
out,
" next %lu", t.rng().get());
213 lines.push_back(
out);
220 for (
int i = 0; i < 64; ++i)
221 add(
out,
" %lu",
static_cast<unsigned long>(
l.generateRandomLevel()));
223 for (
int i = 0; i < 64; ++i)
224 add(
out,
" %lu",
static_cast<unsigned long>(
l.generateRandomLevel()));
228 lines.push_back(
out);
232 for (
int k :
keys(200))
234 out =
"dynskiplist: next ";
237 for (
int k = 200;
k < 260; ++
k)
240 lines.push_back(
out);
245 for (
int k = 0;
k < 50; ++
k)
248 for (
int k = 0;
k < 20; ++
k)
249 add(
out,
" %lu",
static_cast<unsigned long>(q.
get()));
250 for (
int k = 50;
k < 60; ++
k)
253 add(
out,
" %lu",
static_cast<unsigned long>(q.
get()));
255 lines.push_back(
out);
259 for (
int k :
keys(30))
261 out =
"interval_tree:";
262 interval_pre(t.getRoot(),
out);
263 add(
out,
" next %lu", t.rng().get());
264 lines.push_back(
out);
271TEST(RandomStructures, WhatTheyMadeWithGsl)
274 static const std::uint64_t golden[] = {
277 0xbf6aeb12f149fed4ULL, 0xaf0b5ce48c61a393ULL, 0x1d6615e62a03143dULL,
278 0x6a2166086b45a98cULL, 0x6c63d3b271929409ULL, 0xd12483814d06e78eULL,
279 0xb24574a6225d43afULL, 0x7cf954f63b5b566cULL,
282 0xbf6aec12f14a0087ULL, 0x9278d4c463049723ULL, 0x4a4d7bcbd6401c76ULL,
283 0xc799efa20f1ddcc9ULL, 0x7d0232dce7b87affULL, 0xe9fc8e3d9141e44bULL,
284 0x348d2efb0da28b7dULL, 0xe155284b0cd406abULL,
287 0x118e2e24d31a4c4fULL, 0x9371ee5b6011fe82ULL, 0x023bbd9db1128c32ULL,
288 0x73f6f6e38ba2c668ULL, 0x99c9ec1e34beceadULL, 0x9a7271d69b2bb3f2ULL,
289 0xc2282e94fa57e861ULL, 0x279881ccaeee8bb8ULL,
292 0x0d24555385f3f048ULL, 0x4f9361527a9e7f0bULL, 0xd30abfc5d0c391a5ULL,
293 0xbd37d372779f3bccULL, 0x799d90ae07e107c6ULL, 0xc197f84291991cd4ULL,
294 0xb18163959293eb53ULL, 0x25c23e87534c6092ULL,
297 0x0d24565385f3f1fbULL, 0xaf0b5ce48c61a393ULL, 0x1d6615e62a03143dULL,
298 0x6a2166086b45a98cULL, 0x653c1a017fdf92a8ULL, 0x098538cb93321526ULL,
299 0x57f5924f1a57c31bULL, 0x7cf954f63b5b566cULL,
302 0x042cb55380c9916dULL, 0x521310421689f20dULL, 0x4f50ad59636c17e3ULL,
303 0xdcef84f3309fdf82ULL, 0xfe21fd5f50f37f28ULL, 0xbe5611ca7fbc90b9ULL,
304 0xf18df4da3be551c4ULL, 0x491cadffd8e29cefULL,
309 const unsigned long long seeds[] = {0, 1, 12345, 0xfffffffeULL, 0xffffffffULL,
311 const size_t lines_per_seed = 8;
313 for (
const unsigned long long s : seeds)
315 if (s > std::numeric_limits<unsigned long>::max())
321 const std::vector<std::string> lines = lines_for(
static_cast<unsigned long>(s));
322 ASSERT_EQ(lines.size(), lines_per_seed);
323 for (
const std::string & line : lines)
325 ASSERT_LT(i, std::size(golden));
326 if (not line.empty())
327 EXPECT_EQ(fnv1a(line), golden[i]) << line;
331 EXPECT_EQ(i, std::size(golden));
335TEST(RandomStructures, TheTreapPrioritiesAreTheNumbersOfGsl)
339 for (
const unsigned long s : {0UL, 7UL, 0xfffffffeUL, 0xffffffffUL})
341 gsl_rng *
r = gsl_rng_alloc(gsl_rng_mt19937);
342 gsl_rng_set(
r,
static_cast<unsigned int>(s) % gsl_rng_max(
r));
344 std::vector<Treap<int>::Node *>
nodes;
345 for (
int k :
keys(100))
348 t.insert(
nodes.back());
350 for (
auto * p :
nodes)
351 EXPECT_EQ(
PRIO(p), gsl_rng_get(
r)) <<
"seed " << s <<
", key " <<
KEY(p);
357# if defined(__GNUC__)
358# pragma GCC diagnostic push
359# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
360# elif defined(_MSC_VER)
361# pragma warning(push)
362# pragma warning(disable: 4996)
370 void check_adapter(S & s, gsl_rng *
r)
372 ASSERT_NE(
r,
nullptr);
373 EXPECT_EQ(gsl_rng_min(
r), 0UL);
374 EXPECT_EQ(gsl_rng_max(
r), 0xffffffffUL);
378 for (
int i = 0; i < 100; ++i)
380 EXPECT_EQ(gsl_rng_uniform_int(
r, 1000), twin.uniform_int(1000));
381 EXPECT_EQ(gsl_rng_get(
r), twin.get());
382 EXPECT_EQ(gsl_rng_uniform(
r), twin.uniform());
384 EXPECT_EQ(s.rng(), twin);
388TEST(RandomStructures, TheDeprecatedGslRngActsOnTheGenerator)
391 check_adapter(treap, treap.gsl_rng_object());
392 EXPECT_EQ(treap.gsl_rng_object(), treap.gsl_rng_object());
395 check_adapter(rand_tree, rand_tree.gsl_rng_object());
399 check_adapter(skip_list, skip_list.gsl_rng_object());
403 check_adapter(dyn_skip_list, dyn_skip_list.gsl_rng_object());
406 check_adapter(random_set, random_set.get_rng());
409 check_adapter(interval_tree, interval_tree.gsl_rng_object());
412TEST(RandomStructures, TheDeprecatedGslRngFollowsItsGenerator)
417 gsl_rng * ra = a.gsl_rng_object();
421 EXPECT_EQ(gsl_rng_get(ra), twin.
get());
422 EXPECT_EQ(b.rng(), twin);
425TEST(RandomStructures, TheDeprecatedGslRngCopiesSavesAndRestoresTheGenerator)
430 gsl_rng *
r = t.gsl_rng_object();
432 EXPECT_EQ(gsl_rng_state(
r),
static_cast<void *
>(&t.rng()));
435 gsl_rng * c = gsl_rng_clone(
r);
436 ASSERT_NE(c,
nullptr);
437 for (
int i = 0; i < 1000; ++i)
438 ASSERT_EQ(gsl_rng_get(c), gsl_rng_get(
r)) <<
"at " << i;
439 (void) gsl_rng_get(c);
446# if !defined(_MSC_VER)
447 std::FILE * f = std::tmpfile();
448 ASSERT_NE(f,
nullptr);
449 ASSERT_EQ(gsl_rng_fwrite(f,
r), 0);
451 std::vector<unsigned long>
next(100);
452 for (
auto & x :
next)
455 ASSERT_EQ(gsl_rng_fread(f,
r), 0);
457 EXPECT_EQ(t.rng(), saved);
458 for (
const auto x :
next)
459 EXPECT_EQ(gsl_rng_get(
r), x);
464 ASSERT_EQ(gsl_rng_memcpy(u.gsl_rng_object(),
r), 0);
465 EXPECT_EQ(u.rng(), t.rng());
468TEST(RandomStructures, TheDeprecatedGslRngIsMadeOnceFromSeveralThreads)
472 for (
int round = 0; round < 50; ++round)
475 std::vector<gsl_rng *> got(4,
nullptr);
476 std::vector<std::thread> threads;
477 for (
size_t k = 0;
k < got.size(); ++
k)
478 threads.emplace_back([&t, &got,
k] { got[k] = t.gsl_rng_object(); });
479 for (
auto & th : threads)
481 for (gsl_rng * p : got)
482 ASSERT_EQ(p, got[0]) <<
"round " << round;
483 EXPECT_EQ(gsl_rng_state(got[0]),
static_cast<void *
>(&t.rng()));
487# if defined(__GNUC__)
488# pragma GCC diagnostic pop
489# elif defined(_MSC_VER)
497TEST(RandomStructures, TheTreapPrioritiesAreTheNumbersOfGsl)
499 GTEST_SKIP() <<
"without GSL (ALEPH_WITH_GSL is 0)";
502TEST(RandomStructures, TheDeprecatedGslRngActsOnTheGenerator)
504 GTEST_SKIP() <<
"without GSL (ALEPH_WITH_GSL is 0)";
507TEST(RandomStructures, TheDeprecatedGslRngFollowsItsGenerator)
509 GTEST_SKIP() <<
"without GSL (ALEPH_WITH_GSL is 0)";
512TEST(RandomStructures, TheDeprecatedGslRngCopiesSavesAndRestoresTheGenerator)
514 GTEST_SKIP() <<
"without GSL (ALEPH_WITH_GSL is 0)";
517TEST(RandomStructures, TheDeprecatedGslRngIsMadeOnceFromSeveralThreads)
519 GTEST_SKIP() <<
"without GSL (ALEPH_WITH_GSL is 0)";
523TEST(RandomStructures, MovesAndSwapsKeepTheGenerators)
528 EXPECT_EQ(b.
rng(), twin);
532 EXPECT_EQ(c.
rng(), twin);
546 EXPECT_EQ(f.
rng(), before);
562 template <
class Tree>
566 Tree & t = first ? a : b;
576 template <
class Tree>
577 bool owns_its_root(
Tree & t)
579 const auto self =
reinterpret_cast<std::uintptr_t
>(&t);
580 const auto root =
reinterpret_cast<std::uintptr_t
>(&t.getRoot());
584 template <
class Tree>
587 for (
const bool first : {
true,
false})
591 Tree t = one_of_two<Tree>(first, &n1, twin);
592 EXPECT_TRUE(owns_its_root(t));
593 EXPECT_EQ(t.rng(), twin);
594 EXPECT_EQ(t.
search(1), &n1);
596 EXPECT_EQ(t.
search(2), &n2);
598 Tree u(std::move(t));
599 EXPECT_TRUE(owns_its_root(u));
600 EXPECT_EQ(u.search(1), &n1);
601 EXPECT_EQ(u.search(2), &n2);
602 EXPECT_EQ(t.getRoot(), Tree::Node::NullPtr);
608 EXPECT_EQ(v.getRoot(), Tree::Node::NullPtr);
613TEST(RandomStructures, AMovedTreapTakesItsNodesAndItsGenerator)
617 check_moves<Treap<int>>();
618 check_moves<Treap_Rk<int>>();
621TEST(RandomStructures, TheSeedsAreTakenAsForGsl)
658 unsigned long through_an_unsigned_int(
const unsigned long s)
660 return static_cast<unsigned int>(s) % 0xffffffffUL;
663 unsigned long modulo_the_greatest_number(
const unsigned long s) {
return s % 0xffffffffUL; }
665 unsigned long as_it_is(
const unsigned long s) {
return s; }
672 template <
class S>
struct Randomized;
675 template <
class Tree,
bool Priorities>
678 static void build_with_seed(std::optional<Tree> & tree,
const unsigned long s)
682 static unsigned long seed_of_the_constructor(
const unsigned long s)
684 return through_an_unsigned_int(s);
686 static unsigned long seed_of_set_seed(
const unsigned long s) {
return as_it_is(s); }
688 static bool is_right(
const Tree & tree) {
return tree.verify(); }
689 static std::string shape(
Tree & tree)
692 pre<Priorities>(tree.getRoot(),
out);
696 static constexpr bool has_a_shape =
true;
698 [[maybe_unused]]
static constexpr bool allocates_when_built =
false;
701 template <>
struct Randomized<
Treap<int>> : Of_A_Tree<Treap<int>, true> {};
703 template <>
struct Randomized<
Treap_Rk<int>> : Of_A_Tree<Treap_Rk<int>, true> {};
705 template <>
struct Randomized<
Rand_Tree<int>> : Of_A_Tree<Rand_Tree<int>, false>
707 static void build_with_seed(std::optional<
Rand_Tree<int>> & tree,
const unsigned long s)
709 tree.emplace(
static_cast<unsigned int>(s));
711 static unsigned long seed_of_set_seed(
const unsigned long s)
713 return modulo_the_greatest_number(s);
720 template <>
struct Randomized<
SkipList<int, int>>
724 static void build_with_seed(std::optional<List> & list,
const unsigned long s)
728 static unsigned long seed_of_the_constructor(
const unsigned long s)
730 return modulo_the_greatest_number(s);
732 static unsigned long seed_of_set_seed(
const unsigned long s) {
return as_it_is(s); }
733 static void put(
List & list,
int) { (void) list.generateRandomLevel(); }
734 static bool is_right(
const List & list) {
return list.checkSkipList(); }
735 static std::string shape(
List &) {
return ""; }
736 static void empty(
List &) {}
737 static constexpr bool has_a_shape =
false;
739 [[maybe_unused]]
static constexpr bool allocates_when_built =
false;
747 static void build_with_seed(std::optional<List> & list,
const unsigned long s)
751 static unsigned long seed_of_the_constructor(
const unsigned long s)
753 return modulo_the_greatest_number(s);
755 static unsigned long seed_of_set_seed(
const unsigned long s) {
return as_it_is(s); }
756 static void put(
List & list,
const int k) { (void) list.
insert(
k); }
757 static bool is_right(
const List & list)
760 long last = std::numeric_limits<long>::min();
761 for (
auto it = list.begin(); it.has_curr(); it.next())
763 if (it.get_curr() <= last)
765 last = it.get_curr();
768 return count == list.size();
770 static std::string shape(
List &) {
return ""; }
771 static void empty(
List & list) { list.empty(); }
772 static constexpr bool has_a_shape =
false;
774 [[maybe_unused]]
static constexpr bool allocates_when_built =
true;
777 template <>
struct Randomized<
Random_Set<int>>
782 static void build_with_seed(std::optional<Set> & set,
const unsigned long s)
787 static unsigned long seed_of_the_constructor(
const unsigned long s) {
return as_it_is(s); }
788 static unsigned long seed_of_set_seed(
const unsigned long s) {
return as_it_is(s); }
789 static void put(Set & set,
const int k) { (void) set.append(
k); }
790 static bool is_right(
const Set &) {
return true; }
791 static std::string shape(Set & set)
794 set.for_each([&
out] (
const int k) {
add(
out,
" %lu",
static_cast<unsigned long>(
k)); });
797 static void empty(Set &) {}
798 static constexpr bool has_a_shape =
true;
800 [[maybe_unused]]
static constexpr bool allocates_when_built =
true;
807 static void build_with_seed(std::optional<Tree> & tree,
const unsigned long s)
811 static unsigned long seed_of_the_constructor(
const unsigned long s)
813 return through_an_unsigned_int(s);
815 static unsigned long seed_of_set_seed(
const unsigned long s) {
return as_it_is(s); }
816 static void put(
Tree & tree,
const int k)
820 static bool is_right(
const Tree & tree) {
return tree.verify(); }
821 static std::string shape(
Tree & tree)
824 interval_pre(tree.getRoot(),
out);
828 static constexpr bool has_a_shape =
true;
830 [[maybe_unused]]
static constexpr bool allocates_when_built =
false;
833 template <
class S>
class Randomized_Structure :
public ::testing::Test {};
848 void fill(S & s,
const int first,
const int n)
850 for (
int i = 0; i < n; ++i)
851 Randomized<S>::put(s, first + (i * 37 + 11) % n);
855 enum class Given { nothing, a_seed_for_splitmix64, a_seed_for_the_mersenne_twister };
857 constexpr Given every_gift[] = {Given::nothing, Given::a_seed_for_splitmix64,
858 Given::a_seed_for_the_mersenne_twister};
861 void give(S & s,
const Given what,
const unsigned long seed)
863 if (what == Given::a_seed_for_splitmix64)
864 s.random_source().seed(
seed);
865 else if (what == Given::a_seed_for_the_mersenne_twister)
869 Engine engine_of(
const Given what)
871 return what == Given::a_seed_for_the_mersenne_twister ? Engine::Mersenne_Twister
872 : Engine::SplitMix64;
875#if TEST_SEES_THE_HEAP
879 const struct mallinfo2
m = mallinfo2();
880 return m.uordblks +
m.hblkhd;
885TYPED_TEST(Randomized_Structure, BuiltWithoutASeedItDrawsFromASplitMix64OfItsOwn)
887 using R = Randomized<TypeParam>;
889 EXPECT_EQ(a.random_source().engine(), Engine::SplitMix64);
890 EXPECT_EQ(b.random_source().engine(), Engine::SplitMix64);
891 EXPECT_FALSE(a.random_source() == b.random_source());
896 EXPECT_EQ(a.random_source().engine(), Engine::SplitMix64);
897 EXPECT_EQ(b.random_source().engine(), Engine::SplitMix64);
898 EXPECT_FALSE(a.random_source() == b.random_source());
899 EXPECT_TRUE(R::is_right(a));
900 EXPECT_TRUE(R::is_right(b));
902 EXPECT_NE(R::shape(a), R::shape(b));
907 const TypeParam & c = a;
908 static_assert(std::is_same_v<
decltype(c.random_source()),
const Random_Source &>);
909 static_assert(std::is_same_v<
decltype(a.random_source()),
Random_Source &>);
910 EXPECT_EQ(c.random_source().engine(), Engine::SplitMix64);
913TYPED_TEST(Randomized_Structure, WithASeedItDrawsFromTheMersenneTwisterOfThatSeed)
915 using R = Randomized<TypeParam>;
919 for (
const unsigned long long seed : {0ULL, 1ULL, 12345ULL, 0xfffffffeULL, 0xffffffffULL,
922 if (
seed > std::numeric_limits<unsigned long>::max())
924 const auto s =
static_cast<unsigned long>(
seed);
926 std::optional<TypeParam> built;
927 R::build_with_seed(built, s);
928 EXPECT_EQ(built->random_source().engine(), Engine::Mersenne_Twister) <<
"seed " << s;
929 EXPECT_EQ(built->random_source(),
930 Random_Source(Engine::Mersenne_Twister, R::seed_of_the_constructor(s)))
935 TypeParam seeded_later;
936 seeded_later.set_seed(s);
937 EXPECT_EQ(seeded_later.random_source(),
expected) <<
"seed " << s;
943 EXPECT_EQ(built->random_source(),
expected) <<
"seed " << s;
944 EXPECT_EQ(&built->random_source().mersenne_twister(), mt) <<
"seed " << s;
950 static_assert(not
noexcept(s.set_seed(1UL)));
951 static_assert(not
noexcept(s.rng()));
952 static_assert(
noexcept(s.random_source()));
955TYPED_TEST(Randomized_Structure, WithTheSameSeedTwoMakeTheSameWithEachEngine)
957 using R = Randomized<TypeParam>;
958 for (
const Engine
engine : {Engine::SplitMix64, Engine::Mersenne_Twister})
961 a.random_source().seed(
engine, 42);
962 b.random_source().seed(
engine, 42);
963 c.random_source().seed(
engine, 43);
967 EXPECT_EQ(a.random_source().engine(),
engine);
968 EXPECT_EQ(a.random_source(), b.random_source());
969 EXPECT_FALSE(a.random_source() == c.random_source());
973 EXPECT_EQ(R::shape(a), R::shape(b));
974 EXPECT_NE(R::shape(a), R::shape(c));
976 EXPECT_TRUE(R::is_right(a));
977 EXPECT_TRUE(R::is_right(c));
984TYPED_TEST(Randomized_Structure, TheDeprecatedRngMakesTheMersenneTwisterOfOneWithoutASeed)
986 using R = Randomized<TypeParam>;
990 a.random_source().seed(7);
993 EXPECT_EQ(a.random_source().engine(), Engine::Mersenne_Twister);
995 EXPECT_EQ(&mt, &a.random_source().mersenne_twister());
996 EXPECT_EQ(&a.rng(), &mt);
1005 EXPECT_EQ(a.random_source(), b.random_source());
1007 EXPECT_EQ(R::shape(a), R::shape(b));
1012 if constexpr (
requires (
const TypeParam & c) { c.rng(); })
1016 EXPECT_EQ(c.random_source().engine(), Engine::Mersenne_Twister);
1020TYPED_TEST(Randomized_Structure, TheDeprecatedGslRngMakesTheMersenneTwisterOfOneWithoutASeed)
1024 gsl_rng *
r =
nullptr;
1025 if constexpr (
requires { a.gsl_rng_object(); })
1026 r = a.gsl_rng_object();
1027 else if constexpr (
requires { a.get_rng(); })
1036 ASSERT_NE(
r,
nullptr);
1037 EXPECT_EQ(a.random_source().engine(), Engine::Mersenne_Twister);
1038 EXPECT_EQ(gsl_rng_state(
r),
static_cast<void *
>(&a.random_source().mersenne_twister()));
1040 EXPECT_EQ(a.random_source(),
Random_Source(Engine::Mersenne_Twister, 99));
1042 GTEST_SKIP() <<
"without GSL (ALEPH_WITH_GSL is 0)";
1046TYPED_TEST(Randomized_Structure, ACopyDrawsFromASplitMix64OfItsOwn)
1048 using R = Randomized<TypeParam>;
1050 const bool copied = std::is_same_v<TypeParam, DynSkipList<int>>
1051 or std::is_same_v<TypeParam, Random_Set<int>>;
1052 EXPECT_EQ(std::is_copy_constructible_v<TypeParam>, copied);
1053 EXPECT_EQ(std::is_copy_assignable_v<TypeParam>, copied);
1055 if constexpr (std::is_copy_constructible_v<TypeParam>)
1056 for (
const Given what : every_gift)
1060 give(twin, what, 5);
1067 EXPECT_EQ(c.size(), a.size());
1068 EXPECT_EQ(c.random_source().engine(), Engine::SplitMix64);
1069 EXPECT_FALSE(c.random_source() == a.random_source());
1070 EXPECT_EQ(a.random_source().engine(), engine_of(what));
1071 if (what != Given::nothing)
1072 EXPECT_EQ(a.random_source(), twin.random_source());
1074 EXPECT_TRUE(R::is_right(c));
1078 for (
const Given target : every_gift)
1083 EXPECT_EQ(d.size(), a.size());
1084 EXPECT_EQ(d.random_source().engine(), engine_of(target));
1085 EXPECT_FALSE(d.random_source() == a.random_source());
1086 if (what != Given::nothing)
1087 EXPECT_EQ(a.random_source(), twin.random_source());
1092TYPED_TEST(Randomized_Structure, AMoveTakesTheGeneratorAndTheOneMovedFromDrawsFromAnother)
1094 using R = Randomized<TypeParam>;
1096 const bool moved = not std::is_same_v<TypeParam, Interval_Tree<int>>;
1097 EXPECT_EQ(std::is_move_constructible_v<TypeParam>, moved);
1099 if constexpr (std::is_move_constructible_v<TypeParam>)
1100 for (
const Given what : every_gift)
1104 give(twin, what, 5);
1108 TypeParam b(std::move(a));
1109 EXPECT_EQ(b.random_source().engine(), engine_of(what));
1110 if (what != Given::nothing)
1111 EXPECT_EQ(b.random_source(), twin.random_source());
1114 EXPECT_EQ(a.random_source().engine(), Engine::SplitMix64);
1115 EXPECT_FALSE(a.random_source() == b.random_source());
1116 if (what == Given::a_seed_for_splitmix64)
1122 fill(twin, 2000, 50);
1123 EXPECT_TRUE(R::is_right(a));
1124 EXPECT_TRUE(R::is_right(b));
1125 EXPECT_EQ(a.random_source().engine(), Engine::SplitMix64);
1126 if (what != Given::nothing)
1128 EXPECT_EQ(b.random_source(), twin.random_source());
1130 EXPECT_EQ(R::shape(b), R::shape(twin));
1135 if constexpr (std::is_move_assignable_v<TypeParam>)
1139 EXPECT_EQ(c.random_source().engine(), engine_of(what));
1140 if (what != Given::nothing)
1141 EXPECT_EQ(c.random_source(), twin.random_source());
1142 EXPECT_EQ(b.random_source().engine(), Engine::SplitMix64);
1143 EXPECT_FALSE(b.random_source() == c.random_source());
1145 EXPECT_TRUE(R::is_right(b));
1146 EXPECT_TRUE(R::is_right(c));
1157 using R = Randomized<TypeParam>;
1158 TypeParam a, b, twin;
1161 b.random_source().
seed(6);
1166 EXPECT_EQ(b.random_source().engine(), Engine::Mersenne_Twister);
1167 EXPECT_EQ(b.random_source(), twin.random_source());
1169 EXPECT_TRUE(R::is_right(a));
1170 EXPECT_TRUE(R::is_right(b));
1176#if TEST_SEES_THE_HEAP
1184 long after_drawing = 0;
1185 long with_a_generator = 0;
1187 long with_a_seeded_one = 0;
1188 long at_the_end = 0;
1190 long after_the_move = 0;
1194 Heap_Seen look_at_the_heap()
1197 const auto since = [before = heap_in_use()]
1199 return static_cast<long>(heap_in_use()) -
static_cast<long>(before);
1204 seen.built = since();
1205 for (
int i = 0; i < 100; ++i)
1206 (
void) s->random_source().uniform_int(1000);
1207 seen.after_drawing = since();
1209 seen.with_a_generator = since();
1210 s->random_source().seed(1);
1211 seen.back = since();
1212 std::optional<S> seeded;
1213 Randomized<S>::build_with_seed(seeded, 1);
1214 seen.with_a_seeded_one = since();
1216 seen.at_the_end = since();
1218 if constexpr (std::is_move_constructible_v<S>)
1220 std::optional<S> a, b;
1222 const long with_one = since();
1223 b.emplace(std::move(*a));
1224 seen.of_a_move = since() - with_one;
1226 seen.after_the_move = since();
1232TYPED_TEST(Randomized_Structure, ItHoldsNoMersenneTwisterUntilItIsAskedForOne)
1234#if TEST_SEES_THE_HEAP
1235 using R = Randomized<TypeParam>;
1245 if (std::getenv(
"GLIBC_TUNABLES") !=
nullptr)
1246 GTEST_SKIP() <<
"GLIBC_TUNABLES is set: the cache of malloc() may keep a Mersenne Twister";
1247 (void) look_at_the_heap<TypeParam>();
1248 const Heap_Seen seen = look_at_the_heap<TypeParam>();
1251 if (not R::allocates_when_built)
1252 EXPECT_EQ(seen.built, 0);
1253 EXPECT_LT(seen.built, generator);
1254 EXPECT_EQ(seen.after_drawing, seen.built);
1258 EXPECT_GE(seen.with_a_generator, seen.built + generator);
1259 EXPECT_EQ(seen.back, seen.built);
1262 EXPECT_GE(seen.with_a_seeded_one, 2 * seen.built + generator);
1263 EXPECT_LT(seen.with_a_seeded_one, 2 * seen.built + 2 * generator);
1264 EXPECT_EQ(seen.at_the_end, 0);
1268 if constexpr (std::is_move_constructible_v<TypeParam>)
1269 EXPECT_EQ(seen.of_a_move, seen.built);
1270 EXPECT_EQ(seen.after_the_move, 0);
1272 GTEST_SKIP() <<
"the heap of malloc() is not seen: not glibc, or under a sanitizer";
1276TEST(RandomStructures, ThePrioritiesAreTheNumbersOfTheSource)
1280 for (
const Engine
engine : {Engine::SplitMix64, Engine::Mersenne_Twister})
1298 const auto put = [] (
auto & tree,
auto * node)
1300 const bool taken = tree.insert(node) == node;
1305 for (
const int k :
keys(100))
1308 ASSERT_TRUE(put(treap, p)) <<
"key " <<
k;
1310 ASSERT_TRUE(put(treap_rk, q)) <<
"key " <<
k;
1312 ASSERT_TRUE(put(interval_tree, i)) <<
"key " <<
k;
1313 const unsigned long priority = twin.
get();
1314 ASSERT_EQ(
PRIO(p), priority) <<
"key " <<
k;
1315 ASSERT_EQ(
PRIO(q), priority) <<
"key " <<
k;
1316 ASSERT_EQ(
PRIO(i), priority) <<
"key " <<
k;
1318 EXPECT_TRUE(treap.
verify());
1319 EXPECT_TRUE(treap_rk.
verify());
1320 EXPECT_TRUE(interval_tree.
verify());
1324TEST(RandomStructures, TheTreesWithoutASeedAreBalanced)
1333 for (
const bool mersenne : {
false,
true})
1353 for (
int k = 0;
k < n; ++
k)
1359 const char *
const engine = mersenne ?
"Mersenne Twister" :
"SplitMix64";
1364 mersenne ? Engine::Mersenne_Twister : Engine::SplitMix64);
1371TEST(RandomStructures, TheTreesAreBuiltAndMovedWithoutThrowing)
1376 static_assert(std::is_nothrow_default_constructible_v<Treap<int>>);
1377 static_assert(std::is_nothrow_default_constructible_v<Treap_Vtl<int>>);
1378 static_assert(std::is_nothrow_default_constructible_v<Treap_Rk<int>>);
1379 static_assert(std::is_nothrow_default_constructible_v<Treap_Rk_Vtl<int>>);
1380 static_assert(std::is_nothrow_default_constructible_v<Rand_Tree<int>>);
1381 static_assert(std::is_nothrow_default_constructible_v<Rand_Tree_Vtl<int>>);
1382 static_assert((std::is_nothrow_constructible_v<Treap<int>,
const Aleph::less<int> &>));
1383 static_assert((std::is_nothrow_constructible_v<Treap_Rk<int>,
Aleph::less<int>>));
1384 static_assert((std::is_nothrow_constructible_v<Rand_Tree<int>,
const Aleph::less<int> &>));
1385 static_assert(std::is_nothrow_move_constructible_v<Treap<int>>);
1386 static_assert(std::is_nothrow_move_constructible_v<Treap_Rk<int>>);
1387 static_assert(std::is_nothrow_move_constructible_v<Rand_Tree<int>>);
1389 static_assert(not std::is_nothrow_constructible_v<Treap<int>,
unsigned long>);
1390 static_assert(not std::is_nothrow_constructible_v<Treap_Rk<int>,
unsigned long>);
1391 static_assert(not std::is_nothrow_constructible_v<Rand_Tree<int>,
unsigned int>);
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
RAII guard for exception-safe resource cleanup.
TYPED_TEST_SUITE(ArcHeapLifecycle, ArcHeapKinds)
TYPED_TEST(ArcHeapLifecycle, ClearNullsTheHandles)
WeightedDigraph::Node Node
size_t size_t int32_t * out
void insert(Dlink *node) noexcept
Insert node after this.
Node belonging to a double circular linked list with header node.
T & insert(const T &item)
Dynamic ordered set implemented with a Skip List.
Key * insert(const Key &key)
Insert an element into the set.
Gsl_Compatible_Rng & rng()
The Mersenne Twister of the list, which a list that was given no seed makes here, and draws from it f...
bool verify() const
Verify BST, heap, and max_endpoint invariants.
Gsl_Compatible_Rng & rng() const
The Mersenne Twister of the tree, which a tree that was given no seed makes here, and draws from it f...
Node *& getRoot() noexcept
Return the tree's root.
Random_Source & random_source() noexcept
The source of random numbers, which gives the priorities.
Gsl_Compatible_Rng & rng()
The Mersenne Twister of the tree, which a tree that was given no seed makes here, and draws from it f...
Random_Source & random_source() noexcept
The source of random numbers, which draws the positions of the nodes.
Node *& getRoot() noexcept
Return the tree's root.
void set_seed(const unsigned long seed)
Seeds the Mersenne Twister of the tree, modulo 2^32 - 1: the positions are from then on those of GSL ...
Node * insert(Node *p) noexcept(nothrow_compare_copy_v< Compare, Key >)
Insert a node in the tree.
Node *& getRoot() noexcept
Return the tree's root.
bool verify() const
Return true if the treap is consistent.
bool insert(Node *&root, Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Random_Source & random_source() noexcept
The source of random numbers, which gives the priorities.
void set_seed(const unsigned long seed)
Seeds the Mersenne Twister of the treap, as gsl_rng_set() did: the priorities are from then on those ...
Random_Source & random_source() noexcept
The source of random numbers, which gives the priorities.
Node *& getRoot() noexcept
Return the tree's root.
bool verify() const
Return true if this is a consistent treap.
void set_seed(const unsigned long seed)
Seeds the Mersenne Twister of the treap, as gsl_rng_set() did: the priorities are from then on those ...
Node * insert(Node *root, Node *p) noexcept(nothrow_compare_v< Compare, Key >)
Gsl_Compatible_Rng & rng()
The Mersenne Twister of the treap, which a treap that was given no seed makes here,...
A random number generator with the sequences of GSL's gsl_rng_mt19937.
void seed(const unsigned long s) noexcept
Seeds again, as gsl_rng_set(r, s).
unsigned long get() noexcept
The next number, as operator()().
RAII guard for graph algorithm initialization.
void put(const T &item)
Insert an item by copy.
void set_seed(unsigned long seed) const
Seeds the Mersenne Twister of the set, as gsl_rng_set() did: the positions are from then on those of ...
Gsl_Compatible_Rng & rng() const
The Mersenne Twister of the set, which a set that was given no seed makes here, and draws from it fro...
bool is_empty() const
Return true if the queue is empty.
T get()
Extract randomly an item.
T & append(const T &item)
Insert randomly an item by copy.
void swap(Random_Set &s) noexcept(noexcept(array.swap(s.array)))
Swap in constant time this with s: their items and their generators.
The source of random numbers of a randomized structure, which draws from one of two generators.
Engine
The generators that a source draws from.
unsigned long get() noexcept
The next number, as operator()().
Gsl_Compatible_Rng & mersenne_twister()
The Mersenne Twister of the source, which is its engine from now on.
Engine engine() const noexcept
The generator that the source draws from.
void seed(const std::uint64_t s) noexcept
Draws from now on from the SplitMix64 of a seed.
Skip List - a probabilistic ordered data structure.
Gsl_Compatible_Rng & rng()
The Mersenne Twister of the list, which a list that was given no seed makes here, and draws from it f...
void set_seed(unsigned long seed)
Seeds the Mersenne Twister of the list, as gsl_rng_set() did: the levels are from then on those of GS...
Node * insert(Node *p) noexcept
Insert a node into the skip list.
Minimal std::expected-style result type for C++20.
void emplace(Args &&... args)
Appends a new element into the container by constructing it from the given args.
Node for QuadTree spatial data structure.
QuadTree - Hierarchical spatial index for 2D points.
Point * search(const Point &p) noexcept
Search for a point in the tree.
Point * insert(Node *&r, const Point &p)
Recursive insert helper.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
DynArray< Graph::Node * > nodes
void destroyRec(Node *&root) noexcept
Free all the memory occupied by the tree root
unsigned long & PRIO(Node *p) noexcept
Access the priority of a treap node.
size_t computeHeightRec(Node *root) noexcept
Compute the height of root
Main namespace for Aleph-w library functions.
static long & low(typename GT::Node *p)
Internal helper: low-link value stored directly in NODE_COOKIE(p).
void fill(Itor beg, const Itor &end, const T &value)
Fill a range with a value.
and
Check uniqueness with explicit hash + equality functors.
std::ostream & join(const C &c, const std::string &sep, std::ostream &out)
Join elements of an Aleph-style container into a stream.
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.
double add(double a, double b)
Interval tree using nodes without virtual destructor.
Closed interval [low, high].
Randomized binary search tree.
Extended treap (a special type of randomized binary search tree) which manages selection and splittin...
Treap (a special type of randomized binary search tree) using nodes without virtual destructor.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Dynamic ordered set implemented with a Skip List.
Interval tree: augmented BST for overlap/stabbing queries.
Randomized binary search tree.
Random access queue (bag) with O(1) random pop.
Skip list: probabilistic sorted linked structure.
Treap with rank (order statistics).
Treap: randomized BST combining tree and heap properties.