51#include <gtest/gtest.h>
65static_assert(LatticeLike<Graph_Lattice<int>>);
66static_assert(LatticeLike<Graph_Lattice<bool>>);
72TEST(CAGraphBuilders, PathGraphAdjacencyShape)
75 ASSERT_EQ(adj.size(), 5u);
83TEST(CAGraphBuilders, RingGraphAdjacencyShape)
86 ASSERT_EQ(adj.size(), 4u);
93TEST(CAGraphBuilders, GridGraphAdjacencyEdgeCount)
97 std::size_t total_deg = 0;
98 for (
const auto &
row : adj)
99 total_deg +=
row.size();
100 EXPECT_EQ(total_deg, 2u * 17u);
103 EXPECT_EQ(adj[0].
size(), 2u);
105 EXPECT_EQ(adj[1 * 4 + 1].
size(), 4u);
108TEST(CAGraphBuilders, ToroidalGridAlwaysHasFourNeighbours)
111 for (
const auto &
row : adj)
112 EXPECT_EQ(
row.size(), 4u);
119TEST(CAGraphLattice, ConstructorRejectsOutOfRangeNeighbour)
126TEST(CAGraphLattice, AtAndSetRoundTrip)
129 EXPECT_EQ(lat.
size(), 6u);
130 EXPECT_EQ(lat.
degree(0), 1u);
131 EXPECT_EQ(lat.
degree(3), 2u);
133 for (std::size_t n = 0; n < lat.
size(); ++n)
134 lat.
set_node(n,
static_cast<int>(n + 1));
135 for (std::size_t n = 0; n < lat.
size(); ++n)
136 EXPECT_EQ(lat.
at_node(n),
static_cast<int>(n + 1));
139TEST(CAGraphLattice, AtSafeOnOutOfRangeReturnsDefault)
143 EXPECT_EQ(lat.
at_safe(coord_t{-1}), 0);
144 EXPECT_EQ(lat.
at_safe(coord_t{99}), 0);
145 EXPECT_EQ(lat.
at_safe(coord_t{1}), 7);
146 EXPECT_THROW((
void)lat.
at(coord_t{99}), std::out_of_range);
149TEST(CAGraphLattice, FillResetsEveryCell)
155 for (std::size_t n = 0; n < lat.
size(); ++n)
167struct Majority_Functor
171 std::size_t alive = 0;
175 const std::size_t deg = neigh.size();
188class Voter_Rule_Driver
191 mutable std::uint64_t calls_ = 0;
194 explicit Voter_Rule_Driver(std::uint64_t
seed) noexcept : seed_(
seed) {}
200 std::uint64_t key = seed_;
201 key = (key * 1099511628211ull) ^ (calls_ + 1);
203 std::mt19937_64
rng(key);
204 std::uniform_int_distribution<std::size_t> pick(0, neigh.size() - 1);
205 return neigh[pick(
rng)];
211TEST(CAGraphEngine, MajorityRulePreservesAllZero)
214 using Rule = Majority_Functor;
217 for (std::size_t n = 0; n < eng.frame().
size(); ++n)
218 EXPECT_EQ(eng.frame().at_node(n), 0);
221TEST(CAGraphEngine, MajorityRulePreservesAllOnes)
224 using Rule = Majority_Functor;
227 for (std::size_t n = 0; n < eng.frame().
size(); ++n)
228 EXPECT_EQ(eng.frame().at_node(n), 1);
231TEST(CAGraphEngine, MajorityFlipsIsolatedSeedBackToZero)
239 seed, Majority_Functor{});
241 for (std::size_t n = 0; n < eng.frame().
size(); ++n)
242 EXPECT_EQ(eng.frame().at_node(n), 0);
245TEST(CAGraphEngine, VoterModelConvergesOnConnectedNonBipartiteGraph)
253 auto edge = [&](std::size_t a, std::size_t b)
258 edge(0, 1); edge(0, 2); edge(1, 2);
259 edge(0, 3); edge(0, 4); edge(3, 4);
260 edge(0, 5); edge(0, 6); edge(5, 6);
263 seed.set_node(1, 1);
seed.set_node(2, 1);
264 seed.set_node(5, 1);
seed.set_node(6, 1);
266 Voter_Rule_Driver driver(2026u);
271 const int v0 = eng.
frame().at_node(0);
272 for (std::size_t n = 1; n < eng.
frame().
size(); ++n)
273 EXPECT_EQ(eng.
frame().at_node(n), v0)
274 <<
"voter model did not reach consensus on the bowtie graph";
277TEST(CAGraphEngine, HookFiringSemantics)
282 seed, Majority_Functor{});
285 std::size_t post = 0;
286 std::size_t last_pre = 0;
287 std::size_t last_post = 0;
300 EXPECT_EQ(last_pre, 3u);
301 EXPECT_EQ(last_post, 4u);
302 EXPECT_EQ(eng.steps_run(), 4u);
305TEST(CAGraphEngine, EquivalenceWithSequentialEngineOnGrid)
317 std::vector<int> initial(16, 0);
324 for (std::size_t n = 0; n <
seed.size(); ++n)
325 seed.set_node(n, initial[n]);
333 for (std::size_t n = 0; n < eng.frame().
size(); ++n)
334 EXPECT_EQ(eng.frame().at_node(n), initial[n])
335 <<
"block at node " << n <<
" was not still under graph engine";
Common typedefs and tag types for the Cellular Automata module.
Simple dynamic array with automatic resizing and functional operations.
T & append(const T &data)
Append a copy of data
Graph lattice: one cell per node + precomputed adjacency.
std::size_t degree(std::size_t n) const
Degree of node n.
ca_size_t size() const noexcept
T at_node(std::size_t n) const
Direct read at node id n.
Coord_Vec< 1 > coord_type
void set_node(std::size_t n, const T &v)
Direct write at node id n.
T at(const coord_type &c) const
std::size_t max_degree() const noexcept
Maximum degree across the graph (computed in O(N)).
T at_safe(const coord_type &c) const
Boundary-aware read.
void fill(const T &value)
Set every cell to value.
Synchronous double-buffered engine for graph CAs.
void run(const std::size_t steps)
Run several synchronous steps.
const Lattice & frame() const noexcept
void on_pre_step(F &&f)
Register a hook fired before every step().
Rule whose next state depends on (current, alive_count).
std::span< const T > Neighbor_View
Read-only view over a contiguous range of neighbour values.
Array< Array< std::size_t > > make_grid_graph_adjacency(std::size_t rows, std::size_t cols, bool periodic=false)
Build the adjacency of a 2D 4-neighbour grid graph.
Array< Array< std::size_t > > make_path_graph_adjacency(std::size_t n, bool cycle=false)
Build the adjacency of a path graph with n nodes.
decltype(auto) current(It &it) noexcept(current_is_noexcept< It >)
Current item of an iterator that is positioned on one.
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
See compute_cardinality_rec(Node * root).
Functor implementing Conway's Game of Life canonical rule (B3/S23).
C++20 concepts for the Cellular Automata module.
CA whose underlying topology is an arbitrary undirected graph.
Rule mechanisms for Aleph::CA.