Kumo

A hand-rolled game engine.

git clone https://git.sentomk.me/Kumo.git
commit 12f45926209df5fe25a2c5a52956173745973ebc
parent a239e22718794310e68a70b4a42a2ff4c649956a
Author: sentomk <sentomk@proton.me>
Date:   Tue, 13 Oct 2026 20:12:42 +0800

test: check the demo picture against a golden hash

demo_scene.h draws a fixed picture that uses every primitive, and
golden_test.cpp compares the hash of the result with a known value. The
hash is the same on every platform, which is the point: it is how pixel
for pixel identical output is checked. The picture is also saved to
build/golden_demo.tga so it can be looked at when the hash changes.

The scene is a header in tests/e2e because the builder compiles only the
*_test files of the tests directory.

Diffstat:
Atests/e2e/demo_scene.h | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atests/e2e/golden_test.cpp | 39+++++++++++++++++++++++++++++++++++++++
2 files changed, 95 insertions(+), 0 deletions(-)

diff --git a/tests/e2e/demo_scene.h b/tests/e2e/demo_scene.h @@ -0,0 +1,56 @@ +/// @file demo_scene.h +/// @brief A fixed picture that exercises every drawing primitive. +/// +/// A test fixture: tests draw it and compare the hash of the result with a +/// known value, which is how "the same pixels on every platform" is checked. +/// It is a header because the builder compiles only the `*_test` files of the +/// tests directory; the functions are `inline` so that every test program can +/// include it. +#pragma once + +#include "render/frame_buffer.h" +#include "render/raster.h" + +namespace kumo_test { + +/// @brief Width of the picture drawn by draw_demo_scene(). +inline constexpr int kDemoWidth = 640; + +/// @brief Height of the picture drawn by draw_demo_scene(). +inline constexpr int kDemoHeight = 360; + +/// @brief Draws the demo picture. +/// +/// Changing anything here changes the expected hash in golden_test.cpp +/// +/// @param framebuffer A buffer of kDemoWidth by kDemoHeight pixels. +inline void draw_demo_scene(kumo::FrameBuffer& framebuffer) { + using kumo::Color; + using kumo::make_color; + using kumo::pixel_point; + using kumo::Point; + + kumo::clear(framebuffer, make_color(24, 26, 38)); + + kumo::fill_rect(framebuffer, 40, 40, 200, 120, make_color(230, 140, 50)); + + // A triangle whose corners are not on pixel boundaries: the values are in + // sixteenths of a pixel. + kumo::fill_triangle(framebuffer, {1004, 700}, {2520, 893}, {1810, 2155}, + make_color(60, 190, 180)); + + // A square cut into two triangles that share the diagonal. + const Point tl = pixel_point(380, 60); + const Point tr = pixel_point(560, 60); + const Point br = pixel_point(560, 240); + const Point bl = pixel_point(380, 240); + kumo::fill_triangle(framebuffer, tl, tr, br, make_color(200, 80, 120)); + kumo::fill_triangle(framebuffer, tl, br, bl, make_color(120, 90, 210)); + + // A fan of lines from one point, reaching past the edge of the buffer. + const Color white = make_color(240, 240, 240); + for (int i = 0; i <= 8; ++i) { + kumo::draw_line(framebuffer, 320, 300, i * 90 - 40, 170, white); + } +} +} // namespace kumo_test diff --git a/tests/e2e/golden_test.cpp b/tests/e2e/golden_test.cpp @@ -0,0 +1,39 @@ +#include <cinttypes> +#include <cstdio> + +#include "check.h" +#include "demo_scene.h" +#include "render/frame_buffer.h" + +using kumo::FrameBuffer; +using kumo_test::draw_demo_scene; +using kumo_test::kDemoHeight; +using kumo_test::kDemoWidth; + +// The hash of the demo picture. It must be the same on every platform, so a +// change here is only right if the picture was changed on purpose. Then run +// `kumoko test golden`, look at the picture in build/golden_demo.tga, and take +// the new value from the "actual hash" line it prints. +constexpr uint64_t kExpectedHash = 0x635d63193c43fe8aull; + +TEST(demo_scene_has_the_expected_hash) { + FrameBuffer frame(kDemoWidth, kDemoHeight); + draw_demo_scene(frame); + + // Always saved, so that the picture can be looked at when the hash changes. + CHECK(kumo::write_tga(frame, "build/golden_demo.tga")); + + const uint64_t hash = kumo::hash_pixels(frame); + if (hash != kExpectedHash) { + std::printf(" actual hash: %016" PRIx64 "\n", hash); + } + CHECK(hash == kExpectedHash); +} + +TEST(drawing_the_scene_twice_gives_the_same_picture) { + FrameBuffer first(kDemoWidth, kDemoHeight); + FrameBuffer second(kDemoWidth, kDemoHeight); + draw_demo_scene(first); + draw_demo_scene(second); + CHECK(first.pixels == second.pixels); +}