Skip to content

Commit ba1d0f2

Browse files
ax3lfranzpoeschel
andauthored
Migrate C++ Tests to Catch2 v3 (#1823)
* Migrate to Catch2 v3 Co-authored-by: Franz Pöschel <franz.poeschel@gmail.com>
1 parent a2f5836 commit ba1d0f2

14 files changed

+76
-91
lines changed

CMakeLists.txt

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,9 @@ if(openPMD_BUILD_TESTING)
525525
add_library(openPMD::thirdparty::Catch2 INTERFACE IMPORTED)
526526
target_link_libraries(openPMD::thirdparty::Catch2
527527
INTERFACE Catch2::Catch2)
528+
add_library(openPMD::thirdparty::Catch2WithMain INTERFACE IMPORTED)
529+
target_link_libraries(openPMD::thirdparty::Catch2WithMain
530+
INTERFACE Catch2::Catch2WithMain)
528531
endif()
529532

530533
if(openPMD_HAVE_MPI)
@@ -770,14 +773,11 @@ if(openPMD_HAVE_PYTHON)
770773
endif()
771774

772775
if(openPMD_BUILD_TESTING)
773-
# compile Catch2 implementation part separately
774-
add_library(CatchRunner ${_openpmd_lib_type}
775-
test/CatchRunner.cpp) # Always MPI_Init with Serial Fallback
776-
add_library(CatchMain ${_openpmd_lib_type}
777-
test/CatchMain.cpp) # Serial only
776+
# CatchRunner: custom main with MPI support for parallel tests
777+
# In Catch2 v3, we link to Catch2 (not Catch2WithMain) since we provide our own main
778+
add_library(CatchRunner ${_openpmd_lib_type} test/CatchRunner.cpp)
778779
openpmd_cxx_required(CatchRunner)
779-
openpmd_cxx_required(CatchMain)
780-
set_target_properties(CatchRunner CatchMain PROPERTIES
780+
set_target_properties(CatchRunner PROPERTIES
781781
ARCHIVE_OUTPUT_DIRECTORY ${openPMD_ARCHIVE_OUTPUT_DIRECTORY}
782782
LIBRARY_OUTPUT_DIRECTORY ${openPMD_LIBRARY_OUTPUT_DIRECTORY}
783783
RUNTIME_OUTPUT_DIRECTORY ${openPMD_RUNTIME_OUTPUT_DIRECTORY}
@@ -788,7 +788,6 @@ if(openPMD_BUILD_TESTING)
788788
WINDOWS_EXPORT_ALL_SYMBOLS ON
789789
)
790790
set_target_properties(CatchRunner PROPERTIES COMPILE_PDB_NAME CatchRunner)
791-
set_target_properties(CatchMain PROPERTIES COMPILE_PDB_NAME CatchMain)
792791
# note: same as above, but for Multi-Config generators
793792
if(isMultiConfig)
794793
foreach(CFG IN LISTS CMAKE_CONFIGURATION_TYPES)
@@ -801,20 +800,10 @@ if(openPMD_BUILD_TESTING)
801800
PDB_OUTPUT_DIRECTORY_${CFG_UPPER} ${openPMD_PDB_OUTPUT_DIRECTORY}/${CFG}
802801
COMPILE_PDB_OUTPUT_DIRECTORY_${CFG_UPPER} ${openPMD_COMPILE_PDB_OUTPUT_DIRECTORY}/${CFG}
803802
)
804-
set_target_properties(CatchMain PROPERTIES
805-
COMPILE_PDB_NAME_${CFG_UPPER} CatchMain
806-
ARCHIVE_OUTPUT_DIRECTORY_${CFG_UPPER} ${openPMD_ARCHIVE_OUTPUT_DIRECTORY}/${CFG}
807-
LIBRARY_OUTPUT_DIRECTORY_${CFG_UPPER} ${openPMD_LIBRARY_OUTPUT_DIRECTORY}/${CFG}
808-
RUNTIME_OUTPUT_DIRECTORY_${CFG_UPPER} ${openPMD_RUNTIME_OUTPUT_DIRECTORY}/${CFG}
809-
PDB_OUTPUT_DIRECTORY_${CFG_UPPER} ${openPMD_PDB_OUTPUT_DIRECTORY}/${CFG}
810-
COMPILE_PDB_OUTPUT_DIRECTORY_${CFG_UPPER} ${openPMD_COMPILE_PDB_OUTPUT_DIRECTORY}/${CFG}
811-
)
812803
endforeach()
813804
endif()
814805
target_compile_options(CatchRunner PUBLIC ${_msvc_options})
815-
target_compile_options(CatchMain PUBLIC ${_msvc_options})
816806
target_link_libraries(CatchRunner PUBLIC openPMD::thirdparty::Catch2)
817-
target_link_libraries(CatchMain PUBLIC openPMD::thirdparty::Catch2)
818807
if(openPMD_HAVE_MPI)
819808
target_link_libraries(CatchRunner PUBLIC ${openPMD_MPI_TARGETS})
820809
target_compile_definitions(CatchRunner PUBLIC openPMD_HAVE_MPI=1)
@@ -875,9 +864,11 @@ if(openPMD_BUILD_TESTING)
875864
endif()
876865
target_link_libraries(${testname}Tests PRIVATE openPMD)
877866
if(${testname} MATCHES "Parallel.+$")
867+
# Parallel tests use CatchRunner (custom main with MPI)
878868
target_link_libraries(${testname}Tests PRIVATE CatchRunner)
879869
else()
880-
target_link_libraries(${testname}Tests PRIVATE CatchMain)
870+
# Serial tests use Catch2WithMain (provides default main)
871+
target_link_libraries(${testname}Tests PRIVATE openPMD::thirdparty::Catch2WithMain)
881872
endif()
882873

883874
if(${testname} STREQUAL JSON)

cmake/dependencies/catch.cmake

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
function(catch_preconfigure)
2+
set(_old_BUILD_SHARED_LIBS ${BUILD_SHARED_LIBS})
3+
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
4+
endfunction()
5+
6+
function(catch_postconfigure)
7+
set(BUILD_SHARED_LIBS ${_old_BUILD_SHARED_LIBS})
8+
unset(_old_BUILD_SHARED_LIBS)
9+
# Mark Catch2 as system code to suppress warnings and set position independent code
10+
# Ensure Catch2 is built with PIC so it can be linked into shared libraries
11+
set_target_properties(Catch2 PROPERTIES
12+
POSITION_INDEPENDENT_CODE ON
13+
INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "$<TARGET_PROPERTY:Catch2,INTERFACE_INCLUDE_DIRECTORIES>"
14+
)
15+
if(CMAKE_CXX_CLANG_TIDY)
16+
set_target_properties(Catch2 PROPERTIES CXX_CLANG_TIDY "")
17+
endif()
18+
endfunction()
19+
120
function(find_catch2)
221
if(TARGET Catch2::Catch2)
322
message(STATUS "Catch2::Catch2 target already imported")
@@ -19,8 +38,11 @@ function(find_catch2)
1938
if(TARGET Catch2::Catch2)
2039
# nothing to do, target already exists in the superbuild
2140
elseif(openPMD_USE_INTERNAL_CATCH AND openPMD_catch_src)
41+
catch_preconfigure()
2242
add_subdirectory(${openPMD_catch_src} _deps/localCatch2-build/)
43+
catch_postconfigure()
2344
elseif(openPMD_USE_INTERNAL_CATCH AND (openPMD_catch_tar OR openPMD_catch_branch))
45+
catch_preconfigure()
2446
include(FetchContent)
2547
if(openPMD_catch_tar)
2648
FetchContent_Declare(fetchedCatch2
@@ -36,7 +58,7 @@ function(find_catch2)
3658
)
3759
endif()
3860
FetchContent_MakeAvailable(fetchedCatch2)
39-
61+
catch_postconfigure()
4062
# advanced fetch options
4163
mark_as_advanced(FETCHCONTENT_BASE_DIR)
4264
mark_as_advanced(FETCHCONTENT_FULLY_DISCONNECTED)
@@ -45,7 +67,7 @@ function(find_catch2)
4567
mark_as_advanced(FETCHCONTENT_UPDATES_DISCONNECTED)
4668
#mark_as_advanced(FETCHCONTENT_UPDATES_DISCONNECTED_FETCHEDCatch2)
4769
elseif(NOT openPMD_USE_INTERNAL_CATCH)
48-
find_package(Catch2 2.13.10 CONFIG REQUIRED)
70+
find_package(Catch2 3.0.0 CONFIG REQUIRED)
4971
message(STATUS "Catch2: Found version '${Catch2_VERSION}'")
5072
endif()
5173
endfunction()
@@ -56,18 +78,18 @@ set(openPMD_catch_src ""
5678
"Local path to Catch2 source directory (preferred if set)")
5779

5880
# tarball fetcher
59-
set(openPMD_catch_tar "https://github.com/catchorg/Catch2/archive/refs/tags/v2.13.10.tar.gz"
81+
set(openPMD_catch_tar "https://github.com/catchorg/Catch2/archive/refs/tags/v3.11.0.tar.gz"
6082
CACHE STRING
6183
"Remote tarball link to pull and build Catch2 from if(openPMD_USE_INTERNAL_CATCH)")
62-
set(openPMD_catch_tar_hash "SHA256=d54a712b7b1d7708bc7a819a8e6e47b2fde9536f487b89ccbca295072a7d9943"
84+
set(openPMD_catch_tar_hash "SHA256=82fa1cb59dc28bab220935923f7469b997b259eb192fb9355db62da03c2a3137"
6385
CACHE STRING
6486
"Hash checksum of the tarball of Catch2 if(openPMD_USE_INTERNAL_CATCH)")
6587

6688
# Git fetcher
6789
set(openPMD_catch_repo "https://github.com/catchorg/Catch2.git"
6890
CACHE STRING
6991
"Repository URI to pull and build Catch2 from if(openPMD_USE_INTERNAL_CATCH)")
70-
set(openPMD_catch_branch "v2.13.10"
92+
set(openPMD_catch_branch "v3.11.0"
7193
CACHE STRING
7294
"Repository branch for openPMD_catch_repo if(openPMD_USE_INTERNAL_CATCH)")
7395

test/AuxiliaryTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
#include "openPMD/backend/Writable.hpp"
3838
#include "openPMD/config.hpp"
3939

40-
#include <catch2/catch.hpp>
40+
#include <catch2/catch_test_macros.hpp>
4141

4242
#include <array>
4343
#include <exception>

test/CatchMain.cpp

Lines changed: 0 additions & 22 deletions
This file was deleted.

test/CatchRunner.cpp

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
* and the GNU Lesser General Public License along with openPMD-api.
1919
* If not, see <http://www.gnu.org/licenses/>.
2020
*/
21-
#define CATCH_CONFIG_RUNNER
22-
#include <catch2/catch.hpp>
21+
#include <catch2/catch_session.hpp>
2322

2423
#if openPMD_HAVE_MPI
2524
#include <mpi.h>
@@ -29,29 +28,20 @@ int main(int argc, char *argv[])
2928
MPI_Init(&argc, &argv);
3029

3130
Catch::Session session;
32-
int result = 0;
31+
// session.configData().runOrder = Catch::TestRunOrder::Declared;
32+
MPI_Bcast(
33+
&session.configData().rngSeed, 1, MPI_UINT32_T, 0, MPI_COMM_WORLD);
34+
int result = session.applyCommandLine(argc, argv);
35+
if (result == 0)
3336
{
34-
// Indicates a command line parsing
35-
result = session.applyCommandLine(argc, argv);
36-
// RT tests
37-
if (result == 0)
38-
result = session.run();
37+
result = session.run();
3938
}
4039
MPI_Finalize();
4140
return result;
4241
}
4342
#else
44-
int main(int argc, char *argv[])
43+
int main()
4544
{
46-
Catch::Session session;
47-
int result = 0;
48-
{
49-
// Indicates a command line parsing
50-
result = session.applyCommandLine(argc, argv);
51-
// RT tests
52-
if (result == 0)
53-
result = session.run();
54-
}
55-
return result;
45+
throw std::runtime_error("Serial tests use Catch2's provided main.");
5646
}
5747
#endif

test/CoreTest.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
#include "openPMD/auxiliary/Memory_internal.hpp"
3535
#include "openPMD/auxiliary/UniquePtr.hpp"
3636

37-
#include <catch2/catch.hpp>
37+
#include <catch2/catch_test_macros.hpp>
38+
#include <catch2/matchers/catch_matchers_string.hpp>
3839

3940
#include <algorithm>
4041
#include <array>
@@ -1100,7 +1101,7 @@ TEST_CASE("wrapper_test", "[core]")
11001101
// it once? value = 43.; mrc2.makeConstant(value);
11011102
REQUIRE_THROWS_WITH(
11021103
mrc2.makeConstant(value),
1103-
Catch::Equals(
1104+
Catch::Matchers::Equals(
11041105
"A recordComponent can not (yet) be made constant after "
11051106
"it has been written."));
11061107
std::array<double, 1> moreData = {{112233.}};
@@ -1121,7 +1122,7 @@ TEST_CASE("wrapper_test", "[core]")
11211122
int wrongData = 42;
11221123
REQUIRE_THROWS_WITH(
11231124
o.iterations[5].meshes["E"]["y"].storeChunkRaw(&wrongData, {0}, {1}),
1124-
Catch::Equals(
1125+
Catch::Matchers::Equals(
11251126
"Datatypes of chunk data (INT) and record component "
11261127
"(DOUBLE) do not match."));
11271128
std::shared_ptr<double> storeData = std::make_shared<double>(44);
@@ -1192,7 +1193,7 @@ TEST_CASE("wrapper_test", "[core]")
11921193
.particles["electrons"]
11931194
.particlePatches["numParticles"][RecordComponent::SCALAR]
11941195
.store(idx + 1, 42.),
1195-
Catch::Equals(
1196+
Catch::Matchers::Equals(
11961197
"Datatypes of patch data (DOUBLE) and dataset (" + u64str.str() +
11971198
") do not match."));
11981199
o.iterations[6]
@@ -1285,7 +1286,7 @@ TEST_CASE("empty_record_test", "[core]")
12851286
"No assumption about contained RecordComponents will be made");
12861287
REQUIRE_THROWS_WITH(
12871288
o.flush(),
1288-
Catch::Equals(
1289+
Catch::Matchers::Equals(
12891290
"A Record can not be written without any contained "
12901291
"RecordComponents: E"));
12911292
o.iterations[1].meshes["E"][RecordComponent::SCALAR].resetDataset(
@@ -1302,28 +1303,29 @@ TEST_CASE("zero_extent_component", "[core]")
13021303
auto E_x = o.iterations[1].meshes["E"]["x"];
13031304
E_x.setComment("Datasets must contain dimensions.");
13041305
// REQUIRE_THROWS_WITH(E_x.resetDataset(Dataset(Datatype::LONG, {})),
1305-
// Catch::Equals("Dataset extent must be at least 1D."));
1306+
// Catch::Matchers::Equals("Dataset extent must be at
1307+
// least 1D."));
13061308
REQUIRE_THROWS_WITH(
13071309
E_x.makeEmpty<int>(0),
1308-
Catch::Equals("Dataset extent must be at least 1D."));
1310+
Catch::Matchers::Equals("Dataset extent must be at least 1D."));
13091311
E_x.resetDataset(Dataset(Datatype::DOUBLE, {1}));
13101312
}
13111313

13121314
TEST_CASE("no_file_ending", "[core]")
13131315
{
13141316
REQUIRE_THROWS_WITH(
13151317
Series("./new_openpmd_output", Access::CREATE),
1316-
Catch::Equals(
1318+
Catch::Matchers::Equals(
13171319
"Unknown file format! Did you specify a file ending? "
13181320
"Specified file name was './new_openpmd_output'."));
13191321
REQUIRE_THROWS_WITH(
13201322
Series("./new_openpmd_output_%T", Access::CREATE),
1321-
Catch::Equals(
1323+
Catch::Matchers::Equals(
13221324
"Unknown file format! Did you specify a file ending? "
13231325
"Specified file name was './new_openpmd_output_%T'."));
13241326
REQUIRE_THROWS_WITH(
13251327
Series("./new_openpmd_output_%05T", Access::CREATE),
1326-
Catch::Equals(
1328+
Catch::Matchers::Equals(
13271329
"Unknown file format! Did you specify a file ending? "
13281330
"Specified file name was './new_openpmd_output_%05T'."));
13291331
{
@@ -1625,7 +1627,7 @@ TEST_CASE("load_chunk_wrong_datatype", "[core]")
16251627
read.iterations[0]
16261628
.meshes["rho"][RecordComponent::SCALAR]
16271629
.loadChunk<double>({0}, {10}),
1628-
Catch::Equals(err_msg));
1630+
Catch::Matchers::Equals(err_msg));
16291631
}
16301632
}
16311633

@@ -1694,7 +1696,7 @@ TEST_CASE("DoConvert_single_value_to_vector", "[core]")
16941696
REQUIRE(attr.get<std::array<int, 7>>() == arrayint);
16951697
REQUIRE_THROWS_WITH(
16961698
(attr.get<std::array<int, 8>>()),
1697-
Catch::Equals(
1699+
Catch::Matchers::Equals(
16981700
"getCast: no vector to array conversion possible "
16991701
"(wrong requested array size)."));
17001702
#endif

test/Files_Core/automatic_variable_encoding.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
*/
2121
#include "CoreTests.hpp"
2222

23-
#include <catch2/catch.hpp>
23+
#include <catch2/catch_test_macros.hpp>
2424

2525
namespace automatic_variable_encoding
2626
{

test/Files_ParallelIO/iterate_nonstreaming_series.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
#include "openPMD/IO/ADIOS/macros.hpp"
2424

25-
#include <catch2/catch.hpp>
25+
#include <catch2/catch_test_macros.hpp>
2626
#include <mpi.h>
2727

2828
namespace iterate_nonstreaming_series

test/Files_ParallelIO/read_variablebased_randomaccess.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525

2626
#include <numeric>
2727

28-
#include <catch2/catch.hpp>
28+
#include <catch2/catch_test_macros.hpp>
2929
#include <optional>
3030

3131
#if openPMD_HAVE_ADIOS2 && openPMD_HAVE_MPI

test/Files_SerialIO/close_and_reopen_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include "openPMD/Series.hpp"
2525
#include "openPMD/auxiliary/Filesystem.hpp"
2626

27-
#include <catch2/catch.hpp>
27+
#include <catch2/catch_test_macros.hpp>
2828

2929
namespace close_and_reopen_test
3030
{

0 commit comments

Comments
 (0)