Skip to content

Commit

Permalink
ENH: Increase code coverage
Browse files Browse the repository at this point in the history
Increase the code coverage:
- Exercise the basic object methods using the
  `ITK_EXERCISE_BASIC_OBJECT_METHODS` macro.
- 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.
- Test the exceptions.
  • Loading branch information
jhlegarreta authored and dzenanz committed Oct 11, 2021
1 parent 182d302 commit c4a359c
Show file tree
Hide file tree
Showing 20 changed files with 155 additions and 34 deletions.
4 changes: 4 additions & 0 deletions Modules/Core/Common/test/itkStreamingImageFilterTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "itkShrinkImageFilter.h"
#include "itkStreamingImageFilter.h"
#include "itkPipelineMonitorImageFilter.h"
#include "itkTestingMacros.h"

int
itkStreamingImageFilterTest(int, char *[])
Expand Down Expand Up @@ -69,6 +70,9 @@ itkStreamingImageFilterTest(int, char *[])
itk::StreamingImageFilter<ShortImage, ShortImage>::Pointer streamer;
streamer = itk::StreamingImageFilter<ShortImage, ShortImage>::New();

ITK_EXERCISE_BASIC_OBJECT_METHODS(streamer, StreamingImageFilter, ImageToImageFilter);


// test MakeOutput by name
streamer->MakeOutput(streamer->GetOutputNames()[0]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ itkGaussianSpatialObjectTest(int, char *[])
myGaussian->SetSigmaInObjectSpace(sigma);
ITK_TEST_SET_GET_VALUE(sigma, myGaussian->GetSigmaInObjectSpace());

GaussianType::PointType center;
center.Fill(0.0);
myGaussian->SetCenterInObjectSpace(center);
ITK_TEST_SET_GET_VALUE(center, myGaussian->GetCenterInObjectSpace());

// Point consistency

itk::Point<double, 4> in;
Expand Down
29 changes: 25 additions & 4 deletions Modules/Core/Transform/test/itkSplineKernelTransformTest.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "itkThinPlateR2LogRSplineKernelTransform.h"
#include "itkVolumeSplineKernelTransform.h"
#include "itkMath.h"
#include "itkTestingMacros.h"


int
Expand Down Expand Up @@ -92,7 +93,12 @@ itkSplineKernelTransformTest(int, char *[])
// Poisson's ration = 0.25, Alpha = 12.0 * ( 1 - \nu ) - 1
ebs2D->SetSourceLandmarks(sourceLandmarks2D);
ebs2D->SetTargetLandmarks(targetLandmarks2D);
ebs2D->SetAlpha(12.0 * (1 - 0.25) - 1.0);

ParametersValueType alpha = 12.0 * (1 - 0.25) - 1.0;
ebs2D->SetAlpha(alpha);
ITK_TEST_SET_GET_VALUE(alpha, ebs2D->GetAlpha());


ebs2D->ComputeWMatrix();

{ // Testing the number of parameters
Expand Down Expand Up @@ -132,7 +138,12 @@ itkSplineKernelTransformTest(int, char *[])
std::cout << "EBRS 2D Test:" << std::endl;
ebrs2D->SetSourceLandmarks(sourceLandmarks2D);
ebrs2D->SetTargetLandmarks(targetLandmarks2D);
ebrs2D->SetAlpha(12.0 * (1 - 0.25) - 1.0);

alpha = 12.0 * (1 - 0.25) - 1.0;
ebrs2D->SetAlpha(alpha);
ITK_TEST_SET_GET_VALUE(alpha, ebrs2D->GetAlpha());


ebrs2D->ComputeWMatrix();

source2Dit = sourceLandmarks2D->GetPoints()->Begin();
Expand Down Expand Up @@ -388,7 +399,12 @@ itkSplineKernelTransformTest(int, char *[])

std::cout << "EBS 3D Test:" << std::endl;
// Poisson's ration = 0.25, Alpha = 12.0 * ( 1 - \nu ) - 1
ebs3D->SetAlpha(12.0 * (1 - 0.25) - 1.0);

alpha = 12.0 * (1 - 0.25) - 1.0;
ebs3D->SetAlpha(alpha);
ITK_TEST_SET_GET_VALUE(alpha, ebs3D->GetAlpha());


ebs3D->ComputeWMatrix();

ebs3Ds = ebs3D->GetModifiableSourceLandmarks()->GetPoints()->Begin();
Expand Down Expand Up @@ -513,7 +529,12 @@ itkSplineKernelTransformTest(int, char *[])
}
std::cout << "EBS 4D Test:" << std::endl;
// Poisson's ration = 0.25, Alpha = 12.0 * ( 1 - \nu ) - 1
ebs4D->SetAlpha(12.0 * (1 - 0.25) - 1.0);

alpha = 12.0 * (1 - 0.25) - 1.0;
ebs4D->SetAlpha(alpha);
ITK_TEST_SET_GET_VALUE(alpha, ebs4D->GetAlpha());


ebs4D->ComputeWMatrix();

ebs4Ds = ebs4D->GetModifiableSourceLandmarks()->GetPoints()->Begin();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ itkBinaryThinningImageFilterTest(int argc, char * argv[])
// Set up the reader
reader->SetFileName(argv[1]);

// Set up the filter parameters.
ITK_EXERCISE_BASIC_OBJECT_METHODS(thinning, BinaryThinningImageFilter, ImageToImageFilter);


thinning->SetInput(reader->GetOutput());

// Rescale the image so that it can be seen.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "itkSquaredDifferenceImageFilter.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "itkTestingMacros.h"


int
Expand Down Expand Up @@ -119,6 +120,8 @@ itkSquaredDifferenceImageFilterTest(int, char *[])
// Create a MagnitudeImageFilter
myFilterType::Pointer filter = myFilterType::New();

ITK_EXERCISE_BASIC_OBJECT_METHODS(filter, SquaredDifferenceImageFilter, BinaryGeneratorImageFilter);


// Connect the input images
filter->SetInput1(inputImageA);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "itkGradientImageFilter.h"
#include "itkVectorMagnitudeImageFilter.h"
#include "itkGradientVectorFlowImageFilter.h"
#include "itkTestingMacros.h"

int
itkGradientVectorFlowImageFilterTest(int, char *[])
Expand Down Expand Up @@ -141,11 +142,26 @@ itkGradientVectorFlowImageFilterTest(int, char *[])
myLaplacianFilterType::Pointer m_LFilter = myLaplacianFilterType::New();
myGVFFilterType::Pointer m_GVFFilter = myGVFFilterType::New();

ITK_EXERCISE_BASIC_OBJECT_METHODS(m_GVFFilter, GradientVectorFlowImageFilter, ImageToImageFilter);


m_GVFFilter->SetInput(gfilter->GetOutput());

m_GVFFilter->SetLaplacianFilter(m_LFilter);
m_GVFFilter->SetNoiseLevel(500);
m_GVFFilter->SetTimeStep(0.001);
m_GVFFilter->SetIterationNum(2);
ITK_TEST_SET_GET_VALUE(m_LFilter, m_GVFFilter->GetLaplacianFilter());

double noiseLevel = 500;
m_GVFFilter->SetNoiseLevel(noiseLevel);
ITK_TEST_SET_GET_VALUE(noiseLevel, m_GVFFilter->GetNoiseLevel());

double timeStep = 0.001;
m_GVFFilter->SetTimeStep(timeStep);
ITK_TEST_SET_GET_VALUE(timeStep, m_GVFFilter->GetTimeStep());

int iterationNum = 2;
m_GVFFilter->SetIterationNum(iterationNum);
ITK_TEST_SET_GET_VALUE(iterationNum, m_GVFFilter->GetIterationNum());


// Get the Smart Pointer to the Filter Output
// It is important to do it AFTER the filter is Updated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,29 @@ operator<<(std::ostream & o, const itk::Vector<float, 3> & v)
int
itkZeroCrossingImageFilterTest(int, char *[])
{
using ImageType = itk::Image<float, 2>;

constexpr unsigned int Dimension = 2;
using PixelType = float;

using ImageType = itk::Image<PixelType, Dimension>;
using FilterType = itk::ZeroCrossingImageFilter<ImageType, ImageType>;

// Set up filter
itk::ZeroCrossingImageFilter<ImageType, ImageType>::Pointer filter =
itk::ZeroCrossingImageFilter<ImageType, ImageType>::New();
FilterType::Pointer filter = FilterType::New();

ITK_EXERCISE_BASIC_OBJECT_METHODS(filter, ZeroCrossingImageFilter, ImageToImageFilter);


std::cout << "filter: " << filter;
typename FilterType::OutputImagePixelType foregroundValue =
itk::NumericTraits<typename FilterType::OutputImagePixelType>::OneValue();
filter->SetForegroundValue(foregroundValue);
ITK_TEST_SET_GET_VALUE(foregroundValue, filter->GetForegroundValue());

typename FilterType::OutputImagePixelType backgroundValue =
itk::NumericTraits<typename FilterType::OutputImagePixelType>::ZeroValue();
filter->SetBackgroundValue(backgroundValue);
ITK_TEST_SET_GET_VALUE(backgroundValue, filter->GetBackgroundValue());

// Run Test
itk::Size<2> sz;
sz[0] = 100;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ itkLabelMapOverlayImageFilterTest1(int argc, char * argv[])
ColorizerType::Pointer colorizer = ColorizerType::New();
colorizer->SetInput(converter->GetOutput());
colorizer->SetFeatureImage(reader2->GetOutput());
colorizer->SetOpacity(std::stod(argv[4]));

double opacity = std::stod(argv[4]);
colorizer->SetOpacity(opacity);
ITK_TEST_SET_GET_VALUE(opacity, colorizer->GetOpacity());

itk::SimpleFilterWatcher watcher(colorizer, "filter");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,26 @@ itkLabelToRGBImageFilterTest(int argc, char * argv[])
using FilterType = itk::LabelToRGBImageFilter<ImageType, ColorImageType>;
FilterType::Pointer filter = FilterType::New();

ITK_EXERCISE_BASIC_OBJECT_METHODS(filter, LabelToRGBImageFilter, UnaryFunctorImageFilter);


// Exercising Background Value methods
filter->SetBackgroundValue(10);
if (filter->GetBackgroundValue() != 10)
{
std::cerr << "Background value Set/Get Problem" << std::endl;
return EXIT_FAILURE;
}
typename FilterType::LabelPixelType backgroundValue = 10;
filter->SetBackgroundValue(backgroundValue);
ITK_TEST_SET_GET_VALUE(backgroundValue, filter->GetBackgroundValue());

typename FilterType::OutputPixelType backgroundColor;
backgroundColor.Fill(itk::NumericTraits<typename FilterType::OutputPixelValueType>::ZeroValue());
filter->SetBackgroundColor(backgroundColor);
ITK_TEST_SET_GET_VALUE(backgroundColor, filter->GetBackgroundColor());


// Set the filter input and label images
filter->SetInput(reader->GetOutput());
filter->SetBackgroundValue(0);

backgroundValue = 0;
filter->SetBackgroundValue(backgroundValue);


itk::SimpleFilterWatcher watcher(filter, "filter");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,17 @@ itkProjectionImageFilterTest(int argc, char * argv[])
using FilterType = itk::ProjectionImageFilter<ImageType, ImageType, FunctionType>;

FilterType::Pointer filter = FilterType::New();

ITK_EXERCISE_BASIC_OBJECT_METHODS(filter, ProjectionImageFilter, ImageToImageFilter);


filter->SetInput(change->GetOutput());

unsigned int projectionDimension = ImageType::ImageDimension - 1;
filter->SetProjectionDimension(projectionDimension);
ITK_TEST_SET_GET_VALUE(projectionDimension, filter->GetProjectionDimension());


itk::SimpleFilterWatcher watcher(filter, "filter");

using WriterType = itk::ImageFileWriter<ImageType>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "itkSpatialNeighborSubsampler.h"
#include "itkImageToNeighborhoodSampleAdaptor.h"
#include "itkImageRegionConstIteratorWithIndex.h"
#include "itkTestingMacros.h"

int
itkSpatialNeighborSubsamplerTest(int, char *[])
Expand Down Expand Up @@ -100,6 +101,10 @@ itkSpatialNeighborSubsamplerTest(int, char *[])
sample->SetImage(inImage);

SamplerType::Pointer sampler_orig = SamplerType::New();

ITK_EXERCISE_BASIC_OBJECT_METHODS(sampler_orig, SpatialNeighborSubsampler, RegionConstrainedSubsampler);


sampler_orig->SetSample(sample);
sampler_orig->SetSampleRegion(region);
sampler_orig->SetRegionConstraint(regionConstraint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,8 @@ CompareMeshSources(bool computeSquaredDistance)
metric->SetComputeSquaredDistance(computeSquaredDistance);
ITK_TEST_SET_GET_VALUE(computeSquaredDistance, metric->GetComputeSquaredDistance());

if (computeSquaredDistance)
{
metric->ComputeSquaredDistanceOn();
ITK_TEST_EXPECT_TRUE(metric->GetComputeSquaredDistance());
}
else
{
metric->ComputeSquaredDistanceOff();
ITK_TEST_EXPECT_TRUE(!metric->GetComputeSquaredDistance());
}
ITK_TEST_SET_GET_BOOLEAN(metric, ComputeSquaredDistance, computeSquaredDistance);


metric->SetFixedPointSet(fixed->GetOutput());
metric->SetMovingPointSet(moving->GetOutput());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,11 @@ itkPointSetToPointSetRegistrationTest(int, char *[])

ITK_TRY_EXPECT_NO_EXCEPTION(ddFilter->Update());

metric->SetDistanceMap(ddFilter->GetOutput());

typename DDFilterType::OutputImageType::Pointer distanceMap = ddFilter->GetOutput();
metric->SetDistanceMap(distanceMap);
ITK_TEST_SET_GET_VALUE(distanceMap, metric->GetDistanceMap());

metric->ComputeSquaredDistanceOn();

// Initialize the offset/vector part
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "itkNearestNeighborInterpolateImageFunction.h"
#include "itkCommand.h"
#include "itkCastImageFilter.h"
#include "itkTestingMacros.h"


namespace
Expand Down Expand Up @@ -190,7 +191,8 @@ itkDemonsRegistrationFilterTest(int, char *[])
registrator->InPlaceOn();

// turn on/off use moving image gradient
registrator->UseMovingImageGradientOff();
auto useMovingImageGradient = false;
ITK_TEST_SET_GET_BOOLEAN(registrator, UseMovingImageGradient, useMovingImageGradient);

using FunctionType = RegistrationType::DemonsRegistrationFunctionType;
auto * fptr = dynamic_cast<FunctionType *>(registrator->GetDifferenceFunction().GetPointer());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*=========================================================================*/

#include "itkLaplacianSegmentationLevelSetImageFilter.h"
#include "itkTestingMacros.h"


namespace LSIFTN
Expand Down Expand Up @@ -180,6 +181,10 @@ itkLaplacianSegmentationLevelSetImageFilterTest(int, char *[])

itk::LaplacianSegmentationLevelSetImageFilter<::LSIFTN::SeedImageType, ::LSIFTN::ImageType>::Pointer filter =
itk::LaplacianSegmentationLevelSetImageFilter<::LSIFTN::SeedImageType, ::LSIFTN::ImageType>::New();

ITK_EXERCISE_BASIC_OBJECT_METHODS(filter, LaplacianSegmentationLevelSetImageFilter, SegmentationLevelSetImageFilter);


filter->SetInput(seedImage);
filter->SetFeatureImage(inputImage);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,13 @@ itkLevelSetDomainPartitionImageTest(int, char *[])

DomainPartitionSourceType::Pointer partitionSource = DomainPartitionSourceType::New();
partitionSource->SetNumberOfLevelSetFunctions(numberOfLevelSetFunctions);
partitionSource->SetImage(binary);

partitionSource->SetLevelSetDomainRegionVector(regionVector);

// Exercise exceptions
ITK_TRY_EXPECT_EXCEPTION(partitionSource->PopulateListDomain());

partitionSource->SetImage(binary);
partitionSource->PopulateListDomain();


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*=========================================================================*/

#include "itkMalcolmSparseLevelSetImage.h"
#include "itkTestingMacros.h"

int
itkMalcolmSparseLevelSetImageTest(int, char *[])
Expand All @@ -40,6 +41,10 @@ itkMalcolmSparseLevelSetImageTest(int, char *[])
}

SparseLevelSetType::Pointer phi = SparseLevelSetType::New();

ITK_EXERCISE_BASIC_OBJECT_METHODS(phi, MalcolmSparseLevelSetImage, LevelSetSparseImage);


phi->SetLabelMap(labelMap);

index[0] = 3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*=========================================================================*/

#include "itkShiSparseLevelSetImage.h"
#include "itkTestingMacros.h"

int
itkShiSparseLevelSetImageTest(int, char *[])
Expand Down Expand Up @@ -52,6 +53,10 @@ itkShiSparseLevelSetImageTest(int, char *[])
labelMap->SetPixel(index, 1);

SparseLevelSetType::Pointer phi = SparseLevelSetType::New();

ITK_EXERCISE_BASIC_OBJECT_METHODS(phi, ShiSparseLevelSetImage, LevelSetSparseImage);


phi->SetLabelMap(labelMap);

index[0] = 3;
Expand Down
Loading

0 comments on commit c4a359c

Please sign in to comment.