37# include <initializer_list>
43# include <gtest/gtest.h>
60 using Cell = std::pair<ca_index_t, ca_index_t>;
62 std::set<Cell>
to_set(std::initializer_list<Cell> cells)
64 return std::set<Cell>(cells.begin(), cells.end());
68 std::initializer_list<Cell> alive)
71 for (
const auto & c : alive)
72 g.set({ c.first, c.second }, 1);
77 const std::vector<Cell> & alive)
80 for (
const auto & c : alive)
81 g.set({ c.first, c.second }, 1);
85 std::set<Cell> alive_cells(
const Grid & g)
92 if (g.at({ i, j }) != 0)
97 Grid manual_life_step(
const Grid & cur)
99 Grid nxt(cur.extents(), 0);
102 std::array<int, Moore<2, 1>::size_v> buf { };
110 std::span<int>(buf.data(), buf.size()));
112 rule(cur.at({ i, j }),
118 Engine make_engine(Grid initial)
123 void expect_matches_reference_for_twenty_steps(Grid initial,
127 auto engine = make_engine(std::move(initial));
128 for (std::size_t t = 0; t < 20; ++t)
130 ASSERT_EQ(alive_cells(
engine.frame()), alive_cells(ref))
131 << name <<
" before step " << t;
133 ref = manual_life_step(ref);
135 EXPECT_EQ(alive_cells(
engine.frame()), alive_cells(ref))
136 << name <<
" after 20 steps";
137 EXPECT_EQ(
engine.steps_run(), 20u);
142 Grid g({ 17, 17 }, 0);
144 const std::array<int, 4> sparse_rows = {{ 0, 5, 7, 12 }};
145 const std::array<int, 6> dense_cols = {{ 2, 3, 4, 8, 9, 10 }};
146 for (
const int r : sparse_rows)
147 for (const int c : dense_cols)
150 const std::array<int, 6> dense_rows = {{ 2, 3, 4, 8, 9, 10 }};
151 const std::array<int, 4> sparse_cols = {{ 0, 5, 7, 12 }};
152 for (
const int r : dense_rows)
153 for (const int c : sparse_cols)
158 std::vector<Cell> glider_cells()
160 return { { 1, 2 }, { 2, 3 }, { 3, 1 }, { 3, 2 }, { 3, 3 } };
163 std::set<Cell> shifted(
const std::vector<Cell> & cells,
167 for (
const auto & c : cells)
168 out.
insert({ c.first + dr, c.second + dc });
173TEST(CAEngineSync, MatchesManualReferenceForCanonicalPatterns)
175 expect_matches_reference_for_twenty_steps(
176 make_grid(5, 5, { { 2, 1 }, { 2, 2 }, { 2, 3 } }),
178 expect_matches_reference_for_twenty_steps(
179 make_grid(6, 6, { { 2, 2 }, { 2, 3 }, { 2, 4 },
180 { 3, 1 }, { 3, 2 }, { 3, 3 } }),
182 expect_matches_reference_for_twenty_steps(
183 make_grid(6, 6, { { 1, 1 }, { 1, 2 }, { 2, 1 }, { 2, 2 },
184 { 3, 3 }, { 3, 4 }, { 4, 3 }, { 4, 4 } }),
186 expect_matches_reference_for_twenty_steps(
187 make_grid(10, 10, glider_cells()),
189 expect_matches_reference_for_twenty_steps(make_pulsar(),
"pulsar");
192TEST(CAEngineSync, OscillatorsFollowKnownPeriodsForTwentySteps)
194 auto blinker = make_engine(
195 make_grid(5, 5, { { 2, 1 }, { 2, 2 }, { 2, 3 } }));
196 auto toad = make_engine(
197 make_grid(6, 6, { { 2, 2 }, { 2, 3 }, { 2, 4 },
198 { 3, 1 }, { 3, 2 }, { 3, 3 } }));
199 auto beacon = make_engine(
200 make_grid(6, 6, { { 1, 1 }, { 1, 2 }, { 2, 1 }, { 2, 2 },
201 { 3, 3 }, { 3, 4 }, { 4, 3 }, { 4, 4 } }));
203 const auto blinker0 =
to_set({ { 2, 1 }, { 2, 2 }, { 2, 3 } });
204 const auto blinker1 =
to_set({ { 1, 2 }, { 2, 2 }, { 3, 2 } });
205 const auto toad0 =
to_set({ { 2, 2 }, { 2, 3 }, { 2, 4 },
206 { 3, 1 }, { 3, 2 }, { 3, 3 } });
207 const auto toad1 =
to_set({ { 1, 3 }, { 2, 1 }, { 2, 4 },
208 { 3, 1 }, { 3, 4 }, { 4, 2 } });
209 const auto beacon0 =
to_set({ { 1, 1 }, { 1, 2 }, { 2, 1 }, { 2, 2 },
210 { 3, 3 }, { 3, 4 }, { 4, 3 }, { 4, 4 } });
211 const auto beacon1 =
to_set({ { 1, 1 }, { 1, 2 }, { 2, 1 },
212 { 3, 4 }, { 4, 3 }, { 4, 4 } });
214 for (std::size_t t = 0; t <= 20; ++t)
216 EXPECT_EQ(alive_cells(blinker.frame()), t % 2 == 0 ? blinker0 : blinker1)
217 <<
"blinker t=" << t;
218 EXPECT_EQ(alive_cells(toad.frame()), t % 2 == 0 ? toad0 : toad1)
220 EXPECT_EQ(alive_cells(beacon.frame()), t % 2 == 0 ? beacon0 : beacon1)
231TEST(CAEngineSync, GliderMovesOneCellDiagonallyEveryFourSteps)
233 const auto cells = glider_cells();
234 auto engine = make_engine(make_grid(10, 10, cells));
236 for (std::size_t t = 0; t <= 20; ++t)
240 const auto d =
static_cast<ca_index_t>(t / 4);
241 EXPECT_EQ(alive_cells(
engine.frame()), shifted(cells, d, d))
249TEST(CAEngineSync, PulsarKeepsPeriodThree)
251 const auto initial = alive_cells(make_pulsar());
252 auto engine = make_engine(make_pulsar());
254 for (std::size_t t = 1; t <= 20; ++t)
258 EXPECT_EQ(alive_cells(
engine.frame()), initial) <<
"pulsar t=" << t;
260 EXPECT_EQ(
engine.steps_run(), 20u);
263TEST(CAEngineSync, HooksObservePreAndPostStepFrames)
265 auto engine = make_engine(
266 make_grid(5, 5, { { 2, 1 }, { 2, 2 }, { 2, 3 } }));
267 std::vector<std::size_t> pre_steps;
268 std::vector<std::size_t> post_steps;
269 std::vector<std::size_t> pre_alive;
270 std::vector<std::size_t> post_alive;
272 engine.on_pre_step([&](std::size_t step,
const Grid & frame)
274 pre_steps.push_back(step);
275 pre_alive.push_back(alive_cells(frame).
size());
277 engine.on_post_step([&](std::size_t step,
const Grid & frame)
279 post_steps.push_back(step);
280 post_alive.push_back(alive_cells(frame).
size());
285 EXPECT_EQ(pre_steps, (std::vector<std::size_t>{ 0, 1 }));
286 EXPECT_EQ(post_steps, (std::vector<std::size_t>{ 1, 2 }));
287 EXPECT_EQ(pre_alive, (std::vector<std::size_t>{ 3, 3 }));
288 EXPECT_EQ(post_alive, (std::vector<std::size_t>{ 3, 3 }));
289 EXPECT_EQ(
engine.steps_run(), 2u);
292TEST(CAEngineSync, TiledAndColumnMajorOrdersMatchRowMajor)
296 using Column_Engine =
299 Grid initial = make_pulsar();
300 auto row_major = make_engine(initial);
305 for (std::size_t t = 0; t <= 20; ++t)
307 const auto expected = alive_cells(row_major.frame());
308 EXPECT_EQ(alive_cells(tiled.frame()),
expected) <<
"tiled t=" << t;
309 EXPECT_EQ(alive_cells(column.frame()),
expected) <<
"column t=" << t;
319TEST(CAEngineUtils, WolframFactoryRunsSeededRule90Impulse)
327 EXPECT_EQ(
engine.frame().at({ 0 }), 0);
328 EXPECT_EQ(
engine.frame().at({ 1 }), 0);
329 EXPECT_EQ(
engine.frame().at({ 2 }), 1);
330 EXPECT_EQ(
engine.frame().at({ 3 }), 0);
331 EXPECT_EQ(
engine.frame().at({ 4 }), 1);
332 EXPECT_EQ(
engine.frame().at({ 5 }), 0);
333 EXPECT_EQ(
engine.frame().at({ 6 }), 0);
334 EXPECT_EQ(
engine.steps_run(), 1u);
size_t size_t int32_t * out
Convenience builders for the Phase 3 synchronous engine.
Common typedefs and tag types for the Cellular Automata module.
Lattice that adds boundary-aware access on top of a storage.
Moore (Chebyshev) neighborhood of radius R in N dimensions.
Synchronous double-buffered engine.
Minimal std::expected-style result type for C++20.
const long double offset[]
Offset values indexed by symbol string length (bounded by MAX_OFFSET_INDEX)
Wolfram_1D_Engine make_wolfram_engine(std::uint8_t rule_no, ca_size_t width)
Build a 1D elementary Wolfram engine of the given width.
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::array< ca_index_t, N > Coord_Vec
Default coordinate vector.
void gather_neighbors(const Nbh &nh, const L &lat, const typename L::coord_type ¢er, std::span< T > out)
Populate out[0..nh.size()) with neighbour values of center.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
std::set< typename C::Item_Type > to_set(const C &c)
Convert a container to a std::set.
Iterate the lattice in column-major (Fortran) order.
Out-of-range neighbours behave as if the lattice ended.
Iterate a 2D lattice in W x H tiles.
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).