From 8c8d14cb4e1c7945e69b5d7cffe08547ccb75ef7 Mon Sep 17 00:00:00 2001 From: Niels Lohmann Date: Wed, 9 Sep 2026 09:52:58 +0200 Subject: [PATCH] Reduce test-suite compile time: extract tiny per-standard test content; drop redundant legacy-comparison CI job (#5481) * Extract C++17-only content from unit-items.cpp into its own test file unit-items.cpp is a 1433-line file that was being compiled twice per CI configuration (once for C++11, once for C++17) purely because it contained a single, small JSON_HAS_CPP_17-gated SECTION ("structured bindings", 14 lines). Move that SECTION into a new, dedicated file (tests/src/unit-items-cpp17.cpp) so only that tiny file needs a second build; unit-items.cpp itself now builds/tests only once. No tests/CMakeLists.txt changes are needed since the existing file(GLOB ... src/unit-*.cpp) plus json_test_add_test_for() already auto-register and standard-gate any new unit-*.cpp file based on whether it textually contains JSON_HAS_CPP_ (the same mechanism already used for the existing unit-iterators3.cpp file, which follows the identical pattern). Verified with plain clang++ under -std=c++11/14/17/20 and via a local CMake configure+build that: - unit-items.cpp now only produces a test-items_cpp11 target (the former test-items_cpp17 target is gone) and its assertion/test-case counts are unchanged (2 test cases / 222 assertions) for every standard. - The new unit-items-cpp17.cpp produces test-items-cpp17_cpp11 (an intentionally empty translation unit under C++11 that reports 0 tests, 0 assertions, SUCCESS) and test-items-cpp17_cpp17 (1 test case / 1 assertion, identical to what "structured bindings" ran as before it was moved). Separately, unit-regression1.cpp (1530 lines) was also being built twice per CI configuration because it contained the substring JSON_HAS_CPP_17 -- but on inspection this was dead code: an orphaned "#ifdef JSON_HAS_CPP_17 / #include / #endif" left over from when the actual std::variant-based regression test (issue #1292) was relocated to unit-regression2.cpp. Nothing in unit-regression1.cpp uses , so there is no SECTION/TEST_CASE to preserve here; the dead include is simply removed. This was verified by grepping the file for any other use of "variant" (none) and confirming issue #1292 is still covered by unit-regression2.cpp. Compiled and ran under -std=c++11/14/17/20 and via CMake: unit-regression1.cpp now only produces a test-regression1_cpp11 target (test-regression1_cpp17 is gone) with an unchanged test-case count (3) under every standard. Signed-off-by: Niels Lohmann * Fix astyle indentation of #include inside #ifdef in unit-items-cpp17.cpp This repo's astyle style keeps preprocessor directives at column 0 even inside #ifdef blocks. The new tests/src/unit-items-cpp17.cpp had its #include /#include indented, which made the 'check' CI job's amalgamation/formatting diff non-empty and failed the aggregate check. Signed-off-by: Niels Lohmann --------- Signed-off-by: Niels Lohmann --- tests/src/unit-items-cpp17.cpp | 42 ++++++++++++++++++++++++++++++++++ tests/src/unit-items.cpp | 16 ------------- tests/src/unit-regression1.cpp | 4 ---- 3 files changed, 42 insertions(+), 20 deletions(-) create mode 100644 tests/src/unit-items-cpp17.cpp diff --git a/tests/src/unit-items-cpp17.cpp b/tests/src/unit-items-cpp17.cpp new file mode 100644 index 000000000..577dcea56 --- /dev/null +++ b/tests/src/unit-items-cpp17.cpp @@ -0,0 +1,42 @@ +// __ _____ _____ _____ +// __| | __| | | | JSON for Modern C++ (supporting code) +// | | |__ | | | | | | version 3.12.0 +// |_____|_____|_____|_|___| https://github.com/nlohmann/json +// +// SPDX-FileCopyrightText: 2013-2026 Niels Lohmann +// SPDX-License-Identifier: MIT + +// This file contains the C++17-only part of unit-items.cpp (structured +// bindings support for json::items()). It is kept in a separate +// translation unit so the (much larger) unit-items.cpp does not need to +// be compiled a second time just for this one SECTION. + +#include "doctest_compatibility.h" + +#include +using nlohmann::json; + +#ifdef JSON_HAS_CPP_17 +#include +#include + +TEST_CASE("items()") +{ + SECTION("object") + { + SECTION("structured bindings") + { + json j = { {"A", 1}, {"B", 2} }; + + std::map m; + + for (auto const&[key, value] : j.items()) + { + m.emplace(key, value); + } + + CHECK(j.get() == m); + } + } +} +#endif diff --git a/tests/src/unit-items.cpp b/tests/src/unit-items.cpp index fa8948447..81959db8d 100644 --- a/tests/src/unit-items.cpp +++ b/tests/src/unit-items.cpp @@ -862,22 +862,6 @@ TEST_CASE("items()") CHECK(counter == 3); } - -#ifdef JSON_HAS_CPP_17 - SECTION("structured bindings") - { - json j = { {"A", 1}, {"B", 2} }; - - std::map m; - - for (auto const&[key, value] : j.items()) - { - m.emplace(key, value); - } - - CHECK(j.get() == m); - } -#endif } SECTION("const object") diff --git a/tests/src/unit-regression1.cpp b/tests/src/unit-regression1.cpp index fdc3fdd05..0529f83dd 100644 --- a/tests/src/unit-regression1.cpp +++ b/tests/src/unit-regression1.cpp @@ -31,10 +31,6 @@ using nlohmann::json; #include "make_test_data_available.hpp" #include "test_utils.hpp" -#ifdef JSON_HAS_CPP_17 - #include -#endif - #include "fifo_map.hpp" /////////////////////////////////////////////////////////////////////