Skip to content

Commit

Permalink
ENH: Increase coverage for miscellaneous classes
Browse files Browse the repository at this point in the history
Increase coverage for miscellaneous classes:
- Exercise basic object methods using the
  `ITK_EXERCISE_BASIC_OBJECT_METHODS` macro. Remove redundant calls to
  print the filter: rely on the basic method exercising macro call.
- Test the Set/Get methods using the `ITK_TEST_SET_GET_VALUE` macro.
- Test the boolean ivars using the `ITK_TEST_SET_GET_BOOLEAN` macro.
- Use other testing macros to check the expected value of a number of
  ivars and method return values.
- Test exceptions using the `ITK_TRY_EXPECT_EXCEPTION` macro.
- Refactor the tests to accept (more) input parameters to allow checking
  the classes under a broader range of conditions.
- Set additional data to filter ivars as necessary to increase coverage.

Take advantage of the commit to improve the input argument check message
where it had to be modified.
  • Loading branch information
jhlegarreta authored and dzenanz committed Apr 17, 2022
1 parent 755e894 commit 6765363
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 38 deletions.
5 changes: 5 additions & 0 deletions Modules/Core/Common/test/itkImageLinearIteratorTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <iostream>

#include "itkImageLinearIteratorWithIndex.h"
#include "itkTestingMacros.h"

int
itkImageLinearIteratorTest(int, char *[])
Expand Down Expand Up @@ -103,6 +104,10 @@ itkImageLinearIteratorTest(int, char *[])
// Verification
ConstIteratorType cot(myConstImage, region0);

// Test exceptions
int direction = ImageType::GetImageDimension() + 1;
ITK_TRY_EXPECT_EXCEPTION(cot.SetDirection(direction));

cot.GoToBegin();
cot.SetDirection(0); // 0=x, 1=y, 2=z

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,18 @@ itkQuadEdgeMeshScalarDataVTKPolyDataWriterTest1(int argc, char * argv[])
}
}

// Assign a value to each of the mesh points
for (unsigned int i = 0; i < myMesh->GetNumberOfPoints(); ++i)
{
myMesh->SetPointData(i, 5.0);
}

// Assign a different value to each of the mesh cells
for (unsigned int i = 0; i < myMesh->GetNumberOfCells(); ++i)
{
myMesh->SetCellData(i, 10.0);
}

using WriterType = itk::QuadEdgeMeshScalarDataVTKPolyDataWriter<MeshType>;

