Skip to content

Commit d8bd849

Browse files
committed
STYLE: Add scoped blocks to improve readability and structure
Introduced scoped blocks for logical grouping and better separation of iterator test sections. Improved variable naming consistency for clarity.
1 parent 1761e31 commit d8bd849

1 file changed

Lines changed: 181 additions & 144 deletions

File tree

Modules/Core/Common/test/itkImageRandomNonRepeatingIteratorWithIndexTest.cxx

Lines changed: 181 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -56,176 +56,193 @@ itkImageRandomNonRepeatingIteratorWithIndexTest(int, char *[])
5656
constexpr PriorityImageType::SizeType subsize{ 3, 4, 5 };
5757
constexpr auto numberOfPixelsSub = subsize.CalculateProductOfElements();
5858

59-
auto priorityImage = PriorityImageType::New();
60-
const PriorityImageType::RegionType priorityRegion{ prioritySize };
61-
priorityImage->SetRegions(priorityRegion);
62-
priorityImage->Allocate();
63-
// we will make most of this image of ones, with a small region of
64-
// zeros. Then pixels from the zero region should be selected
65-
// preferentially.
66-
std::cout << "Building Priority image" << std::endl;
67-
priorityImage->FillBuffer(1);
68-
69-
const PriorityImageType::RegionType subregion{ substart, subsize };
70-
PriorityIteratorType subit(priorityImage, subregion);
71-
subit.GoToBegin();
72-
while (!subit.IsAtEnd())
59+
auto priorityImage = PriorityImageType::New();
7360
{
74-
subit.Set(0);
75-
++subit;
61+
const PriorityImageType::RegionType priorityRegion{ prioritySize };
62+
priorityImage->SetRegions(priorityRegion);
63+
priorityImage->Allocate();
64+
// we will make most of this image of ones, with a small region of
65+
// zeros. Then pixels from the zero region should be selected
66+
// preferentially.
67+
std::cout << "Building Priority image" << std::endl;
68+
priorityImage->FillBuffer(1);
7669
}
7770

78-
//********
79-
std::cout << "Filling image with indices" << std::endl;
80-
81-
RandomIteratorType it(myImage, region0);
82-
it.SetNumberOfSamples(numberOfPixelsSize0);
83-
it.GoToBegin();
84-
ImageType::IndexType index0;
85-
// Because the random iterator does not repeat, this should
86-
// fill the image with indices
87-
while (!it.IsAtEnd())
71+
const PriorityImageType::RegionType subregion{ substart, subsize };
8872
{
89-
index0 = it.GetIndex();
90-
it.Set(index0);
91-
++it;
73+
PriorityIteratorType subit(priorityImage, subregion);
74+
subit.GoToBegin();
75+
while (!subit.IsAtEnd())
76+
{
77+
subit.Set(0);
78+
++subit;
79+
}
9280
}
9381

