Skip to content

Commit 67d18e4

Browse files
jhlegarretadzenanz
authored andcommitted
STYLE: Make non-macro comparison conditional blocks consistent in tests
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.
1 parent f9b153a commit 67d18e4

20 files changed

+291
-186
lines changed

Modules/Core/Common/test/itkBSplineKernelFunctionTest.cxx

Lines changed: 56 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -63,26 +63,30 @@ itkBSplineKernelFunctionTest(int, char *[])
6363

6464

6565
// Testing the output of BSplineKernelFunction
66-
#define TEST_BSPLINE_KERNEL(ORDERNUM) \
67-
{ \
68-
using FunctionType = itk::BSplineKernelFunction<ORDERNUM>; \
69-
auto function = FunctionType::New(); \
70-
\
71-
function->Print(std::cout); \
72-
for (unsigned int j = 0; j < npoints; ++j) \
73-
{ \
74-
double results = function->Evaluate(x[j]); \
75-
/* compare with external results */ \
76-
if (itk::Math::abs(results - b##ORDERNUM[j]) > 1e-6) \
77-
{ \
78-
std::cout << "Error with " << ORDERNUM << " order BSplineKernelFunction" << std::endl; \
79-
std::cout << "Expected: " << b##ORDERNUM[j] << " but got " << results; \
80-
std::cout << " at x = " << x[j] << std::endl; \
81-
std::cout << "Test failed" << std::endl; \
82-
return EXIT_FAILURE; \
83-
} \
84-
} \
85-
} \
66+
#define TEST_BSPLINE_KERNEL(ORDERNUM) \
67+
{ \
68+
using FunctionType = itk::BSplineKernelFunction<ORDERNUM>; \
69+
auto function = FunctionType::New(); \
70+
\
71+
function->Print(std::cout); \
72+
const double epsilon = 1e-6; \
73+
for (unsigned int j = 0; j < npoints; ++j) \
74+
{ \
75+
double results = function->Evaluate(x[j]); \
76+
/* compare with external results */ \
77+
if (itk::Math::abs(results - b##ORDERNUM[j]) > epsilon) \
78+
{ \
79+
std::cerr.precision(static_cast<int>(itk::Math::abs(std::log10(epsilon)))); \
80+
std::cerr << "Test failed!" << std::endl; \
81+
std::cerr << "Error with " << ORDERNUM << " order BSplineKernelFunction "; \
82+
std::cerr << "at index [" << j << "] " << std::endl; \
83+
std::cerr << "Expected value " << b##ORDERNUM[j] << std::endl; \
84+
std::cerr << " differs from " << results; \
85+
std::cerr << " by more than " << epsilon << std::endl; \
86+
return EXIT_FAILURE; \
87+
} \
88+
} \
89+
} \
8690
ITK_MACROEND_NOOP_STATEMENT
8791

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

104-
if (itk::Math::abs(results - expectedValue) > 1e-6)
108+
const double epsilon = 1e-6;
109+
if (itk::Math::abs(results - expectedValue) > epsilon)
105110
{
106-
std::cout << "Error with " << SplineOrder << " order BSplineDerivativeKernelFunction" << std::endl;
107-
std::cout << "Expected: " << expectedValue << " but got " << results;
108-
std::cout << " at x = " << xx << std::endl;
109-
std::cout << "Test failed" << std::endl;
111+
std::cerr.precision(static_cast<int>(itk::Math::abs(std::log10(epsilon))));
112+
std::cerr << "Test failed!" << std::endl;
113+
std::cerr << "Error with " << SplineOrder << " order BSplineDerivativeKernelFunction at " << xx << std::endl;
114+
std::cerr << "Expected value " << expectedValue << std::endl;
115+
std::cerr << " differs from " << results;
116+
std::cerr << " by more than " << epsilon << std::endl;
110117
return EXIT_FAILURE;
111118
}
112119
}
@@ -125,12 +132,15 @@ itkBSplineKernelFunctionTest(int, char *[])
125132
double expectedValue = function->Evaluate(xx + 0.5) - function->Evaluate(xx - 0.5);
126133
double results = derivFunction->Evaluate(xx);
127134

128-
if (itk::Math::abs(results - expectedValue) > 1e-6)
135+
const double epsilon = 1e-6;
136+
if (itk::Math::abs(results - expectedValue) > epsilon)
129137
{
130-
std::cout << "Error with " << SplineOrder << " order BSplineDerivativeKernelFunction" << std::endl;
131-
std::cout << "Expected: " << expectedValue << " but got " << results;
132-
std::cout << " at x = " << xx << std::endl;
133-
std::cout << "Test failed" << std::endl;
138+
std::cerr.precision(static_cast<int>(itk::Math::abs(std::log10(epsilon))));
139+
std::cerr << "Test failed!" << std::endl;
140+
std::cerr << "Error with " << SplineOrder << " order BSplineDerivativeKernelFunction at " << xx << std::endl;
141+
std::cerr << "Expected value " << expectedValue << std::endl;
142+
std::cerr << " differs from " << results;
143+
std::cerr << " by more than " << epsilon << std::endl;
134144
return EXIT_FAILURE;
135145
}
136146
}
@@ -151,12 +161,15 @@ itkBSplineKernelFunctionTest(int, char *[])
151161
double expectedValue = function->Evaluate(xx + 0.5) - function->Evaluate(xx - 0.5);
152162
double results = derivFunction->Evaluate(xx);
153163

154-
if (itk::Math::abs(results - expectedValue) > 1e-6)
164+
const double epsilon = 1e-6;
165+
if (itk::Math::abs(results - expectedValue) > epsilon)
155166
{
156-
std::cout << "Error with " << SplineOrder << " order BSplineDerivativeKernelFunction" << std::endl;
157-
std::cout << "Expected: " << expectedValue << " but got " << results;
158-
std::cout << " at x = " << xx << std::endl;
159-
std::cout << "Test failed" << std::endl;
167+
std::cerr.precision(static_cast<int>(itk::Math::abs(std::log10(epsilon))));
168+
std::cerr << "Test failed!" << std::endl;
169+
std::cerr << "Error with " << SplineOrder << " order BSplineDerivativeKernelFunction at " << xx << std::endl;
170+
std::cerr << "Expected value " << expectedValue << std::endl;
171+
std::cerr << " differs from " << results;
172+
std::cerr << " by more than " << epsilon << std::endl;
160173
return EXIT_FAILURE;
161174
}
162175
}
@@ -177,12 +190,15 @@ itkBSplineKernelFunctionTest(int, char *[])
177190
double expectedValue = function->Evaluate(xx + 0.5) - function->Evaluate(xx - 0.5);
178191
double results = derivFunction->Evaluate(xx);
179192

180-
if (itk::Math::abs(results - expectedValue) > 1e-6)
193+
const double epsilon = 1e-6;
194+
if (itk::Math::abs(results - expectedValue) > epsilon)
181195
{
182-
std::cout << "Error with " << SplineOrder << " order BSplineDerivativeKernelFunction" << std::endl;
183-
std::cout << "Expected: " << expectedValue << " but got " << results;
184-
std::cout << " at x = " << xx << std::endl;
185-
std::cout << "Test failed" << std::endl;
196+
std::cerr.precision(static_cast<int>(itk::Math::abs(std::log10(epsilon))));
197+
std::cerr << "Test failed!" << std::endl;
198+
std::cerr << "Error with " << SplineOrder << " order BSplineDerivativeKernelFunction at " << xx << std::endl;
199+
std::cerr << "Expected value " << expectedValue << std::endl;
200+
std::cerr << " differs from " << results;
201+
std::cerr << " by more than " << epsilon << std::endl;
186202
return EXIT_FAILURE;
187203
}
188204
}

