Kumo

A hand-rolled game engine.

git clone https://git.sentomk.me/Kumo.git
commit 6725e0fcab4f57bd89bd26f172167090e79e70a3
parent 58b1a8f5893b418282cb3512daa326e3a7062395
Author: sentomk <sentomk@proton.me>
Date:   Fri,  9 Oct 2026 17:16:42 +0800

test: add a minimal test framework and frame buffer tests

tests/check.h provides TEST, CHECK and CHECK_EQ and defines main(), since
every test file is a program of its own. frame_buffer_test.cpp covers the
row stride on a buffer whose width and height differ, which would have
caught the width/height mix-up in FrameBuffer::at(), bounds checks, and
what the pixel hash depends on.

Diffstat:
Atests/check.h | 110+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atests/frame_buffer_test.cpp | 40++++++++++++++++++++++++++++++++++++++++
2 files changed, 150 insertions(+), 0 deletions(-)

diff --git a/tests/check.h b/tests/check.h @@ -0,0 +1,110 @@ +/// @file check.h +/// @brief A minimal test framework: TEST cases, CHECK and CHECK_EQ. +/// +/// Each test file is a program of its own and includes this header exactly +/// once. The header defines `main`, which runs every TEST of the file in the +/// order they are written, so a test file looks like this: +/// @code +/// #include "check.h" +/// +/// TEST(addition) { +/// CHECK_EQ(1 + 1, 2); +/// } +/// @endcode +/// A failed check is reported and the test case goes on, so one run shows every +/// problem. The program exits with status 1 if any check failed. +#pragma once + +#include <cstddef> +#include <cstdio> +#include <string> +#include <type_traits> +#include <vector> + +namespace kumo_test { + +/// @brief One test case: a name and the function that runs it. +struct Case { + const char* name; + void (*run)(); +}; + +/// @brief Every test case of the program, filled in before `main` starts. +inline std::vector<Case>& cases() { + static std::vector<Case> list; + return list; +} + +/// @brief The number of failed checks in the test case that is running. +inline int& failed_checks() { + static int count = 0; + return count; +} + +/// @brief Adds a test case to cases() when a global of this type is created. +struct Registrar { + Registrar(const char* name, void (*run)()) { cases().push_back({name, run}); } +}; + +/// @brief Turns a value into text for a failure message. +template <typename T> +std::string show(const T& value) { + if constexpr (std::is_arithmetic_v<T>) { + return std::to_string(value); + } else if constexpr (std::is_enum_v<T>) { + return std::to_string(static_cast<long long>(value)); + } else { + return "(value cannot be printed)"; + } +} + +} // namespace kumo_test + +/// @brief Defines a test case and registers it; the body follows the macro. +#define TEST(name) \ + static void name(); \ + static ::kumo_test::Registrar kumo_test_registrar_##name(#name, name); \ + static void name() + +/// @brief Reports a failure, with the line, if @p condition is false. +#define CHECK(condition) \ + do { \ + if (!(condition)) { \ + std::printf(" %s:%d: CHECK(%s) failed\n", __FILE__, __LINE__, \ + #condition); \ + ++::kumo_test::failed_checks(); \ + } \ + } while (0) + +/// @brief Reports a failure, with both values, if @p actual != @p expected. +/// +/// Both arguments are evaluated once. +#define CHECK_EQ(actual, expected) \ + do { \ + const auto& kumo_test_actual = (actual); \ + const auto& kumo_test_expected = (expected); \ + if (!(kumo_test_actual == kumo_test_expected)) { \ + std::printf(" %s:%d: CHECK_EQ(%s, %s) failed: %s != %s\n", __FILE__, \ + __LINE__, #actual, #expected, \ + ::kumo_test::show(kumo_test_actual).c_str(), \ + ::kumo_test::show(kumo_test_expected).c_str()); \ + ++::kumo_test::failed_checks(); \ + } \ + } while (0) + +/// @brief Runs every registered test case and reports the result. +int main() { + int failed_cases = 0; + for (const kumo_test::Case& test : kumo_test::cases()) { + kumo_test::failed_checks() = 0; + test.run(); + if (kumo_test::failed_checks() == 0) { + std::printf(" ok %s\n", test.name); + } else { + std::printf(" FAIL %s (%d checks failed)\n", test.name, + kumo_test::failed_checks()); + ++failed_cases; + } + } + return failed_cases == 0 ? 0 : 1; +} diff --git a/tests/frame_buffer_test.cpp b/tests/frame_buffer_test.cpp @@ -0,0 +1,40 @@ +#include "render/frame_buffer.h" + +#include "check.h" + +using kumo::FrameBuffer; + +// A buffer whose width and height differ, so that a row stride taken from the +// wrong one lands on the wrong pixel. +TEST(at_uses_the_width_as_row_stride) { + FrameBuffer frame(5, 3); + frame.at(4, 0) = 1; + frame.at(0, 1) = 2; + frame.at(4, 2) = 3; + CHECK_EQ(frame.pixels[4], 1u); // row 0, last column + CHECK_EQ(frame.pixels[5], 2u); // row 1, first column + CHECK_EQ(frame.pixels[14], 3u); // the last pixel +} + +TEST(contains_checks_both_sides) { + const FrameBuffer frame(5, 3); + CHECK(frame.contains(0, 0)); + CHECK(frame.contains(4, 2)); + CHECK(!frame.contains(5, 0)); + CHECK(!frame.contains(0, 3)); + CHECK(!frame.contains(-1, 0)); + CHECK(!frame.contains(0, -1)); +} + +TEST(hash_depends_on_pixels_and_on_shape) { + FrameBuffer a(4, 2); + FrameBuffer b(4, 2); + CHECK(kumo::hash_pixels(a) == kumo::hash_pixels(b)); + + b.at(3, 1) = 1; + CHECK(kumo::hash_pixels(a) != kumo::hash_pixels(b)); + + // The same eight zero pixels, arranged differently. + const FrameBuffer tall(2, 4); + CHECK(kumo::hash_pixels(a) != kumo::hash_pixels(tall)); +}