diff --git a/Modules/Filtering/BiasCorrection/itk-module.cmake b/Modules/Filtering/BiasCorrection/itk-module.cmake index 84dcb9e669a..db8439c55a0 100644 --- a/Modules/Filtering/BiasCorrection/itk-module.cmake +++ b/Modules/Filtering/BiasCorrection/itk-module.cmake @@ -19,6 +19,7 @@ itk_module( ITKImageGrid ITKPolynomials TEST_DEPENDS + ITKGoogleTest ITKTestKernel ITKOptimizers ITKPolynomials diff --git a/Modules/Filtering/BiasCorrection/test/CMakeLists.txt b/Modules/Filtering/BiasCorrection/test/CMakeLists.txt index acf3e242f00..cf41af1bccb 100644 --- a/Modules/Filtering/BiasCorrection/test/CMakeLists.txt +++ b/Modules/Filtering/BiasCorrection/test/CMakeLists.txt @@ -1,19 +1,16 @@ itk_module_test() set( ITKBiasCorrectionTests - itkCompositeValleyFunctionTest.cxx itkMRIBiasFieldCorrectionFilterTest.cxx itkN4BiasFieldCorrectionImageFilterTest.cxx ) createtestdriver(ITKBiasCorrection "${ITKBiasCorrection-Test_LIBRARIES}" "${ITKBiasCorrectionTests}") -itk_add_test( - NAME itkCompositeValleyFunctionTest - COMMAND - ITKBiasCorrectionTestDriver - itkCompositeValleyFunctionTest -) +set(ITKBiasCorrectionGTests itkCompositeValleyFunctionGTest.cxx) + +creategoogletestdriver(ITKBiasCorrection "${ITKBiasCorrection-Test_LIBRARIES}" "${ITKBiasCorrectionGTests}") + itk_add_test( NAME itkMRIBiasFieldCorrectionFilterTest COMMAND diff --git a/Modules/Filtering/BiasCorrection/test/itkCompositeValleyFunctionGTest.cxx b/Modules/Filtering/BiasCorrection/test/itkCompositeValleyFunctionGTest.cxx new file mode 100644 index 00000000000..e398a2e042b --- /dev/null +++ b/Modules/Filtering/BiasCorrection/test/itkCompositeValleyFunctionGTest.cxx @@ -0,0 +1,61 @@ +/*========================================================================= + * + * Copyright NumFOCUS + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + *=========================================================================*/ + +#include "itkCompositeValleyFunction.h" +#include "itkMath.h" +#include + +TEST(CompositeValleyFunction, BoundsAndInterval) +{ + itk::Array means(2); + itk::Array sigmas(2); + + means[0] = 0.0; + means[1] = 100.0; + sigmas[0] = 20.0; + sigmas[1] = 20.0; + + itk::CompositeValleyFunction function(means, sigmas); + + EXPECT_DOUBLE_EQ(function.GetUpperBound(), 280.0); + EXPECT_DOUBLE_EQ(function.GetLowerBound(), -180.0); + + const double interval1 = function.GetInterval(); + const double interval2 = (function.GetUpperBound() - function.GetLowerBound()) / (1000000.0 - 1.0); + EXPECT_NEAR(interval1, interval2, itk::NumericTraits::epsilon()); +} + +TEST(CompositeValleyFunction, EvaluateMatchesCallOperator) +{ + itk::Array means(2); + itk::Array sigmas(2); + + means[0] = 0.0; + means[1] = 100.0; + sigmas[0] = 20.0; + sigmas[1] = 20.0; + + itk::CompositeValleyFunction function(means, sigmas); + + const long numberOfSamples = function.GetNumberOfSamples(); + const double measure = function.GetLowerBound() + function.GetInterval() * numberOfSamples * 0.5; + const double value1 = function(measure); + const double value2 = function.Evaluate(measure); + + EXPECT_NEAR(value1, value2, itk::NumericTraits::epsilon()); +} diff --git a/Modules/Filtering/BiasCorrection/test/itkCompositeValleyFunctionTest.cxx b/Modules/Filtering/BiasCorrection/test/itkCompositeValleyFunctionTest.cxx deleted file mode 100644 index 683b9a6b7ef..00000000000 --- a/Modules/Filtering/BiasCorrection/test/itkCompositeValleyFunctionTest.cxx +++ /dev/null @@ -1,82 +0,0 @@ -/*========================================================================= - * - * Copyright NumFOCUS - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0.txt - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - *=========================================================================*/ - -#include -#include "itkMath.h" -#include "itkCompositeValleyFunction.h" -#include "itkStdStreamStateSave.h" - -int -itkCompositeValleyFunctionTest(int, char *[]) -{ - // Save the format stream variables for std::cout - // They will be restored when coutState goes out of scope - // scope. - const itk::StdStreamStateSave coutState(std::cout); - - itk::Array means(2); - itk::Array sigmas(2); - - means[0] = 0.0; - means[1] = 100.0; - sigmas[0] = 20.0; - sigmas[1] = 20.0; - - itk::CompositeValleyFunction function(means, sigmas); - - if (function.GetUpperBound() != 280.0) - { - std::cout << "Test fails: GetUpperBound()" << std::endl; - return EXIT_FAILURE; - } - - if (itk::Math::NotAlmostEquals(function.GetLowerBound(), -180.0)) - { - std::cout << "Test fails: GetLowerBound()" << std::endl; - return EXIT_FAILURE; - } - - std::cout.setf(std::ios::scientific); - std::cout.precision(12); - - const double interval1 = function.GetInterval(); - const double interval2 = (function.GetUpperBound() - function.GetLowerBound()) / (1000000.0 - 1.0); - if (itk::Math::Absolute(interval1 - interval2) > itk::NumericTraits::epsilon()) - { - std::cout << "Test fails: GetInterval()" << std::endl; - std::cout << "Interval from the GetInterval() = " << interval1 << std::endl; - std::cout << "Interval value using the calculation = " << interval2 << std::endl; - return EXIT_FAILURE; - } - - const long numberOfSamples = function.GetNumberOfSamples(); - const double measure = function.GetLowerBound() + interval1 * numberOfSamples * 0.5; - const double value1 = function(measure); - const double value2 = function.Evaluate(measure); - - if (itk::Math::Absolute(value1 - value2) > itk::NumericTraits::epsilon()) - { - std::cout << "diff = " << itk::Math::Absolute(value1 - value2) << std::endl; - std::cout << "Test fails: operator()" << std::endl; - - return EXIT_FAILURE; - } - - std::cout << "Test succeed" << std::endl; - return EXIT_SUCCESS; -} diff --git a/Modules/Filtering/Deconvolution/itk-module.cmake b/Modules/Filtering/Deconvolution/itk-module.cmake index 5caa4ccc90d..f8b63ad75de 100644 --- a/Modules/Filtering/Deconvolution/itk-module.cmake +++ b/Modules/Filtering/Deconvolution/itk-module.cmake @@ -10,6 +10,7 @@ itk_module( ITKConvolution ITKImageSources TEST_DEPENDS + ITKGoogleTest ITKTestKernel DESCRIPTION "${DOCUMENTATION}" ) diff --git a/Modules/Filtering/Deconvolution/test/CMakeLists.txt b/Modules/Filtering/Deconvolution/test/CMakeLists.txt index c19c99421ac..eb9eebead9c 100644 --- a/Modules/Filtering/Deconvolution/test/CMakeLists.txt +++ b/Modules/Filtering/Deconvolution/test/CMakeLists.txt @@ -3,16 +3,22 @@ set( ITKDeconvolutionTests itkInverseDeconvolutionImageFilterTest.cxx itkLandweberDeconvolutionImageFilterTest.cxx - itkProjectedIterativeDeconvolutionImageFilterTest.cxx + itkParametricBlindLeastSquaresDeconvolutionImageFilterTest.cxx itkProjectedLandweberDeconvolutionImageFilterTest.cxx itkRichardsonLucyDeconvolutionImageFilterTest.cxx itkTikhonovDeconvolutionImageFilterTest.cxx itkWienerDeconvolutionImageFilterTest.cxx - itkParametricBlindLeastSquaresDeconvolutionImageFilterTest.cxx ) createtestdriver(ITKDeconvolution "${ITKDeconvolution-Test_LIBRARIES}" "${ITKDeconvolutionTests}") +set( + ITKDeconvolutionGTests + itkProjectedIterativeDeconvolutionImageFilterGTest.cxx +) + +creategoogletestdriver(ITKDeconvolution "${ITKDeconvolution-Test_LIBRARIES}" "${ITKDeconvolutionGTests}") + itk_add_test( NAME itkRichardsonLucyDeconvolutionImageFilterGaussianKernelTest COMMAND @@ -81,12 +87,6 @@ itk_add_test( 1 2.0 ) -itk_add_test( - NAME itkProjectedIterativeDeconvolutionimageFilterTest - COMMAND - ITKDeconvolutionTestDriver - itkProjectedIterativeDeconvolutionImageFilterTest -) itk_add_test( NAME itkProjectedLandweberDeconvolutionImageFilterGaussianKernelTest COMMAND diff --git a/Modules/Filtering/Deconvolution/test/itkProjectedIterativeDeconvolutionImageFilterTest.cxx b/Modules/Filtering/Deconvolution/test/itkProjectedIterativeDeconvolutionImageFilterGTest.cxx similarity index 73% rename from Modules/Filtering/Deconvolution/test/itkProjectedIterativeDeconvolutionImageFilterTest.cxx rename to Modules/Filtering/Deconvolution/test/itkProjectedIterativeDeconvolutionImageFilterGTest.cxx index bac895af7e7..ebfe9f1119a 100644 --- a/Modules/Filtering/Deconvolution/test/itkProjectedIterativeDeconvolutionImageFilterTest.cxx +++ b/Modules/Filtering/Deconvolution/test/itkProjectedIterativeDeconvolutionImageFilterGTest.cxx @@ -19,25 +19,16 @@ #include "itkImage.h" #include "itkLandweberDeconvolutionImageFilter.h" #include "itkProjectedIterativeDeconvolutionImageFilter.h" -#include "itkSimpleFilterWatcher.h" +#include "itkGTest.h" -int -itkProjectedIterativeDeconvolutionImageFilterTest(int, char *[]) +TEST(ProjectedIterativeDeconvolutionImageFilter, BasicObjectMethods) { - // Declare the image type using ImageType = itk::Image; - - // Declare the base deconvolution filter choice using BaseDeconvolutionFilterType = itk::LandweberDeconvolutionImageFilter; - - // Declare a projected version of the base deconvolution image filter using ProjectedDeconvolutionFilterType = itk::ProjectedIterativeDeconvolutionImageFilter; - // Just instantiate the filter and print it auto deconvolutionFilter = ProjectedDeconvolutionFilterType::New(); - deconvolutionFilter->Print(std::cout); - - const itk::SimpleFilterWatcher watcher(deconvolutionFilter); - return EXIT_SUCCESS; + ITK_GTEST_EXERCISE_BASIC_OBJECT_METHODS( + deconvolutionFilter, ProjectedIterativeDeconvolutionImageFilter, IterativeDeconvolutionImageFilter); } diff --git a/Modules/Filtering/ImageLabel/itk-module.cmake b/Modules/Filtering/ImageLabel/itk-module.cmake index 2aa61eb4358..a8a4d49b804 100644 --- a/Modules/Filtering/ImageLabel/itk-module.cmake +++ b/Modules/Filtering/ImageLabel/itk-module.cmake @@ -12,6 +12,7 @@ itk_module( DEPENDS ITKImageIntensity TEST_DEPENDS + ITKGoogleTest ITKTestKernel DESCRIPTION "${DOCUMENTATION}" ) diff --git a/Modules/Filtering/ImageLabel/test/CMakeLists.txt b/Modules/Filtering/ImageLabel/test/CMakeLists.txt index 1897b5f9f0f..817f8987884 100644 --- a/Modules/Filtering/ImageLabel/test/CMakeLists.txt +++ b/Modules/Filtering/ImageLabel/test/CMakeLists.txt @@ -1,19 +1,16 @@ itk_module_test() set( ITKImageLabelTests - itkChangeLabelImageFilterTest.cxx - itkLabelContourImageFilterTest.cxx itkBinaryContourImageFilterTest.cxx + itkLabelContourImageFilterTest.cxx ) createtestdriver(ITKImageLabel "${ITKImageLabel-Test_LIBRARIES}" "${ITKImageLabelTests}") -itk_add_test( - NAME itkChangeLabelImageFilterTest - COMMAND - ITKImageLabelTestDriver - itkChangeLabelImageFilterTest -) +set(ITKImageLabelGTests itkChangeLabelImageFilterGTest.cxx) + +creategoogletestdriver(ITKImageLabel "${ITKImageLabel-Test_LIBRARIES}" "${ITKImageLabelGTests}") + itk_add_test( NAME itkLabelContourImageFilterTest0 COMMAND diff --git a/Modules/Filtering/ImageLabel/test/itkChangeLabelImageFilterGTest.cxx b/Modules/Filtering/ImageLabel/test/itkChangeLabelImageFilterGTest.cxx new file mode 100644 index 00000000000..babb96f5349 --- /dev/null +++ b/Modules/Filtering/ImageLabel/test/itkChangeLabelImageFilterGTest.cxx @@ -0,0 +1,112 @@ +/*========================================================================= + * + * Copyright NumFOCUS + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + *=========================================================================*/ + +#include "itkRandomImageSource.h" +#include "itkChangeLabelImageFilter.h" +#include "itkImageRegionIteratorWithIndex.h" +#include "itkGTest.h" + +TEST(ChangeLabelImageFilter, FilterLabelsAboveThreshold) +{ + constexpr unsigned int ImageDimension{ 3 }; + + using InputImageType = itk::Image; + using OutputImageType = itk::Image; + using InputPixelType = InputImageType::PixelType; + + using SourceType = itk::RandomImageSource; + auto source = SourceType::New(); + + InputImageType::SizeValueType sizeArray[ImageDimension] = { 3, 3, 3 }; + + constexpr InputPixelType upper{ 10 }; + source->SetMin(InputPixelType{}); + source->SetMax(upper); + source->SetSize(sizeArray); + + using FilterType = itk::ChangeLabelImageFilter; + auto filter = FilterType::New(); + + ITK_GTEST_EXERCISE_BASIC_OBJECT_METHODS(filter, ChangeLabelImageFilter, UnaryFunctorImageFilter); + + constexpr InputPixelType background{ 0 }; + constexpr InputPixelType maxRemainingLabel{ 2 }; + for (InputPixelType i = maxRemainingLabel; i <= upper; ++i) + { + filter->SetChange(i, background); + } + + filter->SetInput(source->GetOutput()); + + EXPECT_NO_THROW(filter->Update()); + filter->SetFunctor(filter->GetFunctor()); + + const auto outputImage = filter->GetOutput(); + + itk::ImageRegionIteratorWithIndex ot(outputImage, outputImage->GetRequestedRegion()); + for (ot.GoToBegin(); !ot.IsAtEnd(); ++ot) + { + EXPECT_LE(ot.Get(), maxRemainingLabel); + } +} + +TEST(ChangeLabelImageFilter, ClearChangeMapPassesThrough) +{ + constexpr unsigned int ImageDimension{ 3 }; + + using InputImageType = itk::Image; + using OutputImageType = itk::Image; + using InputPixelType = InputImageType::PixelType; + + using SourceType = itk::RandomImageSource; + auto source = SourceType::New(); + + InputImageType::SizeValueType sizeArray[ImageDimension] = { 3, 3, 3 }; + + constexpr InputPixelType upper{ 10 }; + source->SetMin(InputPixelType{}); + source->SetMax(upper); + source->SetSize(sizeArray); + + using FilterType = itk::ChangeLabelImageFilter; + auto filter = FilterType::New(); + + constexpr InputPixelType background{ 0 }; + constexpr InputPixelType maxRemainingLabel{ 2 }; + for (InputPixelType i = maxRemainingLabel; i <= upper; ++i) + { + filter->SetChange(i, background); + } + + filter->SetInput(source->GetOutput()); + filter->Update(); + + // Clear the change map so input passes through unchanged + filter->ClearChangeMap(); + + EXPECT_NO_THROW(filter->Update()); + + const auto outputImage = filter->GetOutput(); + + itk::ImageRegionIteratorWithIndex it(source->GetOutput(), source->GetOutput()->GetRequestedRegion()); + itk::ImageRegionIteratorWithIndex ot(outputImage, outputImage->GetRequestedRegion()); + for (it.GoToBegin(), ot.GoToBegin(); !ot.IsAtEnd(); ++it, ++ot) + { + EXPECT_EQ(it.Get(), ot.Get()); + } +} diff --git a/Modules/Filtering/ImageLabel/test/itkChangeLabelImageFilterTest.cxx b/Modules/Filtering/ImageLabel/test/itkChangeLabelImageFilterTest.cxx deleted file mode 100644 index 5b5eb9a974e..00000000000 --- a/Modules/Filtering/ImageLabel/test/itkChangeLabelImageFilterTest.cxx +++ /dev/null @@ -1,185 +0,0 @@ -/*========================================================================= - * - * Copyright NumFOCUS - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0.txt - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - *=========================================================================*/ - -#include "itkRandomImageSource.h" -#include "itkChangeLabelImageFilter.h" -#include "itkImageRegionIteratorWithIndex.h" - - -int -itkChangeLabelImageFilterTest(int, char *[]) -{ - - // Define the dimension of the images - constexpr unsigned int ImageDimension{ 3 }; - - // Declare the types of the images - using InputImageType = itk::Image; - using OutputImageType = itk::Image; - using InputPixelType = InputImageType::PixelType; - using OutputPixelType = OutputImageType::PixelType; - - // Declare iterator type - using InputIteratorType = itk::ImageRegionIteratorWithIndex; - - using OutputIteratorType = itk::ImageRegionIteratorWithIndex; - - // Use a random image source as input - using SourceType = itk::RandomImageSource; - auto source = SourceType::New(); - - InputImageType::SizeValueType sizeArray[ImageDimension] = { 3, 3, 3 }; - - // limit to a few labels - constexpr InputPixelType upper{ 10 }; - source->SetMin(InputPixelType{}); - source->SetMax(upper); - source->SetSize(sizeArray); - - // Declare the type for the binary threshold filter - using FilterType = itk::ChangeLabelImageFilter; - - - // Create a filter - auto filter = FilterType::New(); - - // Eliminate most labels - constexpr InputPixelType background{ 0 }; - constexpr InputPixelType maxRemainingLabel{ 2 }; - for (InputPixelType i = maxRemainingLabel; i <= upper; ++i) - { - filter->SetChange(i, background); - } - - filter->Print(std::cout); - - // exercise Get methods - - - // Connect the input images - filter->SetInput(source->GetOutput()); - - // Get the Smart Pointer to the Filter Output - const OutputImageType::Pointer outputImage = filter->GetOutput(); - - // Execute the filter - try - { - filter->Update(); - filter->SetFunctor(filter->GetFunctor()); - } - catch (...) - { - std::cerr << "Caught an unexpected exception. " << std::endl; - std::cerr << "Test failed. " << std::endl; - return EXIT_FAILURE; - } - - // Create an iterator for going through the image output - InputIteratorType it(source->GetOutput(), source->GetOutput()->GetRequestedRegion()); - OutputIteratorType ot(outputImage, outputImage->GetRequestedRegion()); - - bool pass = true; - - // Check the content of the result image - std::cout << "Verification of the output " << std::endl; - ot.GoToBegin(); - it.GoToBegin(); - while (!ot.IsAtEnd()) - { - - const InputPixelType input = it.Get(); - const OutputPixelType output = ot.Get(); - std::cout << static_cast(input) << ' ' << static_cast(output) << std::endl; - - if (output > maxRemainingLabel) - { - pass = false; - } - if (!pass) - { - std::cerr << "Error in itkChangeLaelImageFilterTest " << std::endl; - std::cerr << " input = " << input; - std::cerr << " output = " << output; - std::cerr << std::endl; - return EXIT_FAILURE; - } - - ++ot; - ++it; - } - - - // Test to see if clearing the changemap works - filter->ClearChangeMap(); - - // re-execute the filter - try - { - filter->Update(); - } - catch (...) - { - std::cerr << "Caught an unexpected exception. " << std::endl; - std::cerr << "Test failed. " << std::endl; - return EXIT_FAILURE; - } - - // Create an iterator for going through the image output - InputIteratorType ita(source->GetOutput(), source->GetOutput()->GetRequestedRegion()); - OutputIteratorType ota(outputImage, outputImage->GetRequestedRegion()); - - - // Check the content of the result image - // Since the change map is clear, input is expected to be the same as output - std::cout << "Verification of the output " << std::endl; - ota.GoToBegin(); - ita.GoToBegin(); - while (!ota.IsAtEnd()) - { - - const InputPixelType input = ita.Get(); - const OutputPixelType output = ota.Get(); - std::cout << static_cast(input) << ' ' << static_cast(output) << std::endl; - - if (input != output) - { - pass = false; - } - if (!pass) - { - std::cerr << "Error in itkChangeLaelImageFilterTest " << std::endl; - std::cerr << " input = " << input; - std::cerr << " output = " << output; - std::cerr << std::endl; - return EXIT_FAILURE; - } - - ++ota; - ++ita; - } - - if (pass) - { - std::cout << "Test passed. " << std::endl; - return EXIT_SUCCESS; - } - - std::cout << "Test failed. " << std::endl; - return EXIT_FAILURE; -} diff --git a/Modules/Filtering/ImageSources/itk-module.cmake b/Modules/Filtering/ImageSources/itk-module.cmake index 1a949ba4142..6c733716f81 100644 --- a/Modules/Filtering/ImageSources/itk-module.cmake +++ b/Modules/Filtering/ImageSources/itk-module.cmake @@ -5,6 +5,7 @@ itk_module( COMPILE_DEPENDS ITKCommon TEST_DEPENDS + ITKGoogleTest ITKTestKernel ITKImageIntensity DESCRIPTION "${DOCUMENTATION}" diff --git a/Modules/Filtering/ImageSources/test/CMakeLists.txt b/Modules/Filtering/ImageSources/test/CMakeLists.txt index 7f4348b85d3..b32318e86c7 100644 --- a/Modules/Filtering/ImageSources/test/CMakeLists.txt +++ b/Modules/Filtering/ImageSources/test/CMakeLists.txt @@ -1,7 +1,6 @@ itk_module_test() set( ITKImageSourcesTests - itkGaborKernelFunctionTest.cxx itkGaborImageSourceTest.cxx itkGaussianImageSourceTest.cxx itkGridImageSourceTest.cxx @@ -11,12 +10,10 @@ set( createtestdriver(ITKImageSources "${ITKImageSources-Test_LIBRARIES}" "${ITKImageSourcesTests}") -itk_add_test( - NAME itkGaborKernelFunctionTest - COMMAND - ITKImageSourcesTestDriver - itkGaborKernelFunctionTest -) +set(ITKImageSourcesGTests itkGaborKernelFunctionGTest.cxx) + +creategoogletestdriver(ITKImageSources "${ITKImageSources-Test_LIBRARIES}" "${ITKImageSourcesGTests}") + itk_add_test( NAME itkGaborImageSourceTest0 COMMAND diff --git a/Modules/Filtering/ImageSources/test/itkGaborKernelFunctionGTest.cxx b/Modules/Filtering/ImageSources/test/itkGaborKernelFunctionGTest.cxx new file mode 100644 index 00000000000..edb34021807 --- /dev/null +++ b/Modules/Filtering/ImageSources/test/itkGaborKernelFunctionGTest.cxx @@ -0,0 +1,90 @@ +/*========================================================================= + * + * Copyright NumFOCUS + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + *=========================================================================*/ + +#include "itkGaborKernelFunction.h" +#include "itkMath.h" +#include "itkGTest.h" + +TEST(GaborKernelFunction, BasicObjectMethods) +{ + using KernelFunctionType = itk::GaborKernelFunction; + auto gabor = KernelFunctionType::New(); + + ITK_GTEST_EXERCISE_BASIC_OBJECT_METHODS(gabor, GaborKernelFunction, KernelFunctionBase); +} + +TEST(GaborKernelFunction, SetGetParameters) +{ + using KernelFunctionType = itk::GaborKernelFunction; + auto gabor = KernelFunctionType::New(); + + constexpr double sigma{ 1.5 }; + gabor->SetSigma(sigma); + EXPECT_DOUBLE_EQ(gabor->GetSigma(), sigma); + + constexpr double frequency{ 2.0 }; + gabor->SetFrequency(frequency); + EXPECT_DOUBLE_EQ(gabor->GetFrequency(), frequency); + + constexpr double phaseOffset{ 0.8 }; + gabor->SetPhaseOffset(phaseOffset); + EXPECT_DOUBLE_EQ(gabor->GetPhaseOffset(), phaseOffset); + + gabor->SetCalculateImaginaryPart(true); + EXPECT_TRUE(gabor->GetCalculateImaginaryPart()); + + gabor->CalculateImaginaryPartOn(); + EXPECT_TRUE(gabor->GetCalculateImaginaryPart()); + + gabor->CalculateImaginaryPartOff(); + EXPECT_FALSE(gabor->GetCalculateImaginaryPart()); +} + +TEST(GaborKernelFunction, EvaluateImaginaryPart) +{ + using KernelFunctionType = itk::GaborKernelFunction; + auto gabor = KernelFunctionType::New(); + + gabor->SetSigma(1.5); + gabor->SetFrequency(2.0); + gabor->SetPhaseOffset(0.8); + gabor->CalculateImaginaryPartOn(); + + constexpr double tolerance{ 1e-12 }; + constexpr double point{ 2.86 }; + constexpr double expectedValue{ -0.13297125073713259 }; + + EXPECT_NEAR(gabor->Evaluate(point), expectedValue, tolerance); +} + +TEST(GaborKernelFunction, EvaluateRealPart) +{ + using KernelFunctionType = itk::GaborKernelFunction; + auto gabor = KernelFunctionType::New(); + + gabor->SetSigma(1.5); + gabor->SetFrequency(2.0); + gabor->SetPhaseOffset(0.8); + gabor->CalculateImaginaryPartOff(); + + constexpr double tolerance{ 1e-12 }; + constexpr double point{ 2.86 }; + constexpr double expectedValue{ 0.093234196962237226 }; + + EXPECT_NEAR(gabor->Evaluate(point), expectedValue, tolerance); +} diff --git a/Modules/Filtering/ImageSources/test/itkGaborKernelFunctionTest.cxx b/Modules/Filtering/ImageSources/test/itkGaborKernelFunctionTest.cxx deleted file mode 100644 index ac6da3e5c5b..00000000000 --- a/Modules/Filtering/ImageSources/test/itkGaborKernelFunctionTest.cxx +++ /dev/null @@ -1,83 +0,0 @@ -/*========================================================================= - * - * Copyright NumFOCUS - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0.txt - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - *=========================================================================*/ - -#include "itkGaborKernelFunction.h" -#include "itkMacro.h" -#include "itkTestingMacros.h" - -int -itkGaborKernelFunctionTest(int itkNotUsed(argc), char * itkNotUsed(argv)[]) -{ - using KernelFunctionType = itk::GaborKernelFunction; - auto gabor = KernelFunctionType::New(); - - ITK_EXERCISE_BASIC_OBJECT_METHODS(gabor, GaborKernelFunction, KernelFunctionBase); - - constexpr double sigma{ 1.5 }; - gabor->SetSigma(sigma); - ITK_TEST_SET_GET_VALUE(sigma, gabor->GetSigma()); - - constexpr double frequency{ 2. }; - gabor->SetFrequency(frequency); - ITK_TEST_SET_GET_VALUE(frequency, gabor->GetFrequency()); - - constexpr double phaseOffset{ 0.8 }; - gabor->SetPhaseOffset(phaseOffset); - ITK_TEST_SET_GET_VALUE(phaseOffset, gabor->GetPhaseOffset()); - - constexpr bool calculateImaginaryPart{ true }; - gabor->SetCalculateImaginaryPart(calculateImaginaryPart); - ITK_TEST_SET_GET_VALUE(calculateImaginaryPart, gabor->GetCalculateImaginaryPart()); - - gabor->CalculateImaginaryPartOn(); - ITK_TEST_SET_GET_VALUE(true, gabor->GetCalculateImaginaryPart()); - - constexpr double tolerance{ 1e-12 }; - constexpr double point{ 2.86 }; - double expectedValue = -0.13297125073713259; - double result = gabor->Evaluate(point); - if (!itk::Math::FloatAlmostEqual(expectedValue, result, 10, tolerance)) - { - std::cerr.precision(static_cast(itk::Math::Absolute(std::log10(tolerance)))); - std::cerr << "Test failed!" << std::endl; - std::cerr << "Error in Evaluate at point " << point << std::endl; - std::cerr << "Expected value " << expectedValue << std::endl; - std::cerr << " differs from: " << result; - std::cerr << " by more than " << tolerance << std::endl; - return EXIT_FAILURE; - } - - gabor->CalculateImaginaryPartOff(); - ITK_TEST_SET_GET_VALUE(false, gabor->GetCalculateImaginaryPart()); - - expectedValue = 0.093234196962237226; - result = gabor->Evaluate(point); - if (!itk::Math::FloatAlmostEqual(expectedValue, result, 10, tolerance)) - { - std::cerr.precision(static_cast(itk::Math::Absolute(std::log10(tolerance)))); - std::cerr << "Test failed!" << std::endl; - std::cerr << "Error in Evaluate at point " << point << std::endl; - std::cerr << "Expected value " << expectedValue << std::endl; - std::cerr << " differs from " << result << std::endl; - std::cerr << " by more than " << tolerance << std::endl; - return EXIT_FAILURE; - } - - std::cout << "Test finished." << std::endl; - return EXIT_SUCCESS; -}