Modules/Core/Common/test/itkGaussianSpatialFunctionTest.cxx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,10 @@ itkGaussianSpatialFunctionTest(int argc, char * argv[])
104104

105105
if (itk::Math::NotAlmostEquals(expectedValueAtMean, computedValueAtMean))
106106
{
107-
std::cout << "Error in point " << point << ": ";
108-
std::cout << "expected: " << expectedValueAtMean << ", but got " << computedValueAtMean << std::endl;
109-
std::cout << "Test failed" << std::endl;
107+
std::cerr << "Test failed!" << std::endl;
108+
std::cerr << "Error in Evaluate at point " << point << std::endl;
109+
std::cerr << "Expected value " << expectedValueAtMean << std::endl;
110+
std::cerr << " differs from " << computedValueAtMean << std::endl;
110111
return EXIT_FAILURE;
111112
}
112113

Modules/Core/ImageFunction/test/itkBSplineDecompositionImageFilterTest.cxx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ itkBSplineDecompositionImageFilterTest(int argc, char * argv[])
120120
FilterType::SplinePolesVectorType::value_type resultSplinePole = resultSplinePoles[i];
121121
if (!itk::Math::FloatAlmostEqual(expectedSplinePole, resultSplinePole, 10, tolerance1))
122122
{
123-
std::cout.precision(static_cast<int>(itk::Math::abs(std::log10(tolerance1))));
124-
std::cout << "Test failed!" << std::endl;
125-
std::cout << "Error in GetSplinePoles() at index: [" << i << ']' << std::endl;
126-
std::cout << "Expected: " << expectedSplinePole << std::endl;
127-
std::cout << " , but got: " << resultSplinePole << std::endl;
128-
std::cout << " Values differ by more than: " << tolerance1 << std::endl;
123+
std::cerr.precision(static_cast<int>(itk::Math::abs(std::log10(tolerance1))));
124+
std::cerr << "Test failed!" << std::endl;
125+
std::cerr << "Error in GetSplinePoles() at index [" << i << ']' << std::endl;
126+
std::cerr << "Expected value " << expectedSplinePole << std::endl;
127+
std::cerr << " differs from " << resultSplinePole;
128+
std::cerr << " by more than " << tolerance1 << std::endl;
129129
return EXIT_FAILURE;
130130
}
131131
}

