54#include <gtest/gtest.h>
72static_assert(LatticeLike<Ghost_Lattice<Dense_Cell_Storage<int, 2>,
OpenBoundary, 1>>);
73static_assert(LatticeLike<Ghost_Lattice<Dense_Cell_Storage<int, 2>,
ToroidalBoundary, 1>>);
74static_assert(LatticeLike<Ghost_Lattice<Dense_Cell_Storage<int, 2>,
ReflectiveBoundary, 2>>);
75static_assert(LatticeLike<Ghost_Lattice<Dense_Cell_Storage<int, 2>,
NeumannBoundary, 1>>);
86 template <
typename Boundary, std::
size_t H = 1>
93 g.
set({ i, j },
static_cast<int>(i * 10 + j));
99 template <
typename Boundary>
101 make_3x3_classic_int()
106 lat.
set({ i, j },
static_cast<int>(i * 10 + j));
115TEST(CAGhostLatticeStrictAccess, InRangeMatchesStampedValues)
117 auto g = make_3x3_ghost_int<OpenBoundary>();
120 EXPECT_EQ(g.at({ i, j }), i * 10 + j);
123TEST(CAGhostLatticeStrictAccess, OutOfRangeThrows)
125 auto g = make_3x3_ghost_int<OpenBoundary>();
126 EXPECT_THROW(g.at({ -1, 0 }), std::out_of_range);
127 EXPECT_THROW(g.at({ 3, 0 }), std::out_of_range);
128 EXPECT_THROW(g.set({ -1, 0 }, 1), std::out_of_range);
129 EXPECT_THROW(g.set({ 0, 3 }, 1), std::out_of_range);
132TEST(CAGhostLatticeShape, DimensionSizeExtentsHaloRadius)
135 EXPECT_EQ(g.dimension(), 2u);
136 EXPECT_EQ(g.size(), 4u * 5u);
137 EXPECT_EQ(g.size(0), 4u);
138 EXPECT_EQ(g.size(1), 5u);
139 EXPECT_EQ(g.extents()[0], 4u);
140 EXPECT_EQ(g.extents()[1], 5u);
141 EXPECT_EQ(g.store_extents()[0], 4u + 4u);
142 EXPECT_EQ(g.store_extents()[1], 5u + 4u);
143 EXPECT_EQ(g.halo_radius(), 2u);
146TEST(CAGhostLatticeShape, SizeAxisOutOfRangeThrows)
149 EXPECT_THROW(g.size(2), std::out_of_range);
152TEST(CAGhostLatticeShapeEmptyExtents, OneDimensionalEmptyIsStable)
156 EXPECT_EQ(g.dimension(), 1u);
157 EXPECT_EQ(g.size(), 0u);
158 EXPECT_EQ(g.size(0), 0u);
159 EXPECT_EQ(g.extents()[0], 0u);
160 EXPECT_EQ(g.store_extents()[0], 0u);
161 EXPECT_EQ(g.halo_radius(), 2u);
162 EXPECT_EQ(g.storage().size(), 0u);
164 EXPECT_EQ(g.at_safe({0}), 0);
165 EXPECT_EQ(g.at_safe({-1}), 0);
166 EXPECT_NO_THROW(g.refresh_halo());
167 EXPECT_NO_THROW(g.fill(3));
168 EXPECT_EQ(g.size(), 0u);
169 EXPECT_EQ(g.storage().size(), 0u);
172TEST(CAGhostLatticeShapeEmptyExtents, TwoDimensionalEmptyAxesAreStable)
175 Ghost row_empty({0, 5}, 9);
176 Ghost col_empty({5, 0}, 9);
178 for (Ghost *g : {&row_empty, &col_empty})
180 EXPECT_EQ(g->dimension(), 2u);
181 EXPECT_EQ(g->size(), 0u);
182 EXPECT_EQ(g->storage().size(), 0u);
183 EXPECT_EQ(g->halo_radius(), 1u);
185 EXPECT_EQ(g->at_safe({0, 0}), 0);
186 EXPECT_EQ(g->at_safe({-1, 0}), 0);
187 EXPECT_EQ(g->at_safe({0, -1}), 0);
188 EXPECT_NO_THROW(g->refresh_halo());
189 EXPECT_NO_THROW(g->fill(4));
190 EXPECT_EQ(g->size(), 0u);
191 EXPECT_EQ(g->storage().size(), 0u);
194 EXPECT_EQ(row_empty.extents()[0], 0u);
195 EXPECT_EQ(row_empty.extents()[1], 5u);
196 EXPECT_EQ(row_empty.store_extents()[0], 0u);
197 EXPECT_EQ(row_empty.store_extents()[1], 7u);
199 EXPECT_EQ(col_empty.extents()[0], 5u);
200 EXPECT_EQ(col_empty.extents()[1], 0u);
201 EXPECT_EQ(col_empty.store_extents()[0], 7u);
202 EXPECT_EQ(col_empty.store_extents()[1], 0u);
210TEST(CAGhostLatticeFill, FillSetsOnlyInteriorUntilRefresh)
215 EXPECT_EQ(g.at_safe({ -1, 0 }), 9);
216 EXPECT_EQ(g.at_safe({ 3, 0 }), 9);
220 EXPECT_EQ(g.at({ 1, 1 }), 4);
221 EXPECT_EQ(g.at_safe({ -1, 1 }), 9);
225 EXPECT_EQ(g.at_safe({ -1, 1 }), 9);
232TEST(CAGhostLatticeHalo, OpenBoundaryHaloIsZero)
234 auto g = make_3x3_ghost_int<OpenBoundary>();
236 EXPECT_EQ(g.at_safe({ -1, 0 }), 0);
237 EXPECT_EQ(g.at_safe({ -1, -1 }), 0);
238 EXPECT_EQ(g.at_safe({ 3, 3 }), 0);
239 EXPECT_EQ(g.at_safe({ 0, 3 }), 0);
241 EXPECT_EQ(g.at_safe({ 1, 1 }), 11);
244TEST(CAGhostLatticeHalo, DirichletConstantValueHalo)
250 g.
set({ i, j },
static_cast<int>(i * 10 + j));
253 EXPECT_EQ(g.at_safe({ -1, 0 }), 42);
254 EXPECT_EQ(g.at_safe({ 3, 0 }), 42);
255 EXPECT_EQ(g.at_safe({ -1, -1 }), 42);
256 EXPECT_EQ(g.at_safe({ 3, 3 }), 42);
257 EXPECT_EQ(g.at_safe({ 1, 1 }), 11);
260TEST(CAGhostLatticeHalo, PeriodicHaloCopiesOppositeEdge)
262 auto g = make_3x3_ghost_int<ToroidalBoundary>();
263 auto lat = make_3x3_classic_int<ToroidalBoundary>();
266 EXPECT_EQ(g.at_safe({ -1, 1 }), lat.at_safe({ -1, 1 }));
267 EXPECT_EQ(g.at_safe({ 3, 1 }), lat.at_safe({ 3, 1 }));
268 EXPECT_EQ(g.at_safe({ 1, -1 }), lat.at_safe({ 1, -1 }));
269 EXPECT_EQ(g.at_safe({ 1, 3 }), lat.at_safe({ 1, 3 }));
272 EXPECT_EQ(g.at_safe({ -1, -1 }), lat.at_safe({ -1, -1 }));
273 EXPECT_EQ(g.at_safe({ 3, 3 }), lat.at_safe({ 3, 3 }));
276TEST(CAGhostLatticeHalo, ReflectiveHaloMirrorsInterior)
278 auto g = make_3x3_ghost_int<ReflectiveBoundary>();
279 auto lat = make_3x3_classic_int<ReflectiveBoundary>();
282 EXPECT_EQ(g.at_safe({ -1, 1 }), lat.at({ 0, 1 }));
283 EXPECT_EQ(g.at_safe({ 3, 1 }), lat.at({ 2, 1 }));
284 EXPECT_EQ(g.at_safe({ 1, -1 }), lat.at({ 1, 0 }));
285 EXPECT_EQ(g.at_safe({ 1, 3 }), lat.at({ 1, 2 }));
288 EXPECT_EQ(g.at_safe({ -1, -1 }), lat.at({ 0, 0 }));
289 EXPECT_EQ(g.at_safe({ 3, 3 }), lat.at({ 2, 2 }));
292TEST(CAGhostLatticeHalo, NeumannHaloClampsToBoundary)
294 auto g = make_3x3_ghost_int<NeumannBoundary>();
297 EXPECT_EQ(g.at_safe({ -1, 1 }), g.at({ 0, 1 }));
298 EXPECT_EQ(g.at_safe({ 3, 1 }), g.at({ 2, 1 }));
299 EXPECT_EQ(g.at_safe({ 1, -1 }), g.at({ 1, 0 }));
300 EXPECT_EQ(g.at_safe({ 1, 3 }), g.at({ 1, 2 }));
302 EXPECT_EQ(g.at_safe({ -1, -1 }), g.at({ 0, 0 }));
303 EXPECT_EQ(g.at_safe({ 3, 3 }), g.at({ 2, 2 }));
311template <
typename Boundary>
314 auto g = make_3x3_ghost_int<Boundary>();
315 auto l = make_3x3_classic_int<Boundary>();
321 EXPECT_EQ(g.at_safe({ i, j }),
l.at_safe({ i, j }))
322 <<
"mismatch at (" << i <<
", " << j <<
")";
325TEST(CAGhostLatticeEquivalence, OpenMatchesClassic)
327 expect_equivalent_at_safe<OpenBoundary>();
329TEST(CAGhostLatticeEquivalence, ToroidalMatchesClassic)
331 expect_equivalent_at_safe<ToroidalBoundary>();
333TEST(CAGhostLatticeEquivalence, ReflectiveMatchesClassic)
335 expect_equivalent_at_safe<ReflectiveBoundary>();
337TEST(CAGhostLatticeEquivalence, NeumannMatchesClassic)
339 expect_equivalent_at_safe<NeumannBoundary>();
342TEST(CAGhostLatticeEquivalence, DirichletMatchesClassic)
350 g.set({ i, j },
static_cast<int>(i * 10 + j));
351 l.set({ i, j },
static_cast<int>(i * 10 + j));
356 EXPECT_EQ(g.at_safe({ i, j }),
l.at_safe({ i, j }))
357 <<
"mismatch at (" << i <<
", " << j <<
")";
364TEST(CAGhostLatticeHaloRadius2, BothRingsCarryPolicyValues)
367 auto g = make_3x3_ghost_int<ToroidalBoundary, 2>();
368 auto l = make_3x3_classic_int<ToroidalBoundary>();
373 EXPECT_EQ(g.at_safe({ i, j }),
l.at_safe({ i, j }));
376 EXPECT_EQ(g.at_safe({ -2, 1 }),
l.at_safe({ -2, 1 }));
377 EXPECT_EQ(g.at_safe({ 4, 1 }),
l.at_safe({ 4, 1 }));
378 EXPECT_EQ(g.at_safe({ 1, -2 }),
l.at_safe({ 1, -2 }));
379 EXPECT_EQ(g.at_safe({ 1, 4 }),
l.at_safe({ 1, 4 }));
380 EXPECT_EQ(g.at_safe({ -2, -2 }),
l.at_safe({ -2, -2 }));
381 EXPECT_EQ(g.at_safe({ 4, 4 }),
l.at_safe({ 4, 4 }));
385 EXPECT_EQ(g.at_safe({ -3, 1 }),
l.at_safe({ -3, 1 }));
386 EXPECT_EQ(g.at_safe({ 5, 1 }),
l.at_safe({ 5, 1 }));
393TEST(CAGhostLattice1D, PeriodicHaloAndEquivalence)
399 g.set({ i },
static_cast<int>(i + 1));
400 l.set({ i },
static_cast<int>(i + 1));
404 EXPECT_EQ(g.at_safe({ i }),
l.at_safe({ i }));
411TEST(CAGhostLattice3D, ReflectiveCornerConsistency)
420 g.set({ i, j,
k }, v);
421 l.set({ i, j,
k }, v);
428 EXPECT_EQ(g.at_safe({ i, j, k }),
l.at_safe({ i, j, k }));
435TEST(CAGhostLattice, SwapExchangesContents)
440 EXPECT_EQ(a.size(0), 3u);
441 EXPECT_EQ(b.size(0), 2u);
442 EXPECT_EQ(a.at({ 0, 0 }), 9);
443 EXPECT_EQ(b.at({ 0, 0 }), 1);
460 lat.set({ ci - 1, cj }, 1);
461 lat.set({ ci - 1, cj + 1 }, 1);
462 lat.set({ ci, cj - 1 }, 1);
463 lat.set({ ci, cj }, 1);
464 lat.set({ ci + 1, cj }, 1);
467 template <
typename L>
468 bool frames_match(
const L &a,
const L &b)
470 if (a.size(0) != b.size(0) or a.size(1) != b.size(1))
472 for (
ca_index_t i = 0; i < static_cast<ca_index_t>(a.size(0)); ++i)
473 for (
ca_index_t j = 0; j < static_cast<ca_index_t>(a.size(1)); ++j)
474 if (a.at({ i, j }) != b.at({ i, j }))
479 template <
typename A,
typename B>
480 bool frames_match_cross(
const A &a,
const B &b)
482 if (a.size(0) != b.size(0) or a.size(1) != b.size(1))
484 for (
ca_index_t i = 0; i < static_cast<ca_index_t>(a.size(0)); ++i)
485 for (
ca_index_t j = 0; j < static_cast<ca_index_t>(a.size(1)); ++j)
486 if (a.at({ i, j }) != b.at({ i, j }))
492template <
typename Boundary>
502 ClassicL l_init({ ROWS, COLS }, 0);
503 GhostL g_init({ ROWS, COLS }, 0);
504 seed_rpentomino(l_init, ROWS, COLS);
505 seed_rpentomino(g_init, ROWS, COLS);
513 for (std::size_t s = 0; s <
steps; ++s)
517 ASSERT_TRUE(frames_match_cross(classic.frame(), ghost.frame()))
518 <<
"frames diverge at step " << s + 1;
522TEST(CAGhostEngineEquivalence, OpenBoundaryAt100Steps)
524 run_engine_equivalence<OpenBoundary>(100);
527TEST(CAGhostEngineEquivalence, ToroidalBoundaryAt150Steps)
529 run_engine_equivalence<ToroidalBoundary>(150);
532TEST(CAGhostEngineEquivalence, ReflectiveBoundaryAt120Steps)
534 run_engine_equivalence<ReflectiveBoundary>(120);
537TEST(CAGhostEngineEquivalence, NeumannBoundaryAt100Steps)
539 run_engine_equivalence<NeumannBoundary>(100);
542TEST(CAGhostEngineEquivalence, DirichletZeroAt100Steps)
545 run_engine_equivalence<ConstantBoundary<int, 0>>(100);
548TEST(CAGhostEngineEquivalence, DirichletOneAt50Steps)
552 run_engine_equivalence<ConstantBoundary<int, 1>>(50);
565 struct R2_Parity_Functor
567 constexpr int operator()(
const int &, std::size_t alive)
const noexcept
569 return alive % 2 == 0 ? 0 : 1;
574TEST(CAGhostEngineRadius2, VonNeumannR2EquivalenceOnDirichlet)
587 ClassicL l_init({ ROWS, COLS }, 0);
588 GhostL g_init({ ROWS, COLS }, 0);
590 l_init.
set({ 4, 4 }, 1);
591 g_init.set({ 4, 4 }, 1);
594 std::move(l_init), ParityRule(R2_Parity_Functor{}), Nbh{});
596 std::move(g_init), ParityRule(R2_Parity_Functor{}), Nbh{});
598 for (std::size_t s = 0; s < 60; ++s)
602 ASSERT_TRUE(frames_match_cross(classic.frame(), ghost.frame()))
603 <<
"R=2 frames diverge at step " << s + 1;
Common typedefs and tag types for the Cellular Automata module.
Row-major dense storage for N-dimensional cellular automata.
Lattice with Halo ghost layers around the user-visible cells.
void set(const coord_type &c, const state_type &v)
Strict write through a user coordinate.
Lattice that adds boundary-aware access on top of a storage.
Moore (Chebyshev) neighborhood of radius R in N dimensions.
Rule whose next state depends on (current, alive_count).
Synchronous double-buffered engine.
void step()
Apply the rule to every cell once and swap buffers.
Von Neumann (L1) neighborhood of radius R in N dimensions.
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.
Zero-gradient (Neumann) boundary.
Out-of-range neighbours behave as if the lattice ended.
Out-of-range coordinates mirror back into the lattice.
The lattice wraps around on every axis.
C++20 concepts for the Cellular Automata module.
Synchronous double-buffered engine for cellular automata.
Ghost-layer lattice for high-performance boundary handling.
static void expect_equivalent_at_safe()
static void run_engine_equivalence(std::size_t steps)
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).