Skip to content

Commit 0d0ecd7

Browse files
authored
Merge pull request #5895 from hjmjohnson/convert-itkarraytest-to-gtest
ENH: Convert itkArrayTest to GTest
2 parents 67999b0 + 19f34ff commit 0d0ecd7

3 files changed

Lines changed: 82 additions & 135 deletions

File tree

Modules/Core/Common/test/CMakeLists.txt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ set(
5858
itkRealTimeIntervalTest.cxx
5959
itkRealTimeStampTest.cxx
6060
itkBSplineKernelFunctionTest.cxx
61-
itkArrayTest.cxx
6261
itkImageIteratorTest.cxx
6362
itkImageRegionIteratorTest.cxx
6463
itkImageScanlineIteratorTest1.cxx
@@ -395,12 +394,6 @@ itk_add_test(
395394
ITKCommon1TestDriver
396395
itkFixedArrayTest2
397396
)
398-
itk_add_test(
399-
NAME itkArrayTest
400-
COMMAND
401-
ITKCommon1TestDriver
402-
itkArrayTest
403-
)
404397
itk_add_test(
405398
NAME itkMersenneTwisterRandomVariateGeneratorTest
406399
COMMAND

Modules/Core/Common/test/itkArrayGTest.cxx

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
// First include the header file to be tested:
2020
#include "itkArray.h"
21+
#include "itkNumericTraits.h"
2122
#include <gtest/gtest.h>
23+
#include <iostream>
2224

2325
namespace
2426
{
@@ -42,3 +44,83 @@ CheckClassTemplateArgumentDeduction()
4244

4345

4446
static_assert(CheckClassTemplateArgumentDeduction<int>() && CheckClassTemplateArgumentDeduction<float>());
47+
48+
49+
TEST(Array, MemoryManagement)
50+
{
51+
using FloatArrayType = itk::Array<float>;
52+
using DoubleArrayType = itk::Array<double>;
53+
54+
const FloatArrayType fa(10);
55+
const DoubleArrayType da(10);
56+
57+
// Create an itk::Array which manages its own memory
58+
FloatArrayType myOwnBoss;
59+
myOwnBoss.SetSize(5);
60+
myOwnBoss.Fill(2.0 + 1.0f / 3.0f);
61+
myOwnBoss[0] = 2.0f / 3.0f;
62+
myOwnBoss[1] = itk::NumericTraits<float>::max();
63+
myOwnBoss[2] = itk::NumericTraits<float>::min();
64+
myOwnBoss[3] = 1.0f;
65+
66+
// Create an itk::Array which does not manage its own memory
67+
constexpr unsigned int n{ 7 };
68+
float buffer[n];
69+
FloatArrayType notMyOwnBoss;
70+
notMyOwnBoss.SetSize(n);
71+
notMyOwnBoss.SetData(buffer, false);
72+
notMyOwnBoss.Fill(4.0);
73+
74+
FloatArrayType notMyOwnBossToo;
75+
notMyOwnBossToo.SetSize(n);
76+
notMyOwnBossToo.SetData(buffer, false);
77+
78+
// Copy an itk::Array which manages its own memory
79+
const FloatArrayType test1 = myOwnBoss;
80+
std::cout << test1 << std::endl;
81+
EXPECT_EQ(test1.GetSize(), myOwnBoss.GetSize());
82+
83+
// Copy an itk::Array which does not manage its own memory
84+
FloatArrayType test2 = notMyOwnBoss;
85+
std::cout << test2 << std::endl;
86+
EXPECT_EQ(test2.GetSize(), notMyOwnBoss.GetSize());
87+
88+
// itk::Array not managing its memory copying one that does
89+
notMyOwnBoss = myOwnBoss;
90+
std::cout << notMyOwnBoss << std::endl;
91+
EXPECT_EQ(notMyOwnBoss.GetSize(), myOwnBoss.GetSize());
92+
93+
// Calling SetSize with same size
94+
notMyOwnBossToo.SetSize(notMyOwnBossToo.GetSize());
95+
96+
// Calling SetSize with different size
97+
notMyOwnBossToo.SetSize(notMyOwnBossToo.GetSize() + 1);
98+
notMyOwnBossToo.Fill(6.0);
99+
std::cout << notMyOwnBossToo << std::endl;
100+
101+
// Exercise operator=( VnlVectorType& )
102+
test2 = test1;
103+
EXPECT_EQ(test2.GetSize(), test1.GetSize());
104+
105+
// Construct array pointing to user-allocated buffer (user manages deletion)
106+
constexpr size_t testSizeForArraySetDataSameSize{ 10 };
107+
FloatArrayType objectToCopy(testSizeForArraySetDataSameSize);
108+
auto * data = new float[testSizeForArraySetDataSameSize];
109+
objectToCopy.SetDataSameSize(data);
110+
111+
// Copy of array not managing its own memory
112+
const FloatArrayType copy(objectToCopy);
113+
EXPECT_EQ(copy.GetSize(), objectToCopy.GetSize());
114+
115+
// Double array managing its own memory
116+
DoubleArrayType myOwnDouble;
117+
myOwnDouble.SetSize(5);
118+
myOwnDouble.Fill(2.0 + 1.0 / 3.0);
119+
myOwnDouble[0] = 2.0 / 3.0;
120+
myOwnDouble[1] = itk::NumericTraits<double>::max();
121+
myOwnDouble[2] = itk::NumericTraits<double>::min();
122+
myOwnDouble[3] = 1.0;
123+
std::cout << myOwnDouble << std::endl;
124+
125+
delete[] data;
126+
}

Modules/Core/Common/test/itkArrayTest.cxx

Lines changed: 0 additions & 128 deletions
This file was deleted.

0 commit comments

Comments
 (0)