31#include <gtest/gtest.h>
49 Grid make_frame(
const int phase = 0)
68 long long process_id() noexcept
71 return static_cast<long long>(_getpid());
73 return static_cast<long long>(getpid());
77 std::filesystem::path unique_tmp_dir(
const std::string &name)
86 static std::atomic<unsigned long long>
counter{0};
87 const auto tick = std::chrono::steady_clock::now().time_since_epoch().count();
88 return std::filesystem::temp_directory_path()
89 / (name +
"_" + std::to_string(
static_cast<long long>(tick)) +
90 "_" + std::to_string(process_id()) +
91 "_" + std::to_string(
counter++));
94 std::string read_file(
const std::filesystem::path &path,
const bool binary =
false)
96 std::ifstream in(path, binary ? std::ios::binary :
std::ios::in);
97 EXPECT_TRUE(in.is_open()) <<
"read_file: cannot open '" << path.string() <<
"'";
98 std::ostringstream
out;
103 std::size_t count_byte(
const std::string &s,
const unsigned char byte)
105 std::size_t
count = 0;
106 for (
const unsigned char c : s)
113TEST(CAGif, WritesAnimatedGifWithOneImagePerAcceptedFrame)
115 const auto dir = unique_tmp_dir(
"aleph_ca_gif");
116 std::filesystem::remove_all(dir);
117 const auto path = dir /
"blinker.gif";
124 gif.
accept(0, make_frame(0));
125 gif.accept(1, make_frame(1));
128 const std::string bytes = read_file(path,
true);
129 ASSERT_GE(bytes.size(), 16u);
130 EXPECT_EQ(bytes.substr(0, 6),
"GIF89a");
131 EXPECT_EQ(count_byte(bytes, 0x2c), 2u);
132 EXPECT_EQ(bytes.back(),
';');
134 std::filesystem::remove_all(dir);
137TEST(CAHtmlPlayer, WritesSelfContainedCanvasPlayer)
139 const auto dir = unique_tmp_dir(
"aleph_ca_html");
140 std::filesystem::remove_all(dir);
141 const auto path = dir /
"player.html";
144 opts.
title =
"Blinker demo";
149 html.
accept(0, make_frame(0));
150 html.accept(1, make_frame(1));
153 const std::string text = read_file(path);
154 EXPECT_NE(text.find(
"<canvas id=\"view\""), std::string::npos);
155 EXPECT_NE(text.find(
"const frames = ["), std::string::npos);
156 EXPECT_NE(text.find(
"\"step\":0"), std::string::npos);
157 EXPECT_EQ(text.find(
"http://"), std::string::npos);
158 EXPECT_EQ(text.find(
"https://"), std::string::npos);
160 std::filesystem::remove_all(dir);
163TEST(CAFfmpegSink, CanBeConfiguredAsNoOpWhenRuntimeEncoderIsDisabled)
170 EXPECT_FALSE(sink.active());
171 EXPECT_NE(sink.command().find(
"ffmpeg -y"), std::string::npos);
172 EXPECT_NE(sink.command().find(
"movie.mp4"), std::string::npos);
173 sink.accept(0, make_frame());
177TEST(CAX11Viewer, DisabledViewerIsSafeInHeadlessTests)
182 EXPECT_FALSE(viewer.active());
183 viewer.accept(0, make_frame());
185 EXPECT_FALSE(viewer.active());
188TEST(CACompositeSink, FansOutToSeveralBackends)
190 const auto dir = unique_tmp_dir(
"aleph_ca_composite");
191 std::filesystem::remove_all(dir);
201 composite.accept(0, make_frame(0));
202 composite.accept(1, make_frame(1));
205 EXPECT_TRUE(std::filesystem::exists(dir /
"frame_00.png"));
206 EXPECT_TRUE(std::filesystem::exists(dir /
"frame_00.npy"));
207 EXPECT_TRUE(std::filesystem::exists(dir /
"trajectory.html"));
208 EXPECT_NE(read_file(dir /
"trajectory.html").
find(
"\"step\":1"), std::string::npos);
210 std::filesystem::remove_all(dir);
size_t size_t int32_t * out
Runtime ffmpeg sink for publication-quality CA video.
Directory-backed frame sinks for CA trajectories.
Animated GIF sink for cellular-automata trajectories.
Self-contained HTML trajectory player for CA frames.
NumPy .npy writer for CA frames.
Dependency-free PNG frame sink for cellular automata.
Optional live X11 viewer sink for CA simulations.
Fan-out sink that forwards every frame to several sinks.
Frame sink that streams PPM frames into ffmpeg.
Sink that buffers frames and writes an animated GIF on flush.
void accept(const std::size_t step, const Lattice &frame)
Collect one frame.
Sink that writes a self-contained HTML playback file.
void accept(const std::size_t step, const Lattice &frame)
Append one frame to the embedded trajectory.
Lattice that adds boundary-aware access on top of a storage.
Path-pattern sink that writes one .npy file per frame.
Path-pattern sink that writes one PNG file per accepted frame.
Itor find(const Itor &beg, const Itor &end, const T &value)
Find the first element equal to a value.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Default binary RGB mapper.
Options for Ffmpeg_Frame_Sink.
bool auto_start
start ffmpeg in the constructor
Options for animated GIF output.
unsigned delay_cs
frame delay in centiseconds
Options for generated HTML players.
unsigned cell_size
canvas pixel size per CA cell
std::string title
document title
Out-of-range neighbours behave as if the lattice ended.
Options for the live X11 viewer.
bool enabled
disable to force no-op behaviour
Cellular automata lattice with pluggable boundary policies.
Dense, contiguous storage for cellular automata cells (1D/2D/3D).