Skip to content

Commit

Permalink
STYLE: Prefer using the ITK_TRY_EXPECT macros in tests
Browse files Browse the repository at this point in the history
Use the `ITK_TRY_EXPECT_EXCEPTION` and the `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.
  • Loading branch information
jhlegarreta authored and dzenanz committed Sep 19, 2022
1 parent 3b88398 commit 66a9a27
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 91 deletions.
63 changes: 9 additions & 54 deletions Modules/IO/ImageBase/test/itkImageFileReaderTest1.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*=========================================================================*/

#include "itkImageFileReader.h"
#include "itkTestingMacros.h"


int
Expand All @@ -33,65 +34,19 @@ itkImageFileReaderTest1(int argc, char * argv[])
using ReaderType = itk::ImageFileReader<ImageNDType>;

// Try an empty read
int status = 1;
try
{
auto reader = ReaderType::New();
reader->Update();
}
catch (const itk::ExceptionObject & ex)
{
std::cout << "------------------ Caught expected exception!" << std::endl;
std::cout << ex;
status = 0;
}
if (status)
{
std::cout << "Failed to catch expected exception." << std::endl;
return EXIT_FAILURE;
}
auto reader = ReaderType::New();
ITK_TRY_EXPECT_EXCEPTION(reader->Update());


// Now try a read with an image that doesn't exist
status = 1;
try
{
auto reader = ReaderType::New();
reader->SetFileName("this_file_should_not_exist");
reader->Update();
}
catch (const itk::ExceptionObject & ex)
{
std::cout << "------------------ Caught expected exception!" << std::endl;
std::cout << ex;
status = 0;
}
if (status)
{
std::cout << "Failed to catch expected exception." << std::endl;
return EXIT_FAILURE;
}
reader->SetFileName("this_file_should_not_exist");
ITK_TRY_EXPECT_EXCEPTION(reader->Update());


// Let's try to read a file where no ImageIO can read it
status = 1;
try
{
auto reader = ReaderType::New();
// this is the executable and no reader should be able to read it
reader->SetFileName(argv[0]);
reader->Update();
}
catch (const itk::ExceptionObject & ex)
{
std::cout << "------------------ Caught expected exception!" << std::endl;
std::cout << ex;
status = 0;
}
if (status)
{
std::cout << "Failed to catch expected exception." << std::endl;
return EXIT_FAILURE;
}
// This is the executable and no reader should be able to read it
reader->SetFileName(argv[0]);
ITK_TRY_EXPECT_EXCEPTION(reader->Update());


return EXIT_SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*=========================================================================*/

#include "itkImageRegistrationMethodv4.h"
#include "itkTestingMacros.h"

/*
* Test the SetMetricSamplingPercentage and SetMetricSamplingPercentagePerLevel.
Expand All @@ -33,29 +34,14 @@ itkImageRegistrationSamplingTest(int, char *[])
using RegistrationType = itk::ImageRegistrationMethodv4<FixedImageType, MovingImageType>;
auto registrationMethod = RegistrationType::New();

try
{
registrationMethod->SetMetricSamplingPercentage(0.1);
}
catch (const itk::ExceptionObject & e)
{
std::cerr << "Unexpected exception caught: " << e << std::endl;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(registrationMethod->SetMetricSamplingPercentage(0.1));


constexpr unsigned int NUM_ERRORS = 3;
RegistrationType::RealType errorValues[NUM_ERRORS] = { -0.1, 0.0, 1.1 };
for (double errorValue : errorValues)
{
try
{
registrationMethod->SetMetricSamplingPercentage(errorValue);
return EXIT_FAILURE;
}
catch (const itk::ExceptionObject &)
{
std::cerr << "Caught expected exception." << std::endl;
}
ITK_TRY_EXPECT_EXCEPTION(registrationMethod->SetMetricSamplingPercentage(errorValue));
}
return EXIT_SUCCESS;
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,8 @@ ImageRegistration(int itkNotUsed(argc), char * argv[])
auto observer = CommandType::New();
optimizer->AddObserver(itk::IterationEvent(), observer);

try
{
registration->Update();
}
catch (const itk::ExceptionObject & e)
{
std::cerr << "Exception caught: " << e << std::endl;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(registration->Update());


registration->GetTransform()->Print(std::cout);
std::cout << optimizer->GetStopConditionDescription() << std::endl;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,8 @@ itkSimplePointSetRegistrationTest(int itkNotUsed(argc), char * itkNotUsed(argv)[
auto affineObserver = AffineCommandType::New();
affineSimple->AddObserver(itk::MultiResolutionIterationEvent(), affineObserver);

try
{
std::cout << "Point set affine registration update" << std::endl;
affineSimple->Update();
}
catch (const itk::ExceptionObject & e)
{
std::cerr << "Exception caught: " << e << std::endl;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(affineSimple->Update());


// applying the resultant transform to moving points and verify result
std::cout << "Fixed\tMoving\tMovingTransformed\tFixedTransformed\tDiff" << std::endl;
Expand Down

0 comments on commit 66a9a27

Please sign in to comment.