From 9302d09298563dda858f0c43dc11bf2c388db692 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jon=20Haitz=20Legarreta=20Gorro=C3=B1o?= Date: Fri, 15 Apr 2022 14:59:05 -0400 Subject: [PATCH] STYLE: Improve variable value testing information 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. --- .../test/itkQuadEdgeMeshDeleteEdgeTest.cxx | 15 ++++-- ...EdgeMeshFilterWithNumberOfElementsTest.cxx | 14 +++-- .../test/itkSyNImageRegistrationTest.cxx | 54 ++++++++++++++----- 3 files changed, 61 insertions(+), 22 deletions(-) diff --git a/Modules/Core/QuadEdgeMesh/test/itkQuadEdgeMeshDeleteEdgeTest.cxx b/Modules/Core/QuadEdgeMesh/test/itkQuadEdgeMeshDeleteEdgeTest.cxx index 95f255d6018..a72b15af568 100644 --- a/Modules/Core/QuadEdgeMesh/test/itkQuadEdgeMeshDeleteEdgeTest.cxx +++ b/Modules/Core/QuadEdgeMesh/test/itkQuadEdgeMeshDeleteEdgeTest.cxx @@ -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; } diff --git a/Modules/Filtering/FastMarching/test/itkFastMarchingQuadEdgeMeshFilterWithNumberOfElementsTest.cxx b/Modules/Filtering/FastMarching/test/itkFastMarchingQuadEdgeMeshFilterWithNumberOfElementsTest.cxx index ccd042baf07..e9f0809076f 100644 --- a/Modules/Filtering/FastMarching/test/itkFastMarchingQuadEdgeMeshFilterWithNumberOfElementsTest.cxx +++ b/Modules/Filtering/FastMarching/test/itkFastMarchingQuadEdgeMeshFilterWithNumberOfElementsTest.cxx @@ -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; } diff --git a/Modules/Registration/RegistrationMethodsv4/test/itkSyNImageRegistrationTest.cxx b/Modules/Registration/RegistrationMethodsv4/test/itkSyNImageRegistrationTest.cxx index f6a3be641e8..88b19772b31 100644 --- a/Modules/Registration/RegistrationMethodsv4/test/itkSyNImageRegistrationTest.cxx +++ b/Modules/Registration/RegistrationMethodsv4/test/itkSyNImageRegistrationTest.cxx @@ -326,40 +326,70 @@ PerformDisplacementFieldImageRegistration(int itkNotUsed(argc), char * argv[]) itk::NumericTraits::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(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(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(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(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; auto DisplacementFieldObserver = DisplacementFieldCommandType::New();