Skip to content

Commit

Permalink
STYLE: Use macros in tests
Browse files Browse the repository at this point in the history
Use macros in tests:
- Use `ITK_TRY_EXPECT_NO_EXCEPTION` and `ITK_TRY_EXPECT_EXCEPTION`
  macros in lieu of `try/catch` blocks for the sake of readability and
  compactness, and to save typing/avoid boilerplate code.
- Use other testing macros as necessary to check for expected values for
  the sake of readability and compactness, and to save typing/avoid
  boilerplate code.
- Only place with the macro the code that might raise exceptions.

Take advantage of the commit to remove unnecessary/uninformative
comments and messages where the code has been modified.
  • Loading branch information
jhlegarreta committed Sep 2, 2023
1 parent 3eb8fd7 commit 3c23723
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 369 deletions.
40 changes: 4 additions & 36 deletions Modules/Core/Common/test/itkBSplineKernelFunctionTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
*=========================================================================*/

#include "itkBSplineDerivativeKernelFunction.h"
#include "itkTestingMacros.h"


/*
* This test exercises the BSpline kernel function
Expand Down Expand Up @@ -192,24 +194,7 @@ itkBSplineKernelFunctionTest(int, char *[])
using FunctionType = itk::BSplineKernelFunction<7>;
auto function = FunctionType::New();

bool pass = false;
try
{
function->Evaluate(0.0);
}
catch (const itk::ExceptionObject & err)
{
std::cout << "Caught expected exception" << std::endl;
std::cout << err << std::endl;
pass = true;
}

if (!pass)
{
std::cout << "Did not catch expected exception" << std::endl;
std::cout << "Test failed" << std::endl;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_EXCEPTION(function->Evaluate(0.0));
}

// Testing case of unimplemented spline order
Expand All @@ -218,24 +203,7 @@ itkBSplineKernelFunctionTest(int, char *[])
using FunctionType = itk::BSplineDerivativeKernelFunction<5>;
auto function = FunctionType::New();

bool pass = false;
try
{
function->Evaluate(0.0);
}
catch (const itk::ExceptionObject & err)
{
std::cout << "Caught expected exception" << std::endl;
std::cout << err << std::endl;
pass = true;
}

if (!pass)
{
std::cout << "Did not catch expected exception" << std::endl;
std::cout << "Test failed" << std::endl;
return EXIT_FAILURE;
}
ITK_TRY_EXPECT_EXCEPTION(function->Evaluate(0.0));
}


Expand Down
36 changes: 8 additions & 28 deletions Modules/Core/SpatialObjects/test/itkImageSpatialObjectTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -94,28 +94,15 @@ itkImageSpatialObjectTest(int, char *[])
q.Fill(15);

std::cout << "Bounding Box = " << imageSO->GetMyBoundingBoxInWorldSpace()->GetBounds() << std::endl;
std::cout << "IsInside()...";
if (imageSO->IsInsideInWorldSpace(r) || !imageSO->IsInsideInWorldSpace(q))
{
std::cout << "[FAILED]" << std::endl;
return EXIT_FAILURE;
}
else
{
std::cout << "[PASSED]" << std::endl;
}

ITK_TEST_EXPECT_TRUE(!imageSO->IsInsideInWorldSpace(r));
ITK_TEST_EXPECT_TRUE(imageSO->IsInsideInWorldSpace(q));

q.Fill(15.1);
expectedValue = 555;

try
{
imageSO->ValueAtInWorldSpace(q, returnedValue);
}
catch (const itk::ExceptionObject &)
{
throw;
}
ITK_TRY_EXPECT_NO_EXCEPTION(imageSO->ValueAtInWorldSpace(q, returnedValue));


std::cout << "ValueAt()...";
if (itk::Math::NotAlmostEquals(returnedValue, expectedValue))
Expand All @@ -140,16 +127,9 @@ itkImageSpatialObjectTest(int, char *[])
expectedDerivative[1] = expectedPixel;
expectedPixel = 100;
expectedDerivative[2] = expectedPixel;
std::cout << "DerivativeAt()...";
if (derivative != expectedDerivative)
{
std::cout << "[FAILED]" << std::endl;
return EXIT_FAILURE;
}
else
{
std::cout << "[PASSED]" << std::endl;
}

ITK_TEST_EXPECT_EQUAL(derivative, expectedDerivative);


// Now testing the ValueAt() with an interpolator
using InterpolatorType = itk::LinearInterpolateImageFunction<ImageType>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,31 +256,10 @@ itkDisplacementFieldTransformTest(int argc, char * argv[])
DisplacementFieldType::DirectionType direction = field->GetDirection();
DisplacementFieldType::SpacingType spacing = field->GetSpacing();

if (size != size2)
{
std::cerr << "Test failed!" << std::endl;
std::cerr << "Incorrect size from fixed parameters." << std::endl;
return EXIT_FAILURE;
}
if (origin != origin2)
{
std::cerr << "Test failed!" << std::endl;
std::cerr << "Incorrect origin from fixed parameters." << std::endl;
return EXIT_FAILURE;
}
if (spacing != spacing2)
{
std::cerr << "Test failed!" << std::endl;
std::cerr << "Incorrect spacing from fixed parameters." << std::endl;
return EXIT_FAILURE;
}
if (direction != direction2)
{
std::cerr << "Test failed!" << std::endl;
std::cerr << "Incorrect direction from fixed parameters." << std::endl;
return EXIT_FAILURE;
}

ITK_TEST_EXPECT_EQUAL(size, size2);
ITK_TEST_EXPECT_EQUAL(origin, origin2);
ITK_TEST_EXPECT_EQUAL(spacing, spacing2);
ITK_TEST_EXPECT_EQUAL(direction, direction2);

// Initialize Affine transform and use it to create the displacement field
using AffineTransformType = itk::CenteredAffineTransform<ParametersValueType, Dimensions>;
Expand Down
60 changes: 15 additions & 45 deletions Modules/Filtering/ImageGrid/test/itkExpandImageFilterTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "itkCastImageFilter.h"
#include "itkStreamingImageFilter.h"
#include "itkMath.h"
#include "itkTestingMacros.h"

// class to produce a linear image pattern
template <int VDimension>
Expand Down Expand Up @@ -232,58 +233,27 @@ itkExpandImageFilterTest(int, char *[])
++streamIter;
}

// Test error handling

if (!testPassed)
{
std::cout << "Test failed." << std::endl;
return EXIT_FAILURE;
}
std::cout << "Setting Input to nullptr" << std::endl;
expander->SetInput(nullptr);

// Test error handling
ITK_TRY_EXPECT_EXCEPTION(expander->Update());

try
{
testPassed = false;
std::cout << "Setting Input to nullptr" << std::endl;
expander->SetInput(nullptr);
expander->Update();
}
catch (const itk::ExceptionObject & err)
{
std::cout << err << std::endl;
expander->ResetPipeline();
expander->SetInput(input);
testPassed = true;
}

if (!testPassed)
{
std::cout << "Test failed." << std::endl;
return EXIT_FAILURE;
}
expander->ResetPipeline();
expander->SetInput(input);

std::cout << "Setting Interpolator to nullptr" << std::endl;
expander->SetInterpolator(nullptr);

try
{
testPassed = false;
std::cout << "Setting Interpolator to nullptr" << std::endl;
expander->SetInterpolator(nullptr);
expander->Update();
}
catch (const itk::ExceptionObject & err)
{
std::cout << err << std::endl;
expander->ResetPipeline();
expander->SetInterpolator(interpolator);
testPassed = true;
}
ITK_TRY_EXPECT_EXCEPTION(expander->Update());


expander->ResetPipeline();
expander->SetInterpolator(interpolator);

if (!testPassed)
{
std::cout << "Test failed." << std::endl;
return EXIT_FAILURE;
}

std::cout << "Test passed." << std::endl;
return EXIT_SUCCESS;
return testPassed;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "itkVectorImage.h"
#include "itkVariableLengthVector.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "itkTestingMacros.h"

using PixelType = double;
using VectorImage1D = itk::VectorImage<PixelType, 1>;
Expand Down Expand Up @@ -216,11 +217,8 @@ itkExpandImageFilterTest2(int, char *[])
std::cout << PrintTestImage1D<VectorImage1D>(output1D) << '\n';

auto s1 = output1D->GetLargestPossibleRegion().GetSize()[0];
if (s1 != 10)
{
std::cout << "Expected 1D image size 10, actual: " << s1;
statusValue = EXIT_FAILURE;
}

ITK_TEST_EXPECT_EQUAL(s1, 10);

double slice1[10] = { 6, 6, 7, 7, 8, 8, 9, 9, 10, 10 };
double sliceOut1[10] = {};
Expand Down
38 changes: 10 additions & 28 deletions Modules/Filtering/ImageGrid/test/itkWarpImageFilterTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -372,39 +372,21 @@ itkWarpImageFilterTest(int, char *[])
++streamIter;
}


if (!testPassed)
{
std::cout << "Test failed." << std::endl;
return EXIT_FAILURE;
}

// Exercise error handling


using InterpolatorType = WarperType::InterpolatorType;
InterpolatorType::Pointer interp = warper->GetModifiableInterpolator();
try
{
std::cout << "Setting interpolator to nullptr" << std::endl;
testPassed = false;
warper->SetInterpolator(nullptr);
warper->Update();
}
catch (const itk::ExceptionObject & err)
{
std::cout << err << std::endl;
testPassed = true;
warper->ResetPipeline();
warper->SetInterpolator(interp);
}

if (!testPassed)
{
std::cout << "Test failed" << std::endl;
return EXIT_FAILURE;
}
std::cout << "Setting interpolator to nullptr" << std::endl;
warper->SetInterpolator(nullptr);

ITK_TRY_EXPECT_EXCEPTION(warper->Update());


warper->ResetPipeline();
warper->SetInterpolator(interp);


std::cout << "Test passed." << std::endl;
return EXIT_SUCCESS;
return testPassed;
}
25 changes: 9 additions & 16 deletions Modules/Filtering/ImageGrid/test/itkWarpImageFilterTest2.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "itkPipelineMonitorImageFilter.h"
#include "itkMath.h"
#include "itkTestingMacros.h"

using ImageType = itk::Image<float, 3>;
using DisplacementFieldType = itk::Image<itk::Vector<double, 3>, 3>;
Expand Down Expand Up @@ -134,11 +135,9 @@ itkWarpImageFilterTest2(int, char *[])
return EXIT_FAILURE;
}
}
if (it1.IsAtEnd() != it2.IsAtEnd())
{
std::cout << "Iterators don't agree on end of image" << std::endl;
return EXIT_FAILURE;
}

ITK_TEST_EXPECT_EQUAL(it1.IsAtEnd(), it2.IsAtEnd());

//
// try streaming
auto monitor1 = MonitorFilter::New();
Expand Down Expand Up @@ -167,20 +166,14 @@ itkWarpImageFilterTest2(int, char *[])
return EXIT_FAILURE;
}
}
if (streamIt.IsAtEnd() != it2.IsAtEnd())
{
std::cout << "Iterators don't agree on end of image" << std::endl;
return EXIT_FAILURE;
}

ITK_TEST_EXPECT_EQUAL(streamIt.IsAtEnd(), it2.IsAtEnd());


// this verifies that the pipeline was executed as expected along
// with correct region propagation and output information
if (!monitor2->VerifyAllInputCanStream(4))
{
std::cout << "Filter failed to execute as expected!" << std::endl;
std::cout << monitor2;
return EXIT_FAILURE;
}
ITK_TEST_EXPECT_TRUE(!monitor2->VerifyAllInputCanStream(4));


return EXIT_SUCCESS;
}
29 changes: 9 additions & 20 deletions Modules/Filtering/ImageGrid/test/itkWarpVectorImageFilterTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -353,27 +353,16 @@ itkWarpVectorImageFilterTest(int, char *[])
using InterpolatorType = WarperType::InterpolatorType;
InterpolatorType::Pointer interp = warper->GetModifiableInterpolator();

try
{
std::cout << "Setting interpolator to nullptr" << std::endl;
testPassed = false;
warper->SetInterpolator(nullptr);
warper->Update();
}
catch (const itk::ExceptionObject & err)
{
std::cout << err << std::endl;
testPassed = true;
warper->ResetPipeline();
warper->SetInterpolator(interp);
ITK_TEST_SET_GET_VALUE(interp, warper->GetInterpolator());
}
std::cout << "Setting interpolator to nullptr" << std::endl;
warper->SetInterpolator(nullptr);

ITK_TRY_EXPECT_EXCEPTION(warper->Update());

warper->ResetPipeline();
warper->SetInterpolator(interp);

ITK_TEST_SET_GET_VALUE(interp, warper->GetInterpolator());

if (!testPassed)
{
std::cout << "Test failed" << std::endl;
return EXIT_FAILURE;
}

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

0 comments on commit 3c23723

Please sign in to comment.