Line data Source code
1 : /**
2 : * @file test_PartitionDescriptor.cpp
3 : *
4 : * @brief Tests for PartitionDescriptor class
5 : *
6 : * @author Nikola Dinev
7 : */
8 :
9 : #include "doctest/doctest.h"
10 : #include "PartitionDescriptor.h"
11 : #include "VolumeDescriptor.h"
12 : #include "TypeCasts.hpp"
13 :
14 : #include <stdexcept>
15 :
16 : using namespace elsa;
17 : using namespace doctest;
18 :
19 : TEST_SUITE_BEGIN("core");
20 :
21 26 : TEST_CASE("PartitionDescriptor: Testing construction")
22 : {
23 36 : GIVEN("a 1D descriptor")
24 : {
25 20 : VolumeDescriptor dd(IndexVector_t::Constant(1, 10));
26 :
27 11 : WHEN("partitioning it into 1 blocks")
28 : {
29 2 : REQUIRE_THROWS(PartitionDescriptor(dd, 1));
30 3 : REQUIRE_THROWS(PartitionDescriptor(dd, IndexVector_t::Constant(1, 10)));
31 : }
32 :
33 11 : WHEN("partitioning it into more blocks than the size of the last dimension")
34 : {
35 2 : REQUIRE_THROWS(PartitionDescriptor(dd, 11));
36 3 : REQUIRE_THROWS(PartitionDescriptor(dd, IndexVector_t::Ones(11)));
37 3 : REQUIRE_THROWS(PartitionDescriptor(dd, IndexVector_t::Zero(11)));
38 : }
39 :
40 14 : WHEN("partitioning it into 5 blocks with equal sizes")
41 : {
42 4 : index_t blocks = 5;
43 8 : PartitionDescriptor bd(dd, blocks);
44 :
45 5 : THEN("the partitioned descriptor has the same number of coefficients and spacing per "
46 : "dimension as the original")
47 : {
48 1 : REQUIRE_EQ(dd.getNumberOfCoefficientsPerDimension(),
49 : bd.getNumberOfCoefficientsPerDimension());
50 1 : REQUIRE_EQ(dd.getSpacingPerDimension(), bd.getSpacingPerDimension());
51 : }
52 :
53 5 : THEN("there are 5 blocks of the correct size")
54 : {
55 1 : REQUIRE_EQ(bd.getNumberOfBlocks(), blocks);
56 :
57 3 : VolumeDescriptor bd0(IndexVector_t::Constant(1, 2), dd.getSpacingPerDimension());
58 6 : for (index_t i = 0; i < blocks; ++i)
59 5 : REQUIRE_EQ(bd.getDescriptorOfBlock(i), bd0);
60 :
61 2 : REQUIRE_THROWS(bd.getDescriptorOfBlock(blocks));
62 : }
63 :
64 5 : THEN("the block offsets are correct")
65 : {
66 6 : for (index_t i = 0; i < blocks; ++i)
67 5 : REQUIRE_EQ(bd.getOffsetOfBlock(i), i * dd.getNumberOfCoefficients() / blocks);
68 :
69 2 : REQUIRE_THROWS(bd.getOffsetOfBlock(blocks));
70 : }
71 :
72 5 : THEN("original and partitioned descriptor are not equal")
73 : {
74 1 : REQUIRE_NE(bd, dd);
75 1 : REQUIRE_NE(dd, bd);
76 : }
77 : }
78 :
79 14 : WHEN("partitioning it into 5 blocks with chosen sizes")
80 : {
81 4 : index_t blocks = 5;
82 8 : IndexVector_t split(blocks);
83 4 : split << 1, 2, 3, 1, 3;
84 8 : PartitionDescriptor bd(dd, split);
85 :
86 5 : THEN("the partitioned descriptor has the same number of coefficients and spacing per "
87 : "dimension as the original")
88 : {
89 1 : REQUIRE_EQ(dd.getNumberOfCoefficientsPerDimension(),
90 : bd.getNumberOfCoefficientsPerDimension());
91 1 : REQUIRE_EQ(dd.getSpacingPerDimension(), bd.getSpacingPerDimension());
92 : }
93 :
94 5 : THEN("there are 5 blocks of the correct size")
95 : {
96 1 : REQUIRE_EQ(bd.getNumberOfBlocks(), blocks);
97 :
98 6 : for (index_t i = 0; i < blocks; ++i) {
99 5 : REQUIRE_EQ(bd.getDescriptorOfBlock(i).getNumberOfDimensions(), 1);
100 5 : REQUIRE_EQ(bd.getDescriptorOfBlock(i).getNumberOfCoefficients(), split[i]);
101 5 : REQUIRE_EQ(bd.getDescriptorOfBlock(i).getSpacingPerDimension(),
102 : bd.getSpacingPerDimension());
103 : }
104 :
105 2 : REQUIRE_THROWS(bd.getDescriptorOfBlock(blocks));
106 : }
107 :
108 5 : THEN("the block offsets are correct")
109 : {
110 6 : for (index_t i = 0; i < blocks; ++i)
111 5 : REQUIRE_EQ(bd.getOffsetOfBlock(i), split.head(i).sum());
112 :
113 2 : REQUIRE_THROWS(bd.getOffsetOfBlock(blocks));
114 : }
115 :
116 5 : THEN("original and partitioned descriptor are not equal")
117 : {
118 1 : REQUIRE_NE(bd, dd);
119 1 : REQUIRE_NE(dd, bd);
120 : }
121 : }
122 : }
123 :
124 34 : GIVEN("a 2D descriptor with blocks")
125 : {
126 16 : IndexVector_t sizeVector(2);
127 8 : sizeVector << 11, 102;
128 16 : VolumeDescriptor dd(sizeVector);
129 :
130 16 : IndexVector_t coeffs(2);
131 8 : coeffs << 11, 10;
132 24 : VolumeDescriptor bd0(coeffs, dd.getSpacingPerDimension());
133 :
134 8 : coeffs[1] = 11;
135 24 : VolumeDescriptor bdn(coeffs, dd.getSpacingPerDimension());
136 :
137 12 : WHEN("partitioning it into 10 blocks")
138 : {
139 4 : index_t blocks = 10;
140 8 : PartitionDescriptor bd(dd, blocks);
141 :
142 5 : THEN("the partitioned descriptor has the same number of coefficients and spacing per "
143 : "dimension as the original")
144 : {
145 1 : REQUIRE_EQ(dd.getNumberOfCoefficientsPerDimension(),
146 : bd.getNumberOfCoefficientsPerDimension());
147 1 : REQUIRE_EQ(dd.getSpacingPerDimension(), bd.getSpacingPerDimension());
148 : }
149 :
150 5 : THEN("there are 10 blocks of the correct size")
151 : {
152 1 : REQUIRE_EQ(bd.getNumberOfBlocks(), blocks);
153 :
154 9 : for (index_t i = 0; i < blocks - 2; ++i)
155 8 : REQUIRE_EQ(bd.getDescriptorOfBlock(i), bd0);
156 :
157 3 : for (index_t i = blocks - 2; i < blocks; ++i)
158 2 : REQUIRE_EQ(bd.getDescriptorOfBlock(i), bdn);
159 :
160 2 : REQUIRE_THROWS(bd.getDescriptorOfBlock(blocks));
161 : }
162 :
163 5 : THEN("the block offsets are correct")
164 : {
165 1 : index_t size0 = bd0.getNumberOfCoefficients();
166 1 : index_t sizen = bdn.getNumberOfCoefficients();
167 9 : for (index_t i = 0; i < blocks - 2; ++i)
168 8 : REQUIRE_EQ(bd.getOffsetOfBlock(i), i * size0);
169 :
170 3 : for (index_t i = 0; i < 2; ++i)
171 2 : REQUIRE_EQ(bd.getOffsetOfBlock(blocks - 2 + i),
172 : (blocks - 2) * size0 + i * sizen);
173 :
174 2 : REQUIRE_THROWS(bd.getOffsetOfBlock(blocks));
175 : }
176 :
177 5 : THEN("original and partitioned descriptor are not equal")
178 : {
179 1 : REQUIRE_NE(bd, dd);
180 1 : REQUIRE_NE(dd, bd);
181 : }
182 : }
183 :
184 12 : WHEN("partitioning it into 10 blocks with chosen sizes")
185 : {
186 4 : index_t blocks = 10;
187 8 : IndexVector_t split(blocks);
188 4 : split << 1, 2, 3, 4, 5, 6, 7, 8, 9, 57;
189 8 : PartitionDescriptor bd(dd, split);
190 :
191 5 : THEN("the partitioned descriptor has the same number of coefficients and spacing per "
192 : "dimension as the original")
193 : {
194 1 : REQUIRE_EQ(dd.getNumberOfCoefficientsPerDimension(),
195 : bd.getNumberOfCoefficientsPerDimension());
196 1 : REQUIRE_EQ(dd.getSpacingPerDimension(), bd.getSpacingPerDimension());
197 : }
198 :
199 5 : THEN("there are 10 blocks of the correct size")
200 : {
201 1 : REQUIRE_EQ(bd.getNumberOfBlocks(), blocks);
202 :
203 11 : for (index_t i = 0; i < blocks; i++) {
204 10 : auto coeffsPerDim = dd.getNumberOfCoefficientsPerDimension();
205 10 : coeffsPerDim[1] = split[i];
206 :
207 10 : REQUIRE_EQ(bd.getDescriptorOfBlock(i).getNumberOfCoefficientsPerDimension(),
208 : coeffsPerDim);
209 :
210 10 : REQUIRE_EQ(bd.getDescriptorOfBlock(i).getSpacingPerDimension(),
211 : bd.getSpacingPerDimension());
212 : }
213 :
214 2 : REQUIRE_THROWS(bd.getDescriptorOfBlock(blocks));
215 : }
216 :
217 5 : THEN("the block offsets are correct")
218 : {
219 11 : for (index_t i = 0; i < blocks; i++)
220 10 : REQUIRE_EQ(bd.getOffsetOfBlock(i), sizeVector[0] * split.head(i).sum());
221 :
222 2 : REQUIRE_THROWS(bd.getOffsetOfBlock(blocks));
223 : }
224 :
225 5 : THEN("original and partitioned descriptor are not equal")
226 : {
227 1 : REQUIRE_NE(bd, dd);
228 1 : REQUIRE_NE(dd, bd);
229 : }
230 : }
231 : }
232 :
233 34 : GIVEN("a 3D descriptor with blocks")
234 : {
235 16 : IndexVector_t sizeVector(3);
236 8 : sizeVector << 101, 42, 750;
237 16 : VolumeDescriptor dd(sizeVector);
238 :
239 8 : sizeVector[2] = 30;
240 16 : VolumeDescriptor bd0(sizeVector);
241 12 : WHEN("creating 25 blocks")
242 : {
243 4 : index_t blocks = 25;
244 8 : PartitionDescriptor bd(dd, blocks);
245 :
246 5 : THEN("the partitioned descriptor has the same number of coefficients and spacing per "
247 : "dimension as the original")
248 : {
249 1 : REQUIRE_EQ(dd.getNumberOfCoefficientsPerDimension(),
250 : bd.getNumberOfCoefficientsPerDimension());
251 1 : REQUIRE_EQ(dd.getSpacingPerDimension(), bd.getSpacingPerDimension());
252 : }
253 :
254 5 : THEN("there are 25 blocks of the correct size")
255 : {
256 1 : REQUIRE_EQ(bd.getNumberOfBlocks(), blocks);
257 :
258 26 : for (index_t i = 0; i < blocks; ++i)
259 25 : REQUIRE_EQ(bd.getDescriptorOfBlock(i), bd0);
260 :
261 2 : REQUIRE_THROWS(bd.getDescriptorOfBlock(blocks));
262 : }
263 :
264 5 : THEN("the block offsets are correct")
265 : {
266 26 : for (index_t i = 0; i < blocks; ++i)
267 25 : REQUIRE_EQ(bd.getOffsetOfBlock(i), i * bd0.getNumberOfCoefficients());
268 :
269 2 : REQUIRE_THROWS(bd.getDescriptorOfBlock(blocks));
270 : }
271 :
272 5 : THEN("original and partitioned descriptor are not equal")
273 : {
274 1 : REQUIRE_NE(bd, dd);
275 1 : REQUIRE_NE(dd, bd);
276 : }
277 : }
278 :
279 12 : WHEN("creating 25 blocks with chosen sizes")
280 : {
281 4 : index_t blocks = 25;
282 8 : IndexVector_t split = IndexVector_t::Constant(blocks, 30);
283 4 : split.head(10).array() = 40;
284 4 : split.tail(10).array() = 20;
285 8 : PartitionDescriptor bd(dd, split);
286 :
287 5 : THEN("the partitioned descriptor has the same number of coefficients and spacing per "
288 : "dimension as the original")
289 : {
290 1 : REQUIRE_EQ(dd.getNumberOfCoefficientsPerDimension(),
291 : bd.getNumberOfCoefficientsPerDimension());
292 1 : REQUIRE_EQ(dd.getSpacingPerDimension(), bd.getSpacingPerDimension());
293 : }
294 :
295 5 : THEN("there are 25 blocks of the correct size")
296 : {
297 1 : REQUIRE_EQ(bd.getNumberOfBlocks(), blocks);
298 :
299 26 : for (index_t i = 0; i < blocks; ++i) {
300 25 : auto coeffsPerDim = sizeVector;
301 25 : coeffsPerDim[2] = split[i];
302 :
303 25 : REQUIRE_EQ(bd.getDescriptorOfBlock(i).getSpacingPerDimension(),
304 : dd.getSpacingPerDimension());
305 25 : REQUIRE_EQ(bd.getDescriptorOfBlock(i).getNumberOfCoefficientsPerDimension(),
306 : coeffsPerDim);
307 : }
308 :
309 2 : REQUIRE_THROWS(bd.getDescriptorOfBlock(blocks));
310 : }
311 :
312 5 : THEN("the block offsets are correct")
313 : {
314 26 : for (index_t i = 0; i < blocks; ++i)
315 25 : REQUIRE_EQ(bd.getOffsetOfBlock(i),
316 : sizeVector.head(2).prod() * split.head(i).sum());
317 :
318 2 : REQUIRE_THROWS(bd.getDescriptorOfBlock(blocks));
319 : }
320 :
321 5 : THEN("original and partitioned descriptor are not equal")
322 : {
323 1 : REQUIRE_NE(bd, dd);
324 1 : REQUIRE_NE(dd, bd);
325 : }
326 : }
327 : }
328 26 : }
329 :
330 3 : TEST_CASE("PartitionDescriptor: Testing clone()")
331 : {
332 4 : GIVEN("a 1D PartitionDescriptor")
333 : {
334 2 : IndexVector_t sizeVector(1);
335 1 : sizeVector << 3891;
336 2 : VolumeDescriptor dd(sizeVector);
337 1 : index_t blocks = 21;
338 :
339 2 : WHEN("cloning the descriptor")
340 : {
341 2 : PartitionDescriptor bd(dd, blocks);
342 2 : auto bdClone = bd.clone();
343 :
344 2 : THEN("it's a real clone")
345 : {
346 1 : REQUIRE_NE(bdClone.get(), &bd);
347 1 : REQUIRE_UNARY(is<PartitionDescriptor>(bdClone.get()));
348 1 : REQUIRE_EQ(*bdClone, bd);
349 : }
350 : }
351 : }
352 :
353 4 : GIVEN("a 2D PartitionDescriptor")
354 : {
355 2 : IndexVector_t sizeVector(2);
356 1 : sizeVector << 43, 112;
357 2 : VolumeDescriptor dd(sizeVector);
358 1 : index_t blocks = 77;
359 :
360 2 : WHEN("cloning the descriptor")
361 : {
362 2 : PartitionDescriptor bd(dd, blocks);
363 2 : auto bdClone = bd.clone();
364 :
365 2 : THEN("it's a real clone")
366 : {
367 1 : REQUIRE_NE(bdClone.get(), &bd);
368 1 : REQUIRE_UNARY(is<PartitionDescriptor>(bdClone.get()));
369 1 : REQUIRE_EQ(*bdClone, bd);
370 : }
371 : }
372 : }
373 :
374 4 : GIVEN("a 3D PartitionDescriptor")
375 : {
376 2 : IndexVector_t sizeVector(3);
377 1 : sizeVector << 47, 11, 53;
378 2 : VolumeDescriptor dd(sizeVector);
379 1 : index_t blocks = 13;
380 :
381 2 : WHEN("cloning the descriptor")
382 : {
383 2 : PartitionDescriptor bd(dd, blocks);
384 2 : auto bdClone = bd.clone();
385 :
386 2 : THEN("it's a real clone")
387 : {
388 1 : REQUIRE_NE(bdClone.get(), &bd);
389 1 : REQUIRE_UNARY(is<PartitionDescriptor>(bdClone.get()));
390 1 : REQUIRE_EQ(*bdClone, bd);
391 : }
392 : }
393 : }
394 3 : }
395 :
396 : TEST_SUITE_END();
|