Skip to content

Commit

Permalink
STYLE: Use exception checking macros in tests
Browse files Browse the repository at this point in the history
Use exception checking macros in tests:
- Use `ITK_TRY_EXPECT_NO_EXCEPTION` and `ITK_TRY_EXPECT_EXCEPTION`
  macros when updating filters in lieu of `try/catch` blocks for the
  sake of readability and compactness, and to save typing/avoid
  boilerplate code.
- Only place the code that might raise exceptions within the macro.
  • Loading branch information
jhlegarreta committed Aug 8, 2023
1 parent 90799a2 commit 2038fd7
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 225 deletions.
Expand Up @@ -81,15 +81,8 @@ transformImage(const char * inputImageFileName, const char * outputImageFileName
writer->SetFileName(outputImageFileName);
writer->SetInput(inverseFilter->GetOutput());

try
{
writer->Update();
}
catch (const itk::ExceptionObject & error)
{
std::cerr << error << std::endl;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(writer->Update());


return EXIT_SUCCESS;
}
Expand Down
Expand Up @@ -77,15 +77,8 @@ itkPhilipsRECImageIOOrientationTest(int argc, char * argv[])
writer->SetInput(subtract->GetOutput());
writer->UseCompressionOn();

try
{
writer->Update();
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(writer->Update());


return EXIT_SUCCESS;
}
11 changes: 2 additions & 9 deletions Modules/IO/PhilipsREC/test/itkPhilipsRECImageIOPrintTest.cxx
Expand Up @@ -43,15 +43,8 @@ itkPhilipsRECImageIOPrintTest(int argc, char * argv[])
}
imageIO->SetFileName(argv[1]);

