From e27c966d882be95fbb9719c34c17e4f7dbfad4a0 Mon Sep 17 00:00:00 2001 From: Kris Thielemans Date: Sun, 25 Aug 2024 08:28:17 +0100 Subject: [PATCH] change tests with intentional errors to catch std::runtime_error Some tests check if errors are thrown. We caught any exception with `catch (...)`. but that fails on OSX Clang 18. Now we catch `std::runtime_error`, which is more accurate anyway. Fixes #1490 --- .../motion_test/test_BSpline_transformations.cxx | 3 ++- src/include/stir/IO/test/test_IO.h | 6 ++++++ src/include/stir/RunTests.h | 1 + src/recon_test/test_FBP2D.cxx | 3 ++- src/recon_test/test_OSMAPOSL.cxx | 3 ++- src/recon_test/test_blocks_on_cylindrical_projectors.cxx | 2 +- src/test/IO/test_IO_ITKMulticomponent.cxx | 5 +++++ src/test/test_Array.cxx | 2 +- src/test/test_DateTime.cxx | 6 +++--- src/test/test_KeyParser.cxx | 5 +++-- src/test/test_ScatterSimulation.cxx | 2 +- src/test/test_proj_data.cxx | 3 ++- src/test/test_proj_data_in_memory.cxx | 4 ++-- 13 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/experimental/motion_test/test_BSpline_transformations.cxx b/src/experimental/motion_test/test_BSpline_transformations.cxx index ee9c13a3b0..8312397692 100644 --- a/src/experimental/motion_test/test_BSpline_transformations.cxx +++ b/src/experimental/motion_test/test_BSpline_transformations.cxx @@ -142,8 +142,9 @@ TestBSplineTransformation::run_tests() cerr << "Tests for TransformationTests\n"; this->run_transformation(); } - catch (...) + catch (std::exception& e) { + std::cerr << "Error thrown: " << e.what() << "\n\n"; everything_ok = false; } } diff --git a/src/include/stir/IO/test/test_IO.h b/src/include/stir/IO/test/test_IO.h index 61e1a921e3..f21ebc9da5 100644 --- a/src/include/stir/IO/test/test_IO.h +++ b/src/include/stir/IO/test/test_IO.h @@ -43,6 +43,7 @@ # include "stir/IO/ECAT6OutputFileFormat.h" // need this for test on pixel_size #endif #include +#include START_NAMESPACE_STIR @@ -300,6 +301,11 @@ IOTests::run_tests() return; std::cerr << "OK!\n"; } + catch (std::exception& e) + { + std::cerr << "\\Error thrown:\n\t" << e.what() << "\n\n"; + everything_ok = false; + } catch (...) { everything_ok = false; diff --git a/src/include/stir/RunTests.h b/src/include/stir/RunTests.h index b406962608..3cf6c2a353 100644 --- a/src/include/stir/RunTests.h +++ b/src/include/stir/RunTests.h @@ -33,6 +33,7 @@ #include #include #include +#include START_NAMESPACE_STIR diff --git a/src/recon_test/test_FBP2D.cxx b/src/recon_test/test_FBP2D.cxx index f14d6cf609..a80817f1b8 100644 --- a/src/recon_test/test_FBP2D.cxx +++ b/src/recon_test/test_FBP2D.cxx @@ -14,6 +14,7 @@ #include "stir/recon_buildblock/test/ReconstructionTests.h" #include "stir/analytic/FBP2D/FBP2DReconstruction.h" +#include START_NAMESPACE_STIR @@ -96,7 +97,7 @@ TestFBP2D::run_tests() // we shouldn't get here everything_ok = false; } - catch (...) + catch (std::runtime_error&) {} } } diff --git a/src/recon_test/test_OSMAPOSL.cxx b/src/recon_test/test_OSMAPOSL.cxx index a8c7a0586f..81211ff087 100644 --- a/src/recon_test/test_OSMAPOSL.cxx +++ b/src/recon_test/test_OSMAPOSL.cxx @@ -14,6 +14,7 @@ #include "stir/recon_buildblock/test/PoissonLLReconstructionTests.h" #include "stir/OSMAPOSL/OSMAPOSLReconstruction.h" +#include START_NAMESPACE_STIR @@ -88,7 +89,7 @@ TestOSMAPOSL::run_tests() // we shouldn't get here everything_ok = false; } - catch (...) + catch (std::runtime_error&) {} } } diff --git a/src/recon_test/test_blocks_on_cylindrical_projectors.cxx b/src/recon_test/test_blocks_on_cylindrical_projectors.cxx index 06b4561d87..665e446861 100644 --- a/src/recon_test/test_blocks_on_cylindrical_projectors.cxx +++ b/src/recon_test/test_blocks_on_cylindrical_projectors.cxx @@ -449,7 +449,7 @@ BlocksTests::run_voxelOnCartesianGrid_with_negative_offset() auto grid = std::make_shared>( *proj_data_info_blocks_sptr, 1, CartesianCoordinate3D(0.F, 0.F, 0.F), CartesianCoordinate3D(-1, -1, -1)); } - catch (...) + catch (std::runtime_error&) { is_ok = false; } diff --git a/src/test/IO/test_IO_ITKMulticomponent.cxx b/src/test/IO/test_IO_ITKMulticomponent.cxx index e1dbd61c3b..5855ac8f2c 100644 --- a/src/test/IO/test_IO_ITKMulticomponent.cxx +++ b/src/test/IO/test_IO_ITKMulticomponent.cxx @@ -69,6 +69,11 @@ IOTests_ITKMulticomponent::run_tests() check_if_equal(voxels_coords->get_voxel_size()[2], 4.0625F); check_if_equal(voxels_coords->get_voxel_size()[3], 4.0625F); } + catch (const std::exception& error) + { + std::cerr << "\nHere's the error:\n\t" << error.what() << "\n\n"; + everything_ok = false; + } catch (...) { everything_ok = false; diff --git a/src/test/test_Array.cxx b/src/test/test_Array.cxx index ad5db37152..388a10555c 100644 --- a/src/test/test_Array.cxx +++ b/src/test/test_Array.cxx @@ -494,7 +494,7 @@ ArrayTests::run_tests() { t2.at(-4).at(3); } - catch (...) + catch (std::out_of_range&) { exception_thrown = true; } diff --git a/src/test/test_DateTime.cxx b/src/test/test_DateTime.cxx index 830bbae5ba..4be01426cd 100644 --- a/src/test/test_DateTime.cxx +++ b/src/test/test_DateTime.cxx @@ -74,7 +74,7 @@ DateTimeTest::run_tests() DICOM_datetime_to_secs_since_Unix_epoch("19710202230301+020"); check(false, "test ill-formed TZ"); } - catch (...) + catch (std::runtime_error&) { std::cerr << "Test was ok\n"; } @@ -108,7 +108,7 @@ DateTimeTest::run_tests() Interfile_datetime_to_secs_since_Unix_epoch(DateTimeStrings("1971:02:2", "23:03:01")); check(false, "test ill-formed date"); } - catch (...) + catch (std::runtime_error&) { std::cerr << "Test was ok\n"; } @@ -154,7 +154,7 @@ DateTimeTest::check_round_trip(const double secs, const double tz_offset, const check_if_zero(new_secs - secs, str + " : " + dt.date + ", " + dt.time); } } - catch (...) + catch (std::runtime_error&) { check(false, str); } diff --git a/src/test/test_KeyParser.cxx b/src/test/test_KeyParser.cxx index d3d650335d..0f37d811c8 100644 --- a/src/test/test_KeyParser.cxx +++ b/src/test/test_KeyParser.cxx @@ -23,6 +23,7 @@ #include #include #include +#include #include "stir/RunTests.h" START_NAMESPACE_STIR @@ -142,7 +143,7 @@ KeyParserTests::run_tests_one_type() parser.parse(str); check(false, "parsing non-vectorised key with vector should have failed"); } - catch (...) + catch (std::runtime_error&) { // ok } @@ -161,7 +162,7 @@ KeyParserTests::run_tests_one_type() parser.parse(str); check(false, "parsing vectorised key with non-vector should have failed"); } - catch (...) + catch (std::runtime_error&) { // ok } diff --git a/src/test/test_ScatterSimulation.cxx b/src/test/test_ScatterSimulation.cxx index d8d4e5b163..906ba603a7 100644 --- a/src/test/test_ScatterSimulation.cxx +++ b/src/test/test_ScatterSimulation.cxx @@ -439,7 +439,7 @@ ScatterSimulationTests::test_scatter_simulation() test_symmetric(*sss, "act_zoom_rings_zoomxy.3_zoomz.4"); check(false, "Test on zooming of activity image should have thrown."); } - catch (...) + catch (std::runtime_error&) { // ok } diff --git a/src/test/test_proj_data.cxx b/src/test/test_proj_data.cxx index ac0501ebeb..ae948c2da5 100644 --- a/src/test/test_proj_data.cxx +++ b/src/test/test_proj_data.cxx @@ -36,6 +36,7 @@ #include "stir/CPUTimer.h" #include #include +#include START_NAMESPACE_STIR @@ -321,7 +322,7 @@ ProjDataTests::run_tests_on_proj_data(ProjData& proj_data) proj_data2.fill(proj_data); check(false, "test fill wtih too small proj_data should have thrown"); } - catch (...) + catch (std::runtime_error&) { // ok } diff --git a/src/test/test_proj_data_in_memory.cxx b/src/test/test_proj_data_in_memory.cxx index cca884dc3d..a9c6776a7c 100644 --- a/src/test/test_proj_data_in_memory.cxx +++ b/src/test/test_proj_data_in_memory.cxx @@ -148,7 +148,7 @@ ProjDataInMemoryTests::run_tests_no_tof() proj_data2.fill(proj_data); check(false, "test fill with too small proj_data should have thrown"); } - catch (...) + catch (std::runtime_error&) { // ok } @@ -261,7 +261,7 @@ ProjDataInMemoryTests::run_tests_tof() proj_data2.fill(proj_data); check(false, "test fill with too small proj_data should have thrown"); } - catch (...) + catch (std::runtime_error&) { // ok }