/// @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 recorded and the test case goes on, so one run shows every /// problem. The program exits with status 1 if any check failed. /// /// Every test case is listed with its result and its time as it finishes. The /// builder passes `--color` and `--live` when the output goes to a terminal; /// then the case that is running is shown on the last line, as `running`, and /// that line is rewritten when the case ends. A failed case is listed again at /// the end with the messages of its failed checks. `--quiet` lists nothing but /// the failures. #pragma once #include #include #include #include #include #include #include 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& cases() { static std::vector 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 The messages of the failed checks of the test case that is running, /// kept to be printed under its name. inline std::string& failure_report() { static std::string text; return text; } /// @brief Records a failed check. inline void fail(const char* file, int line, const std::string& message) { failure_report() += " " + std::string(file) + ":" + std::to_string(line) + ": " + message + "\n"; ++failed_checks(); } /// @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 std::string show(const T& value) { if constexpr (std::is_arithmetic_v) { return std::to_string(value); } else if constexpr (std::is_enum_v) { return std::to_string(static_cast(value)); } else { return "(value cannot be printed)"; } } /// @brief Pads @p text with spaces on the right up to @p width characters. inline std::string pad(const std::string& text, size_t width) { return text.size() >= width ? text : text + std::string(width - text.size(), ' '); } /// @brief Pads @p text with spaces on the left up to @p width characters. inline std::string pad_left(const std::string& text, size_t width) { return text.size() >= width ? text : std::string(width - text.size(), ' ') + text; } /// @brief Formats the time a test case took: `<1 ms`, `12 ms`, `1.50 s`. inline std::string format_time(std::chrono::steady_clock::duration time) { const auto us = std::chrono::duration_cast(time).count(); if (us < 1000) return "<1 ms"; if (us < 1000000) return std::to_string(us / 1000) + " ms"; char buffer[32]; std::snprintf(buffer, sizeof(buffer), "%.2f s", static_cast(us) / 1e6); return buffer; } } // 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 Records a failure, with the line, if @p condition is false. #define CHECK(condition) \ do { \ if (!(condition)) { \ ::kumo_test::fail(__FILE__, __LINE__, "CHECK(" #condition ") failed"); \ } \ } while (0) /// @brief Records 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)) { \ ::kumo_test::fail( \ __FILE__, __LINE__, \ std::string("CHECK_EQ(") + #actual + ", " + #expected + \ ") failed: " + ::kumo_test::show(kumo_test_actual) + \ " != " + ::kumo_test::show(kumo_test_expected)); \ } \ } while (0) /// @brief Runs every registered test case, listing each one as it goes. /// /// @param argc Argument count. /// @param argv `--color` colors the words `ok` and `FAIL`; `--live` shows the /// running case on the last line and rewrites it when it ends; /// `--quiet` lists only the failures. int main(int argc, char** argv) { bool color = false; bool live = false; bool quiet = false; for (int i = 1; i < argc; ++i) { const std::string arg = argv[i]; if (arg == "--color") color = true; if (arg == "--live") live = true; if (arg == "-q" || arg == "--quiet") quiet = true; } const auto paint = [color](const char* code, const std::string& text) { return color ? std::string("\033[") + code + "m" + text + "\033[0m" : text; }; // The names are padded to one width for every program, not to the longest of // each, so that the results line up from one program to the next. A longer // name only moves the column of its own program. A line is then 75 columns // wide, as wide as the rules the builder draws between the programs. constexpr size_t kNameWidth = 60; size_t width = kNameWidth; for (const kumo_test::Case& test : kumo_test::cases()) { width = std::max(width, std::string(test.name).size()); } struct Failure { std::string name; std::string report; }; std::vector failures; for (const kumo_test::Case& test : kumo_test::cases()) { kumo_test::failed_checks() = 0; kumo_test::failure_report().clear(); // The name goes out before the case runs, and is flushed, so that if the // case crashes the last line says which one it was. const std::string label = " " + kumo_test::pad(test.name, width) + " "; if (!quiet) { std::printf("%s%s", label.c_str(), live ? paint("2", "running").c_str() : ""); std::fflush(stdout); } const auto began = std::chrono::steady_clock::now(); test.run(); const std::string took = kumo_test::format_time(std::chrono::steady_clock::now() - began); const bool failed = kumo_test::failed_checks() != 0; if (!quiet) { // On a terminal, erase "running" and write the result in its place. if (live) std::printf("\r\033[2K%s", label.c_str()); std::printf( "%s %s\n", failed ? paint("31", "FAIL").c_str() : paint("32", "ok ").c_str(), paint("2", kumo_test::pad_left(took, 6)).c_str()); std::fflush(stdout); } if (failed) failures.push_back({test.name, kumo_test::failure_report()}); } // The messages come after the list, so that they do not break it up. if (!failures.empty() && !quiet) std::printf("\n"); for (const Failure& failure : failures) { std::printf(" %s %s\n%s", paint("31", "FAIL").c_str(), failure.name.c_str(), failure.report.c_str()); } return failures.empty() ? 0 : 1; }