1//
2// ArrayTest.cpp
3//
4// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
5// and Contributors.
6//
7// SPDX-License-Identifier: BSL-1.0
8//
9
10
11#include "ArrayTest.h"
12#include "Poco/CppUnit/TestCaller.h"
13#include "Poco/CppUnit/TestSuite.h"
14#include "Poco/Array.h"
15#include <vector>
16#include <algorithm>
17#include <functional>
18
19ArrayTest::ArrayTest(const std::string& rName): CppUnit::TestCase(rName)
20{
21}
22
23
24ArrayTest::~ArrayTest()
25{
26}
27
28struct Element
29{
30 int _data;
31};
32
33void ArrayTest::testConstruction()
34{
35
36 // fundamental type
37 typedef Poco::Array<float,6> FloatArray;
38 FloatArray a = { 42.f };
39
40 for (unsigned i=1; i<a.size(); ++i) {
41 a[i] = a[i-1]+1.f;
42 }
43
44 // copy constructor and assignment operator
45 FloatArray b(a);
46 FloatArray c;
47 c = a;
48 assertTrue (a==b && a==c);
49
50 typedef Poco::Array<double,6> DArray;
51 typedef Poco::Array<int,6> IArray;
52 IArray ia = {{1, 2, 3, 4, 5, 6 }};
53 DArray da;
54 da = ia;
55 da.assign(42);
56
57 // user-defined type
58 typedef Poco::Array<Element,10> ElementArray;
59 ElementArray g;
60
61 for (unsigned i=0; i<g.size(); ++i) {
62 g[i]._data = i;
63 }
64
65 for (unsigned i=0; i<g.size(); ++i) {
66 assertTrue (g[i]._data == i);
67 }
68
69
70}
71
72void ArrayTest::testOperations()
73{
74 const int SIZE = 6;
75 typedef Poco::Array<int,SIZE> Array;
76 Array a = { 1 };
77
78 // use some common STL container operations
79 assertTrue (a.size() == SIZE);
80 assertTrue (a.max_size() == SIZE);
81 assertTrue (a.empty() == false);
82 assertTrue (a.front() == a[0]);
83 assertTrue (a.back() == a[a.size()-1]);
84 //assertTrue (a.data() == &a[0]);
85
86 // assign
87 a.assign(100);
88 for(int i = 0; i<a.size(); i++){
89 assertTrue (a[i] == 100);
90 }
91
92 // swap
93 Array b;
94 b.assign(10);
95 for(int i=0; i<SIZE; i++){
96 assertTrue (a[i] == 100);
97 assertTrue (b[i] == 10);
98 }
99 a.swap(b);
100 for(int i=0; i<SIZE; i++){
101 assertTrue (a[i] == 10);
102 assertTrue (b[i] == 100);
103 }
104
105}
106
107void ArrayTest::testContainer()
108{
109 const int SIZE = 2;
110 typedef Poco::Array<int,SIZE> Array;
111 Array a = {{1, 2}};
112 assertTrue (a[0] == 1);
113 assertTrue (a[1] == 2);
114
115 typedef std::vector<Array> ArrayVec;
116 ArrayVec container;
117 container.push_back(a);
118 container.push_back(a);
119
120 assertTrue (container[0][0] == 1);
121 assertTrue (container[0][1] == 2);
122 assertTrue (container[1][0] == 1);
123 assertTrue (container[1][1] == 2);
124}
125
126void ArrayTest::testIterator()
127{
128 // create array of four seasons
129 Poco::Array<std::string,4> seasons = {
130 { "spring", "summer", "autumn", "winter" }
131 };
132
133 // copy and change order
134 Poco::Array<std::string,4> org = seasons;
135 for (size_t i=seasons.size()-1; i>0; --i) {
136 swap(seasons.at(i),seasons.at((i+1)%seasons.size()));
137 }
138
139 // try swap()
140 swap(seasons,org);
141
142 // try reverse iterators
143 for (Poco::Array<std::string,4>::reverse_iterator pos
144 =seasons.rbegin(); pos<seasons.rend(); ++pos) {
145 }
146}
147
148void ArrayTest::testAlgorithm()
149{
150 // create and initialize array
151 const int SIZE = 10;
152 typedef Poco::Array<int,SIZE> IArray;
153 IArray a = { { 1, 2, 3, 4, 5 } };
154 IArray b(a);
155
156 // modify elements directly
157 for (unsigned i=0; i<b.size(); ++i) {
158 ++b[i];
159 }
160
161 // try iterators
162 for (IArray::iterator pos =b.begin(); pos<b.end(); ++pos) {
163 --(*pos);
164 }
165
166 for (unsigned i=0; i<a.size(); ++i) {
167 assertTrue (a[i] == b[i]);
168 }
169
170 // change order using an STL algorithm
171 std::reverse(a.begin(),a.end());
172
173 for (unsigned i=0; i<a.size(); ++i) {
174 assertTrue (a[SIZE-i-1] == b[i]);
175 }
176
177 std::reverse(a.begin(),a.end());
178
179 // negate elements using STL framework
180 std::transform( a.begin(),a.end(), // source
181 a.begin(), // destination
182 std::negate<int>()); // operation
183
184 for (unsigned i=0; i<a.size(); ++i) {
185 assertTrue (a[i] == -b[i]);
186 }
187
188}
189
190void ArrayTest::testMultiLevelArray()
191{
192 const int SIZE = 2;
193 typedef Poco::Array<int,SIZE> IArray;
194 typedef Poco::Array<IArray,SIZE> MultiArray;
195
196 MultiArray a;
197 a[0][0] = 1;
198 a[0][1] = 2;
199 a[1][0] = 3;
200 a[1][1] = 4;
201
202 MultiArray b = a;
203 assertTrue (b[0][0] == 1);
204 assertTrue (b[0][1] == 2);
205 assertTrue (b[1][0] == 3);
206 assertTrue (b[1][1] == 4);
207}
208
209void ArrayTest::setUp()
210{
211}
212
213
214void ArrayTest::tearDown()
215{
216}
217
218
219CppUnit::Test* ArrayTest::suite()
220{
221 CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ArrayTest");
222
223 CppUnit_addTest(pSuite, ArrayTest, testConstruction);
224 CppUnit_addTest(pSuite, ArrayTest, testOperations);
225 CppUnit_addTest(pSuite, ArrayTest, testContainer);
226 CppUnit_addTest(pSuite, ArrayTest, testIterator);
227 CppUnit_addTest(pSuite, ArrayTest, testAlgorithm);
228 CppUnit_addTest(pSuite, ArrayTest, testMultiLevelArray);
229
230 return pSuite;
231}
232