1 |
/* |
2 |
***************************************************************************** |
3 |
* * |
4 |
* COPYRIGHT ACcESS - All Rights Reserved * |
5 |
* * |
6 |
* This software is the property of ACcESS. No part of this code * |
7 |
* may be copied in any form or by any means without the expressed written * |
8 |
* consent of ACcESS. Copying, use or modification of this software * |
9 |
* by any unauthorised person is illegal unless that person has a software * |
10 |
* license agreement with ACcESS. * |
11 |
* * |
12 |
***************************************************************************** |
13 |
*/ |
14 |
|
15 |
#include "escript/DataArrayView.h" |
16 |
#include "escript/DataArray.h" |
17 |
#include "esysUtils/EsysException.h" |
18 |
|
19 |
#include "DataArrayTestCase.h" |
20 |
|
21 |
#include <iostream> |
22 |
#include <vector> |
23 |
|
24 |
using namespace CppUnitTest; |
25 |
using namespace esysUtils; |
26 |
using namespace escript; |
27 |
using namespace std; |
28 |
|
29 |
void DataArrayTestCase::setUp() { |
30 |
// |
31 |
// This is called before each test is run |
32 |
|
33 |
} |
34 |
|
35 |
void DataArrayTestCase::tearDown() { |
36 |
// |
37 |
// This is called after each test has been run |
38 |
|
39 |
} |
40 |
|
41 |
void DataArrayTestCase::testAll() { |
42 |
// |
43 |
// The test code may be entered here |
44 |
// There is nothing special about the function name, it may be renamed to |
45 |
// something more suitable. |
46 |
// As many test methods as desired may be added. |
47 |
|
48 |
cout << endl; |
49 |
|
50 |
{ |
51 |
cout << endl; |
52 |
cout << "\tTest default DataArray constructor - default value." << endl; |
53 |
|
54 |
cout << "\tConstruct Default DataArray." << endl; |
55 |
DataArrayView::ShapeType newShape; |
56 |
DataArray newArray; |
57 |
DataArrayView newView = newArray.getView(); |
58 |
|
59 |
cout << "\tCheck DataArray view shape." << endl; |
60 |
assert(newView.getShape()==newShape); |
61 |
assert(newView.getRank()==0); |
62 |
assert(newView.noValues()==1); |
63 |
|
64 |
cout << "\tCheck DataArray data vector values." << endl; |
65 |
DataArrayView::ValueType arrayData = newArray.getData(); |
66 |
DataArrayView::ValueType viewData = newView.getData(); |
67 |
assert(viewData==arrayData); |
68 |
int noValues = newView.noValues(); |
69 |
for (int i=0; i<noValues; i++) { |
70 |
assert(arrayData[i]==0.0); |
71 |
} |
72 |
assert(newView()==0.0); |
73 |
} |
74 |
|
75 |
{ |
76 |
cout << endl; |
77 |
cout << "\tTest default DataArray constructor - value 5.0." << endl; |
78 |
|
79 |
cout << "\tConstruct Default DataArray." << endl; |
80 |
DataArrayView::ShapeType newShape; |
81 |
DataArray newArray(5.0); |
82 |
DataArrayView newView = newArray.getView(); |
83 |
|
84 |
cout << "\tCheck DataArray view shape." << endl; |
85 |
assert(newView.getShape()==newShape); |
86 |
assert(newView.getRank()==0); |
87 |
assert(newView.noValues()==1); |
88 |
|
89 |
cout << "\tCheck DataArray data vector values." << endl; |
90 |
DataArrayView::ValueType arrayData = newArray.getData(); |
91 |
DataArrayView::ValueType viewData = newView.getData(); |
92 |
assert(viewData==arrayData); |
93 |
int noValues = newView.noValues(); |
94 |
for (int i=0; i<noValues; i++) { |
95 |
assert(arrayData[i]==5.0); |
96 |
} |
97 |
assert(newView()==5.0); |
98 |
} |
99 |
|
100 |
{ |
101 |
cout << endl; |
102 |
cout << "\tTest DataArray shape constructor, shape() - default value." << endl; |
103 |
|
104 |
cout << "\tConstruct new DataArray with rank 0 view." << endl; |
105 |
DataArrayView::ShapeType newShape; |
106 |
DataArray newArray(newShape); |
107 |
DataArrayView newView = newArray.getView(); |
108 |
|
109 |
cout << "\tCheck DataArray view shape." << endl; |
110 |
assert(newView.getShape()==newShape); |
111 |
assert(newView.getRank()==0); |
112 |
assert(newView.noValues()==1); |
113 |
|
114 |
cout << "\tCheck DataArray data vector values." << endl; |
115 |
DataArrayView::ValueType arrayData = newArray.getData(); |
116 |
DataArrayView::ValueType viewData = newView.getData(); |
117 |
assert(viewData==arrayData); |
118 |
int noValues = newView.noValues(); |
119 |
for (int i=0; i<noValues; i++) { |
120 |
assert(arrayData[i]==0.0); |
121 |
} |
122 |
assert(newView()==0.0); |
123 |
} |
124 |
|
125 |
{ |
126 |
cout << endl; |
127 |
cout << "\tTest DataArray shape constructor, shape() - value 5.0." << endl; |
128 |
|
129 |
cout << "\tConstruct new DataArray with rank 0 view." << endl; |
130 |
DataArrayView::ShapeType newShape; |
131 |
DataArray newArray(newShape, 5.0); |
132 |
DataArrayView newView = newArray.getView(); |
133 |
|
134 |
cout << "\tCheck DataArray view shape." << endl; |
135 |
assert(newView.getShape()==newShape); |
136 |
assert(newView.getRank()==0); |
137 |
assert(newView.noValues()==1); |
138 |
|
139 |
cout << "\tCheck DataArray data vector values." << endl; |
140 |
DataArrayView::ValueType arrayData = newArray.getData(); |
141 |
DataArrayView::ValueType viewData = newView.getData(); |
142 |
assert(viewData==arrayData); |
143 |
int noValues = newView.noValues(); |
144 |
for (int i=0; i<noValues; i++) { |
145 |
assert(arrayData[i]==5.0); |
146 |
} |
147 |
assert(newView()==5.0); |
148 |
} |
149 |
|
150 |
{ |
151 |
cout << endl; |
152 |
cout << "\tTest DataArray shape constructor, shape(5)." << endl; |
153 |
|
154 |
cout << "\tConstruct new DataArray with rank 1 view." << endl; |
155 |
DataArrayView::ShapeType newShape; |
156 |
newShape.push_back(5); |
157 |
DataArray newArray(newShape); |
158 |
DataArrayView newView = newArray.getView(); |
159 |
|
160 |
cout << "\tCheck DataArray view shape." << endl; |
161 |
assert(newView.getShape()==newShape); |
162 |
assert(newView.getRank()==1); |
163 |
assert(newView.noValues()==5); |
164 |
|
165 |
cout << "\tCheck DataArray data vector values." << endl; |
166 |
DataArrayView::ValueType::size_type index; |
167 |
DataArrayView::ValueType arrayData = newArray.getData(); |
168 |
DataArrayView::ValueType viewData = newView.getData(); |
169 |
assert(viewData==arrayData); |
170 |
for (int i=0; i<newShape[0]; i++) { |
171 |
index=newView.index(i); |
172 |
assert(arrayData[index]==0.0); |
173 |
assert(newView(i)==0.0); |
174 |
} |
175 |
} |
176 |
|
177 |
{ |
178 |
cout << endl; |
179 |
cout << "\tTest DataArray shape constructor, shape(2,3)." << endl; |
180 |
|
181 |
cout << "\tConstruct new DataArray with rank 2 view." << endl; |
182 |
DataArrayView::ShapeType newShape; |
183 |
newShape.push_back(2); |
184 |
newShape.push_back(3); |
185 |
DataArray newArray(newShape); |
186 |
DataArrayView newView = newArray.getView(); |
187 |
|
188 |
cout << "\tCheck DataArray view shape." << endl; |
189 |
assert(newView.getShape()==newShape); |
190 |
assert(newView.getRank()==2); |
191 |
assert(newView.noValues()==6); |
192 |
|
193 |
cout << "\tCheck DataArray data vector values." << endl; |
194 |
DataArrayView::ValueType::size_type index; |
195 |
DataArrayView::ValueType arrayData = newArray.getData(); |
196 |
DataArrayView::ValueType viewData = newView.getData(); |
197 |
assert(viewData==arrayData); |
198 |
for (int i=0; i<newShape[0]; i++) { |
199 |
for (int j=0; j<newShape[1]; j++) { |
200 |
index=newView.index(i,j); |
201 |
assert(arrayData[index]==0.0); |
202 |
assert(newView(i,j)==0.0); |
203 |
} |
204 |
} |
205 |
} |
206 |
|
207 |
{ |
208 |
cout << endl; |
209 |
cout << "\tTest DataArray shape constructor, shape(2,3,4)." << endl; |
210 |
|
211 |
cout << "\tConstruct new DataArray with rank 3 view." << endl; |
212 |
DataArrayView::ShapeType newShape; |
213 |
newShape.push_back(2); |
214 |
newShape.push_back(3); |
215 |
newShape.push_back(4); |
216 |
DataArray newArray(newShape); |
217 |
DataArrayView newView = newArray.getView(); |
218 |
|
219 |
cout << "\tCheck DataArray view shape." << endl; |
220 |
assert(newView.getShape()==newShape); |
221 |
assert(newView.getRank()==3); |
222 |
assert(newView.noValues()==24); |
223 |
|
224 |
cout << "\tCheck DataArray data vector values." << endl; |
225 |
DataArrayView::ValueType::size_type index; |
226 |
DataArrayView::ValueType viewData = newView.getData(); |
227 |
DataArrayView::ValueType arrayData = newArray.getData(); |
228 |
assert(viewData==arrayData); |
229 |
for (int i=0; i<newShape[0]; i++) { |
230 |
for (int j=0; j<newShape[1]; j++) { |
231 |
for (int k=0; k<newShape[2]; k++) { |
232 |
index=newView.index(i,j,k); |
233 |
assert(arrayData[index]==0.0); |
234 |
assert(newView(i,j,k)==0.0); |
235 |
} |
236 |
} |
237 |
} |
238 |
} |
239 |
|
240 |
{ |
241 |
cout << endl; |
242 |
cout << "\tTest DataArray shape constructor, shape(2,3,4,7)." << endl; |
243 |
|
244 |
cout << "\tConstruct new DataArray with rank 4 view." << endl; |
245 |
DataArrayView::ShapeType newShape; |
246 |
newShape.push_back(2); |
247 |
newShape.push_back(3); |
248 |
newShape.push_back(4); |
249 |
newShape.push_back(7); |
250 |
DataArray newArray(newShape); |
251 |
DataArrayView newView = newArray.getView(); |
252 |
|
253 |
cout << "\tCheck DataArray view shape." << endl; |
254 |
assert(newView.getShape()==newShape); |
255 |
assert(newView.getRank()==4); |
256 |
assert(newView.noValues()==168); |
257 |
|
258 |
cout << "\tCheck DataArray data vector values." << endl; |
259 |
DataArrayView::ValueType::size_type index; |
260 |
DataArrayView::ValueType viewData = newView.getData(); |
261 |
DataArrayView::ValueType arrayData = newArray.getData(); |
262 |
assert(viewData==arrayData); |
263 |
for (int i=0; i<newShape[0]; i++) { |
264 |
for (int j=0; j<newShape[1]; j++) { |
265 |
for (int k=0; k<newShape[2]; k++) { |
266 |
for (int l=0; l<newShape[3]; l++) { |
267 |
index=newView.index(i,j,k,l); |
268 |
assert(arrayData[index]==0.0); |
269 |
assert(newView(i,j,k,l)==0.0); |
270 |
} |
271 |
} |
272 |
} |
273 |
} |
274 |
} |
275 |
|
276 |
{ |
277 |
cout << endl; |
278 |
cout << "\tTest DataArray copy constructor, default DataArray." << endl; |
279 |
|
280 |
cout << "\tConstruct Default DataArray." << endl; |
281 |
DataArray oldArray; |
282 |
|
283 |
cout << "\tCopy Default DataArray." << endl; |
284 |
DataArrayView::ShapeType newShape; |
285 |
DataArray newArray(oldArray); |
286 |
DataArrayView newView = newArray.getView(); |
287 |
|
288 |
cout << "\tCheck DataArray view shape." << endl; |
289 |
assert(newView.getShape()==newShape); |
290 |
assert(newView.getRank()==0); |
291 |
assert(newView.noValues()==1); |
292 |
|
293 |
cout << "\tCheck DataArray data vector values." << endl; |
294 |
DataArrayView::ValueType arrayData = newArray.getData(); |
295 |
DataArrayView::ValueType viewData = newView.getData(); |
296 |
assert(viewData==arrayData); |
297 |
assert(arrayData[0]==0.0); |
298 |
assert(newView()==0.0); |
299 |
} |
300 |
|
301 |
{ |
302 |
cout << endl; |
303 |
cout << "\tTest DataArray copy constructor, shape(2,3)." << endl; |
304 |
|
305 |
cout << "\tConstruct new DataArray with rank 2 view." << endl; |
306 |
DataArrayView::ShapeType oldShape; |
307 |
oldShape.push_back(2); |
308 |
oldShape.push_back(3); |
309 |
DataArray oldArray(oldShape); |
310 |
|
311 |
cout << "\tCopy rank 2 DataArray." << endl; |
312 |
DataArrayView::ShapeType newShape = oldShape; |
313 |
DataArray newArray(oldArray); |
314 |
DataArrayView newView = newArray.getView(); |
315 |
|
316 |
cout << "\tCheck DataArray view shape." << endl; |
317 |
assert(newView.getShape()==newShape); |
318 |
assert(newView.getRank()==2); |
319 |
assert(newView.noValues()==6); |
320 |
|
321 |
cout << "\tCheck DataArray data vector values." << endl; |
322 |
DataArrayView::ValueType::size_type index; |
323 |
DataArrayView::ValueType viewData = newView.getData(); |
324 |
DataArrayView::ValueType arrayData = newArray.getData(); |
325 |
assert(viewData==arrayData); |
326 |
for (int i=0; i<newShape[0]; i++) { |
327 |
for (int j=0; j<newShape[1]; j++) { |
328 |
index=newView.index(i,j); |
329 |
assert(arrayData[index]==0.0); |
330 |
assert(newView(i,j)==0.0); |
331 |
} |
332 |
} |
333 |
} |
334 |
|
335 |
{ |
336 |
cout << endl; |
337 |
cout << "\tTest DataArray copy constructor, shape(2,3,4,7)." << endl; |
338 |
|
339 |
cout << "\tConstruct new DataArray with rank 4 view." << endl; |
340 |
DataArrayView::ShapeType oldShape; |
341 |
oldShape.push_back(2); |
342 |
oldShape.push_back(3); |
343 |
oldShape.push_back(4); |
344 |
oldShape.push_back(7); |
345 |
DataArray oldArray(oldShape); |
346 |
|
347 |
cout << "\tCopy rank 4 DataArray." << endl; |
348 |
DataArrayView::ShapeType newShape = oldShape; |
349 |
DataArray newArray(oldArray); |
350 |
DataArrayView newView = newArray.getView(); |
351 |
|
352 |
cout << "\tCheck DataArray view shape." << endl; |
353 |
assert(newView.getShape()==newShape); |
354 |
assert(newView.getRank()==4); |
355 |
assert(newView.noValues()==168); |
356 |
|
357 |
cout << "\tCheck DataArray data vector values." << endl; |
358 |
DataArrayView::ValueType::size_type index; |
359 |
DataArrayView::ValueType viewData = newView.getData(); |
360 |
DataArrayView::ValueType arrayData = newArray.getData(); |
361 |
assert(viewData==arrayData); |
362 |
for (int i=0; i<newShape[0]; i++) { |
363 |
for (int j=0; j<newShape[1]; j++) { |
364 |
for (int k=0; k<newShape[2]; k++) { |
365 |
for (int l=0; l<newShape[3]; l++) { |
366 |
index=newView.index(i,j,k,l); |
367 |
assert(arrayData[index]==0.0); |
368 |
assert(newView(i,j,k,l)==0.0); |
369 |
} |
370 |
} |
371 |
} |
372 |
} |
373 |
} |
374 |
|
375 |
{ |
376 |
cout << endl; |
377 |
cout << "\tTest DataArray DataArrayView constructor, default DataArray." << endl; |
378 |
|
379 |
cout << "\tConstruct Default DataArray." << endl; |
380 |
DataArray oldArray; |
381 |
|
382 |
cout << "\tExtract Default DataArray DataArrayView." << endl; |
383 |
DataArrayView oldView = oldArray.getView(); |
384 |
|
385 |
cout << "\tCopy Default DataArray from DataArrayView." << endl; |
386 |
DataArrayView::ShapeType newShape; |
387 |
DataArray newArray(oldView); |
388 |
DataArrayView newView = newArray.getView(); |
389 |
|
390 |
cout << "\tCheck DataArray view shape." << endl; |
391 |
assert(newView.getShape()==newShape); |
392 |
assert(newView.getRank()==0); |
393 |
assert(newView.noValues()==1); |
394 |
|
395 |
cout << "\tCheck DataArray data vector values." << endl; |
396 |
DataArrayView::ValueType arrayData = newArray.getData(); |
397 |
DataArrayView::ValueType viewData = newView.getData(); |
398 |
assert(viewData==arrayData); |
399 |
assert(arrayData[0]==0.0); |
400 |
assert(newView()==0.0); |
401 |
} |
402 |
|
403 |
{ |
404 |
cout << endl; |
405 |
cout << "\tTest DataArray DataArrayView constructor, shape(2,3)." << endl; |
406 |
|
407 |
cout << "\tConstruct new DataArray with rank 2 view." << endl; |
408 |
DataArrayView::ShapeType oldShape; |
409 |
oldShape.push_back(2); |
410 |
oldShape.push_back(3); |
411 |
DataArray oldArray(oldShape); |
412 |
|
413 |
cout << "\tExtract rank 2 DataArray DataArrayView." << endl; |
414 |
DataArrayView oldView = oldArray.getView(); |
415 |
|
416 |
cout << "\tCopy rank 2 DataArray from DataArrayView." << endl; |
417 |
DataArrayView::ShapeType newShape = oldShape; |
418 |
DataArray newArray(oldView); |
419 |
DataArrayView newView = newArray.getView(); |
420 |
|
421 |
cout << "\tCheck DataArray view shape." << endl; |
422 |
assert(newView.getShape()==newShape); |
423 |
assert(newView.getRank()==2); |
424 |
assert(newView.noValues()==6); |
425 |
|
426 |
cout << "\tCheck DataArray data vector values." << endl; |
427 |
DataArrayView::ValueType::size_type index; |
428 |
DataArrayView::ValueType viewData = newView.getData(); |
429 |
DataArrayView::ValueType arrayData = newArray.getData(); |
430 |
assert(viewData==arrayData); |
431 |
for (int i=0; i<newShape[0]; i++) { |
432 |
for (int j=0; j<newShape[1]; j++) { |
433 |
index=newView.index(i,j); |
434 |
assert(arrayData[index]==0.0); |
435 |
assert(newView(i,j)==0.0); |
436 |
} |
437 |
} |
438 |
} |
439 |
|
440 |
{ |
441 |
cout << endl; |
442 |
cout << "\tTest DataArray DataArrayView constructor, shape(2,3,4,7)." << endl; |
443 |
|
444 |
cout << "\tConstruct new DataArray with rank 4 view." << endl; |
445 |
DataArrayView::ShapeType oldShape; |
446 |
oldShape.push_back(2); |
447 |
oldShape.push_back(3); |
448 |
oldShape.push_back(4); |
449 |
oldShape.push_back(7); |
450 |
DataArray oldArray(oldShape); |
451 |
|
452 |
cout << "\tExtract rank 4 DataArray DataArrayView." << endl; |
453 |
DataArrayView oldView = oldArray.getView(); |
454 |
|
455 |
cout << "\tCopy rank 4 DataArray from DataArrayView." << endl; |
456 |
DataArrayView::ShapeType newShape = oldShape; |
457 |
DataArray newArray(oldView); |
458 |
DataArrayView newView = newArray.getView(); |
459 |
|
460 |
cout << "\tCheck DataArray view shape." << endl; |
461 |
assert(newView.getShape()==newShape); |
462 |
assert(newView.getRank()==4); |
463 |
assert(newView.noValues()==168); |
464 |
|
465 |
cout << "\tCheck DataArray data vector values." << endl; |
466 |
DataArrayView::ValueType::size_type index; |
467 |
DataArrayView::ValueType viewData = newView.getData(); |
468 |
DataArrayView::ValueType arrayData = newArray.getData(); |
469 |
assert(viewData==arrayData); |
470 |
for (int i=0; i<newShape[0]; i++) { |
471 |
for (int j=0; j<newShape[1]; j++) { |
472 |
for (int k=0; k<newShape[2]; k++) { |
473 |
for (int l=0; l<newShape[3]; l++) { |
474 |
index=newView.index(i,j,k,l); |
475 |
assert(arrayData[index]==0.0); |
476 |
assert(newView(i,j,k,l)==0.0); |
477 |
} |
478 |
} |
479 |
} |
480 |
} |
481 |
} |
482 |
|
483 |
} |
484 |
|
485 |
TestSuite* DataArrayTestCase::suite () |
486 |
{ |
487 |
// |
488 |
// create the suite of tests to perform. |
489 |
TestSuite *testSuite = new TestSuite ("DataArrayTestCase"); |
490 |
|
491 |
testSuite->addTest (new TestCaller< DataArrayTestCase>("testAll",&DataArrayTestCase::testAll)); |
492 |
return testSuite; |
493 |
} |