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
55
using namespace
Aleph
;
56
using namespace
Aleph::CA
;
57
58
TEST
(
CARng
,
SplitMix64IsDistinctForSmallInputs
)
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
68
TEST
(
CARng
,
CellSeedIsSensitiveToCoord
)
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
81
TEST
(
CARng
,
CellSeedIsSensitiveToStep
)
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
88
TEST
(
CARng
,
CellSeedIsSensitiveToMasterSeed
)
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
95
TEST
(
CARng
,
CellSeedIsDeterministic
)
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
104
TEST
(
CARng
,
PerThreadRngForCellIsReproducible
)
105
{
106
Per_Thread_RNG<std::mt19937_64>
rng
(0xDEADBEEFul);
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
114
TEST
(
CARng
,
PerThreadRngDifferentForDifferentCells
)
115
{
116
Per_Thread_RNG<std::mt19937_64>
rng
(0x1234ul);
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
125
TEST
(
CARng
,
PerThreadRngContextOverloadMatches
)
126
{
127
Per_Thread_RNG<std::mt19937_64>
rng
(0x99u);
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
136
TEST
(
CARng
,
PerThreadRngForThreadStepDistinctSubstreams
)
137
{
138
Per_Thread_RNG<std::mt19937_64>
rng
(0xABABul);
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
147
TEST
(
CARng
,
UniformUnitInsideHalfOpenInterval
)
148
{
149
Per_Thread_RNG<std::mt19937_64>
rng
(0x42u);
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
159
TEST
(
CARng
,
UniformIntInsideRange
)
160
{
161
Per_Thread_RNG<std::mt19937_64>
rng
(0x42u);
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
171
TEST
(
CARng
,
UniformIntDegenerateRangeReturnsLow
)
172
{
173
Per_Thread_RNG<std::mt19937_64>
rng
(0x1u);
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
180
TEST
(
CARng
,
CellKeyFromCoordIsCoordSensitive
)
181
{
182
EXPECT_NE
(
cell_key_from_coord<2>
({0, 0}),
cell_key_from_coord<2>
({0, 1}));
183
EXPECT_NE
(
cell_key_from_coord<2>
({0, 0}),
cell_key_from_coord<2>
({1, 0}));
184
EXPECT_NE
(
cell_key_from_coord<3>
({1, 2, 3}),
185
cell_key_from_coord<3>
({3, 2, 1}));
186
}
ca-rng.H
Reproducible random-number support for stochastic CA rules (Phase 8).
ca-traits.H
Common typedefs and tag types for the Cellular Automata module.
Aleph::CA::Per_Thread_RNG
Master seed dispenser for stochastic CA rules.
Definition
ca-rng.H:274
TEST
#define TEST(name)
Definition
disjoint_sparse_table_test.cc:95
rng
static mt19937 rng
Definition
disjoint_sparse_table_test.cc:148
Aleph::blossom_maximum_cardinality_matching
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
Aleph::CA
Definition
bench_support.H:66
Aleph::CA::Coord_Vec
std::array< ca_index_t, N > Coord_Vec
Default coordinate vector.
Definition
ca-traits.H:69
Aleph::CA::uniform_unit
double uniform_unit(Engine &eng)
Map a 64-bit RNG output to a uniform value in [0, 1).
Definition
ca-rng.H:206
Aleph::CA::uniform_int
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
Aleph::CA::splitmix64
constexpr std::uint64_t splitmix64(std::uint64_t x) noexcept
64-bit SplitMix hash.
Definition
ca-rng.H:82
Aleph
Main namespace for Aleph-w library functions.
Definition
ah-arena.H:89
Aleph::CA::Cell_Context
Per-cell context handed to rules that need to know "where" and "when" they are firing.
Definition
ca-traits.H:106
Tests
ca_rng_test.cc
Generated by
1.9.8