From b24635f4a69f81e40e6b73622bd35e2d058ffa96 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Mon, 19 Feb 2024 15:49:22 +0100 Subject: [PATCH] Replace lolunit.h with doctest, found under --- .gitmodules | 8 +- README.md | 9 +- include/lol/3rdparty/doctest | 1 + include/lol/{unit_test => lib/doctest} | 10 +- include/lol/lib/doctest_main | 21 ++ include/lol/private/lolunit.h | 436 ------------------------- 6 files changed, 42 insertions(+), 443 deletions(-) create mode 160000 include/lol/3rdparty/doctest rename include/lol/{unit_test => lib/doctest} (65%) create mode 100644 include/lol/lib/doctest_main delete mode 100644 include/lol/private/lolunit.h diff --git a/.gitmodules b/.gitmodules index 3ca86c86..7c726ab1 100644 --- a/.gitmodules +++ b/.gitmodules @@ -32,8 +32,14 @@ update = checkout upstream = https://github.com/CLIUtils/CLI11 -[submodule "include/lol/3rdparty/fmt"] +[submodule "fmt"] path = include/lol/3rdparty/fmt url = https://github.com/fmtlib/fmt branch = master update = checkout + +[submodule "doctest"] + path = include/lol/3rdparty/doctest + url = https://github.com/doctest/doctest + branch = master + update = checkout diff --git a/README.md b/README.md index abf16e83..bc871792 100644 --- a/README.md +++ b/README.md @@ -31,14 +31,12 @@ The header-only part of the Lol Engine framework. | `` | ensure `std::format` is available, using a fallback if necessary | | | `` | simple message logging | ● `lol::msg::info("hello\n");`
● `lol::msg::debug("%d %d\n", x, y);`
● also `lol::msg::error`, `lol::msg::warn` | | `` | threading and timing | ● `lol::thread`
● `lol::queue` (thread-safe FIFO queue)
● `lol::timer` (high precision timer) | -| `` | unit test framework | | | `` | various utilities: environment variables, std::map and std::vector extensions… | | ## Text utilities | header | description | examples | |--------|-------------|----------| -| `` | the PEGTL parser (imported from [taocpp/pegtl](https://github.com/taocpp/PEGTL)) | | | `` | suffix array for fast string searches (imported from [storm-ptr/step](https://github.com/storm-ptr/step)) | | ## Graphics @@ -48,3 +46,10 @@ The header-only part of the Lol Engine framework. | `` | colorspace conversions (RGB, sRGB, HSV…) | | | `` | image loading, saving, and processing | | +## External libraries + +| header | description | +|--------|-------------| +| `` | the PEGTL parser (imported from [taocpp/pegtl](https://github.com/taocpp/PEGTL)) | +| `` | the doctest unit test framework (imported from [doctest/doctest](https://github.com/doctest/doctest)) | +| `` | a version of doctest that implements `main()` | diff --git a/include/lol/3rdparty/doctest b/include/lol/3rdparty/doctest new file mode 160000 index 00000000..ae7a1353 --- /dev/null +++ b/include/lol/3rdparty/doctest @@ -0,0 +1 @@ +Subproject commit ae7a13539fb71f270b87eb2e874fbac80bc8dda2 diff --git a/include/lol/unit_test b/include/lol/lib/doctest similarity index 65% rename from include/lol/unit_test rename to include/lol/lib/doctest index f6c5c7d5..7700599e 100644 --- a/include/lol/unit_test +++ b/include/lol/lib/doctest @@ -1,7 +1,7 @@ // // Lol Engine // -// Copyright © 2010—2020 Sam Hocevar +// Copyright © 2010–2024 Sam Hocevar // // Lol Engine is free software. It comes without any warranty, to // the extent permitted by applicable law. You can redistribute it @@ -12,7 +12,9 @@ #pragma once -#include "private/push_macros.h" -#include "private/lolunit.h" -#include "private/pop_macros.h" +// +// The doctest library +// ——————————————————— +// +#include "../3rdparty/doctest/doctest/doctest.h" diff --git a/include/lol/lib/doctest_main b/include/lol/lib/doctest_main new file mode 100644 index 00000000..6fdd14c1 --- /dev/null +++ b/include/lol/lib/doctest_main @@ -0,0 +1,21 @@ +// +// Lol Engine +// +// Copyright © 2010–2024 Sam Hocevar +// +// Lol Engine is free software. It comes without any warranty, to +// the extent permitted by applicable law. You can redistribute it +// and/or modify it under the terms of the Do What the Fuck You Want +// to Public License, Version 2, as published by the WTFPL Task Force. +// See http://www.wtfpl.net/ for more details. +// + +#pragma once + +// +// The doctest library +// ——————————————————— +// + +#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN +#include "../3rdparty/doctest/doctest/doctest.h" diff --git a/include/lol/private/lolunit.h b/include/lol/private/lolunit.h deleted file mode 100644 index 26d2dc20..00000000 --- a/include/lol/private/lolunit.h +++ /dev/null @@ -1,436 +0,0 @@ -// -// Lol Engine — Unit test implementation -// -// Copyright © 2010—2020 Sam Hocevar -// -// Lol Engine is free software. It comes without any warranty, to -// the extent permitted by applicable law. You can redistribute it -// and/or modify it under the terms of the Do What the Fuck You Want -// to Public License, Version 2, as published by the WTFPL Task Force. -// See http://www.wtfpl.net/ for more details. -// - -#pragma once - -// -// The Unit test framework -// ——————————————————————— -// - -#include // std::cout -#include // std::setprecision -#include // std::stringstream -#include // std::string -#include // std::fabs - -namespace lol -{ - -/* - * This is the base class for all fixtures. It keeps track of all - * fixtures registered through the lolunit_declare_fixture macro and puts them - * in a linked list. - */ -class FixtureBase -{ - friend class text_runner; - -public: - virtual void setup(void) {}; - virtual void teardown(void) {}; - -protected: - FixtureBase() : m_next(NULL), m_testcases(0), m_failcases(0) {} - virtual ~FixtureBase() {} - - static void AddFixture(FixtureBase *fixture) - { - /* Protect against several instances of the same Fixture subclass */ - if (fixture->MarkFixture()) - return; - FixtureListHelper(fixture); - } - static FixtureBase *FixtureList() { return FixtureListHelper(NULL); } - - virtual void RunFixture() = 0; - virtual bool MarkFixture() = 0; - - /* Prevent compiler complaints about unreachable code */ - static inline bool True() { return true; } - - FixtureBase *m_next; - int m_testcases, m_failcases; - int m_asserts, m_failure; - char const *m_fixturename, *m_currentname; - std::stringstream m_errorlog, m_context; - -private: - /* The FixtureBase class keeps track of all instanciated children - * fixtures through this method. */ - static FixtureBase *FixtureListHelper(FixtureBase *set) - { - static FixtureBase *head = NULL, *tail = NULL; - - if (set) - { - if (!head) head = set; - if (tail) tail->m_next = set; - tail = set; - } - return head; - } -}; - -/* - * This template specialises FixtureBase and provides registration of - * test cases in a linked list through the lolunit_declare_test macro. - */ -template class Fixture : protected FixtureBase -{ -public: - typedef T FixtureClass; - - struct TestCase - { - void (FixtureClass::* m_fun)(); - char const *m_testname; - TestCase *m_next; - - protected: - static inline std::string make_msg(std::string const str) - { - return "- " + str + "\n"; - } - }; - - Fixture() - { - AddFixture(this); - } - - /* Run all test cases in this fixture. */ - virtual void RunFixture() - { - m_errorlog.str(""); - m_testcases = 0; - m_failcases = 0; - for (TestCase *c = TestCaseList(); c; c = c->m_next) - { - m_testcases++; - m_asserts = 0; - m_failure = false; - m_currentname = c->m_testname; - m_context.str(""); - - std::cout << '.'; - (static_cast(this)->*c->m_fun)(); - if (m_failure) std::cout << 'F'; - } - } - - /* Mark the current fixture type as already registered and return whether - * it was seen before. */ - virtual bool MarkFixture() - { - static bool seen = false; - if (seen) - { - SealFixture(); - return true; - } - seen = true; - return false; - } - - /* Manage Fixture sealing. Once SealFixture() has been called, we - * will no longer accept TestCase registrations. */ - static void SealFixture() { SealFixtureHelper(true); } - static bool IsFixtureSealed() { return SealFixtureHelper(false); } - - /* Each Fixture class specialisation keeps track of its instanciated - * test cases. */ - static void AddTestCase(TestCase *that, char const *name, - void (FixtureClass::*fun)()) - { - if (IsFixtureSealed()) - return; - - that->m_fun = fun; - that->m_testname = name; - that->m_next = NULL; - TestCaseListHelper(that); - } - static TestCase *TestCaseList() { return TestCaseListHelper(NULL); } - -private: - static bool SealFixtureHelper(bool set) - { - static bool sealed = false; - if (set) sealed = true; - return sealed; - } - - static TestCase *TestCaseListHelper(TestCase *set) - { - static TestCase *head = NULL, *tail = NULL; - if (set) - { - if (!head) head = set; - if (tail) tail->m_next = set; - tail = set; - } - return head; - } -}; - -/* - * This simple class runs all automatically registered tests and reports - * on error and success in the standard output. - */ -class text_runner -{ -public: - bool Run() - { - bool ret = true; - std::stringstream errors(""); - int failcases = 0, testcases = 0; - - for (FixtureBase *f = FixtureBase::FixtureList(); f; f = f->m_next) - { - f->setup(); - f->RunFixture(); - f->teardown(); - - errors << f->m_errorlog.str(); - testcases += f->m_testcases; - failcases += f->m_failcases; - } - std::cout << "\n"; - - std::stringstream summary; - summary << "\n\n"; - if (failcases) - { - summary << "!!!FAILURES!!!\n"; - summary << "Test Results:\n"; - summary << "Run: " << testcases - << " Failures: " << failcases - << " Errors: 0\n"; /* TODO: handle errors */ - summary << errors.str(); - - ret = false; - } - else - { - summary << "OK (" << testcases << " tests)\n"; - } - summary << "\n\n"; - - std::cout << summary.str(); - - return ret; - } -}; - -#define lolunit_assert_generic(msg, cond) \ - do { \ - m_asserts++; \ - if (True() && !(cond)) \ - { \ - m_errorlog << "\n\n"; \ - m_errorlog << ++m_failcases << ") test: " \ - << lol_unit_helper_name(this) << "::" << m_currentname \ - << " (F) line: " << __LINE__ << " " \ - << __FILE__ << "\n"; \ - m_errorlog << "assertion failed\n"; \ - m_errorlog << "- Expression: " << #cond << "\n"; \ - m_errorlog << msg; \ - m_failure = true; \ - return; \ - } \ - } while (!True()) - -#define lolunit_assert_op(op, modifier, opdesc, msg, a, b) \ - do { \ - m_asserts++; \ - if (True() && !modifier((a) op (b))) \ - { \ - m_errorlog << "\n\n"; \ - m_errorlog << ++m_failcases << ") test: " \ - << lol_unit_helper_name(this) << "::" << m_currentname \ - << " (F) line: " << __LINE__ << " " \ - << __FILE__ << "\n"; \ - m_errorlog << opdesc << " assertion failed\n"; \ - m_errorlog << "- Expected: " << #a << " = " << (a) << "\n"; \ - m_errorlog << "- Actual : " << #b << " = " << (b) << "\n"; \ - m_errorlog << msg; \ - m_errorlog << m_context.str(); \ - m_failure = true; \ - return; \ - } \ - } while (!True()) - -#define lolunit_assert_doubles_equal_generic(msg, a, b, t) \ - do { \ - m_asserts++; \ - using std::fabs; \ - if (True() && fabs(double(a) - double(b)) > fabs(double(t))) \ - { \ - m_errorlog << "\n\n"; \ - m_errorlog << ++m_failcases << ") test: " \ - << lol_unit_helper_name(this) << "::" << m_currentname \ - << " (F) line: " << __LINE__ << " " \ - << __FILE__ << "\n"; \ - m_errorlog << "double equality assertion failed\n"; \ - std::streamsize old_prec = m_errorlog.precision(); \ - m_errorlog << std::setprecision(16); \ - m_errorlog << "- Expected: " << #a << " = " << (a) << "\n"; \ - m_errorlog << "- Actual : " << #b << " = " << (b) << "\n"; \ - m_errorlog << "- Delta : " << (t) << "\n"; \ - m_errorlog << std::setprecision(old_prec); \ - m_errorlog << msg; \ - m_errorlog << m_context.str(); \ - m_failure = true; \ - return; \ - } \ - } while (!True()) - -/* - * Public helper macros - */ - -#define lolunit_declare_fixture(N) \ - class N; \ - /* This pattern allows us to statically create a Fixture instance \ - * before its exact implementation was defined. */ \ - template struct lol_unit_helper_fixture_##N \ - { \ - lol_unit_helper_fixture_##N() { p = new T(); } \ - ~lol_unit_helper_fixture_##N() { delete p; } \ - T *p; \ - }; \ - lol_unit_helper_fixture_##N lol_unit_helper_fixture_##N##_instance; \ - /* Allow to retrieve the class name without using RTTI and without \ - * knowing the type of "this". */ \ - static inline char const *lol_unit_helper_name(N *p) \ - { \ - (void)p; \ - return #N; \ - } \ - /* Now the user can define the implementation */ \ - class N : public lol::Fixture - -#define lolunit_declare_test(N) \ - /* For each test in the fixture, we create an object that will \ - * automatically register the test method in a list global to the \ - * specialised fixture. */ \ - class lol_unit_helper_test_##N : public TestCase \ - { \ - public: \ - lol_unit_helper_test_##N() \ - { \ - (void)lol_unit_helper_name(nullptr); \ - AddTestCase(this, #N, \ - (void (FixtureClass::*)()) &FixtureClass::N); \ - } \ - }; \ - lol_unit_helper_test_##N lol_unit_helper_test_instance_##N; \ - void N() - -/* - * Provide context for error messages - */ - -#define lolunit_set_context(n) \ - do { \ - m_context.str(""); \ - m_context << "- Context : " << #n << " = " << (n) << "\n"; \ - } while (!True()) - -#define lolunit_unset_context(n) \ - m_context.str("") - -/* - * Public assert macros - */ - -#define lolunit_fail(msg) \ - do { \ - m_asserts++; \ - m_errorlog << "\n\n"; \ - m_errorlog << ++m_failcases << ") test: " \ - << lol_unit_helper_name(this) << "::" << m_currentname \ - << " (F) line: " << __LINE__ << " " \ - << __FILE__ << "\n"; \ - m_errorlog << "forced failure\n"; \ - m_errorlog << make_msg(msg); \ - m_errorlog << m_context.str(); \ - m_failure = true; \ - return; \ - } while (!True()) - -#define lolunit_assert(cond) \ - lolunit_assert_generic("", cond) -#define lolunit_assert_message(m, cond) \ - lolunit_assert_generic(make_msg(m), cond) - - -#define lolunit_assert_equal(a, b) \ - lolunit_assert_op(==, (bool), "equality", "", a, b) -#define lolunit_assert_equal_message(m, a, b) \ - lolunit_assert_op(==, (bool), "equality", make_msg(m), a, b) -#define lolunit_assert_different(a, b) \ - lolunit_assert_op(!=, (bool), "inequality", "", a, b) -#define lolunit_assert_different_message(m, a, b) \ - lolunit_assert_op(!=, (bool), "inequality", make_msg(m), a, b) -#define lolunit_assert_less(a, b) \ - lolunit_assert_op(<, (bool), "less than", "", a, b) -#define lolunit_assert_less_message(m, a, b) \ - lolunit_assert_op(<, (bool), "less than", make_msg(m), a, b) -#define lolunit_assert_lequal(a, b) \ - lolunit_assert_op(<=, (bool), "less than or equal", "", a, b) -#define lolunit_assert_lequal_message(m, a, b) \ - lolunit_assert_op(<=, (bool), "less than or equal", make_msg(m), a, b) -#define lolunit_assert_greater(a, b) \ - lolunit_assert_op(>, (bool), "greater than", "", a, b) -#define lolunit_assert_greater_message(m, a, b) \ - lolunit_assert_op(>, (bool), "greater than", make_msg(m), a, b) -#define lolunit_assert_gequal(a, b) \ - lolunit_assert_op(>=, (bool), "greater than or equal", "", a, b) -#define lolunit_assert_gequal_message(m, a, b) \ - lolunit_assert_op(>=, (bool), "greater than or equal", make_msg(m), a, b) - - -#define lolunit_refute_equal(a, b) \ - lolunit_assert_op(==, !, "not equality", "", a, b) -#define lolunit_refute_equal_message(m, a, b) \ - lolunit_assert_op(==, !, "not equality", make_msg(m), a, b) -#define lolunit_refute_different(a, b) \ - lolunit_assert_op(!=, !, "not inequality", "", a, b) -#define lolunit_refute_different_message(m, a, b) \ - lolunit_assert_op(!=, !, "not inequality", make_msg(m), a, b) -#define lolunit_refute_less(a, b) \ - lolunit_assert_op(<, !, "not less than", "", a, b) -#define lolunit_refute_less_message(m, a, b) \ - lolunit_assert_op(<, !, "not less than", make_msg(m), a, b) -#define lolunit_refute_lequal(a, b) \ - lolunit_assert_op(<=, !, "not less than or equal", "", a, b) -#define lolunit_refute_lequal_message(m, a, b) \ - lolunit_assert_op(<=, !, "not less than or equal", make_msg(m), a, b) -#define lolunit_refute_greater(a, b) \ - lolunit_assert_op(>, !, "not greater than", "", a, b) -#define lolunit_refute_greater_message(m, a, b) \ - lolunit_assert_op(>, !, "not greater than", make_msg(m), a, b) -#define lolunit_refute_gequal(a, b) \ - lolunit_assert_op(>=, !, "not greater than or equal", "", a, b) -#define lolunit_refute_gequal_message(m, a, b) \ - lolunit_assert_op(>=, !, "not greater than or equal", make_msg(m), a, b) - -#define lolunit_assert_doubles_equal(a, b, t) \ - lolunit_assert_doubles_equal_generic("", a, b, t) -#define lolunit_assert_doubles_equal_message(msg, a, b, t) \ - lolunit_assert_doubles_equal_generic(make_msg(msg), a, b, t) - -} // namespace lol -