Modules/Core/SpatialObjects/test/itkImageSpatialObjectTest.cxx

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,10 @@ itkImageSpatialObjectTest(int, char *[])
107107
std::cout << "ValueAt()...";
108108
if (itk::Math::NotAlmostEquals(returnedValue, expectedValue))
109109
{
110-
std::cout << "Expected: " << expectedValue << " returned: " << returnedValue << std::endl;
111-
std::cout << "[FAILED]: " << std::endl;
110+
std::cerr << "Test failed!" << std::endl;
111+
std::cerr << "Error in ValueAt at point " << q << std::endl;
112+
std::cerr << "Expected value " << expectedValue << std::endl;
113+
std::cerr << " differs from " << returnedValue << std::endl;
112114
return EXIT_FAILURE;
113115
}
114116
else
@@ -143,9 +145,15 @@ itkImageSpatialObjectTest(int, char *[])
143145

144146

145147
std::cout << "ValueAt() with interpolator...";
146-
if (itk::Math::abs(returnedValue - expectedValue) > 0.001)
148+
double epsilon = 0.001;
149+
if (itk::Math::abs(returnedValue - expectedValue) > epsilon)
147150
{
148-
std::cout << "Expected: " << expectedValue << " returned: " << returnedValue << std::endl;
151+
std::cerr.precision(static_cast<int>(itk::Math::abs(std::log10(epsilon))));
152+
std::cerr << "Test failed!" << std::endl;
153+
std::cerr << "Error in ValueAt at point " << q << std::endl;
154+
std::cerr << "Expected value " << expectedValue << std::endl;
155+
std::cerr << " differs from " << returnedValue;
156+
std::cerr << " by more than " << epsilon << std::endl;
149157
return EXIT_FAILURE;
150158
}
151159
else
@@ -159,12 +167,17 @@ itkImageSpatialObjectTest(int, char *[])
159167
expectedDerivative[1] = 10;
160168
expectedDerivative[2] = 100;
161169
std::cout << "DerivativeAt() with interpolator ...";
162-
if (itk::Math::abs(derivative[0] - expectedDerivative[0]) > 0.00001 ||
163-
itk::Math::abs(derivative[1] - expectedDerivative[1]) > 0.00001 ||
164-
itk::Math::abs(derivative[2] - expectedDerivative[2]) > 0.00001)
170+
epsilon = 0.00001;
171+
if (itk::Math::abs(derivative[0] - expectedDerivative[0]) > epsilon ||
172+
itk::Math::abs(derivative[1] - expectedDerivative[1]) > epsilon ||
173+
itk::Math::abs(derivative[2] - expectedDerivative[2]) > epsilon)
165174
{
166-
std::cout << "Expected: " << derivative << " returned: " << expectedDerivative << std::endl;
167-
std::cout << "[FAILED]" << std::endl;
175+
std::cerr.precision(static_cast<int>(itk::Math::abs(std::log10(epsilon))));
176+
std::cerr << "Test failed!" << std::endl;
177+
std::cerr << "Error in ValueAt at point " << q << std::endl;
178+
std::cerr << "Expected value " << expectedDerivative << std::endl;
179+
std::cerr << " differs from " << derivative;
180+
std::cerr << " by more than " << epsilon << std::endl;
168181
return EXIT_FAILURE;
169182
}
170183
else

