Skip to content

Commit

Permalink
ENH: Increase coverage for itk::RandomImageSource
Browse files Browse the repository at this point in the history
Increase coverage for `itk::RandomImageSource`:
- Exercise basic object methods using the
  `ITK_EXERCISE_BASIC_OBJECT_METHODS` macro.
- Test the Set/Get methods using the `ITK_TEST_SET_GET_VALUE` macro.

Add the accompanying content data link file.
  • Loading branch information
jhlegarreta authored and dzenanz committed Nov 7, 2022
1 parent 056fee3 commit 84fc5be
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 0 deletions.
@@ -0,0 +1 @@
350ff29c22d89ca426c8791b56272b2449f965fb920117406d9c01874bc365f4ddaee3445b529cd831d7c59ce209d060c046bce4cf18dd04e91823b4fca45578
10 changes: 10 additions & 0 deletions Modules/Core/TestKernel/test/CMakeLists.txt
@@ -1,5 +1,7 @@
itk_module_test()
set(ITKTestKernelTests
itkRandomImageSourceAttributesTest.cxx
itkRandomImageSourceValuesTest.cxx
itkTestingExtractSliceImageFilterTest.cxx
itkTestingStretchIntensityImageFilterTest.cxx
)
Expand All @@ -9,6 +11,14 @@ CreateTestDriver(ITKTestKernel "${ITKTestKernel-Test_LIBRARIES}" "${ITKTestKerne
set(BASELINE "${ITK_DATA_ROOT}/Baseline/Common")
set(TEMP ${ITK_TEST_OUTPUT_DIR})

itk_add_test(NAME itkRandomImageSourceAttributesTest
COMMAND ITKTestKernelTestDriver itkRandomImageSourceAttributesTest)

itk_add_test(NAME itkRandomImageSourceValuesTest
COMMAND ITKTestKernelTestDriver
--compare DATA{Baseline/itkRandomImageSourceValuesTest.mha} ${ITK_TEST_OUTPUT_DIR}/itkRandomImageSourceValuesTest.mha
itkRandomImageSourceValuesTest ${ITK_TEST_OUTPUT_DIR}/itkRandomImageSourceValuesTest.mha)

itk_add_test(NAME itkTestingExtractSliceImageFilterTest
COMMAND ITKTestKernelTestDriver itkTestingExtractSliceImageFilterTest)

Expand Down
104 changes: 104 additions & 0 deletions Modules/Core/TestKernel/test/itkRandomImageSourceAttributesTest.cxx
@@ -0,0 +1,104 @@
/*=========================================================================
*
* 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 "itkTestingMacros.h"


template <typename TImage>
int
itkRandomImageSourceAttributesTestHelper(const typename TImage::SizeType size,
const typename TImage::SpacingType spacing,
const typename TImage::PointType origin,
const typename TImage::DirectionType direction,
const typename TImage::ValueType min,
const typename TImage::ValueType max)
{

using ImageSourceType = itk::RandomImageSource<TImage>;
auto randomImageSource = ImageSourceType::New();

ITK_EXERCISE_BASIC_OBJECT_METHODS(randomImageSource, RandomImageSource, ImageSource);


randomImageSource->SetSize(size);
ITK_TEST_SET_GET_VALUE(*size.GetSize(), *randomImageSource->GetSize());

randomImageSource->SetSpacing(spacing);
ITK_TEST_SET_GET_VALUE(spacing, randomImageSource->GetSpacing());

randomImageSource->SetOrigin(origin);
ITK_TEST_SET_GET_VALUE(origin, randomImageSource->GetOrigin());

randomImageSource->SetDirection(direction);
ITK_TEST_SET_GET_VALUE(direction, randomImageSource->GetDirection());

randomImageSource->SetMin(min);
ITK_TEST_SET_GET_VALUE(min, randomImageSource->GetMin());

randomImageSource->SetMax(max);
ITK_TEST_SET_GET_VALUE(max, randomImageSource->GetMax());


return EXIT_SUCCESS;
}


int
itkRandomImageSourceAttributesTest(int, char *[])
{

constexpr unsigned int Dimension2D = 2;
constexpr unsigned int Dimension3D = 3;

using PixelType = float;

int testStatus = EXIT_SUCCESS;

{
using ImageType2D = itk::Image<PixelType, Dimension2D>;

const ImageType2D::SizeType size{ 25, 25 };
const ImageType2D::SpacingType spacing{ { 0.7, 2.1 } };
const ImageType2D::PointType origin{ { -1.7, 5.2 } };
const double d[4] = { 0, 1.0, 1.0, 0 };
const ImageType2D::DirectionType direction = ImageType2D::DirectionType::InternalMatrixType(d);
const ImageType2D::ValueType min{ 0.0 };
const ImageType2D::ValueType max{ 1000.0 };

testStatus = itkRandomImageSourceAttributesTestHelper<ImageType2D>(size, spacing, origin, direction, min, max);
}

{
using ImageType3D = itk::Image<PixelType, Dimension3D>;

const ImageType3D::SizeType size{ 14, 17, 36 };
const ImageType3D::SpacingType spacing{ { 0.7, 0.4, 1.2 } };
const ImageType3D::PointType origin{ { -1.7, 5.2, 3.4 } };
const double d[9] = { 0, 1.0, 0, 1.0, 0, 0, 0, 1.0, 0 };
const ImageType3D::DirectionType direction = ImageType3D::DirectionType::InternalMatrixType(d);
const ImageType3D::ValueType min{ 0.0 };
const ImageType3D::ValueType max{ 10.0 };

testStatus = itkRandomImageSourceAttributesTestHelper<ImageType3D>(size, spacing, origin, direction, min, max);
}


std::cout << "Test finished." << std::endl;
return testStatus;
}
58 changes: 58 additions & 0 deletions Modules/Core/TestKernel/test/itkRandomImageSourceValuesTest.cxx
@@ -0,0 +1,58 @@
/*=========================================================================
*
* 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 "itkImageFileWriter.h"
#include "itkRandomImageSource.h"
#include "itkTestingMacros.h"


int
itkRandomImageSourceValuesTest(int argc, char * argv[])
{
if (argc != 2)
{
std::cerr << "Missing parameters." << std::endl;
std::cerr << "Usage: " << itkNameOfTestExecutableMacro(argv) << " outputFileName" << std::endl;
return EXIT_FAILURE;
}

constexpr unsigned int Dimension = 2;

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

using ImageSourceType = itk::RandomImageSource<ImageType>;
auto randomImageSource = ImageSourceType::New();

const ImageType::SizeType size{ { 10, 10 } };
randomImageSource->SetSize(size);

randomImageSource->SetMin(0.0);
randomImageSource->SetMax(20.0);

randomImageSource->SetNumberOfWorkUnits(1);

ITK_TRY_EXPECT_NO_EXCEPTION(randomImageSource->Update());


itk::WriteImage(randomImageSource->GetOutput(), argv[1]);


std::cout << "Test finished." << std::endl;
return EXIT_SUCCESS;
}

0 comments on commit 84fc5be

Please sign in to comment.