Skip to content

Commit 66a9a27

Browse files
jhlegarretadzenanz
authored andcommitted
STYLE: Prefer using the ITK_TRY_EXPECT macros in tests
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.
1 parent 3b88398 commit 66a9a27

File tree

4 files changed

+17
-91
lines changed

4 files changed

+17
-91
lines changed

Modules/IO/ImageBase/test/itkImageFileReaderTest1.cxx

Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*=========================================================================*/
1818

1919
#include "itkImageFileReader.h"
20+
#include "itkTestingMacros.h"
2021

2122

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

3536
// Try an empty read
36-
int status = 1;
37-
try
38-
{
39-
auto reader = ReaderType::New();
40-
reader->Update();
41-
}
42-
catch (const itk::ExceptionObject & ex)
43-
{
44-
std::cout << "------------------ Caught expected exception!" << std::endl;
45-
std::cout << ex;
46-
status = 0;
47-
}
48-
if (status)
49-
{
50-
std::cout << "Failed to catch expected exception." << std::endl;
51-
return EXIT_FAILURE;
52-
}
37+
auto reader = ReaderType::New();
38+
ITK_TRY_EXPECT_EXCEPTION(reader->Update());
5339

5440

5541
// Now try a read with an image that doesn't exist
56-
status = 1;
57-
try
58-
{
59-
auto reader = ReaderType::New();
60-
reader->SetFileName("this_file_should_not_exist");
61-
reader->Update();
62-
}
63-
catch (const itk::ExceptionObject & ex)
64-
{
65-
std::cout << "------------------ Caught expected exception!" << std::endl;
66-
std::cout << ex;
67-
status = 0;
68-
}
69-
if (status)
70-
{
71-
std::cout << "Failed to catch expected exception." << std::endl;
72-
return EXIT_FAILURE;
73-
}
42+
reader->SetFileName("this_file_should_not_exist");
43+
ITK_TRY_EXPECT_EXCEPTION(reader->Update());
44+
7445

7546
// Let's try to read a file where no ImageIO can read it
76-
status = 1;
77-
try
78-
{
79-
auto reader = ReaderType::New();
80-
// this is the executable and no reader should be able to read it
81-
reader->SetFileName(argv[0]);
82-
reader->Update();
83-
}
84-
catch (const itk::ExceptionObject & ex)
85-
{
86-
std::cout << "------------------ Caught expected exception!" << std::endl;
87-
std::cout << ex;
88-
status = 0;
89-
}
90-
if (status)
91-
{
92-
std::cout << "Failed to catch expected exception." << std::endl;
93-
return EXIT_FAILURE;
94-
}
47+
// This is the executable and no reader should be able to read it
48+
reader->SetFileName(argv[0]);
49+
ITK_TRY_EXPECT_EXCEPTION(reader->Update());
9550

9651

9752
return EXIT_SUCCESS;

Modules/Registration/RegistrationMethodsv4/test/itkImageRegistrationSamplingTest.cxx

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*=========================================================================*/
1818

1919
#include "itkImageRegistrationMethodv4.h"
20+
#include "itkTestingMacros.h"
2021

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

36-
try
37-
{
38-
registrationMethod->SetMetricSamplingPercentage(0.1);
39-
}
40-
catch (const itk::ExceptionObject & e)
41-
{
42-
std::cerr << "Unexpected exception caught: " << e << std::endl;
43-
return EXIT_FAILURE;
44-
}
37+
ITK_TRY_EXPECT_NO_EXCEPTION(registrationMethod->SetMetricSamplingPercentage(0.1));
38+
4539

4640
constexpr unsigned int NUM_ERRORS = 3;
4741
RegistrationType::RealType errorValues[NUM_ERRORS] = { -0.1, 0.0, 1.1 };
4842
for (double errorValue : errorValues)
4943
{
50-
try
51-
{
52-
registrationMethod->SetMetricSamplingPercentage(errorValue);
53-
return EXIT_FAILURE;
54-
}
55-
catch (const itk::ExceptionObject &)
56-
{
57-
std::cerr << "Caught expected exception." << std::endl;
58-
}
44+
ITK_TRY_EXPECT_EXCEPTION(registrationMethod->SetMetricSamplingPercentage(errorValue));
5945
}
6046
return EXIT_SUCCESS;
6147
}

Modules/Registration/RegistrationMethodsv4/test/itkSimpleImageRegistrationTest4.cxx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,8 @@ ImageRegistration(int itkNotUsed(argc), char * argv[])
137137
auto observer = CommandType::New();
138138
optimizer->AddObserver(itk::IterationEvent(), observer);
139139

140-
try
141-
{
142-
registration->Update();
143-
}
144-
catch (const itk::ExceptionObject & e)
145-
{
146-
std::cerr << "Exception caught: " << e << std::endl;
147-
return EXIT_FAILURE;
148-
}
140+
ITK_TRY_EXPECT_NO_EXCEPTION(registration->Update());
141+
149142

150143
registration->GetTransform()->Print(std::cout);
151144
std::cout << optimizer->GetStopConditionDescription() << std::endl;

Modules/Registration/RegistrationMethodsv4/test/itkSimplePointSetRegistrationTest.cxx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -228,16 +228,8 @@ itkSimplePointSetRegistrationTest(int itkNotUsed(argc), char * itkNotUsed(argv)[
228228
auto affineObserver = AffineCommandType::New();
229229
affineSimple->AddObserver(itk::MultiResolutionIterationEvent(), affineObserver);
230230

231-
try
232-
{
233-
std::cout << "Point set affine registration update" << std::endl;
234-
affineSimple->Update();
235-
}
236-
catch (const itk::ExceptionObject & e)
237-
{
238-
std::cerr << "Exception caught: " << e << std::endl;
239-
return EXIT_FAILURE;
240-
}
231+
ITK_TRY_EXPECT_NO_EXCEPTION(affineSimple->Update());
232+
241233

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

0 commit comments

Comments
 (0)