Modules/Core/Transform/test/itkEuler2DTransformTest.cxx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -386,9 +386,12 @@ itkEuler2DTransformTest(int, char *[])
386386
{
387387
if (itk::Math::abs(parameters3[j] - pdash[j]) > epsilon)
388388
{
389-
std::cout << "Expected: " << parameters3 << std::endl;
390-
std::cout << "Got: " << pdash << std::endl;
391-
std::cout << " [ FAILED ] " << std::endl;
389+
std::cerr.precision(static_cast<int>(itk::Math::abs(std::log10(epsilon))));
390+
std::cerr << "Test failed!" << std::endl;
391+
std::cerr << "Error in parameters at index [" << j << "]" << std::endl;
392+
std::cerr << "Expected value " << parameters3 << std::endl;
393+
std::cerr << " differs from " << pdash;
394+
std::cerr << " by more than " << epsilon << std::endl;
392395
return EXIT_FAILURE;
393396
}
394397
}

Modules/Core/Transform/test/itkSimilarity2DTransformTest.cxx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -628,9 +628,12 @@ itkSimilarity2DTransformTest(int, char *[])
628628
{
629629
if (itk::Math::abs(parameters[j] - pdash[j]) > epsilon)
630630
{
631-
std::cout << "Expected: " << parameters << std::endl;
632-
std::cout << "Got: " << pdash << std::endl;
633-
std::cout << " [ FAILED ] " << std::endl;
631+
std::cerr.precision(static_cast<int>(itk::Math::abs(std::log10(epsilon))));
632+
std::cerr << "Test failed!" << std::endl;
633+
std::cerr << "Error in parameters at index [" << j << "]" << std::endl;
634+
std::cerr << "Expected value " << parameters << std::endl;
635+
std::cerr << " differs from " << pdash;
636+
std::cerr << " by more than " << epsilon << std::endl;
634637
return EXIT_FAILURE;
635638
}
636639
}

Modules/Filtering/DisplacementField/test/itkDisplacementFieldJacobianDeterminantFilterTest.cxx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,9 @@ TestDisplacementJacobianDeterminantValue()
108108
double epsilon = 1e-13;
109109
if (itk::Math::abs(jacobianDeterminant - expectedJacobianDeterminant) > epsilon)
110110
{
111+
std::cerr.precision(static_cast<int>(itk::Math::abs(std::log10(epsilon))));
111112
std::cerr << "Test failed!" << std::endl;
112-
std::cerr << "Error in pixel value at index [" << index << ']' << std::endl;
113+
std::cerr << "Error in pixel value at index [" << index << "]" << std::endl;
113114
std::cerr << "Expected value " << jacobianDeterminant << std::endl;
114115
std::cerr << " differs from " << expectedJacobianDeterminant;
115116
std::cerr << " by more than " << epsilon << std::endl;

0 commit comments

Comments
 (0)