Skip to content

Commit

Permalink
STYLE: Prefer using exception checking 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`
exception checking 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 2, 2022
1 parent 2127b6d commit 5f737c0
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 96 deletions.
29 changes: 3 additions & 26 deletions Modules/Core/Common/test/itkMatrixTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "itkMatrix.h"
#include "itkMath.h"
#include "itkTestingMacros.h"

int
itkMatrixTest(int, char *[])
Expand Down Expand Up @@ -334,34 +335,10 @@ itkMatrixTest(int, char *[])
using LargeMatrixType = itk::Matrix<NumericType, 7, 7>;
LargeMatrixType matrixBad;
matrixBad.Fill(2.0);
bool caught = false;
try
{
matrixBad.GetInverse();
}
catch (const itk::ExceptionObject & excp)
{
std::cout << "Caught expected exception!" << std::endl;
std::cout << excp;
caught = true;
}
if (!caught)
{
std::cout << "Failed to catch expected exception!" << std::endl;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_EXCEPTION(matrixBad.GetInverse());

matrixBad.SetIdentity();
try
{
matrixBad.GetInverse();
}
catch (const itk::ExceptionObject & excp)
{
std::cout << "Caught unexpected exception!" << std::endl;
std::cout << excp;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(matrixBad.GetInverse());

{
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,25 +265,9 @@ itkGradientDescentLineSearchOptimizerv4Test(int, char *[])
std::cout << "Stop description = " << itkOptimizer->GetStopConditionDescription() << std::endl;

auto badOptimizer = OptimizerType::New();
bool caught = false;
try
{
badOptimizer->GetCurrentPosition();
}
catch (const itk::ExceptionObject & e)
{
std::cout << "Caught expected exception!";
std::cout << e << std::endl;
caught = true;
}

if (!caught)
{
std::cout << "Failed to catch expected exception! " << std::endl;
return EXIT_FAILURE;
}
std::cout << "Printing self.. " << std::endl;
std::cout << itkOptimizer << std::endl;
ITK_TRY_EXPECT_EXCEPTION(badOptimizer->GetCurrentPosition());


std::cout << "Test passed." << std::endl;
return EXIT_SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,8 @@ itkGaussianMembershipFunctionTest(int, char *[])

// Test if an exception will be thrown if we try to resize the measurement vector
// size
std::cout << "***" << std::endl;
std::cout << "Exception TEST: " << std::endl;
try
{
MeasurementVectorSizeType measurementVector2 = MeasurementVectorSize + 1;
function->SetMeasurementVectorSize(measurementVector2);
std::cerr << "Exception should have been thrown since we are trying to resize\
non-resizeable measurement vector type "
<< std::endl;
// return EXIT_FAILURE;
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << "Caughted expected exception: " << excp << std::endl;
}

MeasurementVectorSizeType measurementVector2 = MeasurementVectorSize + 1;
ITK_TRY_EXPECT_EXCEPTION(function->SetMeasurementVectorSize(measurementVector2));

// Test if the membership function value computed is correct
MembershipFunctionType::MeanVectorType mean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,47 +316,16 @@ ImageToImageMetricv4TestRunSingleTest(const ImageToImageMetricv4TestMetricPointe
ImageToImageMetricv4TestMetricType::DerivativeType derivativeReturn;

// Initialize.
try
{
metric->Initialize();
}
catch (const itk::ExceptionObject & exc)
{
std::cerr << "Caught unexpected exception during Initialize: " << exc;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(metric->Initialize());

// Evaluate using GetValue
try
{
valueReturn1 = metric->GetValue();
}
catch (const itk::ExceptionObject & exc)
{
std::cerr << "Caught unexpected exception during GetValue: " << exc;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(valueReturn1 = metric->GetValue());

// Re-initialize.
try
{
metric->Initialize();
}
catch (const itk::ExceptionObject & exc)
{
std::cerr << "Caught unexpected exception during re-initialize: " << exc;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(metric->Initialize());

// Evaluate using GetValueAndDerivative
try
{
metric->GetValueAndDerivative(valueReturn2, derivativeReturn);
}
catch (const itk::ExceptionObject & exc)
{
std::cerr << "Caught unexpected exception during GetValueAndDerivative: " << exc;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_NO_EXCEPTION(metric->GetValueAndDerivative(valueReturn2, derivativeReturn));

// Test same value returned by different methods
std::cout << "Check Value return values..." << std::endl;
Expand Down

0 comments on commit 5f737c0

Please sign in to comment.