63#include <gtest/gtest.h>
85long long process_id() noexcept
88 return static_cast<long long>(_getpid());
90 return static_cast<long long>(getpid());
94std::filesystem::path tmp_path(
const std::string &name)
103 static std::atomic<unsigned long long>
counter{0};
104 const auto tick = std::chrono::steady_clock::now().time_since_epoch().count();
105 return std::filesystem::temp_directory_path()
106 / (
"aleph_ca_chk_" + name +
"_" + std::to_string(
static_cast<long long>(tick)) +
107 "_" + std::to_string(process_id()) +
108 "_" + std::to_string(
counter++));
111Grid random_grid(
ca_size_t side, std::uint32_t
seed,
double p_alive = 0.4)
114 std::uniform_real_distribution<double> u(0.0, 1.0);
115 Grid g({side, side}, 0);
118 g.set({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)},
119 u(
rng) < p_alive ? 1 : 0);
123bool grids_equal(
const Grid &a,
const Grid &b)
125 if (a.extents() != b.extents())
127 for (
ca_size_t i = 0; i < a.size(0); ++i)
128 for (
ca_size_t j = 0; j < a.size(1); ++j)
129 if (a.at({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)})
130 != b.at({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)}))
135std::string read_file(
const std::filesystem::path &p)
137 std::ifstream in(p, std::ios::binary);
138 std::ostringstream ss;
149TEST(CACheckpoint, RoundTripProducesIdenticalFiles)
153 const auto dir = tmp_path(
"roundtrip");
154 std::filesystem::create_directories(dir);
155 const auto path1 = dir /
"snap1.bin";
156 const auto path2 = dir /
"snap2.bin";
165 EXPECT_EQ(token.header.step_count, 7u);
169 EXPECT_EQ(read_file(path1), read_file(path2))
170 <<
"save → load → save must be bit-identical";
172 std::filesystem::remove_all(dir);
179TEST(CACheckpoint, ResumeReproducesUninterruptedRun)
183 const auto dir = tmp_path(
"resume");
184 std::filesystem::create_directories(dir);
185 const auto path = dir /
"midpoint.bin";
198 EXPECT_EQ(token.header.step_count, 20u);
199 EXPECT_EQ(restored.steps_run(), 20u);
202 EXPECT_EQ(restored.steps_run(), 40u);
203 EXPECT_TRUE(grids_equal(restored.frame(), ref.frame()))
204 <<
"restored run must match uninterrupted run frame-by-frame";
206 std::filesystem::remove_all(dir);
213TEST(CACheckpoint, InspectReturnsValidatedHeader)
217 const auto dir = tmp_path(
"inspect");
218 std::filesystem::create_directories(dir);
219 const auto path = dir /
"snap.bin";
226 EXPECT_EQ(
h.format_version, 2u);
227 EXPECT_EQ(
h.rank, 2u);
228 EXPECT_EQ(
h.extents[0], 8u);
229 EXPECT_EQ(
h.extents[1], 8u);
230 EXPECT_EQ(
h.step_count, 3u);
231 EXPECT_EQ(
h.cell_count, 64u);
232 EXPECT_EQ(
h.has_rng, 0u);
234 std::filesystem::remove_all(dir);
242TEST(CACheckpoint, StochasticRuleResumesBitIdentically)
246 const auto dir = tmp_path(
"stoch");
247 std::filesystem::create_directories(dir);
248 const auto path = dir /
"stoch.bin";
255 Grid initial({16, 16}, 0);
258 initial.set({static_cast<ca_index_t>(i), static_cast<ca_index_t>(j)},
259 static_cast<int>((i + j) % 3));
264 Engine partial(initial, make_rule(),
Moore<2, 1>{});
270 Engine resumed(Grid({16, 16}, 0),
Schelling_Rule<>{0.5, 1.0, 1.0, 0xDEADBEEF},
273 EXPECT_EQ(resumed.rule().master_seed(), 0xCAFEBABEu)
274 <<
"master seed must be restored from the snapshot";
277 EXPECT_TRUE(grids_equal(resumed.frame(), ref.frame()))
278 <<
"stochastic resume must be bit-identical to uninterrupted run";
280 std::filesystem::remove_all(dir);
287TEST(CACheckpoint, LoadRejectsTypeHashMismatch)
295 const auto dir = tmp_path(
"badtype");
296 std::filesystem::create_directories(dir);
297 const auto path = dir /
"snap.bin";
303 const auto offsets = std::array<Offset_Vec<1>, 2>{{{-1}, {1}}};
308 std::filesystem::remove_all(dir);
315TEST(CACheckpoint, PeriodicObserverWritesEveryKSteps)
319 const auto dir = tmp_path(
"periodic");
320 std::filesystem::create_directories(dir);
324 (dir /
"snap_{step}.bin").
string(),
327 eng.on_post_step([&](std::size_t s,
const Grid &f) { obs.on_step_end(s, f); });
331 EXPECT_TRUE(std::filesystem::exists(dir /
"snap_0003.bin"));
332 EXPECT_TRUE(std::filesystem::exists(dir /
"snap_0006.bin"));
333 EXPECT_TRUE(std::filesystem::exists(dir /
"snap_0009.bin"));
335 EXPECT_FALSE(std::filesystem::exists(dir /
"snap_0001.bin"));
336 EXPECT_FALSE(std::filesystem::exists(dir /
"snap_0002.bin"));
337 EXPECT_FALSE(std::filesystem::exists(dir /
"snap_0010.bin"));
342 EXPECT_EQ(h3.step_count, 3u);
343 EXPECT_EQ(h6.step_count, 6u);
344 EXPECT_EQ(h3.extents, h6.extents);
346 std::filesystem::remove_all(dir);
353TEST(CACheckpointStream, TileCacheLruEvictionAndPersistence)
355 const auto dir = tmp_path(
"tile");
356 std::filesystem::remove_all(dir);
360 Tiles tiles({16, 16}, dir, 4);
366 tiles.
set(i, j,
static_cast<int>((i + j) & 1));
368 EXPECT_LE(tiles.resident(), 4u) <<
"cache must respect capacity";
369 EXPECT_GE(tiles.stats().evictions, 12u)
370 <<
"16 distinct tiles touched, capacity 4 → >= 12 evictions";
379 EXPECT_EQ(tiles.at(i, j),
static_cast<int>((i + j) & 1))
380 <<
"checkerboard mismatch at (" << i <<
"," << j <<
")";
384 std::size_t file_count = 0;
385 for (
auto &p : std::filesystem::directory_iterator(dir))
386 if (p.path().extension() ==
".bin")
388 EXPECT_EQ(file_count, 16u);
390 std::filesystem::remove_all(dir);
393TEST(CACheckpointStream, TileCacheRejectsBadExtents)
396 const auto dir = tmp_path(
"tile_bad");
397 std::filesystem::create_directories(dir);
398 EXPECT_THROW((Tiles({15, 16}, dir, 4)), std::domain_error);
399 EXPECT_THROW((Tiles({16, 16}, dir, 0)), std::domain_error);
400 std::filesystem::remove_all(dir);
Binary checkpoint format for Aleph::CA engines (Phase 15 + Phase 17 crash-safe / compress / async wri...
Reproducible random-number support for stochastic CA rules (Phase 8).
Phase 15 tile-based out-of-core storage for Aleph::CA.
Common typedefs and tag types for the Cellular Automata module.
User-supplied list of offsets for arbitrary connectivity.
Lattice that adds boundary-aware access on top of a storage.
Precomputed transition table for (self, neighbours...).
Moore (Chebyshev) neighborhood of radius R in N dimensions.
Observer that auto-saves the engine state every every steps to a templated path.
Local approximation of the Schelling segregation model.
Synchronous double-buffered engine.
Out-of-core 2D tile cache with LRU eviction.
void set(ca_size_t r, ca_size_t c, const State &v)
Write the cell at (r, c) and mark its tile dirty.
constexpr Game_Of_Life_Rule make_game_of_life_rule() noexcept
Build the canonical Game of Life rule.
Checkpoint_Header inspect_checkpoint(const std::filesystem::path &path)
Read just the header from a checkpoint file.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Resume_Token load_checkpoint_into(Engine &engine, const std::filesystem::path &path)
Restore an engine's state in-place from a checkpoint file.
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,...
void save_checkpoint(Engine &engine, const std::filesystem::path &path, const Checkpoint_Options &options={})
Write a complete engine snapshot to disk atomically.
Main namespace for Aleph-w library functions.
Resume handle returned by load_checkpoint_into.
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.
Reproducible stochastic CA rules (Phase 8).
Dense, contiguous storage for cellular automata cells (1D/2D/3D).