File: itkFixedArrayGTest.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 (346 lines) | stat: -rw-r--r-- 10,958 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
/*=========================================================================
 *
 *  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.
 *
 *=========================================================================*/

// Enable testing legacy member functions rBegin() and rEnd().
#define ITK_LEGACY_TEST

// First include the header file to be tested:
#include "itkFixedArray.h"
#include "itkRangeGTestUtilities.h"
#include <gtest/gtest.h>

#include <array>
#include <numeric>     // For iota.
#include <type_traits> // For remove_reference_t.


namespace
{
template <typename TValue, unsigned int VLength>
void
Check_FixedArray_supports_retrieving_values_by_range_based_for_loop()
{
  std::array<TValue, VLength> stdArray;

  // Assign a different value (1, 2, 3, ...) to each element.
  std::iota(stdArray.begin(), stdArray.end(), 1);

  // Test retrieving the values from a const FixedArray:
  const itk::FixedArray<TValue, VLength> constFixedArray{ stdArray };

  auto stdArrayIterator = stdArray.cbegin();

  for (auto value : constFixedArray)
  {
    // Expect the same value as the corresponding std::array element.
    EXPECT_EQ(value, *stdArrayIterator);
    ++stdArrayIterator;
  }

  // Expect that all values are checked, "up to the end".
  EXPECT_EQ(stdArrayIterator, stdArray.cend());

  // Now test retrieving the values from a non-const FixedArray:
  itk::FixedArray<TValue, VLength> nonConstFixedArray{ stdArray };

  stdArrayIterator = stdArray.cbegin();

  for (auto value : nonConstFixedArray)
  {
    // Again, expect the same value as the corresponding std::array element.
    EXPECT_EQ(value, *stdArrayIterator);
    ++stdArrayIterator;
  }

  // Again, expect that all values are checked.
  EXPECT_EQ(stdArrayIterator, stdArray.cend());
}


template <typename TValue, unsigned int VLength>
void
Check_FixedArray_supports_modifying_elements_by_range_based_for_loop()
{
  itk::FixedArray<TValue, VLength> fixedArray{};

  TValue value{};

  // Assign the values 1, 2, 3, etc.
  for (auto & ref : fixedArray)
  {
    ++value;
    ref = value;
  }

  // Now check if the array has got the expected values.
  TValue expectedValue{};

  for (unsigned int i = 0; i < VLength; ++i)
  {
    ++expectedValue;
    EXPECT_EQ(fixedArray[i], expectedValue);
  }
}


#if !defined(ITK_LEGACY_REMOVE)
template <typename TValue, unsigned int VLength>
void
Check_new_reverse_iterator_behaves_like_old_ReverseIterator()
{
  using FixedArrayType = itk::FixedArray<TValue, VLength>;
  FixedArrayType fixedArray{};

  // Assign a different value (1, 2, 3, ...) to each element.
  std::iota(fixedArray.begin(), fixedArray.end(), 1);

  auto newIterator = fixedArray.rbegin();
  auto oldIterator = fixedArray.rBegin();

  const auto newEnd = fixedArray.rend();
  const auto oldEnd = fixedArray.rEnd();

  while ((newIterator != newEnd) && (oldIterator != oldEnd))
  {
    EXPECT_EQ(*newIterator, *oldIterator);
    ++newIterator;
    ++oldIterator;
  }
  EXPECT_EQ(newIterator, newEnd);
  EXPECT_EQ(oldIterator, oldEnd);
}
#endif

template <typename TValue, unsigned int VLength>
void
Check_const_and_non_const_reverse_iterators_retrieve_same_values()
{
  using FixedArrayType = itk::FixedArray<TValue, VLength>;
  using ConstIteratorType = typename FixedArrayType::const_reverse_iterator;
  using NonConstIteratorType = typename FixedArrayType::reverse_iterator;

  static_assert(!std::is_same_v<ConstIteratorType, NonConstIteratorType>,
                "Const and non-const reverse_iterator types must be different!");

  FixedArrayType fixedArray{};

  // Assign a different value (1, 2, 3, ...) to each element.
  std::iota(fixedArray.begin(), fixedArray.end(), 1);

  ConstIteratorType    constIterator = fixedArray.crbegin();
  NonConstIteratorType nonConstIterator = fixedArray.rbegin();

  const ConstIteratorType    constEnd = fixedArray.crend();
  const NonConstIteratorType nonConstEnd = fixedArray.rend();

  while ((constIterator != constEnd) && (nonConstIterator != nonConstEnd))
  {
    EXPECT_EQ(*constIterator, *nonConstIterator);
    ++constIterator;
    ++nonConstIterator;
  }
  EXPECT_EQ(constIterator, constEnd);
  EXPECT_EQ(nonConstIterator, nonConstEnd);
}


template <typename TValue, unsigned int VLength>
void
Check_reverse_iterators_allow_filling_a_FixedArray()
{
  using FixedArrayType = itk::FixedArray<TValue, VLength>;
  FixedArrayType fixedArray{};

  // Fill with ones, and then check the result.
  std::fill(fixedArray.rbegin(), fixedArray.rend(), 1);
  EXPECT_EQ(fixedArray, FixedArrayType::Filled(1));
}


template <typename TValue, unsigned int VLength>
void
Check_iterators_increment_return_value()
{
  using FixedArrayType = itk::FixedArray<TValue, VLength>;
  FixedArrayType fixedArray{};

  std::iota(fixedArray.begin(), fixedArray.end(), 1);

  typename FixedArrayType::iterator         newIterator = fixedArray.begin();
  typename FixedArrayType::Iterator         oldIterator = fixedArray.Begin();
  typename FixedArrayType::reverse_iterator newReverseIterator = fixedArray.rbegin();
#if !defined(ITK_LEGACY_REMOVE)
  typename FixedArrayType::ReverseIterator oldReverseIterator = fixedArray.rBegin();
#endif

  unsigned int index = 0;
  unsigned int reverseIndex = VLength - 1;
  for (unsigned int i = 0; i < VLength; ++i)
  {
    EXPECT_EQ(*newIterator++, fixedArray[index]);
    EXPECT_EQ(*newReverseIterator++, fixedArray[reverseIndex]);
    EXPECT_EQ(*oldIterator++, fixedArray[index]);
#if !defined(ITK_LEGACY_REMOVE)
    EXPECT_EQ(*oldReverseIterator++, fixedArray[reverseIndex]);
#endif
    index++;
    reverseIndex--;
  }

  newIterator = fixedArray.begin();
  oldIterator = fixedArray.Begin();
  newReverseIterator = fixedArray.rbegin();
#if !defined(ITK_LEGACY_REMOVE)
  oldReverseIterator = fixedArray.rBegin();
#endif

  index = 0;
  reverseIndex = VLength - 1;
  for (unsigned int i = 0; i < VLength - 1; ++i)
  {
    index++;
    reverseIndex--;
    EXPECT_EQ(*++newIterator, fixedArray[index]);
    EXPECT_EQ(*++newReverseIterator, fixedArray[reverseIndex]);
    EXPECT_EQ(*++oldIterator, fixedArray[index]);
#if !defined(ITK_LEGACY_REMOVE)
    EXPECT_EQ(*++oldReverseIterator, fixedArray[reverseIndex]);
#endif
  }
}

template <int VFillValue>
constexpr bool
Is_Filled_FixedArray_correctly_filled()
{
  using FixedArrayType = itk::FixedArray<int>;

  constexpr auto filledFixedArray = FixedArrayType::Filled(VFillValue);

  for (unsigned int i{}; i < FixedArrayType::Length; ++i)
  {
    if (filledFixedArray[i] != VFillValue)
    {
      return false;
    }
  }
  return true;
}


} // End of namespace

