44#include <gtest/gtest.h>
56 using Cell = std::pair<ca_index_t, ca_index_t>;
59 std::initializer_list<Cell> alive)
62 for (
const auto &c : alive)
63 g.set({c.first, c.second}, 1);
67 Grid make_values_grid()
77 std::set<Cell> alive_cells(
const Grid &g)
80 for (
ca_index_t r = 0; r < static_cast<ca_index_t>(g.size(0)); ++
r)
81 for (
ca_index_t c = 0; c < static_cast<ca_index_t>(g.size(1)); ++c)
82 if (g.at({r, c}) != 0)
87 void expect_same_frame(
const Grid &a,
const Grid &b)
89 ASSERT_EQ(a.size(0), b.size(0));
90 ASSERT_EQ(a.size(1), b.size(1));
91 for (
ca_index_t r = 0; r < static_cast<ca_index_t>(a.size(0)); ++
r)
92 for (
ca_index_t c = 0; c < static_cast<ca_index_t>(a.size(1)); ++c)
93 EXPECT_EQ(a.at({r, c}), b.at({
r, c})) <<
"at (" <<
r <<
", " << c <<
")";
96 std::string bytes(std::initializer_list<unsigned> values)
99 for (
const unsigned v : values)
100 out.push_back(static_cast<char>(static_cast<unsigned char>(v)));
105TEST(CAIO, RleFullFrameRoundTripPreservesExtentsAndCells)
107 const Grid src = make_grid(4, 5, {{1, 2}, {2, 3}, {3, 1}, {3, 2}, {3, 3}});
109 opts.
name =
"glider";
110 opts.comment =
"full-frame snapshot";
113 EXPECT_NE(rle.find(
"#N glider"), std::string::npos);
114 EXPECT_NE(rle.find(
"#C full-frame snapshot"), std::string::npos);
115 EXPECT_NE(rle.find(
"x = 5, y = 4, rule = B3/S23"), std::string::npos);
118 EXPECT_EQ(pattern.
width, 5u);
119 EXPECT_EQ(pattern.
height, 4u);
120 EXPECT_EQ(pattern.
rule,
"B3/S23");
121 const Grid dst = make_lattice_from_pattern<Grid>(pattern);
122 expect_same_frame(src, dst);
125TEST(CAIO, RleTrimmedOutputMatchesCanonicalGliderShape)
127 const Grid src = make_grid(6, 7, {{2, 3}, {3, 4}, {4, 2}, {4, 3}, {4, 4}});
133 EXPECT_NE(rle.find(
"x = 3, y = 3, rule = B3/S23"), std::string::npos);
134 EXPECT_NE(rle.find(
"bob$2bo$3o!"), std::string::npos) << rle;
136 const Grid trimmed = make_lattice_from_pattern<Grid>(
read_rle_string(rle));
137 EXPECT_EQ(alive_cells(trimmed),
138 (std::set<Cell>{{0, 1}, {1, 2}, {2, 0}, {2, 1}, {2, 2}}));
141TEST(CAIO, RleParserAcceptsCommentsLongRunsAndConwayAliases)
143 const std::string text =
145 "#C A is accepted as alive and . as dead\n"
146 "x = 5, y = 3, rule = B36/S23\n"
149 EXPECT_EQ(pattern.
name,
"mixed dialect");
151 EXPECT_EQ(pattern.
rule,
"B36/S23");
152 EXPECT_EQ(pattern.
width, 5u);
153 EXPECT_EQ(pattern.
height, 3u);
154 EXPECT_EQ(pattern.
alive,
158TEST(CAIO, RleParserRejectsMalformedInput)
161 EXPECT_THROW(
read_rle_string(
"x = 2, y = 1\n3o!\n"), std::domain_error);
163 EXPECT_THROW(
read_rle_string(
"x = 2, y = 1\nbo?\n"), std::domain_error);
170TEST(CAIO, RleParserBoundsTheNumberOfLiveCells)
172 const std::string hostile =
"x = 1000000000, y = 1\n999999999o!\n";
176 EXPECT_FALSE(
r.has_value());
179 EXPECT_THROW((
void)
read_rle_string(
"x = 3, y = 2\n2o$2o!\n", 3), std::length_error);
180 EXPECT_EQ(
read_rle_string(
"x = 3, y = 2\n2o$o!\n", 3).alive.size(), 3u);
187TEST(CAIO, PlaintextRoundTripPreservesFullFrame)
189 const Grid src = make_grid(3, 4, {{0, 1}, {1, 2}, {2, 0}, {2, 1}, {2, 2}});
191 opts.
comment =
"plaintext glider";
193 EXPECT_NE(text.find(
"!plaintext glider"), std::string::npos);
194 EXPECT_NE(text.find(
".O.."), std::string::npos);
197 EXPECT_EQ(pattern.
width, 4u);
198 EXPECT_EQ(pattern.
height, 3u);
199 const Grid dst = make_lattice_from_pattern<Grid>(pattern);
200 expect_same_frame(src, dst);
203TEST(CAIO, PlaintextParserAcceptsStarsAndRejectsUnknownGlyphs)
206 EXPECT_EQ(pattern.
width, 3u);
207 EXPECT_EQ(pattern.
height, 2u);
212TEST(CAIO, Life106NormalisesNegativeCoordinates)
214 const std::string text =
219 std::istringstream in(text);
223 EXPECT_EQ(pattern.
width, 3u);
224 EXPECT_EQ(pattern.
height, 3u);
228TEST(CAIO, Life106WriterProducesReadableCoordinates)
230 const Grid src = make_grid(3, 3, {{0, 2}, {2, 0}});
231 std::ostringstream
out;
234 std::istringstream in(
out.str());
236 EXPECT_EQ(pattern.
width, 3u);
237 EXPECT_EQ(pattern.
height, 3u);
241TEST(CAIO, Life105RoundTripPreservesFullFrame)
243 const Grid src = make_grid(3, 4, {{0, 0}, {1, 2}, {2, 3}});
244 std::ostringstream
out;
246 EXPECT_NE(
out.str().find(
"#Life 1.05"), std::string::npos);
247 EXPECT_NE(
out.str().find(
"#P 0 0"), std::string::npos);
249 std::istringstream in(
out.str());
251 const Grid dst = make_lattice_from_pattern<Grid>(pattern);
252 expect_same_frame(src, dst);
255TEST(CAIO, Life105ParserMergesNegativeBlocks)
257 const std::string text =
264 std::istringstream in(text);
268 EXPECT_EQ(pattern.
width, 4u);
269 EXPECT_EQ(pattern.
height, 2u);
270 EXPECT_EQ(pattern.
alive,
274TEST(CAIO, CsvRoundTripPreservesNumericSnapshot)
276 const Grid src = make_values_grid();
280 std::ostringstream
out;
282 EXPECT_EQ(
out.str(),
"1;2;3\n4;5;6\n");
284 std::istringstream in(
out.str());
286 const Grid dst = make_lattice_from_snapshot<Grid>(snap);
287 expect_same_frame(src, dst);
290TEST(CAIO, CsvReaderRejectsRaggedRowsAndBadScalars)
293 std::istringstream in(
"1,2\n3\n");
294 EXPECT_THROW(read_csv_snapshot<int>(in), std::domain_error);
297 std::istringstream in(
"1,x\n");
298 EXPECT_THROW(read_csv_snapshot<int>(in), std::domain_error);
302TEST(CAIO, JsonRoundTripPreservesNumericSnapshot)
304 const Grid src = make_values_grid();
309 std::ostringstream
out;
311 EXPECT_NE(
out.str().find(
"\"type\": \"Aleph::CA::frame\""), std::string::npos);
312 EXPECT_NE(
out.str().find(
"\"rule\": \"demo\""), std::string::npos);
314 std::istringstream in(
out.str());
316 const Grid dst = make_lattice_from_snapshot<Grid>(snap);
317 expect_same_frame(src, dst);
320TEST(CAIO, CsvAndJsonWriteByteStatesAsNumbers)
322 ByteGrid frame({1, 3}, std::uint8_t{0});
323 frame.set({0, 0}, std::uint8_t{7});
324 frame.set({0, 1}, std::uint8_t{42});
325 frame.set({0, 2}, std::uint8_t{255});
327 std::ostringstream csv;
329 EXPECT_EQ(csv.str(),
"7,42,255\n");
331 std::ostringstream json;
333 EXPECT_NE(json.str().find(
"\"cells\":[[7,42,255]]"), std::string::npos);
336TEST(CAIO, JsonReaderRejectsShapeMismatch)
338 std::istringstream in(
339 "{\"type\":\"Aleph::CA::frame\",\"rank\":2,\"height\":1,\"width\":2,"
340 "\"cells\":[[1,2],[3,4]]}");
341 EXPECT_THROW(read_json_snapshot<int>(in), std::domain_error);
344TEST(CAIO, JsonFrameStreamWriterEmitsStepTaggedFrames)
346 const Grid frame = make_grid(2, 2, {{0, 0}, {1, 1}});
347 std::ostringstream
out;
354 EXPECT_NE(
out.str().find(
"\"type\": \"Aleph::CA::frame-stream\""), std::string::npos);
355 EXPECT_NE(
out.str().find(
"\"step\": 0"), std::string::npos);
356 EXPECT_NE(
out.str().find(
"\"step\": 4"), std::string::npos);
357 EXPECT_NE(
out.str().find(
"\"cells\":[[1,0],[0,1]]"), std::string::npos);
360TEST(CAIO, PgmBinaryOutputIsBitStable)
362 const Grid frame = make_grid(2, 3, {{0, 1}, {1, 2}});
363 std::ostringstream
out(std::ios::binary);
366 std::string
expected =
"P5\n3 2\n255\n";
367 expected += bytes({255, 0, 255, 255, 255, 0});
371TEST(CAIO, PpmBinaryOutputUsesCustomPalette)
373 const Grid frame = make_grid(1, 2, {{0, 1}});
374 std::ostringstream
out(std::ios::binary);
377 return v == 0 ?
RGB8{1, 2, 3} :
RGB8{250, 0, 7};
380 std::string
expected =
"P6\n2 1\n255\n";
381 expected += bytes({1, 2, 3, 250, 0, 7});
389TEST(CAIO, Life106RejectsAnExtentThatDoesNotFit)
392 std::istringstream too_wide(
"#Life 1.06\n-4611686018427387904 0\n4611686018427387904 0\n");
393 EXPECT_THROW((
void)
read_life_106(too_wide), std::overflow_error);
395 std::istringstream too_tall(
"#Life 1.06\n0 -4611686018427387904\n0 4611686018427387904\n");
396 EXPECT_THROW((
void)
read_life_106(too_tall), std::overflow_error);
398 std::istringstream again(
"#Life 1.06\n-4611686018427387904 0\n4611686018427387904 0\n");
400 ASSERT_FALSE(attempt.has_value());
401 EXPECT_NE(attempt.error().find(
"extent"), std::string::npos);
404 std::istringstream widest(
"#Life 1.06\n-4611686018427387904 0\n4611686018427387903 0\n");
406 EXPECT_EQ(pattern.
width,
407 static_cast<ca_size_t>(std::numeric_limits<ca_index_t>::max()) + 1);
408 EXPECT_EQ(pattern.
height, 1u);
409 EXPECT_EQ(pattern.
origin_col, -4611686018427387904LL);
411 const std::set<std::pair<ca_index_t, ca_index_t>> cells = {
413 const std::set<std::pair<ca_index_t, ca_index_t>>
expected = {
414 {0, 0}, {0, std::numeric_limits<ca_index_t>::max()}};
418TEST(CAIO, Life105RejectsCellsBeyondTheLargestCoordinate)
421 std::istringstream past_end(
"#Life 1.05\n#P 9223372036854775807 0\n**\n");
422 EXPECT_THROW((
void)
read_life_105(past_end), std::overflow_error);
424 std::istringstream past_bottom(
"#Life 1.05\n#P 0 9223372036854775807\n*\n*\n");
425 EXPECT_THROW((
void)
read_life_105(past_bottom), std::overflow_error);
427 std::istringstream again(
"#Life 1.05\n#P 9223372036854775807 0\n**\n");
431 std::istringstream at_end(
"#Life 1.05\n#P 9223372036854775806 0\n**\n");
433 EXPECT_EQ(pattern.
origin_col, std::numeric_limits<ca_index_t>::max() - 1);
434 EXPECT_EQ(pattern.
width, 2u);
438 std::istringstream far_apart(
"#Life 1.05\n#P -9223372036854775807 0\n*\n#P 1 0\n*\n");
439 EXPECT_THROW((
void)
read_life_105(far_apart), std::overflow_error);
442 std::istringstream negative(
"#Life 1.05\n#P -9223372036854775807 -5\n*.*\n");
444 EXPECT_EQ(left.
origin_col, -std::numeric_limits<ca_index_t>::max());
446 EXPECT_EQ(left.
width, 3u);
451TEST(CAIO, RleRunsThatWouldWrapAreRejected)
453 EXPECT_THROW((
void)
read_rle_string(
"x = 5, y = 1\n3o18446744073709551615b2o!"),
455 EXPECT_THROW((
void)
read_rle_string(
"x = 5, y = 1\n3o18446744073709551615o!"),
457 EXPECT_THROW((
void)
read_rle_string(
"x = 1, y = 2\no$18446744073709551615$o!"),
461 EXPECT_THROW((
void)
read_rle_string(
"x = 9223372036854775809, y = 1\n!"),
462 std::overflow_error);
463 EXPECT_THROW((
void)
read_rle_string(
"x = 1, y = 9223372036854775809\n!"),
464 std::overflow_error);
469 "9223372036854775807bo9223372036854775807$9223372036854775807bo!");
470 EXPECT_EQ(largest.
width,
static_cast<ca_size_t>(std::numeric_limits<ca_index_t>::max()) + 1);
473 EXPECT_EQ(largest.
alive(0)[0], 0);
474 EXPECT_EQ(largest.
alive(0)[1], std::numeric_limits<ca_index_t>::max());
475 EXPECT_EQ(largest.
alive(1)[0], std::numeric_limits<ca_index_t>::max());
476 EXPECT_EQ(largest.
alive(1)[1], std::numeric_limits<ca_index_t>::max());
480TEST(CAIO, LoadBinaryPatternRejectsSizesThatWouldWrap)
483 pattern.
height = std::numeric_limits<ca_size_t>::max();
486 Grid grid({4, 4}, 0);
490 pattern.
width = std::numeric_limits<ca_size_t>::max();
496 EXPECT_EQ(grid.at({3, 3}), 1);
size_t size_t int32_t * out
File-format readers and writers for cellular-automata frames.
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
T & append(const T &data)
Append a copy of data
Streaming JSON writer for CA trajectories.
void close()
Finish the stream.
void accept(std::size_t step, const Lattice &frame)
Append one frame to the stream.
Lattice that adds boundary-aware access on top of a storage.
Minimal std::expected-style result type for C++20.
expected< Binary_Cell_Pattern, std::string > try_read_life_106(std::istream &in)
Non-throwing variant of read_life_106.
void write_pgm(std::ostream &out, const Lattice &frame, Mapper &&mapper, const NetPBM_Write_Options &opts={})
Write a binary PGM (P5) image.
void write_life_105(std::ostream &out, const Lattice &frame, const std::string &comment={}, const typename Lattice::state_type &dead=typename Lattice::state_type{})
Write a Life 1.05 file.
Binary_Cell_Pattern read_plaintext_string(const std::string &s)
Read a Life plaintext pattern from a string.
void write_life_106(std::ostream &out, const Lattice &frame, const typename Lattice::state_type &dead=typename Lattice::state_type{})
Write a Life 1.06 file.
std::ptrdiff_t ca_index_t
Signed coordinate component used by lattices and neighborhoods.
expected< RLE_Pattern, std::string > try_read_rle_string(const std::string &s, const ca_size_t max_live_cells=Rle_Default_Max_Live_Cells)
Non-throwing variant of read_rle_string.
std::array< ca_index_t, N > Coord_Vec
Default coordinate vector.
std::string write_plaintext_string(const Lattice &frame, const Plaintext_Write_Options &opts={}, const typename Lattice::state_type &dead=typename Lattice::state_type{})
Return a Life plaintext document.
void write_ppm(std::ostream &out, const Lattice &frame, Mapper &&mapper, const NetPBM_Write_Options &opts={})
Write a binary PPM (P6) image.
Binary_Cell_Pattern read_life_105(std::istream &in)
Read a Life 1.05 pattern.
void write_csv(std::ostream &out, const Lattice &frame, const CSV_Options &opts={})
Write a numeric CSV snapshot.
std::string write_rle_string(const Lattice &frame, const RLE_Write_Options &opts={}, const typename Lattice::state_type &dead=typename Lattice::state_type{})
Return a frame encoded as Conway RLE.
Binary_Cell_Pattern read_life_106(std::istream &in)
Read a Life 1.06 pattern.
void write_json(std::ostream &out, const Lattice &frame, const JSON_Write_Options &opts={})
Write a JSON snapshot.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
RLE_Pattern read_rle_string(const std::string &s, const ca_size_t max_live_cells=Rle_Default_Max_Live_Cells)
Read a Conway RLE pattern from a string.
void load_binary_pattern(const Binary_Cell_Pattern &pattern, Lattice &frame, const typename Lattice::state_type &alive=static_cast< typename Lattice::state_type >(1), const typename Lattice::state_type &dead=typename Lattice::state_type{}, const ca_index_t origin_row=0, const ca_index_t origin_col=0)
Fill a lattice from a binary pattern.
expected< Binary_Cell_Pattern, std::string > try_read_life_105(std::istream &in)
Non-throwing variant of read_life_105.
Binary pattern shared by RLE, plaintext and Life formats.
std::string name
optional pattern name
ca_index_t origin_row
original row offset before normalisation
ca_size_t height
number of rows
ca_size_t width
number of columns
Array< std::string > comments
format comments without marker prefixes
ca_index_t origin_col
original column offset before normalisation
std::string rule
optional rule string, e.g. B3/S23
Array< Coord_Vec< 2 > > alive
live cells as {row, col}
Options controlling CSV output and input.
char delimiter
separator between cells
Dense numeric snapshot used by CSV and JSON readers.
Options controlling JSON snapshot output.
bool pretty
pretty-print with indentation
std::string rule
optional rule field
Out-of-range neighbours behave as if the lattice ended.
Options controlling Life plaintext output.
std::string comment
optional ! comments, split on newlines
RGB byte triplet used by PPM exporters.
Options controlling Conway RLE output.
bool trim_dead_border
crop to the live-cell bounding box
std::string name
optional #N line
Cellular automata lattice with pluggable boundary policies.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).