File: itkVariableLengthVectorTest.cxx

package info (click to toggle)
insighttoolkit5 5.4.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 704,384 kB
  • sloc: cpp: 783,592; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,874; sh: 4,078; pascal: 2,636; lisp: 2,158; makefile: 464; yacc: 328; asm: 205; perl: 203; lex: 146; tcl: 132; javascript: 98; csh: 81
file content (415 lines) | stat: -rw-r--r-- 16,265 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*=========================================================================
 *
 *  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 <iostream>
#include "itkVariableLengthVector.h"
#include "itkMath.h"

#define ASSERT(cond, text)                                                                                         \
  ITK_GCC_PRAGMA_PUSH                                                                                              \
  ITK_GCC_SUPPRESS_Wfloat_equal                                                                                    \
  if (!(cond))                                                                                                     \
  {                                                                                                                \
    std::cerr << __FILE__ << ':' << __LINE__ << ':' << "Assertion failed: " << #cond << ": " << text << std::endl; \
    result = EXIT_FAILURE;                                                                                         \
  }                                                                                                                \
  ITK_GCC_PRAGMA_POP                                                                                               \
  ITK_MACROEND_NOOP_STATEMENT

int
itkVariableLengthVectorTest(int, char *[])
{
  using FloatVariableLengthVectorType = itk::VariableLengthVector<float>;
  using DoubleVariableLengthVectorType = itk::VariableLengthVector<double>;
  int result = EXIT_SUCCESS;

  FloatVariableLengthVectorType f(3);
  f[0] = 1.0;
  f[1] = 2.0;
  f[2] = 3.0;
  DoubleVariableLengthVectorType g(3);
  g[0] = 4.0;
  g[1] = 5.0;
  g[2] = 6.0;
  FloatVariableLengthVectorType h;
  h = g + f;
  g = h++;
  h -= 1.1;
  h *= 2.0;
  h /= 2.0;
  h += g;
  h -= g;
  h = g - h;
  h = -h;

  std::cout << h << std::endl; // should be [-1.1 -1.1 -1.1]

  h = (FloatVariableLengthVectorType)g;
  if (h != static_cast<FloatVariableLengthVectorType>(g))
  {
    std::cerr << "Casts: [FAILED]" << std::endl;
  }

  {
    double d[3];
    d[0] = 0.1;
    d[1] = 0.2;
    d[2] = 0.3;
    {
      DoubleVariableLengthVectorType x(d, 3, false);
    }
    {
      DoubleVariableLengthVectorType x(d, 3, false);
      if ((itk::Math::NotExactlyEquals(d[0], 0.1)) || (itk::Math::NotExactlyEquals(x[0], 0.1)))
      {
        std::cerr << "Memory management(1): [FAILED]" << std::endl;
      }
      std::cout << x << std::endl;
      x.SetSize(5, false);
      x[3] = 3.0;
      x[4] = 4.0;
      std::cout << d[0] << "->" << x << std::endl;
      if (itk::Math::NotExactlyEquals(d[0], 0.1) ||
          itk::Math::NotExactlyEquals(x[0], 0.1)) // increase length but preserve existing data
      {
        std::cerr << "Memory management(2): [FAILED]" << std::endl;
      }
      x.SetSize(2, false); // reduce length but preserve existing data
      std::cout << x << std::endl;
      if ((x.GetSize() != 2) || (itk::Math::NotExactlyEquals(d[0], 0.1)) || (itk::Math::NotExactlyEquals(x[0], 0.1)))
      {
        std::cerr << "Memory management(3): [FAILED]" << std::endl;
      }
      x.SetSize(5, true); // increase size, destroy data.
      x.SetSize(7, true); // increase size, destroy data.
      x.SetSize(6, true); // decrease size, destroy data.
    }

    // Tests for SetSize(size, allocation policy, values keeping policy)
    {
      DoubleVariableLengthVectorType ref(d, 3, false);
      ASSERT(ref.IsAProxy(), "Unexpected Reference VLV value");
      ASSERT((ref[0] == 0.1) && (d[0] == 0.1), "Unexpected Reference VLV value");

      DoubleVariableLengthVectorType x(d, 3, false);
      ASSERT(x.IsAProxy(), "Unexpected VLV value");
      ASSERT((x[0] == 0.1) && (x[0] == 0.1), "Unexpected VLV value");

      // ===[ Keep old values
      // ---[ Shrink To Fit
      x.SetSize(5, DoubleVariableLengthVectorType::ShrinkToFit(), DoubleVariableLengthVectorType::KeepOldValues());
      ASSERT(!x.IsAProxy(), "After resizing a proxy, it shall not be a proxy anymore");
      ASSERT(ref[0] == x[0] && ref[1] == x[1] && ref[2] == x[2], "Old Values shall have been kept");
      x[3] = 3.0;
      x[4] = 4.0;
      double * start = &x[0];

      x.SetSize(3, DoubleVariableLengthVectorType::ShrinkToFit(), DoubleVariableLengthVectorType::KeepOldValues());
      ASSERT(!x.IsAProxy(), "After resizing, it shall never be a proxy");
      ASSERT(x == ref, "Values haven't been preserved");
      ASSERT(&x[0] != start, "ShrinkToFit shall induce a resizing");
      start = &x[0];
      x.SetSize(3, DoubleVariableLengthVectorType::ShrinkToFit(), DoubleVariableLengthVectorType::KeepOldValues());
      ASSERT(&x[0] == start, "ShrinkToFit on the same size shall not induce a reallocation");

      // ---[ Don't Shrink To Fit
      x.SetSize(5, DoubleVariableLengthVectorType::DontShrinkToFit(), DoubleVariableLengthVectorType::KeepOldValues());
      ASSERT(!x.IsAProxy(), "After resizing, it shall never be a proxy");
      ASSERT(ref[0] == x[0] && ref[1] == x[1] && ref[2] == x[2], "Old Values shall have been kept");
      ASSERT(&x[0] != start, "DontShrinkToFit shall induce a resizing when the size grows");
      x[3] = 3.0;
      x[4] = 4.0;
      start = &x[0];

      x.SetSize(3, DoubleVariableLengthVectorType::DontShrinkToFit(), DoubleVariableLengthVectorType::KeepOldValues());
      ASSERT(!x.IsAProxy(), "After resizing, it shall never be a proxy");
      ASSERT(ref == x, "Old Values shall have been kept");
      ASSERT(&x[0] == start, "DontShrinkToFit shall not induce a resizing when the size diminishes");
      start = &x[0];

      x.SetSize(3, DoubleVariableLengthVectorType::DontShrinkToFit(), DoubleVariableLengthVectorType::KeepOldValues());
      ASSERT(!x.IsAProxy(), "After resizing, it shall never be a proxy");
      ASSERT(ref == x, "Old Values shall have been kept");
      ASSERT(&x[0] == start, "DontShrinkToFit shall not induce a resizing when the size stays the same");

      // ---[ Always Reallocate
      x.SetSize(5, DoubleVariableLengthVectorType::AlwaysReallocate(), DoubleVariableLengthVectorType::KeepOldValues());
      ASSERT(!x.IsAProxy(), "After resizing, it shall never be a proxy");
      ASSERT(ref[0] == x[0] && ref[1] == x[1] && ref[2] == x[2], "Old Values shall have been kept");
      ASSERT(&x[0] != start, "AlwaysReallocate shall induce a reallocation when resizing");
      start = &x[0];

      x.SetSize(3, DoubleVariableLengthVectorType::AlwaysReallocate(), DoubleVariableLengthVectorType::KeepOldValues());
      ASSERT(!x.IsAProxy(), "After resizing, it shall never be a proxy");
      ASSERT(ref[0] == x[0] && ref[1] == x[1] && ref[2] == x[2], "Old Values shall have been kept");
      ASSERT(&x[0] != start, "AlwaysReallocate shall induce a reallocation when resizing");
      start = &x[0];

      x.SetSize(3, DoubleVariableLengthVectorType::AlwaysReallocate(), DoubleVariableLengthVectorType::KeepOldValues());
      ASSERT(!x.IsAProxy(), "After resizing, it shall never be a proxy");
      ASSERT(ref[0] == x[0] && ref[1] == x[1] && ref[2] == x[2], "Old Values shall have been kept");
      ASSERT(&x[0] != start, "AlwaysReallocate shall induce a reallocation when resizing, even with the same size");
      start = &x[0];

      // ===[ Don't keep old values
      // ---[ ShrinkToFit
      x.SetSize(5, DoubleVariableLengthVectorType::ShrinkToFit(), DoubleVariableLengthVectorType::DumpOldValues());
      ASSERT(&x[0] != start, "ShrintToFit(bigger) => reallocate");
      // ASSERT(x[0] is uninitialized);
      x[0] = ref[0];
      start = &x[0];

      x.SetSize(3, DoubleVariableLengthVectorType::ShrinkToFit(), DoubleVariableLengthVectorType::DumpOldValues());
      ASSERT(&x[0] != start, "ShrintToFit(smaller) => reallocate");
      // ASSERT(x[0] is uninitialized);
      x[0] = ref[0];
      start = &x[0];

      x.SetSize(5, DoubleVariableLengthVectorType::DontShrinkToFit(), DoubleVariableLengthVectorType::DumpOldValues());
      ASSERT(&x[0] != start, "DontShrintToFit(bigger) => reallocate");
      // ASSERT(x[0] is uninitialized);
      x[0] = ref[0];
      start = &x[0];
    }

    // Test on assignments
    {
      // We won't be able to test that old values are dumped.
      // Only when reallocations will be avoided.
      DoubleVariableLengthVectorType ref1(3);
      ref1[0] = 0.1;
      ref1[1] = 0.2;
      ref1[2] = 0.3;
      DoubleVariableLengthVectorType ref2(3);
      ref2[0] = 1.1;
      ref2[1] = 1.2;
      ref2[2] = 1.3;
      DoubleVariableLengthVectorType ref4(4);
      ref4[0] = 1.1;
      ref4[1] = 1.2;
      ref4[2] = 1.3;
      ref4[3] = 1.4;

      DoubleVariableLengthVectorType x;
      ASSERT(x != ref1, "New VLV shall be empty");

      x = ref1;
      ASSERT(x == ref1, "Ref1 is expected to be copied into x");
      double * start = &x[0];

      x = ref2;
      ASSERT(x == ref2, "Ref2 is expected to be copied into x");
      ASSERT(start == &x[0], "Assignment doesn't imply reallocation when the new size is identical to the current one");

      x = ref4;
      ASSERT(x == ref4, "Ref4 is expected to be copied into x");
      ASSERT(start != &x[0],
             "Assignment implies reallocation when the current size is insufficient to hold the new value");
      start = &x[0];

      x = ref1;
      ASSERT(x == ref1, "Ref1 is expected to be copied into x");
      ASSERT(start == &x[0], "Assignment doesn't imply reallocation when the current size is enough");

      // NB: From here, x=ref4; will induce a reallocation even if enough memory has already
      // been allocated.
    }

    // Test Swap
    {
      DoubleVariableLengthVectorType ref1(3);
      ref1[0] = 0.1;
      ref1[1] = 0.2;
      ref1[2] = 0.3;
      DoubleVariableLengthVectorType ref2(3);
      ref2[0] = 1.1;
      ref2[1] = 1.2;
      ref2[2] = 1.3;

      ref1.Swap(ref2);
      ASSERT(ref1[0] == 1.1, "Swap shall ... swap VLVs");
      ASSERT(ref1[1] == 1.2, "Swap shall ... swap VLVs");
      ASSERT(ref1[2] == 1.3, "Swap shall ... swap VLVs");
      ASSERT(ref2[0] == 0.1, "Swap shall ... swap VLVs");
      ASSERT(ref2[1] == 0.2, "Swap shall ... swap VLVs");
      ASSERT(ref2[2] == 0.3, "Swap shall ... swap VLVs");
    }

    // Test FastAssign
    {
      DoubleVariableLengthVectorType ref1(3);
      ref1[0] = 0.1;
      ref1[1] = 0.2;
      ref1[2] = 0.3;
      DoubleVariableLengthVectorType ref2(3);
      ref2[0] = 1.1;
      ref2[1] = 1.2;
      ref2[2] = 1.3;
      DoubleVariableLengthVectorType ref4(4);
      ref4[0] = 1.1;
      ref4[1] = 1.2;
      ref4[2] = 1.3;
      ref4[3] = 1.4;

      DoubleVariableLengthVectorType x(3);
      // FastAssign pre conditions
      assert(x.GetSize() == ref1.GetSize());
      assert(!x.IsAProxy());
      double * start = &x[0];
      x.FastAssign(ref1);
      ASSERT(start == &x[0], "FastAssign shall never reallocate");
      ASSERT(x == ref1, "FastAssign shall ... assign VLVs");

      assert(x.GetSize() == ref2.GetSize());
      x.FastAssign(ref2);
      ASSERT(start == &x[0], "FastAssign shall never reallocate");
      ASSERT(x == ref2, "FastAssign shall ... assign VLVs");

      // As ref4.GetSize() is different from x.GetSize(),
      //    x.FastAssign(ref4);
      // is an invalid instruction: Indeed FastAssign preconditions are not met.
    }
  }


  { // Testing arithmetic operations (and rvalue references)
    { FloatVariableLengthVectorType v = f + f + f;
  ASSERT(v[0] == 3.0 && v[1] == 6.0 && v[2] == 9.0, "Chained additions failed");
}
{ // rvref + lv
  FloatVariableLengthVectorType v = (f + f) + f;
  ASSERT(v[0] == 3.0 && v[1] == 6.0 && v[2] == 9.0, "Chained additions failed");
}
{ // lv + rvref
  FloatVariableLengthVectorType v = f + (f + f);
  ASSERT(v[0] == 3.0 && v[1] == 6.0 && v[2] == 9.0, "Chained additions failed");
}
{ // 2xlv+lv ; rvref + rvref
  FloatVariableLengthVectorType v = (f + f) + (f + f);
  ASSERT(v[0] == 4.0 && v[1] == 8.0 && v[2] == 12.0, "Chained additions failed");
}

{
  FloatVariableLengthVectorType v = f - f - f;
  ASSERT(v[0] == -1.0 && v[1] == -2.0 && v[2] == -3.0, "Chained subtractions failed");
}
{ // rvref - lv
  FloatVariableLengthVectorType v = (f - f) - f;
  ASSERT(v[0] == -1.0 && v[1] == -2.0 && v[2] == -3.0, "Chained subtractions failed");
}
{ // lv - rvref
  FloatVariableLengthVectorType v = f - (f - f);
  ASSERT(v[0] == 1.0 && v[1] == 2.0 && v[2] == 3.0, "Chained subtractions failed");
}
{ // 2xlv-lv ; rvref - rvref
  FloatVariableLengthVectorType v = (f - f) - (f - f);
  ASSERT(v[0] == 0.0 && v[1] == 0.0 && v[2] == 0.0, "Chained subtractions failed");
}

{ // c + lv
  FloatVariableLengthVectorType v = 2.f + f;
  ASSERT(v[0] == 3.0 && v[1] == 4.0 && v[2] == 5.0, "Addition with scalar failed");
}
{ // lv + c
  FloatVariableLengthVectorType v = f + 2.f;
  ASSERT(v[0] == 3.0 && v[1] == 4.0 && v[2] == 5.0, "Addition with scalar failed");
}
{ // rvref + c
  FloatVariableLengthVectorType v = (f + f) + 2.f;
  ASSERT(v[0] == 4.0 && v[1] == 6.0 && v[2] == 8.0, "Addition with scalar failed");
}
{ // c + rvref
  FloatVariableLengthVectorType v = 2.f + (f + f);
  ASSERT(v[0] == 4.0 && v[1] == 6.0 && v[2] == 8.0, "Addition with scalar failed");
}

{ // c - lv
  FloatVariableLengthVectorType v = 2.f - f;
  ASSERT(v[0] == 1.0 && v[1] == 0.0 && v[2] == -1.0, "Subtraction with scalar failed");
}
{ // lv - c
  FloatVariableLengthVectorType v = f - 2.f;
  ASSERT(v[0] == -1.0 && v[1] == 0.0 && v[2] == 1.0, "Subtraction with scalar failed");
}
{ // rvref - c
  FloatVariableLengthVectorType v = (f + f) - 2.f;
  ASSERT(v[0] == 0.0 && v[1] == 2.0 && v[2] == 4.0, "Subtraction with scalar failed");
}
{ // c - rvref
  FloatVariableLengthVectorType v = 2.f - (f + f);
  ASSERT(v[0] == 0.0 && v[1] == -2.0 && v[2] == -4.0, "Subtraction with scalar failed");
}

{ // c * lv
  FloatVariableLengthVectorType v = 2 * f;
  ASSERT(v[0] == 2.0 && v[1] == 4.0 && v[2] == 6.0, "Multiplication with scalar failed");
}
{ // lv * c
  FloatVariableLengthVectorType v = f * 2;
  ASSERT(v[0] == 2.0 && v[1] == 4.0 && v[2] == 6.0, "Multiplication with scalar failed");
}
{ // rvref * c
  FloatVariableLengthVectorType v = (f + f) * 2;
  ASSERT(v[0] == 4.0 && v[1] == 8.0 && v[2] == 12.0, "Multiplication with scalar failed");
}
{ // c * rvref
  FloatVariableLengthVectorType v = 2 * (f + f);
  ASSERT(v[0] == 4.0 && v[1] == 8.0 && v[2] == 12.0, "Multiplication with scalar failed");
}

{ // lv / c
  FloatVariableLengthVectorType v = f / 2;
  ASSERT(v[0] == 0.5 && v[1] == 1.0 && v[2] == 1.5, "Division with scalar failed");
}
{ // rvref / c
  FloatVariableLengthVectorType v = (f + f) / 2;
  ASSERT(v[0] == 1.0 && v[1] == 2.0 && v[2] == 3.0, "Division with scalar failed");
}
}

{   // Testing arithmetic operations and on the fly conversions.
  { // f[0]=1.0; f[1] = 2.0; f[2] = 3.0;
    // g[0]=4.0; g[1] = 5.0; g[2] = 6.0;
    // g += f+1
    FloatVariableLengthVectorType v = f + 2 * g;
ASSERT(v[0] == 13.0 && v[1] == 18.0 && v[2] == 23.0, "On-the-fly conversion failed; v=" << v);
}
{
  DoubleVariableLengthVectorType v = f + 2 * g;
  ASSERT(v[0] == 13.0 && v[1] == 18.0 && v[2] == 23.0, "On-the-fly conversion failed; v=" << v);
}
}

{
  // Testing empty vectors
  FloatVariableLengthVectorType v1;
  v1.Fill(0);
  FloatVariableLengthVectorType v2 = v1;
  v1 = v2;

  FloatVariableLengthVectorType v3, v4;
  v1 = 2 * v2 + (v3 - v4) / 6;

  v1.SetSize(0, FloatVariableLengthVectorType::DontShrinkToFit(), FloatVariableLengthVectorType::KeepOldValues());
  v1.SetSize(1, FloatVariableLengthVectorType::DontShrinkToFit(), FloatVariableLengthVectorType::KeepOldValues());
}

std::cout << (result == EXIT_SUCCESS ? "[PASSED]" : "[FAILED]") << std::endl;

return result;
}