From ae4cd1ebf18789b31cf01e465c0702b029c98d38 Mon Sep 17 00:00:00 2001 From: Lars Mueller Date: Fri, 7 Jun 2024 17:58:36 +0200 Subject: [PATCH] Corresponding code changes --- lib/catch2/CMakeLists.txt | 7 ++--- lib/catch2/catch_amalgamated.cpp | 4 +-- src/CMakeLists.txt | 4 +++ src/benchmark/benchmark.cpp | 5 ++-- src/benchmark/benchmark_activeobjectmgr.cpp | 2 +- src/benchmark/benchmark_lighting.cpp | 2 +- src/benchmark/benchmark_mapblock.cpp | 2 +- src/benchmark/benchmark_mapmodify.cpp | 2 +- src/benchmark/benchmark_serialize.cpp | 2 +- src/benchmark/benchmark_setup.h | 22 ---------------- src/catch.cpp | 18 +++++++++++++ src/catch.h | 14 ++++++++++ src/unittest/test.cpp | 29 +++++++-------------- src/unittest/test_clientactiveobjectmgr.cpp | 2 +- 14 files changed, 60 insertions(+), 55 deletions(-) delete mode 100644 src/benchmark/benchmark_setup.h create mode 100644 src/catch.cpp create mode 100644 src/catch.h diff --git a/lib/catch2/CMakeLists.txt b/lib/catch2/CMakeLists.txt index 077272571..252dc11e3 100644 --- a/lib/catch2/CMakeLists.txt +++ b/lib/catch2/CMakeLists.txt @@ -1,8 +1,8 @@ -# catch2 is distributed as a standalone header. +# catch2 is distributed as a standalone header / source amalgamation. # # Downloaded from: # -# https://github.com/catchorg/Catch2/releases/download/v2.13.9/catch.hpp +# https://github.com/catchorg/Catch2/releases/tag/v3.6.0 # # The following changes were made to always print in microseconds, fixed format: # @@ -12,6 +12,7 @@ # - return os << duration.value() << ' ' << duration.unitsAsString(); # + return os << std::fixed << duration.value() << ' ' << duration.unitsAsString(); -add_library(catch2 INTERFACE) +add_library(catch2 STATIC catch_amalgamated.cpp) +target_compile_definitions(catch2 PRIVATE CATCH_CONFIG_NOSTDOUT CATCH_AMALGAMATED_CUSTOM_MAIN) add_library(Catch2::Catch2 ALIAS catch2) target_include_directories(catch2 INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}") diff --git a/lib/catch2/catch_amalgamated.cpp b/lib/catch2/catch_amalgamated.cpp index c080ad19a..9cb764be8 100644 --- a/lib/catch2/catch_amalgamated.cpp +++ b/lib/catch2/catch_amalgamated.cpp @@ -8952,7 +8952,7 @@ class Duration { Unit m_units; public: - explicit Duration(double inNanoseconds, Unit units = Unit::Auto) + explicit Duration(double inNanoseconds, Unit units = Unit::Microseconds) : m_inNanoseconds(inNanoseconds), m_units(units) { if (m_units == Unit::Auto) { @@ -9002,7 +9002,7 @@ public: } friend auto operator << (std::ostream& os, Duration const& duration) -> std::ostream& { - return os << duration.value() << ' ' << duration.unitsAsString(); + return os << std::fixed << duration.value() << ' ' << duration.unitsAsString(); } }; } // end anon namespace diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2ea416a4d..955bf05f2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -474,6 +474,10 @@ if(BUILD_BENCHMARKS) set(common_SRCS ${common_SRCS} ${BENCHMARK_SRCS}) endif() +if(BUILD_UNITTESTS OR BUILD_BENCHMARKS) + set(common_SRCS ${common_SRCS} catch.cpp) +endif() + # This gives us the icon and file version information if(WIN32) set(WINRESOURCE_FILE "${CMAKE_CURRENT_SOURCE_DIR}/../misc/winresource.rc") diff --git a/src/benchmark/benchmark.cpp b/src/benchmark/benchmark.cpp index 2eb35d9e0..8b7077703 100644 --- a/src/benchmark/benchmark.cpp +++ b/src/benchmark/benchmark.cpp @@ -19,9 +19,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "benchmark/benchmark.h" -// This must be set in just this file -#define CATCH_CONFIG_RUNNER -#include "benchmark_setup.h" +#define CATCH_CONFIG_ENABLE_BENCHMARKING +#include "catch.h" bool run_benchmarks(const char *arg) { diff --git a/src/benchmark/benchmark_activeobjectmgr.cpp b/src/benchmark/benchmark_activeobjectmgr.cpp index a21137a68..d9036c632 100644 --- a/src/benchmark/benchmark_activeobjectmgr.cpp +++ b/src/benchmark/benchmark_activeobjectmgr.cpp @@ -1,7 +1,7 @@ // Minetest // SPDX-License-Identifier: LGPL-2.1-or-later -#include "benchmark_setup.h" +#include "catch.h" #include "server/activeobjectmgr.h" #include "util/numeric.h" diff --git a/src/benchmark/benchmark_lighting.cpp b/src/benchmark/benchmark_lighting.cpp index 498553715..1ca2d441a 100644 --- a/src/benchmark/benchmark_lighting.cpp +++ b/src/benchmark/benchmark_lighting.cpp @@ -17,7 +17,7 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include "benchmark_setup.h" +#include "catch.h" #include "voxelalgorithms.h" #include "dummygamedef.h" #include "dummymap.h" diff --git a/src/benchmark/benchmark_mapblock.cpp b/src/benchmark/benchmark_mapblock.cpp index 3ce202240..0b3f7c921 100644 --- a/src/benchmark/benchmark_mapblock.cpp +++ b/src/benchmark/benchmark_mapblock.cpp @@ -17,7 +17,7 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include "benchmark_setup.h" +#include "catch.h" #include "mapblock.h" #include diff --git a/src/benchmark/benchmark_mapmodify.cpp b/src/benchmark/benchmark_mapmodify.cpp index 9643c0eb9..2ce346fb7 100644 --- a/src/benchmark/benchmark_mapmodify.cpp +++ b/src/benchmark/benchmark_mapmodify.cpp @@ -17,7 +17,7 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include "benchmark_setup.h" +#include "catch.h" #include "util/container.h" // Testing the standard library is not useful except to compare diff --git a/src/benchmark/benchmark_serialize.cpp b/src/benchmark/benchmark_serialize.cpp index 97cc7d59d..649d4b43a 100644 --- a/src/benchmark/benchmark_serialize.cpp +++ b/src/benchmark/benchmark_serialize.cpp @@ -17,7 +17,7 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#include "benchmark_setup.h" +#include "catch.h" #include "util/serialize.h" #include #include diff --git a/src/benchmark/benchmark_setup.h b/src/benchmark/benchmark_setup.h deleted file mode 100644 index 34a4eca4c..000000000 --- a/src/benchmark/benchmark_setup.h +++ /dev/null @@ -1,22 +0,0 @@ -/* -Minetest -Copyright (C) 2022 Minetest Authors - -This program is free software; you can redistribute it and/or modify -it under the terms of the GNU Lesser General Public License as published by -the Free Software Foundation; either version 2.1 of the License, or -(at your option) any later version. - -This program is distributed in the hope that it will be useful, -but WITHOUT ANY WARRANTY; without even the implied warranty of -MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -GNU Lesser General Public License for more details. - -You should have received a copy of the GNU Lesser General Public License along -with this program; if not, write to the Free Software Foundation, Inc., -51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -*/ - -#define CATCH_CONFIG_ENABLE_BENCHMARKING -#define CATCH_CONFIG_CONSOLE_WIDTH 160 -#include diff --git a/src/catch.cpp b/src/catch.cpp new file mode 100644 index 000000000..d4ebcc141 --- /dev/null +++ b/src/catch.cpp @@ -0,0 +1,18 @@ +// Minetest +// SPDX-License-Identifier: LGPL-2.1-or-later + +#include "catch.h" + +#include "log.h" + +namespace Catch { + std::ostream& cout() { + return rawstream; + } + std::ostream& clog() { + return rawstream; + } + std::ostream& cerr() { + return rawstream; + } +} diff --git a/src/catch.h b/src/catch.h new file mode 100644 index 000000000..7faa0de2a --- /dev/null +++ b/src/catch.h @@ -0,0 +1,14 @@ +// Minetest +// SPDX-License-Identifier: LGPL-2.1-or-later + +// We want to have catch write to rawstream (stderr) instead of stdout. +// This should be included instead of +// to patch the output streams accordingly. +#define CATCH_CONFIG_NOSTDOUT +#include + +namespace Catch { + std::ostream& cout(); + std::ostream& clog(); + std::ostream& cerr(); +} diff --git a/src/unittest/test.cpp b/src/unittest/test.cpp index 2fbba5e4a..33f8dcbb5 100644 --- a/src/unittest/test.cpp +++ b/src/unittest/test.cpp @@ -17,10 +17,7 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -#define CATCH_CONFIG_RUNNER -// we want to have catch write to rawstream (stderr) instead of stdout -#define CATCH_CONFIG_NOSTDOUT -#include +#include "catch.h" #include "test.h" @@ -34,18 +31,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include -// make catch write everything to rawstream -namespace Catch { - std::ostream& cout() { - return rawstream; - } - std::ostream& clog() { - return rawstream; - } - std::ostream& cerr() { - return rawstream; - } -} +#include "catch.h" content_t t_CONTENT_STONE; content_t t_CONTENT_GRASS; @@ -255,11 +241,16 @@ bool run_tests() } rawstream << "Catch test results: " << std::endl; - auto num_catch_tests_failed = Catch::Session().run(); - // We count the all the Catch tests as one test for Minetest's own logging + Catch::Session session{}; + auto config = session.configData(); + config.skipBenchmarks = true; + config.allowZeroTests = true; + session.useConfigData(config); + auto exit_code = session.run(); + // We count all the Catch tests as one test for Minetest's own logging // because we don't have a way to tell how many individual tests Catch ran. ++num_total_tests_run; - if (num_catch_tests_failed > 0) { + if (exit_code != 0) { ++num_modules_failed; ++num_total_tests_failed; } diff --git a/src/unittest/test_clientactiveobjectmgr.cpp b/src/unittest/test_clientactiveobjectmgr.cpp index 2df099fb1..28fc1c1a4 100644 --- a/src/unittest/test_clientactiveobjectmgr.cpp +++ b/src/unittest/test_clientactiveobjectmgr.cpp @@ -21,7 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "client/activeobjectmgr.h" -#include +#include "catch.h" #include #include