56#include <gtest/gtest.h>
100 std::bernoulli_distribution flip(
density);
103 lat.set({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)},
114static_assert(ObserverLike<Density_Observer<int>, L>);
115static_assert(ObserverLike<Activity_Observer<L>, L>);
116static_assert(ObserverLike<Entropy_Observer<2>, L>);
117static_assert(ObserverLike<Stationary_Detector, L>);
118static_assert(ObserverLike<Sampling_Observer<L>, L>);
126struct Counting_Observer
128 std::size_t begins = 0;
129 std::size_t ends = 0;
131 template <
typename Frame>
132 void on_step_begin(std::size_t,
const Frame &)
137 template <
typename Frame>
138 void on_step_end(std::size_t,
const Frame &)
146TEST(CAComposite, FansOutBothCallbacksToEveryChild)
155 EXPECT_EQ(a.begins, 2u);
156 EXPECT_EQ(a.ends, 1u);
157 EXPECT_EQ(b.begins, 2u);
158 EXPECT_EQ(b.ends, 1u);
161TEST(CAComposite, FactoryPreservesLvalueObserverReferences)
168 comp.on_step_begin(0, frame);
169 comp.on_step_end(1, frame);
170 comp.on_step_begin(1, frame);
172 EXPECT_EQ(a.begins, 2u);
173 EXPECT_EQ(a.ends, 1u);
174 EXPECT_EQ(b.begins, 2u);
175 EXPECT_EQ(b.ends, 1u);
182TEST(CAActivityObserver, BlinkerReportsFourChangesPerStep)
184 L
seed = make_blinker();
190 ASSERT_EQ(act.activity().size(), 6u);
191 for (
auto a : act.activity())
195TEST(CAActivityObserver, BlockIsStillSoActivityIsZero)
197 L
seed = make_block();
203 ASSERT_EQ(act.activity().size(), 5u);
204 for (
auto a : act.activity())
212TEST(CADensityObserver, InitialFrameIsCapturedAndStepsAccumulate)
214 L
seed = make_blinker();
221 ASSERT_EQ(dens.size(), 5u);
223 for (std::size_t i = 0; i < dens.size(); ++i)
224 EXPECT_EQ(dens.counts()[i], 3u);
227TEST(CADensityObserver, GoLRandomDensityLandsInExpectedBand)
232 L
seed = make_random(64, 64, 0xCAFEu, 0.4);
241 for (std::size_t i = dens.size() - 200; i < dens.size(); ++i)
243 sum += dens.density_at(i);
246 const double avg =
sum /
static_cast<double>(cnt);
250 EXPECT_GT(avg, 0.005);
251 EXPECT_LT(avg, 0.20);
258TEST(CAStationaryDetector, BlinkerYieldsCycleLengthTwo)
260 L
seed = make_blinker();
266 ASSERT_TRUE(det.cycle_detected());
267 EXPECT_EQ(*det.cycle_length(), 2u);
270TEST(CAStationaryDetector, BlockYieldsCycleLengthOne)
272 L
seed = make_block();
278 ASSERT_TRUE(det.cycle_detected());
279 EXPECT_EQ(*det.cycle_length(), 1u);
282TEST(CAStationaryDetector, NoCycleDetectedWhenWindowIsTooSmall)
287 seed.set({0, 1}, 1);
seed.set({1, 2}, 1);
288 seed.set({2, 0}, 1);
seed.set({2, 1}, 1);
seed.set({2, 2}, 1);
295 EXPECT_FALSE(det.cycle_detected());
298TEST(CAStationaryDetector, TheWindowKeepsOneFrameMoreThanTheCycleLength)
300 L
seed = make_blinker();
306 ASSERT_TRUE(det.cycle_detected());
307 EXPECT_EQ(*det.cycle_length(), 2u);
308 EXPECT_EQ(det.buffered_hashes(), 5u);
314TEST(CAStationaryDetector, AWindowOfSizeMaxKeepsEveryFrame)
316 L
seed = make_blinker();
322 ASSERT_TRUE(det.cycle_detected());
323 EXPECT_EQ(*det.cycle_length(), 2u);
324 EXPECT_EQ(det.buffered_hashes(), 11u);
331TEST(CASamplingObserver, SnapshotsAtConfiguredPeriod)
333 L
seed = make_blinker();
340 ASSERT_EQ(samp.size(), 4u);
341 EXPECT_EQ(samp.step_indices(),
345TEST(CASamplingObserver, ZeroPeriodDisablesSampling)
347 L
seed = make_blinker();
353 EXPECT_EQ(samp.size(), 0u);
360TEST(CAEntropyObserver, EntropyZeroForEmptyAndConstantFrames)
368 ASSERT_EQ(ent.size(), 4u);
369 for (
double e : ent.entropy())
370 EXPECT_DOUBLE_EQ(e, 0.0);
373TEST(CAEntropyObserver, EntropyPositiveOnRandomFrame)
375 L
seed = make_random(16, 16, 42u, 0.5);
380 ASSERT_GE(ent.size(), 1u);
381 EXPECT_GT(ent.entropy()[0], 0.0);
388TEST(CAComposite, MultipleObserversObserveTheSameRun)
390 L
seed = make_blinker();
399 comp(dens, act, det);
404 EXPECT_EQ(dens.size(), 7u);
405 EXPECT_EQ(act.
size(), 6u);
Convenience builders for the Phase 3 synchronous engine.
Free metric helpers for cellular automata frames.
Phase-7 observability layer for cellular automata engines.
Common typedefs and tag types for the Cellular Automata module.
Simple dynamic array with automatic resizing and functional operations.
Records the number of cells that changed across each step.
std::size_t size() const noexcept
Return the number of recorded steps.
Variadic adaptor that fan-outs notifications to many observers.
void on_step_end(std::size_t step, const Lattice &frame)
Forward a step-end event to every child observer.
void on_step_begin(std::size_t step, const Lattice &frame)
Forward a step-begin event to every child observer.
Records the count of cells in tracked_state per step.
Records the Shannon entropy of the state distribution.
Lattice that adds boundary-aware access on top of a storage.
Moore (Chebyshev) neighborhood of radius R in N dimensions.
Snapshots the lattice every period steps.
Detects fixed points and short cycles via frame hashing.
bool cycle_detected() const noexcept
Return whether a cycle has been detected.
std::optional< std::size_t > cycle_length() const noexcept
Return the detected cycle length.
Synchronous double-buffered engine.
void attach_observer(Engine &engine, Observer &observer)
Plug an observer into an engine's pre/post-step hooks.
constexpr Game_Of_Life_Rule make_game_of_life_rule() noexcept
Build the canonical Game of Life rule.
double density(const Lattice &lat, const typename Lattice::state_type &s)
auto make_composite_observer(Observers &&...observers)
Build a Composite_Observer with deduced ownership semantics.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Main namespace for Aleph-w library functions.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
The lattice wraps around on every axis.
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).