Skip to content

Commit

Permalink
change tests with intentional errors to catch std::runtime_error
Browse files Browse the repository at this point in the history
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 UCL#1490
  • Loading branch information
KrisThielemans committed Aug 25, 2024
1 parent fea050b commit e27c966
Show file tree
Hide file tree
Showing 13 changed files with 31 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/include/stir/IO/test/test_IO.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
# include "stir/IO/ECAT6OutputFileFormat.h" // need this for test on pixel_size
#endif
#include <fstream>
#include <exception>

START_NAMESPACE_STIR

Expand Down Expand Up @@ -300,6 +301,11 @@ IOTests<A>::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;
Expand Down
1 change: 1 addition & 0 deletions src/include/stir/RunTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <vector>
#include <complex>
#include <string>
#include <exception>

START_NAMESPACE_STIR

Expand Down
3 changes: 2 additions & 1 deletion src/recon_test/test_FBP2D.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "stir/recon_buildblock/test/ReconstructionTests.h"
#include "stir/analytic/FBP2D/FBP2DReconstruction.h"
#include <exception>

START_NAMESPACE_STIR

Expand Down Expand Up @@ -96,7 +97,7 @@ TestFBP2D::run_tests()
// we shouldn't get here
everything_ok = false;
}
catch (...)
catch (std::runtime_error&)
{}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/recon_test/test_OSMAPOSL.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "stir/recon_buildblock/test/PoissonLLReconstructionTests.h"
#include "stir/OSMAPOSL/OSMAPOSLReconstruction.h"
#include <exception>

START_NAMESPACE_STIR

Expand Down Expand Up @@ -88,7 +89,7 @@ TestOSMAPOSL::run_tests()
// we shouldn't get here
everything_ok = false;
}
catch (...)
catch (std::runtime_error&)
{}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/recon_test/test_blocks_on_cylindrical_projectors.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ BlocksTests::run_voxelOnCartesianGrid_with_negative_offset()
auto grid = std::make_shared<VoxelsOnCartesianGrid<float>>(
*proj_data_info_blocks_sptr, 1, CartesianCoordinate3D<float>(0.F, 0.F, 0.F), CartesianCoordinate3D<int>(-1, -1, -1));
}
catch (...)
catch (std::runtime_error&)
{
is_ok = false;
}
Expand Down
5 changes: 5 additions & 0 deletions src/test/IO/test_IO_ITKMulticomponent.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ IOTests_ITKMulticomponent::run_tests()
check_if_equal<float>(voxels_coords->get_voxel_size()[2], 4.0625F);
check_if_equal<float>(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;
Expand Down
2 changes: 1 addition & 1 deletion src/test/test_Array.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ ArrayTests::run_tests()
{
t2.at(-4).at(3);
}
catch (...)
catch (std::out_of_range&)
{
exception_thrown = true;
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/test_DateTime.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down Expand Up @@ -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";
}
Expand Down Expand Up @@ -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);
}
Expand Down
5 changes: 3 additions & 2 deletions src/test/test_KeyParser.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <sstream>
#include <vector>
#include <algorithm>
#include <exception>
#include "stir/RunTests.h"

START_NAMESPACE_STIR
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/test_ScatterSimulation.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
3 changes: 2 additions & 1 deletion src/test/test_proj_data.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include "stir/CPUTimer.h"
#include <algorithm>
#include <numeric>
#include <exception>

START_NAMESPACE_STIR

Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/test_proj_data_in_memory.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit e27c966

Please sign in to comment.