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 5f3f7e7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 134 deletions.
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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -275,73 +275,34 @@ itkFastSymmetricForcesDemonsRegistrationFilterTest(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_NO_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());
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;
return EXIT_FAILURE;
}
fptr = dynamic_cast<FunctionType *>(registrator->GetDifferenceFunction().GetPointer());
fptr->SetMovingImageInterpolator(nullptr);
registrator->SetInput(initField);

ITK_TRY_EXPECT_NO_EXCEPTION(registrator->Update());


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

0 comments on commit 5f3f7e7

Please sign in to comment.