56#include <gtest/gtest.h>
77 double p_alive, std::uint32_t
seed)
80 std::uniform_real_distribution<double> u(0.0, 1.0);
84 g.set({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)},
85 u(
rng) < p_alive ? 1 : 0);
89bool grids_equal(
const Grid &a,
const Grid &b)
91 if (a.size(0) != b.size(0) or a.size(1) != b.size(1))
95 if (a.at({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)})
96 != b.at({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)}))
101std::size_t alive_count(
const Grid &g)
104 for (
ca_size_t i = 0; i < g.size(0); ++i)
105 for (
ca_size_t j = 0; j < g.size(1); ++j)
106 if (g.at({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)}) != 0)
117TEST(CAUpdateScheme, SynchronousUpdateMatchesSynchronousEngineOnGameOfLife)
121 Grid initial = make_random_grid(24, 24, 0.35, 0xC0FFEEu);
131 for (std::size_t t = 0; t < 30; ++t)
133 ASSERT_TRUE(grids_equal(legacy.frame(), schemed.frame()))
134 <<
"Mismatch at step " << t;
138 EXPECT_TRUE(grids_equal(legacy.frame(), schemed.frame()))
139 <<
"Mismatch after 30 steps";
142TEST(CAUpdateScheme, SynchronousUpdateMatchesSynchronousEngineOnWolfram90)
148 const auto offsets = std::array<Offset_Vec<1>, 2>{{{-1}, {1}}};
150 Row initial({33}, 0);
151 initial.set({16}, 1);
156 initial, rule, Wolfram_Nh(offsets));
159 schemed(std::move(initial), rule, Wolfram_Nh(offsets),
162 for (std::size_t t = 0; t < 32; ++t)
165 ASSERT_EQ(legacy.
frame().
at({static_cast<ca_index_t>(i)}),
166 schemed.frame().at({static_cast<ca_index_t>(i)}))
167 <<
"step " << t <<
", cell " << i;
189 template <
typename State>
192 constexpr State threshold = 4;
194 if (result >= threshold)
196 for (
const auto &n : nh)
205TEST(CAUpdateScheme, SequentialUpdateRelaxesSandpile)
209 for (
int k = 0;
k < 50; ++
k)
211 const auto cur = g.at({6, 6});
212 g.set({6, 6}, cur + 1);
225 for (
ca_size_t i = 0; i < g.size(0); ++i)
226 for (
ca_size_t j = 0; j < g.size(1); ++j)
227 EXPECT_LT(g.at({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)}), 4)
228 <<
"cell (" << i <<
", " << j <<
") still over threshold";
231TEST(CAUpdateScheme, SequentialUpdateUsesSingleBuffer)
243TEST(CAUpdateScheme, RandomAsynchronousUpdateIsReproducible)
245 Grid initial = make_random_grid(24, 24, 0.30, 0xBEEFu);
258 EXPECT_TRUE(grids_equal(a.frame(), b.frame()))
259 <<
"Same master seed must produce identical frames";
262TEST(CAUpdateScheme, RandomAsynchronousUpdateChangesFewCellsPerStep)
265 Grid initial = make_random_grid(20, 20, 0.30, 0xBADu);
272 Grid before = initial;
274 std::size_t
diff = 0;
275 for (
ca_size_t i = 0; i < before.size(0); ++i)
276 for (
ca_size_t j = 0; j < before.size(1); ++j)
277 if (before.at({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)})
278 != eng.
frame().
at({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)}))
281 <<
"5 sub-steps must touch at most 5 cells, got " <<
diff;
288TEST(CAUpdateScheme, BlockSynchronousUpdateOnlyTouchesOneBlockPerStep)
290 Grid initial = make_random_grid(8, 8, 0.50, 0x1234u);
291 Grid before = initial;
299 std::size_t changed_blocks = 0;
303 bool touched =
false;
307 const auto r =
static_cast<ca_index_t>(bi * 4 + i);
308 const auto c =
static_cast<ca_index_t>(bj * 4 + j);
309 if (before.at({
r, c}) != eng.frame().at({
r, c}))
315 EXPECT_LE(changed_blocks, 1u);
325template <
typename Block_Rule>
326void run_reversibility_test(Block_Rule rule, std::size_t
N,
330 Grid initial = make_random_grid(side, side, 0.45,
seed);
331 Grid baseline = initial;
338 ASSERT_NO_THROW(eng.run(
N));
341 ASSERT_NO_THROW(eng.run_back(
N));
343 ASSERT_TRUE(grids_equal(eng.frame(), baseline))
344 << label <<
": forward(" <<
N <<
") then backward(" <<
N
345 <<
") must restore the initial state bit-by-bit";
346 EXPECT_EQ(eng.steps_run(), 0u);
351TEST(CAUpdateScheme, MargolusCrittersIsReversibleFor1000Steps)
353 run_reversibility_test(
Critters_Rule{}, 1000u, 32, 0xDEADBEEFu,
357TEST(CAUpdateScheme, MargolusBBMIsReversibleFor1000Steps)
359 run_reversibility_test(
BBM_Rule{}, 1000u, 32, 0xFEEDFACEu,
"BBM");
362TEST(CAUpdateScheme, MargolusTMGasIsReversibleFor1000Steps)
364 run_reversibility_test(
TM_Gas_Rule{}, 1000u, 32, 0xABCDEF01u,
"TM Gas");
367TEST(CAUpdateScheme, MargolusCrittersConservesPopulationParity)
372 Grid g = make_random_grid(16, 16, 0.40, 0xC0DE);
375 for (std::size_t t = 0; t < 50; ++t)
381 const int v = eng.frame().at({
static_cast<ca_index_t>(i),
383 ASSERT_TRUE(v == 0 or v == 1)
384 <<
"non-binary cell after step " << t;
389TEST(CAUpdateScheme, MargolusBBMConservesParticleCount)
392 Grid g = make_random_grid(16, 16, 0.20, 0xB1B2u);
393 const auto initial_particles = alive_count(g);
396 for (std::size_t t = 0; t < 100; ++t)
399 EXPECT_EQ(alive_count(eng.frame()), initial_particles)
400 <<
"BBM lost particles at step " << t;
404TEST(CAUpdateScheme, MargolusTMGasConservesParticleCount)
406 Grid g = make_random_grid(16, 16, 0.20, 0xEFEFu);
407 const auto initial_particles = alive_count(g);
410 for (std::size_t t = 0; t < 100; ++t)
413 EXPECT_EQ(alive_count(eng.frame()), initial_particles)
414 <<
"TM Gas lost particles at step " << t;
422TEST(CAUpdateScheme, CellRuleAsBlockRuleAppliesCellRuleToEachCorner)
427 int operator()(
int s)
const noexcept {
return s + 1; }
432 EXPECT_EQ(
out[0], 1);
433 EXPECT_EQ(
out[1], 2);
434 EXPECT_EQ(
out[2], 3);
435 EXPECT_EQ(
out[3], 4);
442TEST(CAUpdateScheme, AsyncEnginePreAndPostHooksFire)
444 Grid initial = make_random_grid(6, 6, 0.50, 0x1u);
449 std::vector<std::size_t> pre, post;
450 eng.on_pre_step([&](std::size_t s,
const Grid &) { pre.push_back(s); });
451 eng.on_post_step([&](std::size_t s,
const Grid &) { post.push_back(s); });
454 EXPECT_EQ(pre, (std::vector<std::size_t>{0, 1, 2}));
455 EXPECT_EQ(post, (std::vector<std::size_t>{1, 2, 3}));
size_t size_t int32_t * out
Common typedefs and tag types for the Cellular Automata module.
Update-scheme aware engine.
const Lattice & frame() const noexcept
Fredkin–Toffoli Billiard Ball Machine block rule.
Adapt a regular cell rule (with empty neighborhood) to a block rule.
Critters reversible CA block rule.
User-supplied list of offsets for arbitrary connectivity.
Lattice that adds boundary-aware access on top of a storage.
state_type at(const coord_type &c) const
Strict access: throws if c is out of range.
Precomputed transition table for (self, neighbours...).
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.
const Lattice & frame() const noexcept
Return the current frame.
Toffoli–Margolus (TM) lattice-gas block rule.
Von Neumann (L1) neighborhood of radius R in N dimensions.
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::span< const T > Neighbor_View
Read-only view over a contiguous range of neighbour values.
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.
std::array< State, 4 > Block_2x2
Fixed-size 2×2 block of cell values used by Margolus rules.
constexpr Lookup_Rule< 2, 2 > make_wolfram_elementary_rule(std::uint8_t rule_no) noexcept
Build the elementary 1D Wolfram rule rule_no (0..255) as a Lookup_Rule<2, 2> over neighbourhood {-1,...
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
and
Check uniqueness with explicit hash + equality functors.
bool diff(const C1 &c1, const C2 &c2, Eq e=Eq())
Check if two containers differ.
Synchronous update over rotating sub-blocks.
Margolus 2×2 partition update for reversible CAs.
Pick one cell at random per sub-step, update it in place.
In-place sequential update (no double buffer).
Classical double-buffer synchronous update.
The lattice wraps around on every axis.
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.