Skip to content

Commit 2038fd7

Browse files
committed
STYLE: Use exception checking macros in tests
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.
1 parent 90799a2 commit 2038fd7

11 files changed

+62
-225
lines changed

Modules/Filtering/FFT/test/itkFFTWComplexToComplexFFTImageFilterTest.cxx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,8 @@ transformImage(const char * inputImageFileName, const char * outputImageFileName
8181
writer->SetFileName(outputImageFileName);
8282
writer->SetInput(inverseFilter->GetOutput());
8383

84-
try
85-
{
86-
writer->Update();
87-
}
88-
catch (const itk::ExceptionObject & error)
89-
{
90-
std::cerr << error << std::endl;
91-
return EXIT_FAILURE;
92-
}
84+
ITK_TRY_EXPECT_NO_EXCEPTION(writer->Update());
85+
9386

9487
return EXIT_SUCCESS;
9588
}

Modules/IO/PhilipsREC/test/itkPhilipsRECImageIOOrientationTest.cxx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,8 @@ itkPhilipsRECImageIOOrientationTest(int argc, char * argv[])
7777
writer->SetInput(subtract->GetOutput());
7878
writer->UseCompressionOn();
7979

80-
try
81-
{
82-
writer->Update();
83-
}
84-
catch (const itk::ExceptionObject & excp)
85-
{
86-
std::cerr << excp << std::endl;
87-
return EXIT_FAILURE;
88-
}
80+
ITK_TRY_EXPECT_NO_EXCEPTION(writer->Update());
81+
8982

9083
return EXIT_SUCCESS;
9184
}

Modules/IO/PhilipsREC/test/itkPhilipsRECImageIOPrintTest.cxx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,8 @@ itkPhilipsRECImageIOPrintTest(int argc, char * argv[])
4343
}
4444
imageIO->SetFileName(argv[1]);
4545

46-
try
47-
{
48-
imageIO->ReadImageInformation();
49-
}
50-
catch (const itk::ExceptionObject & excp)
51-
{
52-
std::cerr << excp << std::endl;
53-
return EXIT_FAILURE;
54-
}
46+
ITK_TRY_EXPECT_NO_EXCEPTION(imageIO->ReadImageInformation());
47+
5548

5649
// Print all of the PAR parameters.
5750
// Return EXIT_FAILURE if the value cannot be read.

Modules/IO/PhilipsREC/test/itkPhilipsRECImageIOTest.cxx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,8 @@ itkPhilipsRECImageIOTest(int argc, char * argv[])
4747

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

50-
try
51-
{
52-
writer->Update();
53-
}
54-
catch (const itk::ExceptionObject & excp)
55-
{
56-
std::cerr << excp << std::endl;
57-
return EXIT_FAILURE;
58-
}
50+
ITK_TRY_EXPECT_NO_EXCEPTION(writer->Update());
51+
5952

6053
return EXIT_SUCCESS;
6154
}

Modules/Nonunit/IntegratedTest/test/itkBioRadImageIOTest.cxx

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -46,34 +46,17 @@ itkBioRadImageIOTest(int argc, char * argv[])
4646
reader->SetImageIO(bioradImageIO);
4747
bioradImageIO->DebugOn();
4848

49-
try
50-
{
51-
reader->Update();
52-
}
53-
catch (const itk::ExceptionObject & e)
54-
{
55-
std::cerr << "exception in file reader " << std::endl;
56-
std::cerr << e << std::endl;
57-
return EXIT_FAILURE;
58-
}
49+
ITK_TRY_EXPECT_NO_EXCEPTION(reader->Update());
50+
5951

60-
//
6152
using WriterType = itk::ImageFileWriter<InputImageType>;
6253
auto writer = WriterType::New();
6354
writer->SetImageIO(bioradImageIO);
6455
writer->SetFileName(outfilename);
6556
writer->SetInput(reader->GetOutput());
6657

