Skip to content

Commit 2d8ecb3

Browse files
authored
Merge pull request InsightSoftwareConsortium#5899 from hjmjohnson/convert-itkadaptorcomparisontest-to-gtest
ENH: Convert itkAdaptorComparisonTest to GTest
2 parents 07eb39f + bbdb22b commit 2d8ecb3

File tree

2 files changed

+20
-99
lines changed

2 files changed

+20
-99
lines changed

Modules/Core/Common/test/CMakeLists.txt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ set(
33
ITKCommon1Tests
44
itkImageRegionExplicitTest.cxx
55
itkCommandObserverObjectTest.cxx
6-
itkAdaptorComparisonTest.cxx
76
itkCovariantVectorGeometryTest.cxx
87
itkExtractImage3Dto2DTest.cxx
98
itkExtractImageTest.cxx
@@ -216,12 +215,6 @@ itk_add_test(
216215
ITKCommon1TestDriver
217216
itkCommandObserverObjectTest
218217
)
219-
itk_add_test(
220-
NAME itkAdaptorComparisonTest
221-
COMMAND
222-
ITKCommon1TestDriver
223-
itkAdaptorComparisonTest
224-
)
225218
itk_add_test(
226219
NAME itkThreadedIteratorRangePartitionerTest
227220
COMMAND
@@ -1693,6 +1686,7 @@ set(
16931686
itkPixelAccessGTest.cxx
16941687
itkImageTransformGTest.cxx
16951688
itkAnnulusOperatorGTest.cxx
1689+
itkAdaptorComparisonGTest.cxx
16961690
)
16971691
creategoogletestdriver(ITKCommon "${ITKCommon-Test_LIBRARIES}" "${ITKCommonGTests}")
16981692
# If `-static` was passed to CMAKE_EXE_LINKER_FLAGS, compilation fails. No need to

Modules/Core/Common/test/itkAdaptorComparisonTest.cxx renamed to Modules/Core/Common/test/itkAdaptorComparisonGTest.cxx

Lines changed: 19 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@
1616
*
1717
*=========================================================================*/
1818

19-
#include <iostream>
20-
#include <ctime>
2119
#include "itkImageRegionIteratorWithIndex.h"
2220
#include "itkImageRegionIterator.h"
21+
#include "itkGTest.h"
22+
23+
namespace
24+
{
2325

2426
void
2527
AdaptorSupportedIteratorSpeed(itk::Image<float, 3> * img)
2628
{
2729
itk::ImageRegionIteratorWithIndex<itk::Image<float, 3>> it(img, img->GetRequestedRegion());
28-
2930
while (!it.IsAtEnd())
3031
{
3132
++it;
@@ -36,7 +37,6 @@ void
3637
NoAdaptorSupportIteratorSpeed(itk::Image<float, 3> * img)
3738
{
3839
itk::ImageRegionIterator<itk::Image<float, 3>> it(img, img->GetRequestedRegion());
39-
4040
while (!it.IsAtEnd())
4141
{
4242
++it;
@@ -47,7 +47,6 @@ void
4747
AdaptorSupportedModifyScalars(itk::Image<float, 3> * img)
4848
{
4949
itk::ImageRegionIteratorWithIndex<itk::Image<float, 3>> it(img, img->GetRequestedRegion());
50-
5150
while (!it.IsAtEnd())
5251
{
5352
// *it += 3.435f;
@@ -60,7 +59,6 @@ void
6059
NoAdaptorSupportModifyScalars(itk::Image<float, 3> * img)
6160
{
6261
itk::ImageRegionIterator<itk::Image<float, 3>> it(img, img->GetRequestedRegion());
63-
6462
while (!it.IsAtEnd())
6563
{
6664
// *it += 3.435f;
@@ -73,7 +71,6 @@ void
7371
BypassAdaptorSupportModifyScalars(itk::Image<float, 3> * img)
7472
{
7573
itk::ImageRegionIteratorWithIndex<itk::Image<float, 3>> it(img, img->GetRequestedRegion());
76-
7774
while (!it.IsAtEnd())
7875
{
7976
it.Value() += 3.435f;
@@ -88,15 +85,13 @@ AdaptorSupportedModifyVectors(itk::Image<itk::Vector<float, 3>, 3> * img)
8885
using VectorType = itk::Vector<float, N>;
8986

9087
itk::ImageRegionIteratorWithIndex<itk::Image<VectorType, 3>> it(img, img->GetRequestedRegion());
91-
9288
while (!it.IsAtEnd())
9389
{
9490
VectorType temp_vector = it.Get();
9591
for (unsigned int i = 0; i < N; ++i)
9692
{
9793
temp_vector[i] += 3.435f;
9894
}
99-
10095
it.Set(temp_vector);
10196
++it;
10297
}
@@ -109,7 +104,6 @@ NoAdaptorSupportModifyVectors(itk::Image<itk::Vector<float, 3>, 3> * img)
109104
using VectorType = itk::Vector<float, N>;
110105

111106
itk::ImageRegionIterator<itk::Image<VectorType, 3>> it(img, img->GetRequestedRegion());
112-
113107
while (!it.IsAtEnd())
114108
{
115109
VectorType temp_vector = it.Get();
@@ -131,27 +125,23 @@ BypassAdaptorSupportModifyVectors(itk::Image<itk::Vector<float, 3>, 3> * img)
131125
using VectorType = itk::Vector<float, N>;
132126

133127
itk::ImageRegionIteratorWithIndex<itk::Image<VectorType, 3>> it(img, img->GetRequestedRegion());
134-
135128
while (!it.IsAtEnd())
136129
{
137130
for (unsigned int i = 0; i < N; ++i)
138131
{
139132
(it.Value())[i] += 3.435f;
140133
}
141-
142134
++it;
143135
}
144136
}
145137

146-
147138
void
148139
BypassNoAdaptorSupportModifyVectors(itk::Image<itk::Vector<float, 3>, 3> * img)
149140
{
150141
constexpr unsigned int N{ 3 };
151142
using VectorType = itk::Vector<float, N>;
152143

153144
itk::ImageRegionIterator<itk::Image<VectorType, 3>> it(img, img->GetRequestedRegion());
154-
155145
while (!it.IsAtEnd())
156146
{
157147
for (unsigned int i = 0; i < N; ++i)
@@ -162,17 +152,18 @@ BypassNoAdaptorSupportModifyVectors(itk::Image<itk::Vector<float, 3>, 3> * img)
162152
}
163153
}
164154

155+
} // namespace
156+
165157

166-
int
167-
itkAdaptorComparisonTest(int, char *[])
158+
TEST(AdaptorComparison, IteratorOperationsComplete)
168159
{
169160
using ScalarImageType = itk::Image<float, 3>;
170161
using VectorImageType = itk::Image<itk::Vector<float, 3>, 3>;
171162

172-
// Set up some images
173-
constexpr itk::Size<3> size{ 100, 100, 100 };
163+
constexpr itk::Size<3> size{ { 100, 100, 100 } };
174164
itk::ImageRegion<3> region{ size };
175165

166+
// Set up some images
176167
auto scalar_image = ScalarImageType::New();
177168
auto vector_image = VectorImageType::New();
178169

@@ -182,80 +173,16 @@ itkAdaptorComparisonTest(int, char *[])
182173
vector_image->SetRegions(region);
183174
vector_image->Allocate();
184175

185-
auto initialVectorValue = itk::MakeFilled<VectorImageType::PixelType>(1.2345); // arbitrary value;
176+
auto initialVectorValue = itk::MakeFilled<VectorImageType::PixelType>(1.2345f); // arbitrary value;
186177
vector_image->FillBuffer(initialVectorValue);
187178

188-
// Time trials
189-
190-
std::cout << "Speed of adaptor supporting iterator (for reference) \t";
191-
192-
const clock_t adaptor_comp = [&]() -> auto {
193-
const auto start = clock();
194-
AdaptorSupportedIteratorSpeed(scalar_image);
195-
const auto stop = clock();
196-
return stop - start;
197-
}();
198-
199-
std::cout << adaptor_comp << std::endl;
200-
const clock_t no_adaptor_comp = [=](auto scalarImage) {
201-
std::cout << "Speed of iterator that does not support adaptors (for reference) \t";
202-
const auto start = clock();
203-
NoAdaptorSupportIteratorSpeed(scalarImage);
204-
const auto stop = clock();
205-
return stop - start;
206-
}(scalar_image);
207-
std::cout << no_adaptor_comp << std::endl;
208-
{
209-
std::cout << "Modifying scalar image using adaptor iterator...\t";
210-
const auto start = clock();
211-
AdaptorSupportedModifyScalars(scalar_image);
212-
const auto stop = clock();
213-
std::cout << (stop - start) << "\t compensated = " << (stop - start) - adaptor_comp << std::endl;
214-
}
215-
{
216-
std::cout << "Modifying scalar image using non-adaptor iterator...\t";
217-
const auto start = clock();
218-
NoAdaptorSupportModifyScalars(scalar_image);
219-
const auto stop = clock();
220-
std::cout << (stop - start) << "\t compensated = " << (stop - start) - no_adaptor_comp << std::endl;
221-
}
222-
{
223-
std::cout << "Modifying vector image using adaptor iterator...\t";
224-
const auto start = clock();
225-
AdaptorSupportedModifyVectors(vector_image);
226-
const auto stop = clock();
227-
std::cout << (stop - start) << "\t compensated = " << (stop - start) - adaptor_comp << std::endl;
228-
}
229-
{
230-
std::cout << "Modifying vector image using non-adaptor iterator...\t";
231-
const auto start = clock();
232-
NoAdaptorSupportModifyVectors(vector_image);
233-
const auto stop = clock();
234-
std::cout << (stop - start) << "\t compensated = " << (stop - start) - no_adaptor_comp << std::endl;
235-
}
236-
{
237-
std::cout << "Modifying scalar image bypassing adaptor api using"
238-
<< " adaptor iterator...\t";
239-
const auto start = clock();
240-
BypassAdaptorSupportModifyScalars(scalar_image);
241-
const auto stop = clock();
242-
std::cout << (stop - start) << "\t compensated = " << (stop - start) - adaptor_comp << std::endl;
243-
}
244-
{
245-
std::cout << "Modifying vector image bypassing adaptor api using"
246-
<< " non-adaptor iterator...\t";
247-
const auto start = clock();
248-
BypassNoAdaptorSupportModifyVectors(vector_image);
249-
const auto stop = clock();
250-
std::cout << (stop - start) << "\t compensated = " << (stop - start) - adaptor_comp << std::endl;
251-
}
252-
{
253-
std::cout << "Modifying vector image bypassing adaptor api using"
254-
<< " adaptor iterator...\t";
255-
const auto start = clock();
256-
BypassAdaptorSupportModifyVectors(vector_image);
257-
const auto stop = clock();
258-
std::cout << (stop - start) << "\t compensated = " << (stop - start) - adaptor_comp << std::endl;
259-
}
260-
return EXIT_SUCCESS;
179+
AdaptorSupportedIteratorSpeed(scalar_image);
180+
NoAdaptorSupportIteratorSpeed(scalar_image);
181+
AdaptorSupportedModifyScalars(scalar_image);
182+
NoAdaptorSupportModifyScalars(scalar_image);
183+
AdaptorSupportedModifyVectors(vector_image);
184+
NoAdaptorSupportModifyVectors(vector_image);
185+
BypassAdaptorSupportModifyScalars(scalar_image);
186+
BypassNoAdaptorSupportModifyVectors(vector_image);
187+
BypassAdaptorSupportModifyVectors(vector_image);
261188
}

0 commit comments

Comments
 (0)