Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STYLE: Use exception checking macros in tests #4147

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading