Skip to content

Commit

Permalink
STYLE: Make non-macro comparison conditional blocks consistent in tests
Browse files Browse the repository at this point in the history
Make non-macro comparison conditional blocks consistent in tests:
adhere to the ITK SWG guidelines:
- Report the messages to `std::cerr`.
- Make the printed messages be consistent.
- Declare and re-use epsilon/tolerance values.
  • Loading branch information
jhlegarreta authored and dzenanz committed Sep 6, 2023
1 parent f9b153a commit 67d18e4
Show file tree
Hide file tree
Showing 20 changed files with 291 additions and 186 deletions.
96 changes: 56 additions & 40 deletions Modules/Core/Common/test/itkBSplineKernelFunctionTest.cxx
Expand Up @@ -63,26 +63,30 @@ itkBSplineKernelFunctionTest(int, char *[])


// Testing the output of BSplineKernelFunction
#define TEST_BSPLINE_KERNEL(ORDERNUM) \
{ \
using FunctionType = itk::BSplineKernelFunction<ORDERNUM>; \
auto function = FunctionType::New(); \
\
function->Print(std::cout); \
for (unsigned int j = 0; j < npoints; ++j) \
{ \
double results = function->Evaluate(x[j]); \
/* compare with external results */ \
if (itk::Math::abs(results - b##ORDERNUM[j]) > 1e-6) \
{ \
std::cout << "Error with " << ORDERNUM << " order BSplineKernelFunction" << std::endl; \
std::cout << "Expected: " << b##ORDERNUM[j] << " but got " << results; \
std::cout << " at x = " << x[j] << std::endl; \
std::cout << "Test failed" << std::endl; \
return EXIT_FAILURE; \
} \
} \
} \
#define TEST_BSPLINE_KERNEL(ORDERNUM) \
{ \
using FunctionType = itk::BSplineKernelFunction<ORDERNUM>; \
auto function = FunctionType::New(); \
\
function->Print(std::cout); \
const double epsilon = 1e-6; \
for (unsigned int j = 0; j < npoints; ++j) \
{ \
double results = function->Evaluate(x[j]); \
/* compare with external results */ \
if (itk::Math::abs(results - b##ORDERNUM[j]) > epsilon) \
{ \
std::cerr.precision(static_cast<int>(itk::Math::abs(std::log10(epsilon)))); \
std::cerr << "Test failed!" << std::endl; \
std::cerr << "Error with " << ORDERNUM << " order BSplineKernelFunction "; \
std::cerr << "at index [" << j << "] " << std::endl; \
std::cerr << "Expected value " << b##ORDERNUM[j] << std::endl; \
std::cerr << " differs from " << results; \
std::cerr << " by more than " << epsilon << std::endl; \
return EXIT_FAILURE; \
} \
} \
} \
ITK_MACROEND_NOOP_STATEMENT

TEST_BSPLINE_KERNEL(0);
Expand All @@ -101,12 +105,15 @@ itkBSplineKernelFunctionTest(int, char *[])
double expectedValue = 0.0;
double results = derivFunction->Evaluate(xx);

if (itk::Math::abs(results - expectedValue) > 1e-6)
const double epsilon = 1e-6;
if (itk::Math::abs(results - expectedValue) > epsilon)
{
std::cout << "Error with " << SplineOrder << " order BSplineDerivativeKernelFunction" << std::endl;
std::cout << "Expected: " << expectedValue << " but got " << results;
std::cout << " at x = " << xx << std::endl;
std::cout << "Test failed" << std::endl;
std::cerr.precision(static_cast<int>(itk::Math::abs(std::log10(epsilon))));
std::cerr << "Test failed!" << std::endl;
std::cerr << "Error with " << SplineOrder << " order BSplineDerivativeKernelFunction at " << xx << std::endl;
std::cerr << "Expected value " << expectedValue << std::endl;
std::cerr << " differs from " << results;
std::cerr << " by more than " << epsilon << std::endl;
return EXIT_FAILURE;
}
}
Expand All @@ -125,12 +132,15 @@ itkBSplineKernelFunctionTest(int, char *[])
double expectedValue = function->Evaluate(xx + 0.5) - function->Evaluate(xx - 0.5);
double results = derivFunction->Evaluate(xx);

if (itk::Math::abs(results - expectedValue) > 1e-6)
const double epsilon = 1e-6;
if (itk::Math::abs(results - expectedValue) > epsilon)
{
std::cout << "Error with " << SplineOrder << " order BSplineDerivativeKernelFunction" << std::endl;
std::cout << "Expected: " << expectedValue << " but got " << results;
std::cout << " at x = " << xx << std::endl;
std::cout << "Test failed" << std::endl;
std::cerr.precision(static_cast<int>(itk::Math::abs(std::log10(epsilon))));
std::cerr << "Test failed!" << std::endl;
std::cerr << "Error with " << SplineOrder << " order BSplineDerivativeKernelFunction at " << xx << std::endl;
std::cerr << "Expected value " << expectedValue << std::endl;
std::cerr << " differs from " << results;
std::cerr << " by more than " << epsilon << std::endl;
return EXIT_FAILURE;
}
}
Expand All @@ -151,12 +161,15 @@ itkBSplineKernelFunctionTest(int, char *[])
double expectedValue = function->Evaluate(xx + 0.5) - function->Evaluate(xx - 0.5);
double results = derivFunction->Evaluate(xx);

if (itk::Math::abs(results - expectedValue) > 1e-6)
const double epsilon = 1e-6;
if (itk::Math::abs(results - expectedValue) > epsilon)
{
std::cout << "Error with " << SplineOrder << " order BSplineDerivativeKernelFunction" << std::endl;
std::cout << "Expected: " << expectedValue << " but got " << results;
std::cout << " at x = " << xx << std::endl;
std::cout << "Test failed" << std::endl;
std::cerr.precision(static_cast<int>(itk::Math::abs(std::log10(epsilon))));
std::cerr << "Test failed!" << std::endl;
std::cerr << "Error with " << SplineOrder << " order BSplineDerivativeKernelFunction at " << xx << std::endl;
std::cerr << "Expected value " << expectedValue << std::endl;
std::cerr << " differs from " << results;
std::cerr << " by more than " << epsilon << std::endl;
return EXIT_FAILURE;
}
}
Expand All @@ -177,12 +190,15 @@ itkBSplineKernelFunctionTest(int, char *[])
double expectedValue = function->Evaluate(xx + 0.5) - function->Evaluate(xx - 0.5);
double results = derivFunction->Evaluate(xx);

if (itk::Math::abs(results - expectedValue) > 1e-6)
const double epsilon = 1e-6;
if (itk::Math::abs(results - expectedValue) > epsilon)
{
std::cout << "Error with " << SplineOrder << " order BSplineDerivativeKernelFunction" << std::endl;
std::cout << "Expected: " << expectedValue << " but got " << results;
std::cout << " at x = " << xx << std::endl;
std::cout << "Test failed" << std::endl;
std::cerr.precision(static_cast<int>(itk::Math::abs(std::log10(epsilon))));
std::cerr << "Test failed!" << std::endl;
std::cerr << "Error with " << SplineOrder << " order BSplineDerivativeKernelFunction at " << xx << std::endl;
std::cerr << "Expected value " << expectedValue << std::endl;
std::cerr << " differs from " << results;
std::cerr << " by more than " << epsilon << std::endl;
return EXIT_FAILURE;
}
}
Expand Down
7 changes: 4 additions & 3 deletions Modules/Core/Common/test/itkGaussianSpatialFunctionTest.cxx
Expand Up @@ -104,9 +104,10 @@ itkGaussianSpatialFunctionTest(int argc, char * argv[])