67-
try
68-
{
69-
writer->Update();
70-
}
71-
catch (const itk::ExceptionObject & e)
72-
{
73-
std::cerr << "exception in file writer " << std::endl;
74-
std::cerr << e << std::endl;
75-
return EXIT_FAILURE;
76-
}
58+
ITK_TRY_EXPECT_NO_EXCEPTION(writer->Update());
59+
7760

7861
bioradImageIO->Print(std::cout);
7962

Modules/Nonunit/IntegratedTest/test/itkImageToHistogramFilterTest4.cxx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,8 @@ itkImageToHistogramFilterTest4Templated(int argc, char * argv[])
8787
writer->SetInput(rescale->GetOutput());
8888
writer->SetFileName(argv[3]);
8989

90-
try
91-
{
92-
writer->Update();
93-
}
94-
catch (const itk::ExceptionObject & excp)
95-
{
96-
std::cerr << excp << std::endl;
97-
return EXIT_FAILURE;
98-
}
90+
ITK_TRY_EXPECT_NO_EXCEPTION(writer->Update());
91+
9992

10093
// print the image produced by HistogramToLogProbabilityImageFilter for visual inspection
10194
imageFilter->GetOutput()->Print(std::cout);

Modules/Nonunit/IntegratedTest/test/itkMaskedImageToHistogramFilterTest1.cxx

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "itkComposeImageFilter.h"
2626
#include "itkRescaleIntensityImageFilter.h"
2727
#include "itkSimpleFilterWatcher.h"
28+
#include "itkTestingMacros.h"
2829

2930
int
3031
itkMaskedImageToHistogramFilterTest1(int argc, char * argv[])
@@ -88,15 +89,8 @@ itkMaskedImageToHistogramFilterTest1(int argc, char * argv[])
8889
// histogramFilter->SetHistogramSize( size );
8990

9091
// TODO: this Update() shouldn't be needed - remove it.
91-
try
92-
{
93-
histogramFilter->Update();
94-
}
95-
catch (const itk::ExceptionObject & excp)
96-
{
97-
std::cerr << excp << std::endl;
98-
return EXIT_FAILURE;
99-
}
92+
ITK_TRY_EXPECT_NO_EXCEPTION(histogramFilter->Update());
93+
10094

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

117-
try
118-
{
119-
writer->Update();
120-
}
121-
catch (const itk::ExceptionObject & excp)
122-
{
123-
std::cerr << excp << std::endl;
124-
return EXIT_FAILURE;
125-
}
111+
ITK_TRY_EXPECT_NO_EXCEPTION(writer->Update());
112+
126113

127114
// print the image produced by HistogramToLogProbabilityImageFilter for visual inspection
128115
imageFilter->GetOutput()->Print(std::cout);

Modules/Numerics/FEM/test/itkFEMSolverHyperbolicTest.cxx

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "itkFEMSpatialObjectReader.h"
2121
#include "itkFEMLinearSystemWrapperDenseVNL.h"
2222
#include "itkFEMLinearSystemWrapperItpack.h"
23+
#include "itkTestingMacros.h"
2324

2425

2526
using FEMSolverType = itk::fem::SolverHyperbolic<2>;
@@ -176,16 +177,9 @@ itkFEMSolverHyperbolicTest(int argc, char * argv[])
176177
using FEMSpatialObjectReaderPointer = FEMSpatialObjectReaderType::Pointer;
177178
FEMSpatialObjectReaderPointer SpatialReader = FEMSpatialObjectReaderType::New();
178179
SpatialReader->SetFileName(argv[1]);
179-
try
180-
{
181-
SpatialReader->Update();
182-
}
183-
catch (itk::fem::FEMException & e)
184-
{
185-
std::cout << "Error reading FEM problem: " << argv[1] << "!\n";
186-
e.Print(std::cout);
187-
return EXIT_FAILURE;
188-
}
180+
181+
ITK_TRY_EXPECT_NO_EXCEPTION(SpatialReader->Update());
182+
189183

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

