Skip to content

Commit

Permalink
STYLE: Improve variable value testing information
Browse files Browse the repository at this point in the history
Improve variable value testing information: provide informative messages
when an obtained variable value does not match the expected value,
conforming to the ITK SW Guide style guidelines.

Take advantage of the `ITK_TEST_SET_GET_VALUE` macro to do this where
appropriate.
  • Loading branch information
jhlegarreta authored and dzenanz committed Apr 16, 2022
1 parent 43630a0 commit 9302d09
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 22 deletions.
15 changes: 10 additions & 5 deletions Modules/Core/QuadEdgeMesh/test/itkQuadEdgeMeshDeleteEdgeTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,16 @@ itkQuadEdgeMeshDeleteEdgeTest(int, char *[])
mesh->DeleteEdge(pid0, pid5);

std::cout << indent << "Trying to remove only two edges...";
if (edgesBefore - mesh->ComputeNumberOfEdges() == 2)
itk::IdentifierType expectedEdgeCount = 2;
itk::IdentifierType obtainedEdgeCount = edgesBefore - mesh->ComputeNumberOfEdges();
if (obtainedEdgeCount != expectedEdgeCount)
{
std::cout << "OK." << std::endl;
return (EXIT_SUCCESS);
std::cerr << "Test failed!" << std::endl;
std::cerr << "Error in itk::QuadEdgeMesh::ComputeNumberOfEdges" << std::endl;
std::cerr << "Expected: " << expectedEdgeCount << ", but got: " << obtainedEdgeCount << std::endl;
return EXIT_FAILURE;
}
std::cout << "FAILED." << std::endl;
return (EXIT_FAILURE);

std::cout << "Test finished." << std::endl;
return EXIT_SUCCESS;
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,16 @@ itkFastMarchingQuadEdgeMeshFilterWithNumberOfElementsTest(int, char *[])
++it;
}

if (counter >= 100)
{
return EXIT_SUCCESS;
}
else
unsigned int expectedMinPointCount = 100;
if (counter < expectedMinPointCount)
{
std::cerr << "Test failed!" << std::endl;
std::cerr << "Error in itk::FastMarchingQuadEdgeMeshFilterBase" << std::endl;
std::cerr << "Expected min point count: " << expectedMinPointCount << ", but got: " << counter << std::endl;
return EXIT_FAILURE;
}


std::cout << "Test finished." << std::endl;
return EXIT_SUCCESS;
}
Original file line number Diff line number Diff line change
Expand Up @@ -326,40 +326,70 @@ PerformDisplacementFieldImageRegistration(int itkNotUsed(argc), char * argv[])
itk::NumericTraits<typename DisplacementFieldRegistrationType::RealType>::epsilon();
const typename DisplacementFieldRegistrationType::RealType learningRate = std::stod(argv[6]);
displacementFieldRegistration->SetLearningRate(learningRate);
if (displacementFieldRegistration->GetLearningRate() - learningRate > epsilon)
typename DisplacementFieldRegistrationType::RealType obtainedLearningRate =
displacementFieldRegistration->GetLearningRate();
if (!itk::Math::FloatAlmostEqual(obtainedLearningRate, learningRate, 10, epsilon))
{
std::cerr.precision(static_cast<int>(itk::Math::abs(std::log10(epsilon))));
std::cerr << "Test failed!" << std::endl;
std::cerr << "Error in GetLearningRate" << std::endl;
std::cerr << "Expected value " << learningRate << std::endl;
std::cerr << " differs from " << obtainedLearningRate;
std::cerr << " by more than " << epsilon << std::endl;
return EXIT_FAILURE;
}

displacementFieldRegistration->SetNumberOfIterationsPerLevel(numberOfIterationsPerLevel);
if (displacementFieldRegistration->GetNumberOfIterationsPerLevel() != numberOfIterationsPerLevel)
{
return EXIT_FAILURE;
}
ITK_TEST_SET_GET_VALUE(numberOfIterationsPerLevel, displacementFieldRegistration->GetNumberOfIterationsPerLevel());

