48#include <gtest/gtest.h>
74 long long process_id() noexcept
77 return static_cast<long long>(_getpid());
79 return static_cast<long long>(getpid());
83 std::filesystem::path unique_tmp_dir(
const std::string &name)
92 static std::atomic<unsigned long long>
counter{0};
93 const auto tick = std::chrono::steady_clock::now().time_since_epoch().count();
94 return std::filesystem::temp_directory_path()
95 / (name +
"_" + std::to_string(
static_cast<long long>(tick)) +
96 "_" + std::to_string(process_id()) +
97 "_" + std::to_string(
counter++));
100 std::string read_file(
const std::filesystem::path &path,
bool binary =
false)
102 std::ifstream in(path, binary ? std::ios::binary :
std::ios::in);
103 std::ostringstream
out;
109TEST(CATikz, RectangularRendererIncludesCellsGridAndLegend)
111 const Grid frame = make_frame();
119 [](
int v) {
return v == 0 ? std::string(
"green!10") : std::string(
"red!80"); },
122 EXPECT_NE(tikz.find(
"% Aleph::CA::render_rect_lattice_tikz"), std::string::npos);
123 EXPECT_NE(tikz.find(
"\\begin{tikzpicture}"), std::string::npos);
124 EXPECT_NE(tikz.find(
"fill=green!10, draw=blue!20"), std::string::npos);
125 EXPECT_NE(tikz.find(
"fill=red!80, draw=blue!20"), std::string::npos);
126 EXPECT_NE(tikz.find(
"rectangle (1, 0.5)"), std::string::npos);
127 EXPECT_NE(tikz.find(
"{alive}"), std::string::npos);
130TEST(CATikz, RectangularRendererRejectsInvalidCellSize)
140TEST(CATikz, GenericRendererDispatchesToHexAndTriangularRenderers)
142 HexGrid hex({2, 2}, 0);
143 hex.set_axial({1, 0}, 1);
146 EXPECT_NE(hex_tikz.find(
"% Aleph::CA::render_hex_lattice_tikz"), std::string::npos);
147 EXPECT_NE(hex_tikz.find(
"regular polygon sides=6"), std::string::npos);
148 EXPECT_NE(hex_tikz.find(
"fill=orange"), std::string::npos);
150 TriGrid tri({2, 2}, 0);
151 tri.set_tri({1, 1}, 1);
154 EXPECT_NE(tri_tikz.find(
"% Aleph::CA::render_triangular_lattice_tikz"), std::string::npos);
155 EXPECT_NE(tri_tikz.find(
"regular polygon sides=3"), std::string::npos);
156 EXPECT_NE(tri_tikz.find(
"fill=cyan"), std::string::npos);
159TEST(CAFrameStream, DirectorySinkWritesStrideFilteredPgmAndManifest)
161 const auto dir = unique_tmp_dir(
"aleph_ca_stream_pgm");
162 std::filesystem::remove_all(dir);
167 opts.
format = Frame_Output_Format::PGM;
172 const Grid frame = make_frame();
173 sink.accept(0, frame);
174 sink.accept(1, frame);
175 sink.accept(2, frame);
178 ASSERT_EQ(sink.written_frames().size(), 2u);
179 const auto p0 = dir /
"gol_000.pgm";
180 const auto p2 = dir /
"gol_002.pgm";
181 EXPECT_TRUE(std::filesystem::exists(p0));
182 EXPECT_FALSE(std::filesystem::exists(dir /
"gol_001.pgm"));
183 EXPECT_TRUE(std::filesystem::exists(p2));
184 EXPECT_EQ(read_file(p0,
true).rfind(
"P5\n3 2\n255\n", 0), 0u);
186 const std::string manifest = read_file(dir /
"manifest.csv");
187 EXPECT_NE(manifest.find(
"step,format,path"), std::string::npos);
188 EXPECT_NE(manifest.find(
"0,pgm,"), std::string::npos);
189 EXPECT_NE(manifest.find(
"2,pgm,"), std::string::npos);
191 std::filesystem::remove_all(dir);
194TEST(CAFrameStream, DirectorySinkDispatchesTextFormats)
196 const auto dir = unique_tmp_dir(
"aleph_ca_stream_text");
197 std::filesystem::remove_all(dir);
198 const Grid frame = make_frame();
204 const char *filename;
208 const Case cases[] = {
209 {Frame_Output_Format::RLE,
"rle",
"rle_000001.rle",
"x = 3, y = 2"},
210 {Frame_Output_Format::CSV,
"csv",
"csv_000001.csv",
"0,1,0"},
211 {Frame_Output_Format::JSON,
"json",
"json_000001.json",
"\"cells\":[[0,1,0],[0,0,1]]"},
212 {Frame_Output_Format::TikZ,
"tikz",
"tikz_000001.tex",
"\\begin{tikzpicture}"}
215 for (
const auto &tc : cases)
222 sink.accept(1, frame);
225 const auto path = dir / tc.filename;
226 ASSERT_TRUE(std::filesystem::exists(path)) << path;
227 EXPECT_NE(read_file(path).
find(tc.needle), std::string::npos) << path;
230 std::filesystem::remove_all(dir);
233TEST(CAFrameStream, DirectorySinkRejectsZeroStrideAndBuildsCommandHint)
235 const auto dir = unique_tmp_dir(
"aleph_ca_stream_invalid");
243 opts.prefix =
"frame";
244 opts.format = Frame_Output_Format::PPM;
247 const std::string
cmd = sink.montage_command_hint(
"movie.mp4", 12);
248 EXPECT_NE(
cmd.find(
"ffmpeg -y -framerate 12"), std::string::npos);
249 EXPECT_NE(
cmd.find(
"frame_%04d.ppm"), std::string::npos);
250 EXPECT_NE(
cmd.find(
"movie.mp4"), std::string::npos);
252 std::filesystem::remove_all(dir);
size_t size_t int32_t * out
Directory-backed frame sinks for CA trajectories.
TikZ renderers for cellular-automata frames.
Directory-backed sink for CA frames.
Hexagonal lattice with axial accessors over a 2D storage.
Lattice that adds boundary-aware access on top of a storage.
Triangular lattice with parity helpers over a 2D storage.
Frame_Output_Format
File format used by Directory_Frame_Sink.
void render_rect_lattice_tikz(std::ostream &out, const Lattice &frame, Palette &&palette, const Rect_Tikz_Options &opts={})
Render a rectangular 2D frame as a TikZ picture.
void render_lattice_tikz(std::ostream &out, const Lattice &frame, Palette &&palette, const Rect_Tikz_Options &opts={})
Render any supported 2D CA lattice as TikZ.
static void prefix(Node *root, DynList< Node * > &acc)
Itor find(const Itor &beg, const Itor &end, const T &value)
Find the first element equal to a value.
Default binary TikZ palette.
Options for directory-backed frame streams.
std::filesystem::path directory
destination directory
Frame_Output_Format format
exporter format
std::string prefix
filename prefix
std::size_t stride
write every Nth step
std::size_t zero_pad
step field width in filenames
Out-of-range neighbours behave as if the lattice ended.
TikZ rendering options for rectangular frames.
double cell_size
cell side length in centimetres
std::string grid_colour
TikZ colour used for grid lines.
bool include_legend
append a compact alive/dead legend
Hexagonal lattice with axial / offset / cube coordinate conversions and a TikZ-compatible pixel mappi...
Cellular automata lattice with pluggable boundary policies.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).
Triangular lattice with up/down parity helpers and pixel mapping suitable for visualisation.