try
{
imageIO->ReadImageInformation();
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(imageIO->ReadImageInformation());


// Print all of the PAR parameters.
// Return EXIT_FAILURE if the value cannot be read.
Expand Down
11 changes: 2 additions & 9 deletions Modules/IO/PhilipsREC/test/itkPhilipsRECImageIOTest.cxx
Expand Up @@ -47,15 +47,8 @@ itkPhilipsRECImageIOTest(int argc, char * argv[])

writer->SetInput(reader->GetOutput());

try
{
writer->Update();
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(writer->Update());


return EXIT_SUCCESS;
}
25 changes: 4 additions & 21 deletions Modules/Nonunit/IntegratedTest/test/itkBioRadImageIOTest.cxx
Expand Up @@ -46,34 +46,17 @@ itkBioRadImageIOTest(int argc, char * argv[])
reader->SetImageIO(bioradImageIO);
bioradImageIO->DebugOn();

try
{
reader->Update();
}
catch (const itk::ExceptionObject & e)
{
std::cerr << "exception in file reader " << std::endl;
std::cerr << e << std::endl;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(reader->Update());


//
using WriterType = itk::ImageFileWriter<InputImageType>;
auto writer = WriterType::New();
writer->SetImageIO(bioradImageIO);
writer->SetFileName(outfilename);
writer->SetInput(reader->GetOutput());

try
{
writer->Update();
}
catch (const itk::ExceptionObject & e)
{
std::cerr << "exception in file writer " << std::endl;
std::cerr << e << std::endl;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(writer->Update());


bioradImageIO->Print(std::cout);

Expand Down
Expand Up @@ -87,15 +87,8 @@ itkImageToHistogramFilterTest4Templated(int argc, char * argv[])
writer->SetInput(rescale->GetOutput());
writer->SetFileName(argv[3]);

try
{
writer->Update();
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(writer->Update());


// print the image produced by HistogramToLogProbabilityImageFilter for visual inspection
imageFilter->GetOutput()->Print(std::cout);
Expand Down
Expand Up @@ -25,6 +25,7 @@
#include "itkComposeImageFilter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "itkSimpleFilterWatcher.h"
#include "itkTestingMacros.h"

int
itkMaskedImageToHistogramFilterTest1(int argc, char * argv[])
Expand Down Expand Up @@ -88,15 +89,8 @@ itkMaskedImageToHistogramFilterTest1(int argc, char * argv[])
// histogramFilter->SetHistogramSize( size );

// TODO: this Update() shouldn't be needed - remove it.
try
{
histogramFilter->Update();
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(histogramFilter->Update());


// use a 3D image to check the behavior of HistogramToImageFilter when the image
// is of greater dimension than the histogram
Expand All @@ -114,15 +108,8 @@ itkMaskedImageToHistogramFilterTest1(int argc, char * argv[])
writer->SetInput(rescale->GetOutput());
writer->SetFileName(argv[5]);

try
{
writer->Update();
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(writer->Update());


// print the image produced by HistogramToLogProbabilityImageFilter for visual inspection
imageFilter->GetOutput()->Print(std::cout);
Expand Down
25 changes: 6 additions & 19 deletions Modules/Numerics/FEM/test/itkFEMSolverHyperbolicTest.cxx
Expand Up @@ -20,6 +20,7 @@
#include "itkFEMSpatialObjectReader.h"
#include "itkFEMLinearSystemWrapperDenseVNL.h"
#include "itkFEMLinearSystemWrapperItpack.h"
#include "itkTestingMacros.h"


using FEMSolverType = itk::fem::SolverHyperbolic<2>;
Expand Down Expand Up @@ -176,16 +177,9 @@ itkFEMSolverHyperbolicTest(int argc, char * argv[])
using FEMSpatialObjectReaderPointer = FEMSpatialObjectReaderType::Pointer;
FEMSpatialObjectReaderPointer SpatialReader = FEMSpatialObjectReaderType::New();
SpatialReader->SetFileName(argv[1]);
try
{
SpatialReader->Update();
}
catch (itk::fem::FEMException & e)
{
std::cout << "Error reading FEM problem: " << argv[1] << "!\n";
e.Print(std::cout);
return EXIT_FAILURE;
}

ITK_TRY_EXPECT_NO_EXCEPTION(SpatialReader->Update());


using FEMObjectSpatialObjectType = itk::FEMObjectSpatialObject<2>;
FEMObjectSpatialObjectType::ChildrenListType * children = SpatialReader->GetGroup()->GetChildren();
Expand Down Expand Up @@ -237,15 +231,8 @@ itkFEMSolverHyperbolicTest(int argc, char * argv[])
break;
}

try
{
SH->Update();
}
catch (const itk::ExceptionObject & err)
{
std::cerr << "ITK exception detected: " << err;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(SH->Update());


PrintK(SH);
PrintF(SH);
Expand Down
Expand Up @@ -278,78 +278,38 @@ itkDemonsRegistrationFilterTest(int, char *[])
std::cout << "Test running registrator without initial deformation field.";
std::cout << std::endl;

bool passed = true;
try
{
registrator->SetInput(nullptr);
registrator->SetNumberOfIterations(2);
registrator->Update();
}
catch (const itk::ExceptionObject & err)
{
std::cout << "Unexpected error." << std::endl;
std::cout << err << std::endl;
passed = false;
}
registrator->SetInput(nullptr);
registrator->SetNumberOfIterations(2);

ITK_TRY_EXPECT_NO_EXCEPTION(registrator->Update());

if (!passed)
{
std::cout << "Test failed" << std::endl;
return EXIT_FAILURE;
}

//--------------------------------------------------------------
std::cout << "Test exception handling." << std::endl;

std::cout << "Test nullptr moving image. " << std::endl;
passed = false;
try
{
registrator->SetInput(caster->GetOutput());
registrator->SetMovingImage(nullptr);
registrator->Update();
}
catch (const itk::ExceptionObject & err)
{
std::cout << "Caught expected error." << std::endl;
std::cout << err << std::endl;
passed = true;
}

if (!passed)
{
std::cout << "Test failed" << std::endl;
return EXIT_FAILURE;
}
registrator->SetInput(caster->GetOutput());
registrator->SetMovingImage(nullptr);

ITK_TRY_EXPECT_EXCEPTION(registrator->Update());


registrator->SetMovingImage(moving);
registrator->ResetPipeline();

std::cout << "Test nullptr moving image interpolator. " << std::endl;
passed = false;
try
fptr = dynamic_cast<FunctionType *>(registrator->GetDifferenceFunction().GetPointer());
if (fptr == nullptr)
{
fptr = dynamic_cast<FunctionType *>(registrator->GetDifferenceFunction().GetPointer());
if (fptr == nullptr)
{
std::cerr << "dynamic_cast failed" << std::endl;
return EXIT_FAILURE;
}
fptr->SetMovingImageInterpolator(nullptr);
registrator->SetInput(initField);
registrator->Update();
}
catch (const itk::ExceptionObject & err)
{
std::cout << "Caught expected error." << std::endl;
std::cout << err << std::endl;
passed = true;
}

if (!passed)
{
std::cout << "Test failed" << std::endl;
std::cerr << "dynamic_cast failed" << std::endl;
return EXIT_FAILURE;
}
fptr->SetMovingImageInterpolator(nullptr);
registrator->SetInput(initField);

ITK_TRY_EXPECT_EXCEPTION(registrator->Update());


std::cout << "Test passed" << std::endl;
return EXIT_SUCCESS;
Expand Down

0 comments on commit 2038fd7

Please sign in to comment.