static_assert(Is_Filled_FixedArray_correctly_filled<0>() && Is_Filled_FixedArray_correctly_filled<1>() &&
                Is_Filled_FixedArray_correctly_filled<std::numeric_limits<int>::min()>() &&
                Is_Filled_FixedArray_correctly_filled<std::numeric_limits<int>::max()>(),
              "itk::FixedArray::Filled(value) should be correctly filled at compile-time");

static_assert(itk::RangeGTestUtilities::CheckConstexprBeginAndEndOfContainer<itk::FixedArray<int>>() &&
                itk::RangeGTestUtilities::CheckConstexprBeginAndEndOfContainer<itk::FixedArray<double, 1>>(),
              "Check constexpr begin() and end() of FixedArray.");


// Tests that the values of a FixedArray (either const or non-const) can be retrieved by a
// range-based for-loop.
TEST(FixedArray, SupportsRetrievingValuesByRangeBasedForLoop)
{
  Check_FixedArray_supports_retrieving_values_by_range_based_for_loop<double, 2>();
  Check_FixedArray_supports_retrieving_values_by_range_based_for_loop<int, 3>();
}


// Tests that FixedArray supports modifying its elements by a range-based for-loop.
TEST(FixedArray, SupportsModifyingElementsByRangeBasedForLoop)
{
  Check_FixedArray_supports_modifying_elements_by_range_based_for_loop<double, 2>();
  Check_FixedArray_supports_modifying_elements_by_range_based_for_loop<int, 3>();
}