240-
try
241-
{
242-
SH->Update();
243-
}
244-
catch (const itk::ExceptionObject & err)
245-
{
246-
std::cerr << "ITK exception detected: " << err;
247-
return EXIT_FAILURE;
248-
}
234+
ITK_TRY_EXPECT_NO_EXCEPTION(SH->Update());
235+
249236

250237
PrintK(SH);
251238
PrintF(SH);

Modules/Registration/PDEDeformable/test/itkDemonsRegistrationFilterTest.cxx

Lines changed: 18 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -278,78 +278,38 @@ itkDemonsRegistrationFilterTest(int, char *[])
278278
std::cout << "Test running registrator without initial deformation field.";
279279
std::cout << std::endl;
280280

281-
bool passed = true;
282-
try
283-
{
284-
registrator->SetInput(nullptr);
285-
registrator->SetNumberOfIterations(2);
286-
registrator->Update();
287-
}
288-
catch (const itk::ExceptionObject & err)
289-
{
290-
std::cout << "Unexpected error." << std::endl;
291-
std::cout << err << std::endl;
292-
passed = false;
293-
}
281+
registrator->SetInput(nullptr);
282+
registrator->SetNumberOfIterations(2);
283+
284+
ITK_TRY_EXPECT_NO_EXCEPTION(registrator->Update());
294285

295-
if (!passed)
296-
{
297-
std::cout << "Test failed" << std::endl;
298-
return EXIT_FAILURE;
299-
}
300286

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

304290
std::cout << "Test nullptr moving image. " << std::endl;
305-
passed = false;
306-
try
307-
{
308-
registrator->SetInput(caster->GetOutput());
309-
registrator->SetMovingImage(nullptr);
310-
registrator->Update();
311-
}
312-
catch (const itk::ExceptionObject & err)
313-
{
314-
std::cout << "Caught expected error." << std::endl;
315-
std::cout << err << std::endl;
316-
passed = true;
317-
}
318291

319-
if (!passed)
320-
{
321-
std::cout << "Test failed" << std::endl;
322-
return EXIT_FAILURE;
323-
}
292+
registrator->SetInput(caster->GetOutput());
293+
registrator->SetMovingImage(nullptr);
294+
295+
ITK_TRY_EXPECT_EXCEPTION(registrator->Update());
296+
297+
324298
registrator->SetMovingImage(moving);
325299
registrator->ResetPipeline();
326300

327301
std::cout << "Test nullptr moving image interpolator. " << std::endl;
328-
passed = false;
329-
try
302+
fptr = dynamic_cast<FunctionType *>(registrator->GetDifferenceFunction().GetPointer());
303+
if (fptr == nullptr)
330304
{
331-
fptr = dynamic_cast<FunctionType *>(registrator->GetDifferenceFunction().GetPointer());
332-
if (fptr == nullptr)
333-
{
334-
std::cerr << "dynamic_cast failed" << std::endl;
335-
return EXIT_FAILURE;
336-
}
337-
fptr->SetMovingImageInterpolator(nullptr);
338-
registrator->SetInput(initField);
339-
registrator->Update();
340-
}
341-
catch (const itk::ExceptionObject & err)
342-
{
343-
std::cout << "Caught expected error." << std::endl;
344-
std::cout << err << std::endl;
345-
passed = true;
346-
}
347-
348-
if (!passed)
349-
{
350-
std::cout << "Test failed" << std::endl;
305+
std::cerr << "dynamic_cast failed" << std::endl;
351306
return EXIT_FAILURE;
352307
}
308+
fptr->SetMovingImageInterpolator(nullptr);
309+
registrator->SetInput(initField);
310+
311+
ITK_TRY_EXPECT_EXCEPTION(registrator->Update());
312+
353313

354314
std::cout << "Test passed" << std::endl;
355315
return EXIT_SUCCESS;

0 commit comments

Comments
 (0)