Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ca_rng_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 <array>
46#include <cstdint>
47#include <random>
48#include <set>
49
50#include <gtest/gtest.h>
51
52#include <ca-rng.H>
53#include <ca-traits.H>
54
55using namespace Aleph;
56using namespace Aleph::CA;
57
59{
60 // 1024 sequential inputs should never collide; the SplitMix64
61 // mixing function is bijective by construction.
62 std::set<std::uint64_t> seen;
63 for (std::uint64_t x = 0; x < 1024; ++x)
64 seen.insert(splitmix64(x));
65 EXPECT_EQ(seen.size(), 1024u);
66}
67
69{
70 Coord_Vec<2> a{0, 0};
71 Coord_Vec<2> b{0, 1};
72 Coord_Vec<2> c{1, 0};
73 EXPECT_NE(cell_seed<2>(0xC0FFEE, /*step=*/3, a),
74 cell_seed<2>(0xC0FFEE, /*step=*/3, b));
75 EXPECT_NE(cell_seed<2>(0xC0FFEE, /*step=*/3, a),
76 cell_seed<2>(0xC0FFEE, /*step=*/3, c));
77 EXPECT_NE(cell_seed<2>(0xC0FFEE, /*step=*/3, b),
78 cell_seed<2>(0xC0FFEE, /*step=*/3, c));
79}
80
82{
83 Coord_Vec<2> p{4, 7};
84 EXPECT_NE(cell_seed<2>(0x12345, 0, p), cell_seed<2>(0x12345, 1, p));
85 EXPECT_NE(cell_seed<2>(0x12345, 1, p), cell_seed<2>(0x12345, 2, p));
86}
87
89{
90 Coord_Vec<3> p{1, 2, 3};
91 EXPECT_NE(cell_seed<3>(0xAAAA, /*step=*/5, p),
92 cell_seed<3>(0xBBBB, /*step=*/5, p));
93}
94
96{
97 Coord_Vec<2> p{17, 33};
98 EXPECT_EQ(cell_seed<2>(42, 9, p), cell_seed<2>(42, 9, p));
99
100 Cell_Context<2> ctx{9, p};
101 EXPECT_EQ(cell_seed<2>(42, ctx), cell_seed<2>(42, 9, p));
102}
103
105{
107 Coord_Vec<2> p{5, 11};
108 auto e1 = rng.for_cell(/*step=*/4, p);
109 auto e2 = rng.for_cell(/*step=*/4, p);
110 for (int i = 0; i < 8; ++i)
111 EXPECT_EQ(e1(), e2());
112}
113
115{
117 auto e1 = rng.for_cell(/*step=*/0, Coord_Vec<2>{0, 0});
118 auto e2 = rng.for_cell(/*step=*/0, Coord_Vec<2>{0, 1});
119 // The probability of two independently seeded mt19937_64 producing
120 // the same first output is ~2^-64; if it happens here the seeds are
121 // colliding which would be a real bug.
122 EXPECT_NE(e1(), e2());
123}
124
126{
128 Cell_Context<2> ctx{7, Coord_Vec<2>{2, 3}};
129
130 auto a = rng.for_cell(ctx);
131 auto b = rng.for_cell(/*step=*/7, Coord_Vec<2>{2, 3});
132 for (int i = 0; i < 4; ++i)
133 EXPECT_EQ(a(), b());
134}
135
137{
139 auto a = rng.for_thread_step(0, 0);
140 auto b = rng.for_thread_step(1, 0);
141 auto c = rng.for_thread_step(0, 1);
142 EXPECT_NE(a(), b());
143 EXPECT_NE(a(), c());
144 EXPECT_NE(b(), c());
145}
146
148{
150 auto eng = rng.for_cell(/*step=*/0, Coord_Vec<1>{0});
151 for (int i = 0; i < 1024; ++i)
152 {
153 const double u = uniform_unit(eng);
154 EXPECT_GE(u, 0.0);
155 EXPECT_LT(u, 1.0);
156 }
157}
158
160{
162 auto eng = rng.for_cell(/*step=*/0, Coord_Vec<1>{0});
163 for (int i = 0; i < 256; ++i)
164 {
165 const std::size_t v = uniform_int(eng, 7, 17);
166 EXPECT_GE(v, 7u);
167 EXPECT_LE(v, 17u);
168 }
169}
170
172{
174 auto eng = rng.for_cell(/*step=*/0, Coord_Vec<1>{0});
175 EXPECT_EQ(uniform_int(eng, 5, 5), 5u);
176 // hi < lo collapses to lo (saturating semantics).
177 EXPECT_EQ(uniform_int(eng, 8, 3), 8u);
178}
179
Reproducible random-number support for stochastic CA rules (Phase 8).
Common typedefs and tag types for the Cellular Automata module.
Master seed dispenser for stochastic CA rules.
Definition ca-rng.H:274
#define TEST(name)
static mt19937 rng
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
std::array< ca_index_t, N > Coord_Vec
Default coordinate vector.
Definition ca-traits.H:69
double uniform_unit(Engine &eng)
Map a 64-bit RNG output to a uniform value in [0, 1).
Definition ca-rng.H:206
std::size_t uniform_int(Engine &eng, const std::size_t lo, const std::size_t hi)
Sample a uniform integer in [lo, hi] (inclusive).
Definition ca-rng.H:231
constexpr std::uint64_t splitmix64(std::uint64_t x) noexcept
64-bit SplitMix hash.
Definition ca-rng.H:82
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Per-cell context handed to rules that need to know "where" and "when" they are firing.
Definition ca-traits.H:106