auto writer = WriterType::New();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ itkFastMarchingImageFilterRealTest1(int itkNotUsed(argc), char * itkNotUsed(argv
using FastMarchingType = itk::FastMarchingImageFilterBase<FloatImageType, FloatImageType>;

auto criterion = CriterionType::New();
criterion->SetThreshold(100.);

typename FloatImageType::PixelType threshold = 100.0;
criterion->SetThreshold(threshold);
ITK_TEST_SET_GET_VALUE(threshold, criterion->GetThreshold());

auto marcher = FastMarchingType::New();

Expand Down Expand Up @@ -171,7 +174,7 @@ itkFastMarchingImageFilterRealTest1(int itkNotUsed(argc), char * itkNotUsed(argv

bool passed = true;

double threshold = 1.42;
double outputValueThreshold = 1.42;
while (!iterator.IsAtEnd())
{
FloatImageType::IndexType tempIndex = iterator.GetIndex();
Expand All @@ -188,10 +191,10 @@ itkFastMarchingImageFilterRealTest1(int itkNotUsed(argc), char * itkNotUsed(argv

if (distance > itk::NumericTraits<double>::epsilon())
{
if (itk::Math::abs(outputValue) / distance > threshold)
if (itk::Math::abs(outputValue) / distance > outputValueThreshold)
{
std::cout << "Error at index [" << iterator.GetIndex() << "]" << std::endl;
std::cout << "Expected scaled output value be less than: " << threshold
std::cout << "Expected scaled output value be less than: " << outputValueThreshold
<< ", but got: " << itk::Math::abs(outputValue) / distance
<< ", where output: " << itk::Math::abs(outputValue) << "; scale factor: " << distance << std::endl;
passed = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,12 @@ itkFastMarchingImageFilterRealTest2(int itkNotUsed(argc), char * itkNotUsed(argv
using AdaptorType = itk::FastMarchingImageToNodePairContainerAdaptor<FloatImageType, FloatImageType, FloatImageType>;

auto adaptor = AdaptorType::New();
adaptor->SetIsForbiddenImageBinaryMask(true);

ITK_EXERCISE_BASIC_OBJECT_METHODS(adaptor, FastMarchingImageToNodePairContainerAdaptor, Object);


bool isForbiddenImageBinaryMask = true;
ITK_TEST_SET_GET_BOOLEAN(adaptor, IsForbiddenImageBinaryMask, isForbiddenImageBinaryMask);

adaptor->SetAliveImage(aliveImage.GetPointer());
adaptor->SetAliveValue(0.0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*=========================================================================*/

#include "itkFastMarchingThresholdStoppingCriterion.h"
#include "itkTestingMacros.h"

int
itkFastMarchingThresholdStoppingCriterionTest(int, char *[])
Expand All @@ -30,6 +31,12 @@ itkFastMarchingThresholdStoppingCriterionTest(int, char *[])

auto imageCriterion = ImageStoppingCriterionType::New();

ITK_EXERCISE_BASIC_OBJECT_METHODS(
imageCriterion, FastMarchingThresholdStoppingCriterion, FastMarchingStoppingCriterionBase);


std::cout << "Description: " << imageCriterion->GetDescription() << std::endl;

constexpr unsigned int Dimension3D = 3;
using MeshType = itk::QuadEdgeMesh<PixelType, Dimension3D>;

Expand Down
8 changes: 7 additions & 1 deletion Modules/IO/BioRad/test/itkBioRadImageIOTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ itkBioRadImageIOTest(int argc, char * argv[])
reader->SetFileName(filename);

auto bioradImageIO = ImageIOType::New();

ITK_EXERCISE_BASIC_OBJECT_METHODS(bioradImageIO, BioRadImageIO, ImageIOBase);


// Not used; empty method body; called for coverage purposes
bioradImageIO->WriteImageInformation();

reader->SetImageIO(bioradImageIO);

ITK_TRY_EXPECT_NO_EXCEPTION(reader->Update());
Expand All @@ -59,7 +66,6 @@ itkBioRadImageIOTest(int argc, char * argv[])

ITK_TRY_EXPECT_NO_EXCEPTION(writer->Update());

bioradImageIO->Print(std::cout);

return EXIT_SUCCESS;
}
27 changes: 24 additions & 3 deletions Modules/IO/ImageBase/test/itkNumericSeriesFileNamesTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,33 @@ itkNumericSeriesFileNamesTest(int, char *[])
ITK_EXERCISE_BASIC_OBJECT_METHODS(fit, NumericSeriesFileNames, Object);


itk::SizeValueType startIndex = 10;
// Test exceptions
itk::SizeValueType startIndex = 6;
itk::SizeValueType endIndex = 5;

fit->SetStartIndex(startIndex);
fit->SetEndIndex(endIndex);

ITK_TRY_EXPECT_EXCEPTION(fit->GetFileNames());

endIndex = 7;
fit->SetEndIndex(endIndex);

itk::SizeValueType incrementIndex = 0;
fit->SetIncrementIndex(incrementIndex);

ITK_TRY_EXPECT_EXCEPTION(fit->GetFileNames());


startIndex = 10;
fit->SetStartIndex(startIndex);
ITK_TEST_SET_GET_VALUE(startIndex, fit->GetStartIndex());

itk::SizeValueType endIndex = 20;
endIndex = 20;
fit->SetEndIndex(endIndex);
ITK_TEST_SET_GET_VALUE(endIndex, fit->GetEndIndex());

itk::SizeValueType incrementIndex = 2;
incrementIndex = 2;
fit->SetIncrementIndex(incrementIndex);
ITK_TEST_SET_GET_VALUE(incrementIndex, fit->GetIncrementIndex());

Expand All @@ -61,5 +79,8 @@ itkNumericSeriesFileNamesTest(int, char *[])
std::cout << "File: " << (*nit).c_str() << std::endl;
}

// Exercise the PrintSelf method to print the filenames for coverage purposes
std::cout << fit;

return EXIT_SUCCESS;
}
9 changes: 7 additions & 2 deletions Modules/IO/Stimulate/test/itkStimulateImageIOTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ itkStimulateImageIOTest(int argc, char * argv[])
// Create a mapper (in this case a writer). A mapper
// is templated on the input type.
//
itk::StimulateImageIO::Pointer sprIO;
sprIO = itk::StimulateImageIO::New();
itk::StimulateImageIO::Pointer sprIO = itk::StimulateImageIO::New();

ITK_EXERCISE_BASIC_OBJECT_METHODS(sprIO, StimulateImageIO, ImageIOBase);


// Not used; empty method body; called for coverage purposes
sprIO->WriteImageInformation();

// Write out the image
itk::ImageFileWriter<FloatImageType>::Pointer writer;
Expand Down
4 changes: 3 additions & 1 deletion Modules/IO/Stimulate/test/itkStimulateImageIOTest2.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ itkStimulateImageIOTest2(int argc, char * argv[])
std::cout << "region " << region;

// This is where we call all of the Get Functions to increase coverage.
std::cout << "Display Range " << io->GetDisplayRange() << std::endl;
std::cout << "DisplayRange:" << io->GetDisplayRange() << std::endl;
std::cout << "HighDisplayValue:" << io->GetHighDisplayValue() << std::endl;
std::cout << "LowDisplayValue: " << io->GetLowDisplayValue() << std::endl;


return EXIT_SUCCESS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,12 @@ PerformDisplacementFieldImageRegistration(int itkNotUsed(argc), char * argv[])
displacementFieldRegistration->SetSmoothingSigmasPerLevel(smoothingSigmasPerLevel);
displacementFieldRegistration->SetMetric(correlationMetric);

typename OutputTransformType::Pointer fixedToMiddleTransform;
displacementFieldRegistration->SetFixedToMiddleTransform(fixedToMiddleTransform);

typename OutputTransformType::Pointer movingToMiddleTransform;
displacementFieldRegistration->SetMovingToMiddleTransform(movingToMiddleTransform);

const typename DisplacementFieldRegistrationType::RealType epsilon =
itk::NumericTraits<typename DisplacementFieldRegistrationType::RealType>::epsilon();
const typename DisplacementFieldRegistrationType::RealType learningRate = std::stod(argv[6]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "itkImageGaussianModelEstimator.h"
#include "itkMahalanobisDistanceMembershipFunction.h"
#include "itkMinimumDecisionRule.h"
#include "itkTestingMacros.h"


int
Expand Down Expand Up @@ -254,6 +255,15 @@ itkGibbsTest(int, char *[])
using GibbsPriorFilterType = itk::RGBGibbsPriorFilter<VecImageType, ClassImageType>;
auto applyGibbsImageFilter = GibbsPriorFilterType::New();

ITK_EXERCISE_BASIC_OBJECT_METHODS(applyGibbsImageFilter, RGBGibbsPriorFilter, MRFImageFilter);

// Set the MRF labeller parameters
applyGibbsImageFilter->SetNumberOfClasses(NumClasses);
ITK_TEST_SET_GET_VALUE(NumClasses, applyGibbsImageFilter->GetNumberOfClasses());

applyGibbsImageFilter->SetMaximumNumberOfIterations(MaxNumIter);
ITK_TEST_SET_GET_VALUE(MaxNumIter, applyGibbsImageFilter->GetMaximumNumberOfIterations());

// Set the MRF labeller parameters
applyGibbsImageFilter->SetNumberOfClasses(NumClasses);
applyGibbsImageFilter->SetMaximumNumberOfIterations(MaxNumIter);
Expand All @@ -263,39 +273,42 @@ itkGibbsTest(int, char *[])
applyGibbsImageFilter->SetBoundaryGradient(6);
applyGibbsImageFilter->SetObjectLabel(1);
// applyGibbsImageFilter->SetRecursiveNumber(1);
applyGibbsImageFilter->SetCliqueWeight_1(5.0);
applyGibbsImageFilter->SetCliqueWeight_2(5.0);
applyGibbsImageFilter->SetCliqueWeight_3(5.0);
applyGibbsImageFilter->SetCliqueWeight_4(5.0);
applyGibbsImageFilter->SetCliqueWeight_5(5.0);
applyGibbsImageFilter->SetCliqueWeight_6(0.0);

applyGibbsImageFilter->SetInput(vecImage);
applyGibbsImageFilter->SetClassifier(myClassifier);
double cliqueWeight1 = 5.0;
applyGibbsImageFilter->SetCliqueWeight_1(cliqueWeight1);
ITK_TEST_SET_GET_VALUE(cliqueWeight1, applyGibbsImageFilter->GetCliqueWeight_1());

double cliqueWeight2 = 5.0;
applyGibbsImageFilter->SetCliqueWeight_2(cliqueWeight2);
ITK_TEST_SET_GET_VALUE(cliqueWeight2, applyGibbsImageFilter->GetCliqueWeight_2());

applyGibbsImageFilter->SetObjectThreshold(5.0);
double cliqueWeight3 = 5.0;
applyGibbsImageFilter->SetCliqueWeight_3(cliqueWeight3);
ITK_TEST_SET_GET_VALUE(cliqueWeight3, applyGibbsImageFilter->GetCliqueWeight_3());

double cliqueWeight4 = 5.0;
applyGibbsImageFilter->SetCliqueWeight_4(cliqueWeight4);
ITK_TEST_SET_GET_VALUE(cliqueWeight4, applyGibbsImageFilter->GetCliqueWeight_4());

double cliqueWeight5 = 5.0;
applyGibbsImageFilter->SetCliqueWeight_5(cliqueWeight5);
ITK_TEST_SET_GET_VALUE(cliqueWeight5, applyGibbsImageFilter->GetCliqueWeight_5());

double cliqueWeight6 = 0.0;
applyGibbsImageFilter->SetCliqueWeight_6(cliqueWeight6);
ITK_TEST_SET_GET_VALUE(cliqueWeight6, applyGibbsImageFilter->GetCliqueWeight_6());

/** coverage */
std::cout << applyGibbsImageFilter->GetNumberOfClasses() << std::endl;
std::cout << applyGibbsImageFilter->GetMaximumNumberOfIterations() << std::endl;
applyGibbsImageFilter->SetInput(vecImage);
applyGibbsImageFilter->SetClassifier(myClassifier);

/** coverage */
std::cout << applyGibbsImageFilter->GetCliqueWeight_1() << std::endl;
std::cout << applyGibbsImageFilter->GetCliqueWeight_2() << std::endl;
std::cout << applyGibbsImageFilter->GetCliqueWeight_3() << std::endl;
std::cout << applyGibbsImageFilter->GetCliqueWeight_4() << std::endl;
std::cout << applyGibbsImageFilter->GetCliqueWeight_5() << std::endl;
std::cout << applyGibbsImageFilter->GetCliqueWeight_6() << std::endl;
applyGibbsImageFilter->SetObjectThreshold(5.0);

// Since a suvervised classifier is used, it requires a training image
applyGibbsImageFilter->SetTrainingImage(classImage);

// Kick off the Gibbs labeller function
applyGibbsImageFilter->Update();

std::cout << "applyGibbsImageFilter: " << applyGibbsImageFilter;

ClassImageType::Pointer outClassImage = applyGibbsImageFilter->GetOutput();

// Print the mrf labelled image
Expand Down
1 change: 1 addition & 0 deletions Modules/Video/IO/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ itk_add_test(
DATA{Input/frame3.jpg}
DATA{Input/frame4.jpg}
"${ITK_TEST_OUTPUT_DIR}/frame0.png,${ITK_TEST_OUTPUT_DIR}/frame1.png,${ITK_TEST_OUTPUT_DIR}/frame2.png,${ITK_TEST_OUTPUT_DIR}/frame3.png,${ITK_TEST_OUTPUT_DIR}/frame4.png"
1
24
MP42
)
Expand Down
14 changes: 9 additions & 5 deletions Modules/Video/IO/test/itkVideoFileReaderWriterTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ int
itkVideoFileReaderWriterTest(int argc, char * argv[])
{
// Check parameters
if (argc != 9)
if (argc != 10)
{
std::cerr << "Missing parameters." << std::endl;
std::cerr << "Usage: " << std::endl;
std::cerr << itkNameOfTestExecutableMacro(argv) << " [Video Input] [Image Output] framesPerSecond fourCC"
<< std::endl;
std::cerr << itkNameOfTestExecutableMacro(argv)
<< " VideoInput(5 files) ImageOutput IFrameSafe framesPerSecond fourCC" << std::endl;
return EXIT_FAILURE;
}

Expand All @@ -57,6 +57,10 @@ itkVideoFileReaderWriterTest(int argc, char * argv[])
ITK_EXERCISE_BASIC_OBJECT_METHODS(reader, VideoFileReader, VideoSource);

reader->SetFileName(inFile.c_str());
ITK_TEST_SET_GET_VALUE(inFile, std::string(reader->GetFileName()));

auto iFrameSafe = static_cast<bool>(std::stoi(argv[7]));
ITK_TEST_SET_GET_BOOLEAN(reader, IFrameSafe, iFrameSafe);

// I'm still not sure how to handle this right, but for now, just manually
// register an FileListVideoIO
Expand All @@ -71,11 +75,11 @@ itkVideoFileReaderWriterTest(int argc, char * argv[])
writer->SetFileName(std::string(argv[6]));
ITK_TEST_SET_GET_VALUE(std::string(argv[6]), writer->GetFileName());

auto framesPerSecond = static_cast<VideoWriterType::TemporalRatioType>(std::stod(argv[7]));
auto framesPerSecond = static_cast<VideoWriterType::TemporalRatioType>(std::stod(argv[8]));
writer->SetFramesPerSecond(framesPerSecond);
ITK_TEST_SET_GET_VALUE(framesPerSecond, writer->GetFramesPerSecond());

auto fourCC = std::string(argv[8]);
auto fourCC = std::string(argv[9]);
writer->SetFourCC(fourCC);
ITK_TEST_SET_GET_VALUE(fourCC, writer->GetFourCC());

Expand Down

0 comments on commit 6765363

Please sign in to comment.