57#include <gtest/gtest.h>
78 double p_alive, std::uint32_t
seed)
81 std::uniform_real_distribution<double> u(0.0, 1.0);
85 g.set({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)},
86 u(
rng) < p_alive ? 1 : 0);
90std::size_t alive_count(
const Grid &g)
95 if (g.at({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)}) != 0)
100bool grids_equal(
const Grid &a,
const Grid &b)
102 if (a.size(0) != b.size(0) or a.size(1) != b.size(1))
104 for (
ca_size_t i = 0; i < a.size(0); ++i)
105 for (
ca_size_t j = 0; j < a.size(1); ++j)
106 if (a.at({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)})
107 != b.at({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)}))
116void for_random_grids(
F &&body)
118 static const std::uint32_t seeds[] = {
119 0x00000001u, 0xC0FFEEu, 0xDEADBEEFu, 0xFEEDFACEu,
120 0xABCDEF01u, 0x13572468u, 0x9E3779B9u, 0x5BD1E995u};
121 static const ca_size_t sides[] = {12, 16, 24};
122 for (std::uint32_t s : seeds)
143TEST(CAProperties, BBMConservesParticleCountAcrossRandomGrids)
148 Grid g = make_random_grid(side, side, 0.20,
seed);
149 const auto initial = alive_count(g);
152 for (std::size_t t = 0; t < 60; ++t)
155 ASSERT_EQ(alive_count(eng.frame()), initial)
156 <<
"BBM lost particles (seed=" <<
seed <<
", side=" << side
157 <<
", step=" << t <<
")";
161 EXPECT_GE(grids, 20) <<
"property must sweep enough random grids";
164TEST(CAProperties, TMGasConservesParticleCountAcrossRandomGrids)
168 Grid g = make_random_grid(side, side, 0.20,
seed);
169 const auto initial = alive_count(g);
172 for (std::size_t t = 0; t < 60; ++t)
175 ASSERT_EQ(alive_count(eng.frame()), initial)
176 <<
"TM Gas lost particles (seed=" <<
seed <<
", side=" << side
177 <<
", step=" << t <<
")";
189template <
typename Block_Rule>
190void check_reversible(Block_Rule rule,
const char *label)
195 const ca_size_t s = (side % 2 == 0) ? side : side + 1;
196 Grid initial = make_random_grid(s, s, 0.45,
seed);
197 Grid baseline = initial;
202 const std::size_t
N = 300;
203 ASSERT_NO_THROW(eng.run(
N))
204 << label <<
" forward threw (seed=" <<
seed <<
")";
205 ASSERT_NO_THROW(eng.run_back(
N))
206 << label <<
" backward threw (seed=" <<
seed <<
")";
208 ASSERT_TRUE(grids_equal(eng.frame(), baseline))
209 << label <<
": forward(" <<
N <<
") then backward(" <<
N
210 <<
") must restore the initial state (seed=" <<
seed
211 <<
", side=" << s <<
")";
212 EXPECT_EQ(eng.steps_run(), 0u);
218TEST(CAProperties, MargolusBBMIsReversibleAcrossRandomGrids)
220 check_reversible(
BBM_Rule{},
"BBM");
223TEST(CAProperties, MargolusTMGasIsReversibleAcrossRandomGrids)
228TEST(CAProperties, MargolusCrittersIsReversibleAcrossRandomGrids)
237TEST(CAProperties, SynchronousEngineIsDeterministic)
241 Grid initial = make_random_grid(side, side, 0.35,
seed);
248 for (std::size_t t = 0; t < 40; ++t)
250 ASSERT_TRUE(grids_equal(a.frame(), b.frame()))
251 <<
"non-deterministic frame (seed=" <<
seed <<
", step=" << t <<
")";
255 EXPECT_TRUE(grids_equal(a.frame(), b.frame()));
263TEST(CAProperties, AtSafeIsWellDefinedForAnyCoordinate)
267 Grid g = make_random_grid(side, side, 0.50,
seed);
268 std::mt19937
rng(
seed ^ 0xA5A5A5A5u);
271 std::uniform_int_distribution<int> coord(-4 *
static_cast<int>(side),
272 4 *
static_cast<int>(side));
273 for (
int k = 0;
k < 200; ++
k)
277 const int v = g.at_safe({
r, c});
280 ASSERT_TRUE(v == 0 or v == 1)
281 <<
"at_safe returned out-of-domain value " << v
282 <<
" at (" <<
r <<
", " << c <<
")";
285 const int folded = g.at({wrap(
r, g.size(0)), wrap(c, g.size(1))});
287 <<
"at_safe disagrees with toroidal wrap at (" <<
r <<
", " << c
Common typedefs and tag types for the Cellular Automata module.
Update-scheme aware engine.
Fredkin–Toffoli Billiard Ball Machine block rule.
Critters reversible CA block rule.
Lattice that adds boundary-aware access on top of a storage.
Moore (Chebyshev) neighborhood of radius R in N dimensions.
Placeholder neighbourhood for engines that drive block rules.
Synchronous double-buffered engine.
void step()
Apply the rule to every cell once and swap buffers.
Toffoli–Margolus (TM) lattice-gas block rule.
Number Theoretic Transform over Z / MOD Z.
constexpr Game_Of_Life_Rule make_game_of_life_rule() noexcept
Build the canonical Game of Life rule.
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Main namespace for Aleph-w library functions.
Margolus 2×2 partition update for reversible CAs.
The lattice wraps around on every axis.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Phase 13 update-scheme aware engine.
Block-based local rules for Aleph::CA (Phase 13).
Synchronous double-buffered engine for cellular automata.
Cellular automata lattice with pluggable boundary policies.
Neighborhoods catalogue for Aleph::CA.
Rule mechanisms for Aleph::CA.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).
Phase 13 update-scheme strategies for Aleph::CA.