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
2325namespace
2426{
@@ -42,3 +44,83 @@ CheckClassTemplateArgumentDeduction()
4244
4345
4446static_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+ }
0 commit comments