#if !defined(ITK_LEGACY_REMOVE)
// Tests that the new reverse iterators (`rbegin()` and `rend()`, introduced with ITK 5.0)
// behave just like the old ones (`rBegin()` and `rEnd()`, originally from 2002).
TEST(FixedArray, NewReverseIteratorBehavesLikeOldReverseIterator)
{
  Check_new_reverse_iterator_behaves_like_old_ReverseIterator<double, 2>();
  Check_new_reverse_iterator_behaves_like_old_ReverseIterator<int, 3>();
}
#endif

// Tests that const and non-const reverse iterators retrieve exactly the same values.
TEST(FixedArray, ConstAndNonConstReverseIteratorRetrieveSameValues)
{
  Check_const_and_non_const_reverse_iterators_retrieve_same_values<double, 2>();
  Check_const_and_non_const_reverse_iterators_retrieve_same_values<int, 3>();
}


// Tests that reverse iterators can be used to fill a FixedArray.
TEST(FixedArray, CanBeFilledUsingReverseIterators)
{
  Check_reverse_iterators_allow_filling_a_FixedArray<double, 2>();
  Check_reverse_iterators_allow_filling_a_FixedArray<int, 3>();
}


// Tests that increment operators return a valid iterator
TEST(FixedArray, IteratorIncrementReturnValue)
{
  Check_iterators_increment_return_value<double, 2>();
  Check_iterators_increment_return_value<int, 3>();
}

// Tests data() and size() works
TEST(FixedArray, StdMemberFunctionsWork)
{
  using FixedArrayType = itk::FixedArray<double, 3>;

  // FixedArray::size() may be evaluated at compile-time, just like std::array::size().
  static_assert(FixedArrayType{}.size() == FixedArrayType::Dimension, "FixedArray::size() should equal its dimension");

  auto d3arr = FixedArrayType(3);
  d3arr[0] = 1;
  d3arr[1] = 2;
  d3arr[2] = 3;
  // size
  EXPECT_EQ(d3arr.size(), 3);
  // const and non-const data
  const auto cdata = d3arr.data();
  EXPECT_EQ(cdata[0], 1);
  d3arr.data()[0] = 10;
  EXPECT_EQ(cdata[0], 10);
}


// Tests that each element of a value-initialized FixedArray is itself value-initialized. By definition,
// "value-initialization" is performed when an object is constructed with an empty {} initializer.
TEST(FixedArray, ValueInitialized)
{
  const auto expectEachElementValueInitialized = [](const auto & fixedArray) {
    for (const auto & element : fixedArray)
    {
      using ValueType = std::remove_reference_t<decltype(element)>;

      EXPECT_EQ(element, ValueType{});
    }
  };

  expectEachElementValueInitialized(itk::FixedArray<int, 3>{});
  expectEachElementValueInitialized(itk::FixedArray<float, 3>{});
  expectEachElementValueInitialized(itk::FixedArray<void *, 3>{});
}