94-
// Sample the image
95-
IteratorType ot(myImage, region0);
96-
ot.GoToBegin();
97-
// if it repeated it is going to have missed a few.
98-
std::cout << "Verifying iterators... ";
99-
while (!ot.IsAtEnd())
10082
{
101-
index0 = ot.GetIndex();
102-
if (ot.Get() != index0)
83+
//********
84+
std::cout << "Filling image with indices" << std::endl;
85+
86+
RandomIteratorType it(myImage, region0);
87+
it.SetNumberOfSamples(numberOfPixelsSize0);
88+
it.GoToBegin();
10389
{
104-
std::cerr << "Values don't correspond to what was stored " << std::endl;
105-
std::cerr << "Test failed at index ";
106-
std::cerr << index0 << std::endl;
107-
return EXIT_FAILURE;
90+
ImageType::IndexType indexFill0;
91+
// Because the random iterator does not repeat, this should
92+
// fill the image with indices
93+
while (!it.IsAtEnd())
94+
{
95+
indexFill0 = it.GetIndex();
96+
it.Set(indexFill0);
97+
++it;
98+
}
10899
}
109-
// std::cout <<".";
110-
// std::cout << index0 << std::endl;
111-
++ot;
112-
}
113-
// Now we have walked through all the pixels of low priority,
114-
// the next one should be outside the region.
115-
if (subregion.IsInside(index0))
116-
{
117-
std::cerr << "Iterator in priority region test failed" << std::endl;
118-
std::cerr << index0 << " is outside the region (should be in)" << region0 << std::endl;
119-
return EXIT_FAILURE;
120100
}
121-
std::cout << std::endl << " Done ! " << std::endl;
122-
123-
// Verification
124-
RandomConstIteratorType cot(myConstImage, region0);
125-
cot.SetNumberOfSamples(numberOfSamples);
126-
cot.GoToBegin();
127-
128-
std::cout << "Verifying const iterator... ";
129-
std::cout << "Random walk of the Iterator over the image " << std::endl;
130-
while (!cot.IsAtEnd())
131101
{
132-
index0 = cot.GetIndex();
133-
if (cot.Get() != index0)
102+
// Sample the image
103+
IteratorType ot(myImage, region0);
104+
ot.GoToBegin();
105+
// if it repeated it is going to have missed a few.
106+
std::cout << "Verifying iterators... ";
107+
ImageType::IndexType indexMatch;
108+
while (!ot.IsAtEnd())
134109
{
135-
std::cerr << "Values don't correspond to what was stored " << std::endl;
136-
std::cerr << "Test failed at index ";
137-
std::cerr << index0 << " value is " << cot.Get() << std::endl;
110+
indexMatch = ot.GetIndex();
111+
if (ot.Get() != indexMatch)
112+
{
113+
std::cerr << "Values don't correspond to what was stored " << std::endl;
114+
std::cerr << "Test failed at index ";
115+
std::cerr << indexMatch << std::endl;
116+
return EXIT_FAILURE;
117+
}
118+
// std::cout <<".";
119+
// std::cout << indexMatch << std::endl;
120+
++ot;
121+
}
122+
// Now we have walked through all the pixels of low priority,
123+
// the next one should be outside the region.
124+
if (subregion.IsInside(indexMatch))
125+
{
126+
std::cerr << "Iterator in priority region test failed" << std::endl;
127+
std::cerr << indexMatch << " is outside the region (should be in)" << region0 << std::endl;
138128
return EXIT_FAILURE;
139129
}
140-
std::cout << index0 << std::endl;
141-
++cot;
130+
std::cout << std::endl << " Done ! " << std::endl;
142131
}
143-
// Now we have walked through all the pixels of low priority,
144-
// the next one should be outside the region.
145-
if (subregion.IsInside(index0))
146132
{
147-
std::cerr << "Iterator in priority region test failed" << std::endl;
148-
std::cerr << index0 << " is outside the region (should be in)" << region0 << std::endl;
149-
return EXIT_FAILURE;
150-
}
151-
std::cout << " Done ! " << std::endl;
152-
153-
// Verification
154-
std::cout << "Verifying iterator in reverse direction... " << std::endl;
155-
std::cout << "Should be a random walk too (a different one)" << std::endl;
156-
RandomIteratorType ior(myImage, region0);
157-
ior.SetNumberOfSamples(numberOfSamples);
158-
ior.GoToEnd();
159-
--ior;
133+
// Verification
134+
RandomConstIteratorType cot(myConstImage, region0);
135+
cot.SetNumberOfSamples(numberOfSamples);
136+
cot.GoToBegin();
160137