if (itk::Math::NotAlmostEquals(expectedValueAtMean, computedValueAtMean))
{
std::cout << "Error in point " << point << ": ";
std::cout << "expected: " << expectedValueAtMean << ", but got " << computedValueAtMean << std::endl;
std::cout << "Test failed" << std::endl;
std::cerr << "Test failed!" << std::endl;
std::cerr << "Error in Evaluate at point " << point << std::endl;
std::cerr << "Expected value " << expectedValueAtMean << std::endl;
std::cerr << " differs from " << computedValueAtMean << std::endl;
return EXIT_FAILURE;
}

Expand Down
Expand Up @@ -120,12 +120,12 @@ itkBSplineDecompositionImageFilterTest(int argc, char * argv[])
FilterType::SplinePolesVectorType::value_type resultSplinePole = resultSplinePoles[i];
if (!itk::Math::FloatAlmostEqual(expectedSplinePole, resultSplinePole, 10, tolerance1))
{
std::cout.precision(static_cast<int>(itk::Math::abs(std::log10(tolerance1))));
std::cout << "Test failed!" << std::endl;
std::cout << "Error in GetSplinePoles() at index: [" << i << ']' << std::endl;
std::cout << "Expected: " << expectedSplinePole << std::endl;
std::cout << " , but got: " << resultSplinePole << std::endl;
std::cout << " Values differ by more than: " << tolerance1 << std::endl;
std::cerr.precision(static_cast<int>(itk::Math::abs(std::log10(tolerance1))));
std::cerr << "Test failed!" << std::endl;
std::cerr << "Error in GetSplinePoles() at index [" << i << ']' << std::endl;
std::cerr << "Expected value " << expectedSplinePole << std::endl;
std::cerr << " differs from " << resultSplinePole;
std::cerr << " by more than " << tolerance1 << std::endl;
return EXIT_FAILURE;
}
}
Expand Down
31 changes: 22 additions & 9 deletions Modules/Core/SpatialObjects/test/itkImageSpatialObjectTest.cxx
Expand Up @@ -107,8 +107,10 @@ itkImageSpatialObjectTest(int, char *[])
std::cout << "ValueAt()...";
if (itk::Math::NotAlmostEquals(returnedValue, expectedValue))
{
std::cout << "Expected: " << expectedValue << " returned: " << returnedValue << std::endl;
std::cout << "[FAILED]: " << std::endl;
std::cerr << "Test failed!" << std::endl;
std::cerr << "Error in ValueAt at point " << q << std::endl;
std::cerr << "Expected value " << expectedValue << std::endl;
std::cerr << " differs from " << returnedValue << std::endl;
return EXIT_FAILURE;
}
else
Expand Down Expand Up @@ -143,9 +145,15 @@ itkImageSpatialObjectTest(int, char *[])


