From f0ce6a871face81552a82c884a77ddc91a0761e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20Haitz=20Legarreta=20Gorro=C3=B1o?= Date: Tue, 27 Dec 2022 13:00:12 -0500 Subject: [PATCH] STYLE: Prefer using exception macros in tests Use the `ITK_TRY_EXPECT_EXCEPTION` and `ITK_TRY_EXPECT_NO_EXCEPTION` macros in tests in lieu of `try/catch` blocks for the sake of readability and compactness, and to save typing/avoid boilerplate code. Remove unnecessary print messages that inform about the method being called. --- ...ametricSpaceToImageSpaceMeshFilterTest.cxx | 23 +--- ...caleGeodesicErodeDilateImageFilterTest.cxx | 12 +- .../test/itkLBFGSBOptimizerv4Test.cxx | 42 +------ .../test/itkPowellOptimizerv4Test.cxx | 14 +-- ...tkImageToSpatialObjectRegistrationTest.cxx | 105 ++---------------- ...oodCorrelationImageToImageMetricv4Test.cxx | 79 ++----------- ...llelSparseFieldLevelSetImageFilterTest.cxx | 10 +- 7 files changed, 40 insertions(+), 245 deletions(-) diff --git a/Modules/Core/Mesh/test/itkParametricSpaceToImageSpaceMeshFilterTest.cxx b/Modules/Core/Mesh/test/itkParametricSpaceToImageSpaceMeshFilterTest.cxx index 8f5d92a4cbb..13d107c1968 100644 --- a/Modules/Core/Mesh/test/itkParametricSpaceToImageSpaceMeshFilterTest.cxx +++ b/Modules/Core/Mesh/test/itkParametricSpaceToImageSpaceMeshFilterTest.cxx @@ -88,16 +88,8 @@ InternalTest(int argc, char * argv[]) auto reader = ReaderType::New(); reader->SetFileName(argv[1]); - try - { - reader->Update(); - } - catch (const itk::ExceptionObject & err) - { - std::cout << "ExceptionObject caught !" << std::endl; - std::cout << err << std::endl; - return EXIT_FAILURE; - } + ITK_TRY_EXPECT_NO_EXCEPTION(reader->Update()); + // Store the input image for convenience typename ImageType::Pointer image = reader->GetOutput(); @@ -161,15 +153,8 @@ InternalTest(int argc, char * argv[]) // Set the input mesh for the parametric filter parametricFilter->SetInput(mesh); - try - { - parametricFilter->Update(); - } - catch (const itk::ExceptionObject & e) - { - std::cerr << "Error: " << e.what() << std::endl; - return EXIT_FAILURE; - } + ITK_TRY_EXPECT_NO_EXCEPTION(parametricFilter->Update()); + if (parametricFilter->GetOutput()->GetNumberOfPoints() != mesh->GetNumberOfPoints()) { diff --git a/Modules/Filtering/MathematicalMorphology/test/itkGrayscaleGeodesicErodeDilateImageFilterTest.cxx b/Modules/Filtering/MathematicalMorphology/test/itkGrayscaleGeodesicErodeDilateImageFilterTest.cxx index dd42cd46c95..0f0ec5f4584 100644 --- a/Modules/Filtering/MathematicalMorphology/test/itkGrayscaleGeodesicErodeDilateImageFilterTest.cxx +++ b/Modules/Filtering/MathematicalMorphology/test/itkGrayscaleGeodesicErodeDilateImageFilterTest.cxx @@ -107,16 +107,8 @@ itkGrayscaleGeodesicErodeDilateImageFilterTest(int argc, char * argv[]) itk::SimpleFilterWatcher watchDilate(dilate); itk::SimpleFilterWatcher watchErode(erode); - // Execute the filter - try - { - writer->Update(); - } - catch (const itk::ExceptionObject & excp) - { - std::cerr << "Exception caught:" << excp << std::endl; - return EXIT_FAILURE; - } + ITK_TRY_EXPECT_NO_EXCEPTION(writer->Update()); + std::cout << "Test finished." << std::endl; return EXIT_SUCCESS; diff --git a/Modules/Numerics/Optimizersv4/test/itkLBFGSBOptimizerv4Test.cxx b/Modules/Numerics/Optimizersv4/test/itkLBFGSBOptimizerv4Test.cxx index 3eb06863ff3..45035929c81 100644 --- a/Modules/Numerics/Optimizersv4/test/itkLBFGSBOptimizerv4Test.cxx +++ b/Modules/Numerics/Optimizersv4/test/itkLBFGSBOptimizerv4Test.cxx @@ -317,18 +317,8 @@ itkLBFGSBOptimizerv4Test(int, char *[]) itkOptimizer->AddObserver(itk::IterationEvent(), eventChecker); itkOptimizer->AddObserver(itk::EndEvent(), eventChecker); - try - { - itkOptimizer->StartOptimization(); - } - catch (const itk::ExceptionObject & e) - { - std::cerr << "Exception thrown ! " << std::endl; - std::cerr << "An error occurred during Optimization" << std::endl; - std::cerr << "Location = " << e.GetLocation() << std::endl; - std::cerr << "Description = " << e.GetDescription() << std::endl; - return EXIT_FAILURE; - } + ITK_TRY_EXPECT_NO_EXCEPTION(itkOptimizer->StartOptimization()); + const OptimizerType::ParametersType & finalPosition = itkOptimizer->GetCurrentPosition(); @@ -402,18 +392,8 @@ itkLBFGSBOptimizerv4Test(int, char *[]) metric->SetParameters(initialValue); itkOptimizer->SetNumberOfIterations(1); - try - { - itkOptimizer->StartOptimization(); - } - catch (const itk::ExceptionObject & e) - { - std::cerr << "Exception thrown ! " << std::endl; - std::cerr << "An error occurred during Optimization" << std::endl; - std::cerr << "Location = " << e.GetLocation() << std::endl; - std::cerr << "Description = " << e.GetDescription() << std::endl; - return EXIT_FAILURE; - } + ITK_TRY_EXPECT_NO_EXCEPTION(itkOptimizer->StartOptimization()); + std::cout << "Solution = (" << finalPosition[0] << "," << finalPosition[1] << ")" << std::endl; std::cout << "NumberOfIterations = " << itkOptimizer->GetCurrentIteration() << std::endl; @@ -460,18 +440,8 @@ itkLBFGSBOptimizerv4Test(int, char *[]) itkOptimizer2->AddObserver(itk::IterationEvent(), eventChecker); itkOptimizer2->AddObserver(itk::EndEvent(), eventChecker); - try - { - itkOptimizer2->StartOptimization(); - } - catch (const itk::ExceptionObject & e) - { - std::cerr << "Exception thrown ! " << std::endl; - std::cerr << "An error occurred during Optimization" << std::endl; - std::cerr << "Location = " << e.GetLocation() << std::endl; - std::cerr << "Description = " << e.GetDescription() << std::endl; - return EXIT_FAILURE; - } + ITK_TRY_EXPECT_NO_EXCEPTION(itkOptimizer2->StartOptimization()); + std::cout << "Boundaries after optimization: " << std::endl; std::cout << "Upper bound size: " << itkOptimizer2->GetUpperBound().size() << std::endl; diff --git a/Modules/Numerics/Optimizersv4/test/itkPowellOptimizerv4Test.cxx b/Modules/Numerics/Optimizersv4/test/itkPowellOptimizerv4Test.cxx index ef6edeb0c3e..8e01b8e659e 100644 --- a/Modules/Numerics/Optimizersv4/test/itkPowellOptimizerv4Test.cxx +++ b/Modules/Numerics/Optimizersv4/test/itkPowellOptimizerv4Test.cxx @@ -211,18 +211,8 @@ itkPowellOptimizerv4Test(int argc, char * argv[]) itkOptimizer->SetMetricWorstPossibleValue(metricWorstPossibleValue); ITK_TEST_SET_GET_VALUE(metricWorstPossibleValue, itkOptimizer->GetMetricWorstPossibleValue()); - try - { - itkOptimizer->StartOptimization(); - } - catch (const itk::ExceptionObject & e) - { - std::cout << "Exception thrown ! " << std::endl; - std::cout << "An error occurred during Optimization" << std::endl; - std::cout << "Location = " << e.GetLocation() << std::endl; - std::cout << "Description = " << e.GetDescription() << std::endl; - return EXIT_FAILURE; - } + ITK_TRY_EXPECT_NO_EXCEPTION(itkOptimizer->StartOptimization()); + ParametersType finalPosition = itkOptimizer->GetCurrentPosition(); std::cout << "Solution = (" << finalPosition[0] << "," << finalPosition[1] << ")" << std::endl; diff --git a/Modules/Registration/Common/test/itkImageToSpatialObjectRegistrationTest.cxx b/Modules/Registration/Common/test/itkImageToSpatialObjectRegistrationTest.cxx index 88c6fe2b8e9..29918ffe0a3 100644 --- a/Modules/Registration/Common/test/itkImageToSpatialObjectRegistrationTest.cxx +++ b/Modules/Registration/Common/test/itkImageToSpatialObjectRegistrationTest.cxx @@ -293,79 +293,26 @@ itkImageToSpatialObjectRegistrationTest(int, char *[]) std::cout << "Number of Parameters : " << metric->GetNumberOfParameters() << std::endl; ITK_TEST_EXPECT_EQUAL(metric->GetNumberOfParameters(), 3); - bool catching; - try - { - catching = false; - registration->Update(); - } - catch (...) - { - catching = true; - } - - if (!catching) - { - std::cout << "Test failed!" << std::endl; - return EXIT_FAILURE; - } + // Test exception + ITK_TRY_EXPECT_EXCEPTION(registration->Update()); registration->SetFixedImage(image); ITK_TEST_SET_GET_VALUE(image, registration->GetFixedImage()); - try - { - catching = false; - registration->Update(); - } - catch (...) - { - catching = true; - } - - if (!catching) - { - std::cout << "Test failed!" << std::endl; - return EXIT_FAILURE; - } + // Test exception + ITK_TRY_EXPECT_EXCEPTION(registration->Update()); registration->SetMovingSpatialObject(group); ITK_TEST_SET_GET_VALUE(group, registration->GetMovingSpatialObject()); - try - { - catching = false; - registration->Update(); - } - catch (...) - { - catching = true; - } - - if (!catching) - { - std::cout << "Test failed!" << std::endl; - return EXIT_FAILURE; - } + // Test exception + ITK_TRY_EXPECT_EXCEPTION(registration->Update()); registration->SetMetric(metric); ITK_TEST_SET_GET_VALUE(metric, registration->GetMetric()); - try - { - catching = false; - registration->Update(); - } - catch (...) - { - catching = true; - } - - if (!catching) - { - std::cout << "Test failed!" << std::endl; - return EXIT_FAILURE; - } + // Test exception + ITK_TRY_EXPECT_EXCEPTION(registration->Update()); // Setup the optimizer TransformType::ParametersType m_ParametersScale; @@ -409,42 +356,14 @@ itkImageToSpatialObjectRegistrationTest(int, char *[]) registration->SetOptimizer(optimizer); ITK_TEST_SET_GET_VALUE(optimizer, registration->GetOptimizer()); - try - { - catching = false; - registration->Update(); - } - catch (...) - { - catching = true; - } - - if (!catching) - { - std::cout << "Test failed!" << std::endl; - return EXIT_FAILURE; - } - + // Test exception + ITK_TRY_EXPECT_EXCEPTION(registration->Update()); registration->SetTransform(transform); ITK_TEST_SET_GET_VALUE(transform, registration->GetTransform()); - - try - { - catching = false; - registration->Update(); - } - catch (...) - { - catching = true; - } - - if (!catching) - { - std::cout << "Test failed!" << std::endl; - return EXIT_FAILURE; - } + // Test exception + ITK_TRY_EXPECT_EXCEPTION(registration->Update()); registration->SetInterpolator(interpolator); ITK_TEST_SET_GET_VALUE(interpolator, registration->GetInterpolator()); diff --git a/Modules/Registration/Metricsv4/test/itkANTSNeighborhoodCorrelationImageToImageMetricv4Test.cxx b/Modules/Registration/Metricsv4/test/itkANTSNeighborhoodCorrelationImageToImageMetricv4Test.cxx index ded6e382a65..bd69138e968 100644 --- a/Modules/Registration/Metricsv4/test/itkANTSNeighborhoodCorrelationImageToImageMetricv4Test.cxx +++ b/Modules/Registration/Metricsv4/test/itkANTSNeighborhoodCorrelationImageToImageMetricv4Test.cxx @@ -261,60 +261,23 @@ itkANTSNeighborhoodCorrelationImageToImageMetricv4Test(int, char ** const) std::cout << "movingImage:" << std::endl; ANTSNeighborhoodCorrelationImageToImageMetricv4Test_PrintImage(movingImage); - /* Initialize. */ - try - { - std::cout << "Calling Initialize..." << std::endl; - metric->Initialize(); - } - catch (const itk::ExceptionObject & exc) - { - std::cerr << "Caught unexpected exception during Initialize: " << exc; - std::cerr << "Test FAILED." << std::endl; - return EXIT_FAILURE; - } + // Initialize + ITK_TRY_EXPECT_NO_EXCEPTION(metric->Initialize()); + // Evaluate MetricType::MeasureType valueReturn1; MetricType::DerivativeType derivativeReturn; - try - { - std::cout << "Calling GetValueAndDerivative..." << std::endl; - metric->GetValueAndDerivative(valueReturn1, derivativeReturn); - } - catch (const itk::ExceptionObject & exc) - { - std::cerr << "Caught unexpected exception during GetValueAndDerivative: " << exc; - std::cerr << "Test FAILED." << std::endl; - return EXIT_FAILURE; - } + ITK_TRY_EXPECT_NO_EXCEPTION(metric->GetValueAndDerivative(valueReturn1, derivativeReturn)); + + // Re-initialize + ITK_TRY_EXPECT_NO_EXCEPTION(metric->Initialize()); - /* Re-initialize. */ - try - { - std::cout << "Calling Initialize..." << std::endl; - metric->Initialize(); - } - catch (const itk::ExceptionObject & exc) - { - std::cerr << "Caught unexpected exception during re-initialize: " << exc; - std::cerr << "Test FAILED." << std::endl; - return EXIT_FAILURE; - } // Evaluate with GetValue MetricType::MeasureType valueReturn2; - try - { - std::cout << "Calling GetValue..." << std::endl; - valueReturn2 = metric->GetValue(); - } - catch (const itk::ExceptionObject & exc) - { - std::cerr << "Caught unexpected exception during GetValue: " << exc; - std::cerr << "Test FAILED." << std::endl; - return EXIT_FAILURE; - } + ITK_TRY_EXPECT_NO_EXCEPTION(valueReturn2 = metric->GetValue()); + // Test same value returned by different methods std::cout << "Check Value return values..." << std::endl; @@ -368,31 +331,13 @@ itkANTSNeighborhoodCorrelationImageToImageMetricv4Test(int, char ** const) metricSparse->SetFixedSampledPointSet(pset); metricSparse->SetUseSampledPointSet(true); - try - { - metricSparse->Initialize(); - } - catch (const itk::ExceptionObject & exc) - { - std::cerr << "Caught unexpected exception during Initialize() for sparse threader: " << exc; - std::cerr << "Test FAILED." << std::endl; - return EXIT_FAILURE; - } + ITK_TRY_EXPECT_NO_EXCEPTION(metricSparse->Initialize()); + MetricType::MeasureType valueReturnSparse; MetricType::DerivativeType derivativeReturnSparse; + ITK_TRY_EXPECT_NO_EXCEPTION(metricSparse->GetValueAndDerivative(valueReturnSparse, derivativeReturnSparse)); - try - { - std::cout << "Calling GetValueAndDerivative..." << std::endl; - metricSparse->GetValueAndDerivative(valueReturnSparse, derivativeReturnSparse); - } - catch (const itk::ExceptionObject & exc) - { - std::cerr << "Caught unexpected exception during GetValueAndDrivative() for sparse threader: " << exc; - std::cerr << "Test FAILED." << std::endl; - return EXIT_FAILURE; - } std::cout << "Check Value return values between dense and sparse threader..." << std::endl; std::cout << "dense: " << valueReturn1 << ", sparse: " << valueReturnSparse << std::endl; diff --git a/Modules/Segmentation/LevelSets/test/itkParallelSparseFieldLevelSetImageFilterTest.cxx b/Modules/Segmentation/LevelSets/test/itkParallelSparseFieldLevelSetImageFilterTest.cxx index 0d13e0ad026..25da9a4a3db 100644 --- a/Modules/Segmentation/LevelSets/test/itkParallelSparseFieldLevelSetImageFilterTest.cxx +++ b/Modules/Segmentation/LevelSets/test/itkParallelSparseFieldLevelSetImageFilterTest.cxx @@ -302,14 +302,8 @@ itkParallelSparseFieldLevelSetImageFilterTest(int argc, char * argv[]) mf->SetNumberOfLayers(numberOfLayers); ITK_TEST_SET_GET_VALUE(numberOfLayers, mf->GetNumberOfLayers()); - try - { - mf->Update(); - } - catch (const itk::ExceptionObject & e) - { - std::cerr << e << std::endl; - } + ITK_TRY_EXPECT_NO_EXCEPTION(mf->Update()); + mf->GetOutput()->Print(std::cout);