161-
while (!ior.IsAtBegin())
162-
{
163-
index0 = ior.GetIndex();
164-
if (ior.Get() != index0)
138+
std::cout << "Verifying const iterator... ";
139+
std::cout << "Random walk of the Iterator over the image " << std::endl;
140+
ImageType::IndexType indexConstMatch;
141+
while (!cot.IsAtEnd())
165142
{
166-
std::cerr << "Values don't correspond to what was stored " << std::endl;
167-
std::cerr << "Test failed at index ";
168-
std::cerr << index0 << " value is " << ior.Get() << std::endl;
169-
return EXIT_FAILURE;
143+
indexConstMatch = cot.GetIndex();
144+
if (cot.Get() != indexConstMatch)
145+
{
146+
std::cerr << "Values don't correspond to what was stored " << std::endl;
147+
std::cerr << "Test failed at index ";
148+
std::cerr << indexConstMatch << " value is " << cot.Get() << std::endl;
149+
return EXIT_FAILURE;
150+
}
151+
std::cout << indexConstMatch << std::endl;
152+
++cot;
170153
}
171-
std::cout << index0 << std::endl;
172-
--ior;
173-
}
174-
std::cout << index0 << std::endl; // print the value at the beginning index
175-
std::cout << " Done ! " << std::endl;
176-
177-
// Verification
178-
std::cout << "Verifying const iterator in reverse direction... ";
179-
RandomConstIteratorType cor(myImage, region0);
180-
cor.SetNumberOfSamples(numberOfSamples); // 0=x, 1=y, 2=z
181-
cor.GoToEnd();
182-
--cor; // start at the end position
183-
while (!cor.IsAtBegin())
184-
{
185-
index0 = cor.GetIndex();
186-
if (cor.Get() != index0)
154+
// Now we have walked through all the pixels of low priority,
155+
// the next one should be outside the region.
156+
if (subregion.IsInside(indexConstMatch))
187157
{
188-
std::cerr << "Values don't correspond to what was stored " << std::endl;
189-
std::cerr << "Test failed at index ";
190-
std::cerr << index0 << " value is " << cor.Get() << std::endl;
158+
std::cerr << "Iterator in priority region test failed" << std::endl;
159+
std::cerr << indexConstMatch << " is outside the region (should be in)" << region0 << std::endl;
191160
return EXIT_FAILURE;
192161
}
193-
std::cout << index0 << std::endl;
194-
--cor;
162+
std::cout << " Done ! " << std::endl;
195163
}
196-
std::cout << index0 << std::endl; // print the value at the beginning index
197-
std::cout << " Done ! " << std::endl;
198-
// Verification
199-
std::cout << "Verifying const iterator in both directions... ";
200-
RandomConstIteratorType dor(myImage, region0);
201-
dor.SetNumberOfSamples(numberOfSamples); // 0=x, 1=y, 2=z
202-
dor.GoToEnd();
203-
--dor; // start at the last valid pixel position
204-
for (unsigned int counter = 0; !dor.IsAtEnd(); ++counter)
205164
{
206-
index0 = dor.GetIndex();
207-
if (dor.Get() != index0)
165+
// Verification
166+
std::cout << "Verifying iterator in reverse direction... " << std::endl;
167+
std::cout << "Should be a random walk too (a different one)" << std::endl;
168+
RandomIteratorType ior(myImage, region0);
169+
ior.SetNumberOfSamples(numberOfSamples);
170+
ior.GoToEnd();
171+
--ior;
172+
ImageType::IndexType indexReverse;
173+
while (!ior.IsAtBegin())
208174
{
209-
std::cerr << "Values don't correspond to what was stored " << std::endl;
210-
std::cerr << "Test failed at index ";
211-
std::cerr << index0 << " value is " << dor.Get() << std::endl;
212-
return EXIT_FAILURE;
175+
indexReverse = ior.GetIndex();
176+
if (ior.Get() != indexReverse)
177+
{
178+
std::cerr << "Values don't correspond to what was stored " << std::endl;
179+
std::cerr << "Test failed at index ";
180+
std::cerr << indexReverse << " value is " << ior.Get() << std::endl;
181+
return EXIT_FAILURE;
182+
}
183+
std::cout << indexReverse << std::endl;
184+
--ior;
213185
}
214-
std::cout << index0 << std::endl;
215-
if (counter < 6)
186+
std::cout << indexReverse << std::endl; // print the value at the beginning index
187+
std::cout << " Done ! " << std::endl;
188+
}
189+
{
190+
// Verification
191+
std::cout << "Verifying const iterator in reverse direction... ";
192+
RandomConstIteratorType cor(myImage, region0);
193+
cor.SetNumberOfSamples(numberOfSamples); // 0=x, 1=y, 2=z
194+
cor.GoToEnd();
195+
--cor; // start at the end position
196+
ImageType::IndexType indexConstReverse;
197+
while (!cor.IsAtBegin())
216198
{
217-
--dor;
199+
indexConstReverse = cor.GetIndex();
200+
if (cor.Get() != indexConstReverse)
201+
{
202+
std::cerr << "Values don't correspond to what was stored " << std::endl;
203+
std::cerr << "Test failed at index ";
204+
std::cerr << indexConstReverse << " value is " << cor.Get() << std::endl;
205+
return EXIT_FAILURE;
206+
}
207+
std::cout << indexConstReverse << std::endl;
208+
--cor;
218209
}
219-
else
210+
std::cout << indexConstReverse << std::endl; // print the value at the beginning index
211+
std::cout << " Done ! " << std::endl;
212+
}
213+
{
214+
// Verification
215+
std::cout << "Verifying const iterator in both directions... ";
216+
RandomConstIteratorType dor(myImage, region0);
217+
dor.SetNumberOfSamples(numberOfSamples); // 0=x, 1=y, 2=z
218+
dor.GoToEnd();
219+
--dor; // start at the last valid pixel position
220+
ImageType::IndexType indexBiDirectional;
221+
for (unsigned int counter = 0; !dor.IsAtEnd(); ++counter)
220222
{
221-
++dor;
223+
indexBiDirectional = dor.GetIndex();
224+
if (dor.Get() != indexBiDirectional)
225+
{
226+
std::cerr << "Values don't correspond to what was stored " << std::endl;
227+
std::cerr << "Test failed at index ";
228+
std::cerr << indexBiDirectional << " value is " << dor.Get() << std::endl;
229+
return EXIT_FAILURE;
230+
}
231+
std::cout << indexBiDirectional << std::endl;
232+
if (counter < 6)
233+
{
234+
--dor;
235+
}
236+
else
237+
{
238+
++dor;
239+
}
222240
}
241+
std::cout << indexBiDirectional << std::endl; // print the value at the beginning index
242+
std::cout << " Done ! " << std::endl;
223243
}
224-
std::cout << index0 << std::endl; // print the value at the beginning index
225-
std::cout << " Done ! " << std::endl;
226-
227-
// Verification of the Iterator in a subregion of the image
228244
{
245+
// Verification of the Iterator in a subregion of the image
229246
std::cout << "Verifying Iterator in a Region smaller than the whole image... " << std::endl;
230247
const ImageType::RegionType region{ start, size };
231248
RandomIteratorType cbot(myImage, region);
@@ -250,11 +267,22 @@ itkImageRandomNonRepeatingIteratorWithIndexTest(int, char *[])
250267
std::cout << index << std::endl;
251268
++cbot;
252269
}
270+
{
271+
// Now we have walked through all the pixels of low priority,
272+
// the next one should be outside the region.
273+
ImageType::IndexType indexOnePast = cbot.GetIndex();
274+
if (subregion.IsInside(indexOnePast))
275+
{
276+
std::cerr << "Iterator in priority region test failed" << std::endl;
277+
std::cerr << indexOnePast << " is outside the region (should be in)" << region0 << "\n"
278+
<< __FILE__ << ":" << __LINE__ << std::endl;
279+
return EXIT_FAILURE;
280+
}
281+
}
253282
std::cout << " Done ! " << std::endl;
254283
}
255-
256-
// Verification of the Const Iterator in a subregion of the image
257284
{
285+
// Verification of the Const Iterator in a subregion of the image
258286
std::cout << "Verifying Const Iterator in a Region smaller than the whole image... " << std::endl;
259287
const ImageType::RegionType region{ start, size };
260288
RandomConstIteratorType cbot(myImage, region);
@@ -279,13 +307,22 @@ itkImageRandomNonRepeatingIteratorWithIndexTest(int, char *[])
279307
std::cout << index << std::endl;
280308
++cbot;
281309
}
310+
{
311+
ImageType::IndexType indexConstOnePast = cbot.GetIndex();
312+
// Now we have walked through all the pixels of low priority,
313+
// the next one should be outside the region.
314+
if (subregion.IsInside(indexConstOnePast))
315+
{
316+
std::cerr << "Iterator in priority region test failed" << std::endl;
317+
std::cerr << indexConstOnePast << " is outside the region (should be in)" << region0 << "\n"
318+
<< __FILE__ << ":" << __LINE__ << std::endl;
319+
return EXIT_FAILURE;
320+
}
321+
}
282322
std::cout << " Done ! " << std::endl;
283323
}
284-
285-
286-
// Verifying iterator works with the priority image
287-
288324
{
325+
// Verifying iterator works with the priority image
289326
std::cout << "Verifying Iterator with respect to priority image... " << std::endl;
290327

291328
RandomIteratorType cbot(myImage, region0);

0 commit comments

Comments
 (0)