displacementFieldRegistration->SetTransformParametersAdaptorsPerLevel(adaptors);

displacementFieldRegistration->SetGaussianSmoothingVarianceForTheUpdateField(varianceForUpdateField);
if (displacementFieldRegistration->GetGaussianSmoothingVarianceForTheUpdateField() - varianceForUpdateField > epsilon)
RealType obtainedVarianceForUpdateField =
displacementFieldRegistration->GetGaussianSmoothingVarianceForTheUpdateField();
if (!itk::Math::FloatAlmostEqual(obtainedVarianceForUpdateField, varianceForUpdateField, 10, epsilon))
{
std::cerr.precision(static_cast<int>(itk::Math::abs(std::log10(epsilon))));
std::cerr << "Test failed!" << std::endl;
std::cerr << "Error in GetGaussianSmoothingVarianceForTheUpdateField" << std::endl;
std::cerr << "Expected value " << obtainedVarianceForUpdateField << std::endl;
std::cerr << " differs from " << varianceForUpdateField;
std::cerr << " by more than " << epsilon << std::endl;
return EXIT_FAILURE;
}

displacementFieldRegistration->SetGaussianSmoothingVarianceForTheTotalField(varianceForTotalField);
if (displacementFieldRegistration->GetGaussianSmoothingVarianceForTheTotalField() - varianceForTotalField > epsilon)
RealType obtainedVarianceForTotalField =
displacementFieldRegistration->GetGaussianSmoothingVarianceForTheTotalField();
if (!itk::Math::FloatAlmostEqual(obtainedVarianceForTotalField, varianceForTotalField, 10, epsilon))
{
std::cerr.precision(static_cast<int>(itk::Math::abs(std::log10(epsilon))));
std::cerr << "Test failed!" << std::endl;
std::cerr << "Error in GetGaussianSmoothingVarianceForTheTotalField" << std::endl;
std::cerr << "Expected value " << obtainedVarianceForTotalField << std::endl;
std::cerr << " differs from " << varianceForTotalField;
std::cerr << " by more than " << epsilon << std::endl;
return EXIT_FAILURE;
}

const typename DisplacementFieldRegistrationType::RealType convergenceThreshold = 1.0e-6;
displacementFieldRegistration->SetConvergenceThreshold(convergenceThreshold);
if (displacementFieldRegistration->GetConvergenceThreshold() - convergenceThreshold > epsilon)
typename DisplacementFieldRegistrationType::RealType obtainedConvergenceThreshold =
displacementFieldRegistration->GetConvergenceThreshold();
if (!itk::Math::FloatAlmostEqual(obtainedConvergenceThreshold, convergenceThreshold, 10, epsilon))
{
std::cerr.precision(static_cast<int>(itk::Math::abs(std::log10(epsilon))));
std::cerr << "Test failed!" << std::endl;
std::cerr << "Error in GetConvergenceThreshold" << std::endl;
std::cerr << "Expected value " << obtainedConvergenceThreshold << std::endl;
std::cerr << " differs from " << convergenceThreshold;
std::cerr << " by more than " << epsilon << std::endl;
return EXIT_FAILURE;
}

constexpr unsigned int convergenceWindowSize = 10;
displacementFieldRegistration->SetConvergenceWindowSize(convergenceWindowSize);
if (displacementFieldRegistration->GetConvergenceWindowSize() != convergenceWindowSize)
{
return EXIT_FAILURE;
}
ITK_TEST_SET_GET_VALUE(convergenceWindowSize, displacementFieldRegistration->GetConvergenceWindowSize());

using DisplacementFieldCommandType = CommandIterationUpdate<DisplacementFieldRegistrationType>;
auto DisplacementFieldObserver = DisplacementFieldCommandType::New();
Expand Down

0 comments on commit 9302d09

Please sign in to comment.