std::cout << "ValueAt() with interpolator...";
if (itk::Math::abs(returnedValue - expectedValue) > 0.001)
double epsilon = 0.001;
if (itk::Math::abs(returnedValue - expectedValue) > epsilon)
{
std::cout << "Expected: " << expectedValue << " returned: " << returnedValue << std::endl;
std::cerr.precision(static_cast<int>(itk::Math::abs(std::log10(epsilon))));
std::cerr << "Test failed!" << std::endl;
std::cerr << "Error in ValueAt at point " << q << std::endl;
std::cerr << "Expected value " << expectedValue << std::endl;
std::cerr << " differs from " << returnedValue;
std::cerr << " by more than " << epsilon << std::endl;
return EXIT_FAILURE;
}
else
Expand All @@ -159,12 +167,17 @@ itkImageSpatialObjectTest(int, char *[])
expectedDerivative[1] = 10;
expectedDerivative[2] = 100;
std::cout << "DerivativeAt() with interpolator ...";
if (itk::Math::abs(derivative[0] - expectedDerivative[0]) > 0.00001 ||
itk::Math::abs(derivative[1] - expectedDerivative[1]) > 0.00001 ||
itk::Math::abs(derivative[2] - expectedDerivative[2]) > 0.00001)
epsilon = 0.00001;
if (itk::Math::abs(derivative[0] - expectedDerivative[0]) > epsilon ||
itk::Math::abs(derivative[1] - expectedDerivative[1]) > epsilon ||
itk::Math::abs(derivative[2] - expectedDerivative[2]) > epsilon)
{
std::cout << "Expected: " << derivative << " returned: " << expectedDerivative << std::endl;
std::cout << "[FAILED]" << std::endl;
std::cerr.precision(static_cast<int>(itk::Math::abs(std::log10(epsilon))));
std::cerr << "Test failed!" << std::endl;
std::cerr << "Error in ValueAt at point " << q << std::endl;
std::cerr << "Expected value " << expectedDerivative << std::endl;
std::cerr << " differs from " << derivative;
std::cerr << " by more than " << epsilon << std::endl;
return EXIT_FAILURE;
}
else
Expand Down
9 changes: 6 additions & 3 deletions Modules/Core/Transform/test/itkEuler2DTransformTest.cxx
Expand Up @@ -386,9 +386,12 @@ itkEuler2DTransformTest(int, char *[])
{
if (itk::Math::abs(parameters3[j] - pdash[j]) > epsilon)
{
std::cout << "Expected: " << parameters3 << std::endl;
std::cout << "Got: " << pdash << std::endl;
std::cout << " [ FAILED ] " << std::endl;
std::cerr.precision(static_cast<int>(itk::Math::abs(std::log10(epsilon))));
std::cerr << "Test failed!" << std::endl;
std::cerr << "Error in parameters at index [" << j << "]" << std::endl;
std::cerr << "Expected value " << parameters3 << std::endl;
std::cerr << " differs from " << pdash;
std::cerr << " by more than " << epsilon << std::endl;
return EXIT_FAILURE;
}
}
Expand Down
9 changes: 6 additions & 3 deletions Modules/Core/Transform/test/itkSimilarity2DTransformTest.cxx
Expand Up @@ -628,9 +628,12 @@ itkSimilarity2DTransformTest(int, char *[])
{
if (itk::Math::abs(parameters[j] - pdash[j]) > epsilon)
{
std::cout << "Expected: " << parameters << std::endl;
std::cout << "Got: " << pdash << std::endl;
std::cout << " [ FAILED ] " << std::endl;
std::cerr.precision(static_cast<int>(itk::Math::abs(std::log10(epsilon))));
std::cerr << "Test failed!" << std::endl;
std::cerr << "Error in parameters at index [" << j << "]" << std::endl;
std::cerr << "Expected value " << parameters << std::endl;
std::cerr << " differs from " << pdash;
std::cerr << " by more than " << epsilon << std::endl;
return EXIT_FAILURE;
}
}
Expand Down
Expand Up @@ -108,8 +108,9 @@ TestDisplacementJacobianDeterminantValue()
double epsilon = 1e-13;
if (itk::Math::abs(jacobianDeterminant - expectedJacobianDeterminant) > epsilon)
{
std::cerr.precision(static_cast<int>(itk::Math::abs(std::log10(epsilon))));
std::cerr << "Test failed!" << std::endl;
std::cerr << "Error in pixel value at index [" << index << ']' << std::endl;
std::cerr << "Error in pixel value at index [" << index << "]" << std::endl;
std::cerr << "Expected value " << jacobianDeterminant << std::endl;
std::cerr << " differs from " << expectedJacobianDeterminant;
std::cerr << " by more than " << epsilon << std::endl;
Expand Down

0 comments on commit 67d18e4

Please sign in to comment.