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 |
#include "escript/DataArray.h" |
15 |
#include "escript/DataArrayView.h" |
16 |
#include "escript/DataAlgorithm.h" |
17 |
#include "esysUtils/EsysException.h" |
18 |
|
19 |
#include "DataArrayViewTestCase.h" |
20 |
|
21 |
#include <iostream> |
22 |
|
23 |
using namespace CppUnitTest; |
24 |
using namespace esysUtils; |
25 |
using namespace escript; |
26 |
using namespace std; |
27 |
|
28 |
void DataArrayViewTestCase::setUp() { |
29 |
// |
30 |
// This is called before each test is run |
31 |
|
32 |
} |
33 |
|
34 |
void DataArrayViewTestCase::tearDown() { |
35 |
// |
36 |
// This is called after each test has been run |
37 |
|
38 |
} |
39 |
|
40 |
void DataArrayViewTestCase::testResultSliceShape() { |
41 |
|
42 |
cout << endl; |
43 |
cout << "\tTest getResultSliceShape method." << endl; |
44 |
|
45 |
DataArrayView::RegionType region; |
46 |
DataArrayView::ShapeType resultShape; |
47 |
|
48 |
region.push_back(DataArrayView::RegionType::value_type(1,5)); |
49 |
resultShape.push_back(4); |
50 |
assert(DataArrayView::getResultSliceShape(region)==resultShape); |
51 |
|
52 |
region.push_back(DataArrayView::RegionType::value_type(2,5)); |
53 |
resultShape.push_back(3); |
54 |
assert(DataArrayView::getResultSliceShape(region)==resultShape); |
55 |
|
56 |
region.push_back(DataArrayView::RegionType::value_type(3,9)); |
57 |
resultShape.push_back(6); |
58 |
assert(DataArrayView::getResultSliceShape(region)==resultShape); |
59 |
|
60 |
region.push_back(DataArrayView::RegionType::value_type(1,7)); |
61 |
resultShape.push_back(6); |
62 |
assert(DataArrayView::getResultSliceShape(region)==resultShape); |
63 |
|
64 |
} |
65 |
|
66 |
void DataArrayViewTestCase::testSlicing() { |
67 |
|
68 |
{ |
69 |
cout << endl; |
70 |
cout << "\tSlice a scalar to a scalar."; |
71 |
|
72 |
// Define slice region. |
73 |
DataArrayView::RegionType region; |
74 |
|
75 |
// Define shape of views. |
76 |
DataArrayView::ShapeType sourceShape; |
77 |
|
78 |
// Create source and target views. |
79 |
DataArray source(sourceShape,2.0); |
80 |
DataArrayView sourceView=source.getView(); |
81 |
|
82 |
DataArray target; |
83 |
DataArrayView targetView=target.getView(); |
84 |
|
85 |
// Copy source view to target view. |
86 |
targetView.copySlice(sourceView,region); |
87 |
|
88 |
// Check results of copy. |
89 |
assert(sourceView==targetView); |
90 |
} |
91 |
|
92 |
{ |
93 |
cout << endl; |
94 |
cout << "\tSlice a scalar from a scalar."; |
95 |
|
96 |
// Define slice region. |
97 |
DataArrayView::RegionType region; |
98 |
|
99 |
// Define shape of views. |
100 |
DataArrayView::ShapeType sourceShape; |
101 |
|
102 |
// Create source and target views. |
103 |
DataArray source(sourceShape,2.0); |
104 |
DataArrayView sourceView=source.getView(); |
105 |
|
106 |
DataArray target; |
107 |
DataArrayView targetView=target.getView(); |
108 |
|
109 |
// Copy source view to target view. |
110 |
targetView.copySliceFrom(sourceView,region); |
111 |
|
112 |
// Check results of copy. |
113 |
assert(sourceView==targetView); |
114 |
} |
115 |
|
116 |
{ |
117 |
cout << endl; |
118 |
cout << "\tSlice a 1 dimensional slice to a 1 dimensional array."; |
119 |
|
120 |
// Define slice region. |
121 |
DataArrayView::RegionType region; |
122 |
region.push_back(DataArrayView::RegionType::value_type(2,4)); |
123 |
|
124 |
// Define shapes of views. |
125 |
DataArrayView::ShapeType sourceShape; |
126 |
sourceShape.push_back(6); |
127 |
DataArrayView::ShapeType targetShape = DataArrayView::getResultSliceShape(region); |
128 |
|
129 |
// Create source and target views. |
130 |
DataArray source(sourceShape); |
131 |
DataArrayView sourceView=source.getView(); |
132 |
for (int i=0;i<sourceShape[0];i++) { |
133 |
sourceView(i)=i; |
134 |
} |
135 |
|
136 |
DataArray target(targetShape); |
137 |
DataArrayView targetView=target.getView(); |
138 |
|
139 |
// Copy source view to target view. |
140 |
targetView.copySlice(sourceView,region); |
141 |
|
142 |
// Check results of copy. |
143 |
for (int i=region[0].first;i<region[0].second;i++) { |
144 |
assert(sourceView(i)== |
145 |
targetView(i-region[0].first)); |
146 |
} |
147 |
} |
148 |
|
149 |
{ |
150 |
cout << endl; |
151 |
cout << "\tSlice a 1 dimensional slice to a scalar."; |
152 |
|
153 |
// Define slice region. |
154 |
DataArrayView::RegionType region; |
155 |
region.push_back(DataArrayView::RegionType::value_type(2,3)); |
156 |
|
157 |
// Define shapes of views. |
158 |
DataArrayView::ShapeType sourceShape; |
159 |
sourceShape.push_back(6); |
160 |
DataArrayView::ShapeType targetShape; |
161 |
|
162 |
// Create source and target views. |
163 |
DataArray source(sourceShape); |
164 |
DataArrayView sourceView=source.getView(); |
165 |
for (int i=0;i<sourceShape[0];i++) { |
166 |
sourceView(i)=i; |
167 |
} |
168 |
|
169 |
DataArray target(targetShape); |
170 |
DataArrayView targetView=target.getView(); |
171 |
|
172 |
// Copy source view to target view. |
173 |
targetView.copySlice(sourceView,region); |
174 |
|
175 |
// Check results of copy. |
176 |
for (int i=region[0].first;i<region[0].second;i++) { |
177 |
assert(sourceView(i)== |
178 |
targetView()); |
179 |
} |
180 |
} |
181 |
|
182 |
{ |
183 |
cout << endl; |
184 |
cout << "\tSlice a 1 dimensional slice from a 1 dimensional array."; |
185 |
|
186 |
// Define slice region. |
187 |
DataArrayView::RegionType region; |
188 |
region.push_back(DataArrayView::RegionType::value_type(2,4)); |
189 |
|
190 |
// Define shapes of views. |
191 |
DataArrayView::ShapeType sourceShape = DataArrayView::getResultSliceShape(region); |
192 |
DataArrayView::ShapeType targetShape; |
193 |
targetShape.push_back(6); |
194 |
|
195 |
// Create source and target views. |
196 |
DataArray source(sourceShape); |
197 |
DataArrayView sourceView=source.getView(); |
198 |
for (int i=0;i<sourceShape[0];i++) { |
199 |
sourceView(i)=i; |
200 |
} |
201 |
|
202 |
DataArray target(targetShape); |
203 |
DataArrayView targetView=target.getView(); |
204 |
|
205 |
// Copy source view to target view. |
206 |
targetView.copySliceFrom(sourceView,region); |
207 |
|
208 |
// Check results of copy. |
209 |
for (int i=region[0].first;i<region[0].second;i++) { |
210 |
assert(sourceView(i-region[0].first)== |
211 |
targetView(i)); |
212 |
} |
213 |
} |
214 |
|
215 |
{ |
216 |
cout << endl; |
217 |
cout << "\tSlice a 1 dimensional slice from a scalar."; |
218 |
|
219 |
// Define slice region. |
220 |
DataArrayView::RegionType region; |
221 |
region.push_back(DataArrayView::RegionType::value_type(2,4)); |
222 |
|
223 |
// Define shapes of views. |
224 |
DataArrayView::ShapeType sourceShape; |
225 |
DataArrayView::ShapeType targetShape; |
226 |
targetShape.push_back(6); |
227 |
|
228 |
// Create source and target views. |
229 |
DataArray source(sourceShape); |
230 |
DataArrayView sourceView=source.getView(); |
231 |
sourceView()=5; |
232 |
|
233 |
DataArray target(targetShape); |
234 |
DataArrayView targetView=target.getView(); |
235 |
|
236 |
// Copy source view to target view. |
237 |
targetView.copySliceFrom(sourceView,region); |
238 |
|
239 |
// Check results of copy. |
240 |
for (int i=region[0].first;i<region[0].second;i++) { |
241 |
assert(sourceView()== |
242 |
targetView(i)); |
243 |
} |
244 |
} |
245 |
|
246 |
{ |
247 |
cout << endl; |
248 |
cout << "\tSlice a 2 dimensional slice to a 2 dimensional array."; |
249 |
|
250 |
// Define slice region. |
251 |
DataArrayView::RegionType region; |
252 |
region.push_back(DataArrayView::RegionType::value_type(2,4)); |
253 |
region.push_back(DataArrayView::RegionType::value_type(0,2)); |
254 |
|
255 |
// Define shapes of views. |
256 |
DataArrayView::ShapeType sourceShape; |
257 |
sourceShape.push_back(6); |
258 |
sourceShape.push_back(3); |
259 |
DataArrayView::ShapeType targetShape = DataArrayView::getResultSliceShape(region); |
260 |
|
261 |
// Create source and target views. |
262 |
DataArray source(sourceShape); |
263 |
DataArrayView sourceView=source.getView(); |
264 |
int val=0; |
265 |
for (int i=0;i<sourceShape[0];i++) { |
266 |
for (int j=0;j<sourceShape[1];j++) { |
267 |
sourceView(i,j)=val++; |
268 |
} |
269 |
} |
270 |
|
271 |
DataArray target(targetShape); |
272 |
DataArrayView targetView=target.getView(); |
273 |
|
274 |
// Copy source view to target view. |
275 |
targetView.copySlice(sourceView,region); |
276 |
|
277 |
// Check results of copy. |
278 |
for (int i=region[0].first;i<region[0].second;i++) { |
279 |
for (int j=region[1].first;j<region[1].second;j++) { |
280 |
assert(sourceView(i,j)== |
281 |
targetView(i-region[0].first,j-region[1].first)); |
282 |
} |
283 |
} |
284 |
} |
285 |
|
286 |
{ |
287 |
cout << endl; |
288 |
cout << "\tSlice a 2 dimensional slice to a 1 dimensional array."; |
289 |
|
290 |
// Define slice region. |
291 |
DataArrayView::RegionType region; |
292 |
region.push_back(DataArrayView::RegionType::value_type(0,3)); |
293 |
region.push_back(DataArrayView::RegionType::value_type(1,2)); |
294 |
|
295 |
// Define shapes of views. |
296 |
DataArrayView::ShapeType sourceShape; |
297 |
sourceShape.push_back(3); |
298 |
sourceShape.push_back(6); |
299 |
DataArrayView::ShapeType targetShape; |
300 |
targetShape.push_back(3); |
301 |
|
302 |
// Create source and target views. |
303 |
DataArray source(sourceShape); |
304 |
DataArrayView sourceView=source.getView(); |
305 |
int val=0; |
306 |
for (int i=0;i<sourceShape[0];i++) { |
307 |
for (int j=0;j<sourceShape[1];j++) { |
308 |
sourceView(i,j)=val++; |
309 |
} |
310 |
} |
311 |
|
312 |
DataArray target(targetShape); |
313 |
DataArrayView targetView=target.getView(); |
314 |
|
315 |
// Copy source view to target view. |
316 |
targetView.copySlice(sourceView,region); |
317 |
|
318 |
// Check results of copy. |
319 |
for (int i=region[0].first;i<region[0].second;i++) { |
320 |
for (int j=region[1].first;j<region[1].second;j++) { |
321 |
assert(sourceView(i,j)== |
322 |
targetView(i-region[0].first)); |
323 |
} |
324 |
} |
325 |
} |
326 |
|
327 |
{ |
328 |
cout << endl; |
329 |
cout << "\tSlice a 2 dimensional slice to a scalar."; |
330 |
|
331 |
// Define slice region. |
332 |
DataArrayView::RegionType region; |
333 |
region.push_back(DataArrayView::RegionType::value_type(2,3)); |
334 |
region.push_back(DataArrayView::RegionType::value_type(1,2)); |
335 |
|
336 |
// Define shapes of views. |
337 |
DataArrayView::ShapeType sourceShape; |
338 |
sourceShape.push_back(3); |
339 |
sourceShape.push_back(6); |
340 |
DataArrayView::ShapeType targetShape; |
341 |
|
342 |
// Create source and target views. |
343 |
DataArray source(sourceShape); |
344 |
DataArrayView sourceView=source.getView(); |
345 |
int val=0; |
346 |
for (int i=0;i<sourceShape[0];i++) { |
347 |
for (int j=0;j<sourceShape[1];j++) { |
348 |
sourceView(i,j)=val++; |
349 |
} |
350 |
} |
351 |
|
352 |
DataArray target(targetShape); |
353 |
DataArrayView targetView=target.getView(); |
354 |
|
355 |
// Copy source view to target view. |
356 |
targetView.copySlice(sourceView,region); |
357 |
// Check results of copy. |
358 |
for (int i=region[0].first;i<region[0].second;i++) { |
359 |
for (int j=region[1].first;j<region[1].second;j++) { |
360 |
assert(sourceView(i,j)== |
361 |
targetView()); |
362 |
} |
363 |
} |
364 |
} |
365 |
|
366 |
{ |
367 |
cout << endl; |
368 |
cout << "\tSlice a 2 dimensional slice from a 2 dimensional array."; |
369 |
|
370 |
// Define slice region. |
371 |
DataArrayView::RegionType region; |
372 |
region.push_back(DataArrayView::RegionType::value_type(2,4)); |
373 |
region.push_back(DataArrayView::RegionType::value_type(0,2)); |
374 |
|
375 |
// Define shapes of views. |
376 |
DataArrayView::ShapeType sourceShape = DataArrayView::getResultSliceShape(region); |
377 |
DataArrayView::ShapeType targetShape; |
378 |
targetShape.push_back(6); |
379 |
targetShape.push_back(3); |
380 |
|
381 |
// Create source and target views. |
382 |
DataArray source(sourceShape); |
383 |
DataArrayView sourceView=source.getView(); |
384 |
int val=0; |
385 |
for (int i=0;i<sourceShape[0];i++) { |
386 |
for (int j=0;j<sourceShape[1];j++) { |
387 |
sourceView(i,j)=val++; |
388 |
} |
389 |
} |
390 |
|
391 |
DataArray target(targetShape); |
392 |
DataArrayView targetView=target.getView(); |
393 |
|
394 |
// Copy source view to target view. |
395 |
targetView.copySliceFrom(sourceView,region); |
396 |
|
397 |
// Check results of copy. |
398 |
for (int i=region[0].first;i<region[0].second;i++) { |
399 |
for (int j=region[1].first;j<region[1].second;j++) { |
400 |
assert(sourceView(i-region[0].first,j-region[1].first)== |
401 |
targetView(i,j)); |
402 |
} |
403 |
} |
404 |
} |
405 |
|
406 |
{ |
407 |
cout << endl; |
408 |
cout << "\tSlice a 2 dimensional slice from a scalar."; |
409 |
|
410 |
// Define slice region. |
411 |
DataArrayView::RegionType region; |
412 |
region.push_back(DataArrayView::RegionType::value_type(2,4)); |
413 |
region.push_back(DataArrayView::RegionType::value_type(0,2)); |
414 |
|
415 |
// Define shapes of views. |
416 |
DataArrayView::ShapeType sourceShape; |
417 |
DataArrayView::ShapeType targetShape; |
418 |
targetShape.push_back(6); |
419 |
targetShape.push_back(3); |
420 |
|
421 |
// Create source and target views. |
422 |
DataArray source(sourceShape); |
423 |
DataArrayView sourceView=source.getView(); |
424 |
sourceView()=5; |
425 |
|
426 |
DataArray target(targetShape); |
427 |
DataArrayView targetView=target.getView(); |
428 |
|
429 |
// Copy source view to target view. |
430 |
targetView.copySliceFrom(sourceView,region); |
431 |
|
432 |
// Check results of copy. |
433 |
for (int i=region[0].first;i<region[0].second;i++) { |
434 |
for (int j=region[1].first;j<region[1].second;j++) { |
435 |
assert(sourceView()== |
436 |
targetView(i,j)); |
437 |
} |
438 |
} |
439 |
} |
440 |
|
441 |
{ |
442 |
cout << endl; |
443 |
cout << "\tSlice a 3 dimensional slice to a 3 dimensional array."; |
444 |
|
445 |
// Define slice region. |
446 |
DataArrayView::RegionType region; |
447 |
region.push_back(DataArrayView::RegionType::value_type(2,4)); |
448 |
region.push_back(DataArrayView::RegionType::value_type(0,2)); |
449 |
region.push_back(DataArrayView::RegionType::value_type(5,9)); |
450 |
|
451 |
// Define shapes of views. |
452 |
DataArrayView::ShapeType sourceShape; |
453 |
sourceShape.push_back(6); |
454 |
sourceShape.push_back(3); |
455 |
sourceShape.push_back(13); |
456 |
DataArrayView::ShapeType targetShape = DataArrayView::getResultSliceShape(region); |
457 |
|
458 |
// Create source and target views. |
459 |
DataArray source(sourceShape); |
460 |
DataArrayView sourceView=source.getView(); |
461 |
int val=0; |
462 |
for (int i=0;i<sourceShape[0];i++) { |
463 |
for (int j=0;j<sourceShape[1];j++) { |
464 |
for (int k=0;k<sourceShape[2];k++) { |
465 |
sourceView(i,j,k)=val++; |
466 |
} |
467 |
} |
468 |
} |
469 |
|
470 |
DataArray target(targetShape); |
471 |
DataArrayView targetView=target.getView(); |
472 |
|
473 |
// Copy source view to target view. |
474 |
targetView.copySlice(sourceView,region); |
475 |
|
476 |
// Check results of copy. |
477 |
for (int i=region[0].first;i<region[0].second;i++) { |
478 |
for (int j=region[1].first;j<region[1].second;j++) { |
479 |
for (int k=region[2].first;k<region[2].second;k++) { |
480 |
assert(sourceView(i,j,k)== |
481 |
targetView(i-region[0].first,j-region[1].first,k-region[2].first)); |
482 |
} |
483 |
} |
484 |
} |
485 |
} |
486 |
|
487 |
{ |
488 |
cout << endl; |
489 |
cout << "\tSlice a 3 dimensional slice to a 2 dimensional array."; |
490 |
|
491 |
// Define slice region. |
492 |
DataArrayView::RegionType region; |
493 |
region.push_back(DataArrayView::RegionType::value_type(2,4)); |
494 |
region.push_back(DataArrayView::RegionType::value_type(0,1)); |
495 |
region.push_back(DataArrayView::RegionType::value_type(5,9)); |
496 |
|
497 |
// Define shapes of views. |
498 |
DataArrayView::ShapeType sourceShape; |
499 |
sourceShape.push_back(6); |
500 |
sourceShape.push_back(3); |
501 |
sourceShape.push_back(13); |
502 |
DataArrayView::ShapeType targetShape; |
503 |
targetShape.push_back(2); |
504 |
targetShape.push_back(4); |
505 |
|
506 |
// Create source and target views. |
507 |
DataArray source(sourceShape); |
508 |
DataArrayView sourceView=source.getView(); |
509 |
int val=0; |
510 |
for (int i=0;i<sourceShape[0];i++) { |
511 |
for (int j=0;j<sourceShape[1];j++) { |
512 |
for (int k=0;k<sourceShape[2];k++) { |
513 |
sourceView(i,j,k)=val++; |
514 |
} |
515 |
} |
516 |
} |
517 |
|
518 |
DataArray target(targetShape); |
519 |
DataArrayView targetView=target.getView(); |
520 |
|
521 |
// Copy source view to target view. |
522 |
targetView.copySlice(sourceView,region); |
523 |
|
524 |
// Check results of copy. |
525 |
for (int i=region[0].first;i<region[0].second;i++) { |
526 |
for (int j=region[1].first;j<region[1].second;j++) { |
527 |
for (int k=region[2].first;k<region[2].second;k++) { |
528 |
assert(sourceView(i,j,k)== |
529 |
targetView(i-region[0].first,k-region[2].first)); |
530 |
} |
531 |
} |
532 |
} |
533 |
} |
534 |
|
535 |
{ |
536 |
cout << endl; |
537 |
cout << "\tSlice a 3 dimensional slice to a 1 dimensional array."; |
538 |
|
539 |
// Define slice region. |
540 |
DataArrayView::RegionType region; |
541 |
region.push_back(DataArrayView::RegionType::value_type(3,4)); |
542 |
region.push_back(DataArrayView::RegionType::value_type(0,1)); |
543 |
region.push_back(DataArrayView::RegionType::value_type(5,9)); |
544 |
|
545 |
// Define shapes of views. |
546 |
DataArrayView::ShapeType sourceShape; |
547 |
sourceShape.push_back(6); |
548 |
sourceShape.push_back(3); |
549 |
sourceShape.push_back(13); |
550 |
DataArrayView::ShapeType targetShape; |
551 |
targetShape.push_back(4); |
552 |
|
553 |
// Create source and target views. |
554 |
DataArray source(sourceShape); |
555 |
DataArrayView sourceView=source.getView(); |
556 |
int val=0; |
557 |
for (int i=0;i<sourceShape[0];i++) { |
558 |
for (int j=0;j<sourceShape[1];j++) { |
559 |
for (int k=0;k<sourceShape[2];k++) { |
560 |
sourceView(i,j,k)=val++; |
561 |
} |
562 |
} |
563 |
} |
564 |
|
565 |
DataArray target(targetShape); |
566 |
DataArrayView targetView=target.getView(); |
567 |
|
568 |
// Copy source view to target view. |
569 |
targetView.copySlice(sourceView,region); |
570 |
|
571 |
// Check results of copy. |
572 |
for (int i=region[0].first;i<region[0].second;i++) { |
573 |
for (int j=region[1].first;j<region[1].second;j++) { |
574 |
for (int k=region[2].first;k<region[2].second;k++) { |
575 |
assert(sourceView(i,j,k)== |
576 |
targetView(k-region[2].first)); |
577 |
} |
578 |
} |
579 |
} |
580 |
} |
581 |
|
582 |
{ |
583 |
cout << endl; |
584 |
cout << "\tSlice a 3 dimensional slice to a scalar."; |
585 |
|
586 |
// Define slice region. |
587 |
DataArrayView::RegionType region; |
588 |
region.push_back(DataArrayView::RegionType::value_type(3,4)); |
589 |
region.push_back(DataArrayView::RegionType::value_type(0,1)); |
590 |
region.push_back(DataArrayView::RegionType::value_type(5,6)); |
591 |
|
592 |
// Define shapes of views. |
593 |
DataArrayView::ShapeType sourceShape; |
594 |
sourceShape.push_back(6); |
595 |
sourceShape.push_back(3); |
596 |
sourceShape.push_back(13); |
597 |
DataArrayView::ShapeType targetShape; |
598 |
|
599 |
// Create source and target views. |
600 |
DataArray source(sourceShape); |
601 |
DataArrayView sourceView=source.getView(); |
602 |
int val=0; |
603 |
for (int i=0;i<sourceShape[0];i++) { |
604 |
for (int j=0;j<sourceShape[1];j++) { |
605 |
for (int k=0;k<sourceShape[2];k++) { |
606 |
sourceView(i,j,k)=val++; |
607 |
} |
608 |
} |
609 |
} |
610 |
|
611 |
DataArray target(targetShape); |
612 |
DataArrayView targetView=target.getView(); |
613 |
|
614 |
// Copy source view to target view. |
615 |
targetView.copySlice(sourceView,region); |
616 |
|
617 |
// Check results of copy. |
618 |
for (int i=region[0].first;i<region[0].second;i++) { |
619 |
for (int j=region[1].first;j<region[1].second;j++) { |
620 |
for (int k=region[2].first;k<region[2].second;k++) { |
621 |
assert(sourceView(i,j,k)== |
622 |
targetView()); |
623 |
} |
624 |
} |
625 |
} |
626 |
} |
627 |
|
628 |
{ |
629 |
cout << endl; |
630 |
cout << "\tSlice a 3 dimensional slice from a 3 dimensional array."; |
631 |
|
632 |
// Define slice region. |
633 |
DataArrayView::RegionType region; |
634 |
region.push_back(DataArrayView::RegionType::value_type(4,7)); |
635 |
region.push_back(DataArrayView::RegionType::value_type(2,5)); |
636 |
region.push_back(DataArrayView::RegionType::value_type(6,7)); |
637 |
|
638 |
// Define shapes of views. |
639 |
DataArrayView::ShapeType sourceShape = DataArrayView::getResultSliceShape(region); |
640 |
DataArrayView::ShapeType targetShape; |
641 |
targetShape.push_back(11); |
642 |
targetShape.push_back(8); |
643 |
targetShape.push_back(9); |
644 |
|
645 |
// Create source and target views. |
646 |
DataArray source(sourceShape); |
647 |
DataArrayView sourceView=source.getView(); |
648 |
int val=0; |
649 |
for (int i=0;i<sourceShape[0];i++) { |
650 |
for (int j=0;j<sourceShape[1];j++) { |
651 |
for (int k=0;k<sourceShape[2];k++) { |
652 |
sourceView(i,j,k)=val++; |
653 |
} |
654 |
} |
655 |
} |
656 |
|
657 |
DataArray target(targetShape); |
658 |
DataArrayView targetView=target.getView(); |
659 |
|
660 |
// Copy source view to target view. |
661 |
targetView.copySliceFrom(sourceView,region); |
662 |
|
663 |
// Check results of copy. |
664 |
for (int i=region[0].first;i<region[0].second;i++) { |
665 |
for (int j=region[1].first;j<region[1].second;j++) { |
666 |
for (int k=region[2].first;k<region[2].second;k++) { |
667 |
assert(sourceView(i-region[0].first,j-region[1].first,k-region[2].first)== |
668 |
targetView(i,j,k)); |
669 |
} |
670 |
} |
671 |
} |
672 |
} |
673 |
|
674 |
{ |
675 |
cout << endl; |
676 |
cout << "\tSlice a 3 dimensional slice from a scalar."; |
677 |
|
678 |
// Define slice region. |
679 |
DataArrayView::RegionType region; |
680 |
region.push_back(DataArrayView::RegionType::value_type(4,7)); |
681 |
region.push_back(DataArrayView::RegionType::value_type(2,5)); |
682 |
region.push_back(DataArrayView::RegionType::value_type(6,7)); |
683 |
|
684 |
// Define shapes of views. |
685 |
DataArrayView::ShapeType sourceShape; |
686 |
DataArrayView::ShapeType targetShape; |
687 |
targetShape.push_back(11); |
688 |
targetShape.push_back(8); |
689 |
targetShape.push_back(9); |
690 |
|
691 |
// Create source and target views. |
692 |
DataArray source(sourceShape); |
693 |
DataArrayView sourceView=source.getView(); |
694 |
sourceView()=5; |
695 |
|
696 |
DataArray target(targetShape); |
697 |
DataArrayView targetView=target.getView(); |
698 |
|
699 |
// Copy source view to target view. |
700 |
targetView.copySliceFrom(sourceView,region); |
701 |
|
702 |
// Check results of copy. |
703 |
for (int i=region[0].first;i<region[0].second;i++) { |
704 |
for (int j=region[1].first;j<region[1].second;j++) { |
705 |
for (int k=region[2].first;k<region[2].second;k++) { |
706 |
assert(sourceView()== |
707 |
targetView(i,j,k)); |
708 |
} |
709 |
} |
710 |
} |
711 |
} |
712 |
|
713 |
{ |
714 |
cout << endl; |
715 |
cout << "\tSlice a 4 dimensional slice to a 4 dimensional array."; |
716 |
|
717 |
// Define slice region. |
718 |
DataArrayView::RegionType region; |
719 |
region.push_back(DataArrayView::RegionType::value_type(2,4)); |
720 |
region.push_back(DataArrayView::RegionType::value_type(0,2)); |
721 |
region.push_back(DataArrayView::RegionType::value_type(5,9)); |
722 |
region.push_back(DataArrayView::RegionType::value_type(3,5)); |
723 |
|
724 |
// Define shapes of views. |
725 |
DataArrayView::ShapeType sourceShape; |
726 |
sourceShape.push_back(6); |
727 |
sourceShape.push_back(3); |
728 |
sourceShape.push_back(13); |
729 |
sourceShape.push_back(9); |
730 |
DataArrayView::ShapeType targetShape = DataArrayView::getResultSliceShape(region); |
731 |
|
732 |
// Create source and target views. |
733 |
DataArray source(sourceShape); |
734 |
DataArrayView sourceView=source.getView(); |
735 |
int val=0; |
736 |
for (int i=0;i<sourceShape[0];i++) { |
737 |
for (int j=0;j<sourceShape[1];j++) { |
738 |
for (int k=0;k<sourceShape[2];k++) { |
739 |
for (int l=0;l<sourceShape[3];l++) { |
740 |
sourceView(i,j,k,l)=val++; |
741 |
} |
742 |
} |
743 |
} |
744 |
} |
745 |
|
746 |
DataArray target(targetShape); |
747 |
DataArrayView targetView=target.getView(); |
748 |
|
749 |
// Copy source view to target view. |
750 |
targetView.copySlice(sourceView,region); |
751 |
|
752 |
// Check results of copy. |
753 |
for (int i=region[0].first;i<region[0].second;i++) { |
754 |
for (int j=region[1].first;j<region[1].second;j++) { |
755 |
for (int k=region[2].first;k<region[2].second;k++) { |
756 |
for (int l=region[3].first;l<region[3].second;l++) { |
757 |
assert(sourceView(i,j,k,l)== |
758 |
targetView(i-region[0].first,j-region[1].first,k-region[2].first,l-region[3].first)); |
759 |
} |
760 |
} |
761 |
} |
762 |
} |
763 |
} |
764 |
|
765 |
{ |
766 |
cout << endl; |
767 |
cout << "\tSlice a 4 dimensional slice to a 3 dimensional array."; |
768 |
|
769 |
// Define slice region. |
770 |
DataArrayView::RegionType region; |
771 |
region.push_back(DataArrayView::RegionType::value_type(2,4)); |
772 |
region.push_back(DataArrayView::RegionType::value_type(0,2)); |
773 |
region.push_back(DataArrayView::RegionType::value_type(5,6)); |
774 |
region.push_back(DataArrayView::RegionType::value_type(3,5)); |
775 |
|
776 |
// Define shapes of views. |
777 |
DataArrayView::ShapeType sourceShape; |
778 |
sourceShape.push_back(6); |
779 |
sourceShape.push_back(3); |
780 |
sourceShape.push_back(13); |
781 |
sourceShape.push_back(9); |
782 |
DataArrayView::ShapeType targetShape; |
783 |
targetShape.push_back(2); |
784 |
targetShape.push_back(2); |
785 |
targetShape.push_back(2); |
786 |
|
787 |
// Create source and target views. |
788 |
DataArray source(sourceShape); |
789 |
DataArrayView sourceView=source.getView(); |
790 |
int val=0; |
791 |
for (int i=0;i<sourceShape[0];i++) { |
792 |
for (int j=0;j<sourceShape[1];j++) { |
793 |
for (int k=0;k<sourceShape[2];k++) { |
794 |
for (int l=0;l<sourceShape[3];l++) { |
795 |
sourceView(i,j,k,l)=val++; |
796 |
} |
797 |
} |
798 |
} |
799 |
} |
800 |
|
801 |
DataArray target(targetShape); |
802 |
DataArrayView targetView=target.getView(); |
803 |
|
804 |
// Copy source view to target view. |
805 |
targetView.copySlice(sourceView,region); |
806 |
|
807 |
// Check results of copy. |
808 |
for (int i=region[0].first;i<region[0].second;i++) { |
809 |
for (int j=region[1].first;j<region[1].second;j++) { |
810 |
for (int k=region[2].first;k<region[2].second;k++) { |
811 |
for (int l=region[3].first;l<region[3].second;l++) { |
812 |
assert(sourceView(i,j,k,l)== |
813 |
targetView(i-region[0].first,j-region[1].first,l-region[3].first)); |
814 |
} |
815 |
} |
816 |
} |
817 |
} |
818 |
} |
819 |
|
820 |
{ |
821 |
cout << endl; |
822 |
cout << "\tSlice a 4 dimensional slice to a 2 dimensional array."; |
823 |
|
824 |
// Define slice region. |
825 |
DataArrayView::RegionType region; |
826 |
region.push_back(DataArrayView::RegionType::value_type(2,4)); |
827 |
region.push_back(DataArrayView::RegionType::value_type(0,2)); |
828 |
region.push_back(DataArrayView::RegionType::value_type(5,6)); |
829 |
region.push_back(DataArrayView::RegionType::value_type(4,5)); |
830 |
|
831 |
// Define shapes of views. |
832 |
DataArrayView::ShapeType sourceShape; |
833 |
sourceShape.push_back(6); |
834 |
sourceShape.push_back(3); |
835 |
sourceShape.push_back(13); |
836 |
sourceShape.push_back(9); |
837 |
DataArrayView::ShapeType targetShape; |
838 |
targetShape.push_back(2); |
839 |
targetShape.push_back(2); |
840 |
|
841 |
// Create source and target views. |
842 |
DataArray source(sourceShape); |
843 |
DataArrayView sourceView=source.getView(); |
844 |
int val=0; |
845 |
for (int i=0;i<sourceShape[0];i++) { |
846 |
for (int j=0;j<sourceShape[1];j++) { |
847 |
for (int k=0;k<sourceShape[2];k++) { |
848 |
for (int l=0;l<sourceShape[3];l++) { |
849 |
sourceView(i,j,k,l)=val++; |
850 |
} |
851 |
} |
852 |
} |
853 |
} |
854 |
|
855 |
DataArray target(targetShape); |
856 |
DataArrayView targetView=target.getView(); |
857 |
|
858 |
// Copy source view to target view. |
859 |
targetView.copySlice(sourceView,region); |
860 |
|
861 |
// Check results of copy. |
862 |
for (int i=region[0].first;i<region[0].second;i++) { |
863 |
for (int j=region[1].first;j<region[1].second;j++) { |
864 |
for (int k=region[2].first;k<region[2].second;k++) { |
865 |
for (int l=region[3].first;l<region[3].second;l++) { |
866 |
assert(sourceView(i,j,k,l)== |
867 |
targetView(i-region[0].first,j-region[1].first)); |
868 |
} |
869 |
} |
870 |
} |
871 |
} |
872 |
} |
873 |
|
874 |
{ |
875 |
cout << endl; |
876 |
cout << "\tSlice a 4 dimensional slice to a 1 dimensional array."; |
877 |
|
878 |
// Define slice region. |
879 |
DataArrayView::RegionType region; |
880 |
region.push_back(DataArrayView::RegionType::value_type(3,4)); |
881 |
region.push_back(DataArrayView::RegionType::value_type(0,2)); |
882 |
region.push_back(DataArrayView::RegionType::value_type(5,6)); |
883 |
region.push_back(DataArrayView::RegionType::value_type(4,5)); |
884 |
|
885 |
// Define shapes of views. |
886 |
DataArrayView::ShapeType sourceShape; |
887 |
sourceShape.push_back(6); |
888 |
sourceShape.push_back(3); |
889 |
sourceShape.push_back(13); |
890 |
sourceShape.push_back(9); |
891 |
DataArrayView::ShapeType targetShape; |
892 |
targetShape.push_back(2); |
893 |
|
894 |
// Create source and target views. |
895 |
DataArray source(sourceShape); |
896 |
DataArrayView sourceView=source.getView(); |
897 |
int val=0; |
898 |
for (int i=0;i<sourceShape[0];i++) { |
899 |
for (int j=0;j<sourceShape[1];j++) { |
900 |
for (int k=0;k<sourceShape[2];k++) { |
901 |
for (int l=0;l<sourceShape[3];l++) { |
902 |
sourceView(i,j,k,l)=val++; |
903 |
} |
904 |
} |
905 |
} |
906 |
} |
907 |
|
908 |
DataArray target(targetShape); |
909 |
DataArrayView targetView=target.getView(); |
910 |
|
911 |
// Copy source view to target view. |
912 |
targetView.copySlice(sourceView,region); |
913 |
|
914 |
// Check results of copy. |
915 |
for (int i=region[0].first;i<region[0].second;i++) { |
916 |
for (int j=region[1].first;j<region[1].second;j++) { |
917 |
for (int k=region[2].first;k<region[2].second;k++) { |
918 |
for (int l=region[3].first;l<region[3].second;l++) { |
919 |
assert(sourceView(i,j,k,l)== |
920 |
targetView(j-region[1].first)); |
921 |
} |
922 |
} |
923 |
} |
924 |
} |
925 |
} |
926 |
|
927 |
{ |
928 |
cout << endl; |
929 |
cout << "\tSlice a 4 dimensional slice to a scalar."; |
930 |
|
931 |
// Define slice region. |
932 |
DataArrayView::RegionType region; |
933 |
region.push_back(DataArrayView::RegionType::value_type(3,4)); |
934 |
region.push_back(DataArrayView::RegionType::value_type(1,2)); |
935 |
region.push_back(DataArrayView::RegionType::value_type(5,6)); |
936 |
region.push_back(DataArrayView::RegionType::value_type(4,5)); |
937 |
|
938 |
// Define shapes of views. |
939 |
DataArrayView::ShapeType sourceShape; |
940 |
sourceShape.push_back(6); |
941 |
sourceShape.push_back(3); |
942 |
sourceShape.push_back(13); |
943 |
sourceShape.push_back(9); |
944 |
DataArrayView::ShapeType targetShape; |
945 |
|
946 |
// Create source and target views. |
947 |
DataArray source(sourceShape); |
948 |
DataArrayView sourceView=source.getView(); |
949 |
int val=0; |
950 |
for (int i=0;i<sourceShape[0];i++) { |
951 |
for (int j=0;j<sourceShape[1];j++) { |
952 |
for (int k=0;k<sourceShape[2];k++) { |
953 |
for (int l=0;l<sourceShape[3];l++) { |
954 |
sourceView(i,j,k,l)=val++; |
955 |
} |
956 |
} |
957 |
} |
958 |
} |
959 |
|
960 |
DataArray target(targetShape); |
961 |
DataArrayView targetView=target.getView(); |
962 |
|
963 |
// Copy source view to target view. |
964 |
targetView.copySlice(sourceView,region); |
965 |
|
966 |
// Check results of copy. |
967 |
for (int i=region[0].first;i<region[0].second;i++) { |
968 |
for (int j=region[1].first;j<region[1].second;j++) { |
969 |
for (int k=region[2].first;k<region[2].second;k++) { |
970 |
for (int l=region[3].first;l<region[3].second;l++) { |
971 |
assert(sourceView(i,j,k,l)== |
972 |
targetView()); |
973 |
} |
974 |
} |
975 |
} |
976 |
} |
977 |
} |
978 |
|
979 |
{ |
980 |
cout << endl; |
981 |
cout << "\tSlice a 4 dimensional slice from a 4 dimensional array."; |
982 |
|
983 |
// Define slice region. |
984 |
DataArrayView::RegionType region; |
985 |
region.push_back(DataArrayView::RegionType::value_type(14,37)); |
986 |
region.push_back(DataArrayView::RegionType::value_type(22,57)); |
987 |
region.push_back(DataArrayView::RegionType::value_type(63,71)); |
988 |
region.push_back(DataArrayView::RegionType::value_type(23,51)); |
989 |
|
990 |
// Define shapes of views. |
991 |
DataArrayView::ShapeType sourceShape = DataArrayView::getResultSliceShape(region); |
992 |
DataArrayView::ShapeType targetShape; |
993 |
targetShape.push_back(50); |
994 |
targetShape.push_back(65); |
995 |
targetShape.push_back(80); |
996 |
targetShape.push_back(90); |
997 |
|
998 |
// Create source and target views. |
999 |
DataArray source(sourceShape); |
1000 |
DataArrayView sourceView=source.getView(); |
1001 |
int val=0; |
1002 |
for (int i=0;i<sourceShape[0];i++) { |
1003 |
for (int j=0;j<sourceShape[1];j++) { |
1004 |
for (int k=0;k<sourceShape[2];k++) { |
1005 |
for (int l=0;l<sourceShape[3];l++) { |
1006 |
sourceView(i,j,k,l)=val++; |
1007 |
} |
1008 |
} |
1009 |
} |
1010 |
} |
1011 |
|
1012 |
DataArray target(targetShape); |
1013 |
DataArrayView targetView=target.getView(); |
1014 |
|
1015 |
// Copy source view to target view. |
1016 |
targetView.copySliceFrom(sourceView,region); |
1017 |
|
1018 |
// Check results of copy. |
1019 |
for (int i=region[0].first;i<region[0].second;i++) { |
1020 |
for (int j=region[1].first;j<region[1].second;j++) { |
1021 |
for (int k=region[2].first;k<region[2].second;k++) { |
1022 |
for (int l=region[3].first;l<region[3].second;l++) { |
1023 |
assert(sourceView(i-region[0].first,j-region[1].first,k-region[2].first,l-region[3].first)== |
1024 |
targetView(i,j,k,l)); |
1025 |
} |
1026 |
} |
1027 |
} |
1028 |
} |
1029 |
} |
1030 |
|
1031 |
{ |
1032 |
cout << endl; |
1033 |
cout << "\tSlice a 4 dimensional slice from a scalar."; |
1034 |
|
1035 |
// Define slice region. |
1036 |
DataArrayView::RegionType region; |
1037 |
region.push_back(DataArrayView::RegionType::value_type(14,37)); |
1038 |
region.push_back(DataArrayView::RegionType::value_type(22,57)); |
1039 |
region.push_back(DataArrayView::RegionType::value_type(63,71)); |
1040 |
region.push_back(DataArrayView::RegionType::value_type(23,51)); |
1041 |
|
1042 |
// Define shapes of views. |
1043 |
DataArrayView::ShapeType sourceShape; |
1044 |
DataArrayView::ShapeType targetShape; |
1045 |
targetShape.push_back(50); |
1046 |
targetShape.push_back(65); |
1047 |
targetShape.push_back(80); |
1048 |
targetShape.push_back(90); |
1049 |
|
1050 |
// Create source and target views. |
1051 |
DataArray source(sourceShape); |
1052 |
DataArrayView sourceView=source.getView(); |
1053 |
sourceView()=5; |
1054 |
|
1055 |
DataArray target(targetShape); |
1056 |
DataArrayView targetView=target.getView(); |
1057 |
|
1058 |
// Copy source view to target view. |
1059 |
targetView.copySliceFrom(sourceView,region); |
1060 |
|
1061 |
// Check results of copy. |
1062 |
for (int i=region[0].first;i<region[0].second;i++) { |
1063 |
for (int j=region[1].first;j<region[1].second;j++) { |
1064 |
for (int k=region[2].first;k<region[2].second;k++) { |
1065 |
for (int l=region[3].first;l<region[3].second;l++) { |
1066 |
assert(sourceView()== |
1067 |
targetView(i,j,k,l)); |
1068 |
} |
1069 |
} |
1070 |
} |
1071 |
} |
1072 |
} |
1073 |
|
1074 |
cout << endl; |
1075 |
|
1076 |
} |
1077 |
|
1078 |
void DataArrayViewTestCase::testShapeToString() { |
1079 |
|
1080 |
cout << endl; |
1081 |
cout << "\tTest shapeToString for a variety of shapes." << endl; |
1082 |
|
1083 |
DataArrayView::ShapeType shape; |
1084 |
assert(DataArrayView::shapeToString(shape)=="()"); |
1085 |
shape.push_back(5); |
1086 |
assert(DataArrayView::shapeToString(shape)=="(5)"); |
1087 |
shape.push_back(2); |
1088 |
assert(DataArrayView::shapeToString(shape)=="(5,2)"); |
1089 |
shape.push_back(9); |
1090 |
assert(DataArrayView::shapeToString(shape)=="(5,2,9)"); |
1091 |
shape.push_back(4); |
1092 |
assert(DataArrayView::shapeToString(shape)=="(5,2,9,4)"); |
1093 |
|
1094 |
} |
1095 |
|
1096 |
void DataArrayViewTestCase::testScalarView() { |
1097 |
|
1098 |
cout << endl; |
1099 |
cout << "\tTest functionality of scalar views." << endl; |
1100 |
|
1101 |
// Create a vector containing enough data for three scalars |
1102 |
// and check three scalar views return the appropriate data |
1103 |
DataArrayView::ShapeType vShape; |
1104 |
DataArrayView::ValueType vData(3); |
1105 |
vData[0]=0; |
1106 |
vData[1]=1; |
1107 |
vData[2]=2; |
1108 |
|
1109 |
// create the three scalar views |
1110 |
DataArrayView zView(vData,vShape,0); |
1111 |
DataArrayView oView(vData,vShape,1); |
1112 |
DataArrayView tView(vData,vShape,2); |
1113 |
|
1114 |
// check attributes of the three scalar views |
1115 |
assert(zView()==0); |
1116 |
assert(oView()==1); |
1117 |
assert(tView()==2); |
1118 |
assert(zView.noValues()==1); |
1119 |
assert(oView.noValues()==1); |
1120 |
assert(tView.noValues()==1); |
1121 |
assert(zView.getRank()==0); |
1122 |
assert(oView.getRank()==0); |
1123 |
assert(tView.getRank()==0); |
1124 |
|
1125 |
// copy the one view to the zero view |
1126 |
zView.copy(oView); |
1127 |
assert(zView==oView); |
1128 |
zView.checkShape(oView.getShape()); |
1129 |
|
1130 |
// create a single vector view of all the data |
1131 |
vShape.push_back(3); |
1132 |
DataArrayView oneVView(vData,vShape,0); |
1133 |
|
1134 |
// test shape mismatch functions |
1135 |
if (!zView.checkShape(oneVView.getShape())) { |
1136 |
assert(true); |
1137 |
} else { |
1138 |
assert(false); |
1139 |
} |
1140 |
|
1141 |
// test some unary ops |
1142 |
zView.unaryOp(negate<double>()); |
1143 |
assert(zView()==-1); |
1144 |
zView.binaryOp(oView,plus<double>()); |
1145 |
assert(zView()==0); |
1146 |
|
1147 |
// test operator != |
1148 |
assert(zView!=oView); |
1149 |
|
1150 |
} |
1151 |
|
1152 |
void DataArrayViewTestCase::testAll() |
1153 |
{ |
1154 |
|
1155 |
{ |
1156 |
cout << endl; |
1157 |
cout << "\tTest empty DataArrayView."; |
1158 |
|
1159 |
// default constructor |
1160 |
DataArrayView defView; |
1161 |
|
1162 |
// check all attributes |
1163 |
assert(defView.isEmpty()); |
1164 |
assert(defView.getOffset()==0); |
1165 |
assert(defView.getRank()==0); |
1166 |
assert(defView.noValues()==0); |
1167 |
assert(defView.getShape().empty()); |
1168 |
assert(defView.checkShape(DataArrayView::ShapeType())); |
1169 |
} |
1170 |
|
1171 |
{ |
1172 |
cout << endl; |
1173 |
cout << "\tTest DataArrayView - shape (5)."; |
1174 |
|
1175 |
// define the shape for the DataArrayView |
1176 |
DataArrayView::ShapeType shape; |
1177 |
shape.push_back(5); |
1178 |
|
1179 |
// allocate the data for the DataArrayView |
1180 |
DataArrayView::ValueType data(DataArrayView::noValues(shape),0); |
1181 |
|
1182 |
// constructor |
1183 |
int offset=0; |
1184 |
DataArrayView dataView(data,shape,offset); |
1185 |
|
1186 |
// check all attributes |
1187 |
assert(!dataView.isEmpty()); |
1188 |
assert(dataView.getOffset()==0); |
1189 |
assert(dataView.getRank()==1); |
1190 |
assert(dataView.noValues()==5); |
1191 |
assert(dataView.getShape()==shape); |
1192 |
assert(dataView.checkShape(shape)); |
1193 |
|
1194 |
// assign values to the data |
1195 |
for (int i=0;i<shape[0];i++) { |
1196 |
dataView(i)=dataView.index(i); |
1197 |
assert(dataView(i)==i); |
1198 |
} |
1199 |
|
1200 |
} |
1201 |
|
1202 |
{ |
1203 |
cout << endl; |
1204 |
cout << "\tTest DataArrayView - shape (2,3)."; |
1205 |
|
1206 |
// define the shape for the DataArrayView |
1207 |
DataArrayView::ShapeType shape; |
1208 |
shape.push_back(2); |
1209 |
shape.push_back(3); |
1210 |
|
1211 |
// allocate the data for the DataArrayView |
1212 |
int npoints=4; |
1213 |
DataArrayView::ValueType data(DataArrayView::noValues(shape)*npoints,0); |
1214 |
|
1215 |
// constructor |
1216 |
DataArrayView dataView(data,shape); |
1217 |
|
1218 |
// check all attributes |
1219 |
assert(!dataView.isEmpty()); |
1220 |
assert(dataView.getOffset()==0); |
1221 |
assert(dataView.getRank()==2); |
1222 |
assert(dataView.noValues()==6); |
1223 |
assert(dataView.getShape()==shape); |
1224 |
assert(dataView.checkShape(shape)); |
1225 |
|
1226 |
// step the view along each block of this shape in the underlying data |
1227 |
for (int p=0;p<npoints;p++) { |
1228 |
|
1229 |
assert(dataView.getOffset()==p*dataView.noValues()); |
1230 |
|
1231 |
// assign values to the data |
1232 |
for (int i=0;i<shape[0];i++) { |
1233 |
for (int j=0;j<shape[1];j++) { |
1234 |
dataView(i,j)=dataView.index(i,j); |
1235 |
assert(dataView(i,j)==dataView.index(i,j)); |
1236 |
} |
1237 |
} |
1238 |
|
1239 |
if (p<npoints-1) { |
1240 |
dataView.incrOffset(); |
1241 |
} |
1242 |
|
1243 |
} |
1244 |
|
1245 |
} |
1246 |
|
1247 |
{ |
1248 |
cout << endl; |
1249 |
cout << "\tTest DataArrayView - shape (4,7,9)."; |
1250 |
|
1251 |
// define the shape for the DataArrayView |
1252 |
DataArrayView::ShapeType shape; |
1253 |
shape.push_back(4); |
1254 |
shape.push_back(7); |
1255 |
shape.push_back(9); |
1256 |
|
1257 |
// allocate the data for the DataArrayView |
1258 |
int npoints=10; |
1259 |
DataArrayView::ValueType data(DataArrayView::noValues(shape)*npoints,0); |
1260 |
|
1261 |
// constructor |
1262 |
DataArrayView dataView(data,shape); |
1263 |
|
1264 |
// check all attributes |
1265 |
assert(!dataView.isEmpty()); |
1266 |
assert(dataView.getOffset()==0); |
1267 |
assert(dataView.getRank()==3); |
1268 |
assert(dataView.noValues()==252); |
1269 |
assert(dataView.getShape()==shape); |
1270 |
assert(dataView.checkShape(shape)); |
1271 |
|
1272 |
// step the view along each block of this shape in the underlying data |
1273 |
for (int p=0;p<npoints;p++) { |
1274 |
|
1275 |
assert(dataView.getOffset()==p*dataView.noValues()); |
1276 |
|
1277 |
// assign values to the data |
1278 |
for (int i=0;i<shape[0];i++) { |
1279 |
for (int j=0;j<shape[1];j++) { |
1280 |
for (int k=0;k<shape[2];k++) { |
1281 |
dataView(i,j,k)=dataView.index(i,j,k); |
1282 |
assert(dataView(i,j,k)==dataView.index(i,j,k)); |
1283 |
} |
1284 |
} |
1285 |
} |
1286 |
|
1287 |
if (p<npoints-1) { |
1288 |
dataView.incrOffset(); |
1289 |
} |
1290 |
|
1291 |
} |
1292 |
|
1293 |
} |
1294 |
|
1295 |
{ |
1296 |
cout << endl; |
1297 |
cout << "\tTest DataArrayView - shape (12,4,5,14)."; |
1298 |
|
1299 |
// define the shape for the DataArrayView |
1300 |
DataArrayView::ShapeType shape; |
1301 |
shape.push_back(12); |
1302 |
shape.push_back(4); |
1303 |
shape.push_back(5); |
1304 |
shape.push_back(14); |
1305 |
|
1306 |
// allocate the data for the DataArrayView |
1307 |
int npoints=100; |
1308 |
DataArrayView::ValueType data(DataArrayView::noValues(shape)*npoints,0); |
1309 |
|
1310 |
// constructor |
1311 |
DataArrayView dataView(data,shape); |
1312 |
|
1313 |
// check all attributes |
1314 |
assert(!dataView.isEmpty()); |
1315 |
assert(dataView.getOffset()==0); |
1316 |
assert(dataView.getRank()==4); |
1317 |
assert(dataView.noValues()==3360); |
1318 |
assert(dataView.getShape()==shape); |
1319 |
assert(dataView.checkShape(shape)); |
1320 |
|
1321 |
// step the view along each block of this shape in the underlying data |
1322 |
for (int p=0;p<npoints;p++) { |
1323 |
|
1324 |
assert(dataView.getOffset()==p*dataView.noValues()); |
1325 |
|
1326 |
// assign values to the data |
1327 |
for (int i=0;i<shape[0];i++) { |
1328 |
for (int j=0;j<shape[1];j++) { |
1329 |
for (int k=0;k<shape[2];k++) { |
1330 |
for (int l=0;l<shape[3];l++) { |
1331 |
dataView(i,j,k,l)=dataView.index(i,j,k,l); |
1332 |
assert(dataView(i,j,k,l)==dataView.index(i,j,k,l)); |
1333 |
} |
1334 |
} |
1335 |
} |
1336 |
} |
1337 |
|
1338 |
if (p<npoints-1) { |
1339 |
dataView.incrOffset(); |
1340 |
} |
1341 |
|
1342 |
} |
1343 |
|
1344 |
} |
1345 |
|
1346 |
{ |
1347 |
cout << endl; |
1348 |
cout << "\tTest DataArrayView copy constructor - shape (5)."; |
1349 |
|
1350 |
// define the shape for the DataArrayView |
1351 |
DataArrayView::ShapeType shape; |
1352 |
shape.push_back(5); |
1353 |
|
1354 |
// allocate the data for the DataArrayView |
1355 |
DataArrayView::ValueType data(DataArrayView::noValues(shape),0); |
1356 |
|
1357 |
// constructor |
1358 |
DataArrayView dataView(data,shape); |
1359 |
|
1360 |
// copy constructor |
1361 |
DataArrayView dataViewCopy(dataView); |
1362 |
|
1363 |
// check all attributes |
1364 |
assert(!dataViewCopy.isEmpty()); |
1365 |
assert(dataViewCopy.getOffset()==0); |
1366 |
assert(dataViewCopy.getRank()==1); |
1367 |
assert(dataViewCopy.noValues()==5); |
1368 |
assert(dataViewCopy.getShape()==shape); |
1369 |
assert(dataViewCopy.checkShape(shape)); |
1370 |
|
1371 |
// check data |
1372 |
assert(dataView.getData()==dataViewCopy.getData()); |
1373 |
for (int i=0;i<dataView.getData().size();i++) { |
1374 |
assert(dataView.getData(i)==dataViewCopy.getData(i)); |
1375 |
} |
1376 |
|
1377 |
} |
1378 |
|
1379 |
{ |
1380 |
cout << endl; |
1381 |
cout << "\tTest DataArrayView copy constructor - shape (5,6,7)."; |
1382 |
|
1383 |
// define the shape for the DataArrayView |
1384 |
DataArrayView::ShapeType shape; |
1385 |
shape.push_back(5); |
1386 |
shape.push_back(6); |
1387 |
shape.push_back(7); |
1388 |
|
1389 |
// allocate the data for the DataArrayView |
1390 |
DataArrayView::ValueType data(DataArrayView::noValues(shape),0); |
1391 |
|
1392 |
// constructor |
1393 |
DataArrayView dataView(data,shape); |
1394 |
|
1395 |
// copy constructor |
1396 |
DataArrayView dataViewCopy(dataView); |
1397 |
|
1398 |
// check all attributes |
1399 |
assert(!dataViewCopy.isEmpty()); |
1400 |
assert(dataViewCopy.getOffset()==0); |
1401 |
assert(dataViewCopy.getRank()==3); |
1402 |
assert(dataViewCopy.noValues()==210); |
1403 |
assert(dataViewCopy.getShape()==shape); |
1404 |
assert(dataViewCopy.checkShape(shape)); |
1405 |
|
1406 |
// check data |
1407 |
assert(dataView.getData()==dataViewCopy.getData()); |
1408 |
for (int i=0;i<dataView.getData().size();i++) { |
1409 |
assert(dataView.getData(i)==dataViewCopy.getData(i)); |
1410 |
} |
1411 |
|
1412 |
} |
1413 |
|
1414 |
{ |
1415 |
cout << endl; |
1416 |
cout << "\tTest DataArrayView copy method - shape (2,3)."; |
1417 |
|
1418 |
// define the shape for the DataArrayViews |
1419 |
DataArrayView::ShapeType shape; |
1420 |
shape.push_back(2); |
1421 |
shape.push_back(3); |
1422 |
|
1423 |
// allocate the data for the DataArrayViews |
1424 |
int npoints=4; |
1425 |
DataArrayView::ValueType data1(DataArrayView::noValues(shape)*npoints,0); |
1426 |
DataArrayView::ValueType data2(DataArrayView::noValues(shape)*npoints,0); |
1427 |
|
1428 |
// construct two views |
1429 |
DataArrayView dataView1(data1,shape); |
1430 |
DataArrayView dataView2(data2,shape); |
1431 |
|
1432 |
// step the views along each block of this shape in the underlying data arrays |
1433 |
for (int p=0;p<npoints;p++) { |
1434 |
|
1435 |
// assign values to the data underlying the first view |
1436 |
for (int i=0;i<shape[0];i++) { |
1437 |
for (int j=0;j<shape[1];j++) { |
1438 |
dataView1(i,j)=dataView1.index(i,j); |
1439 |
} |
1440 |
} |
1441 |
|
1442 |
// copy data from the first view to the second |
1443 |
dataView2.copy(dataView1); |
1444 |
|
1445 |
// check the data underlying the second view |
1446 |
for (int i=0;i<shape[0];i++) { |
1447 |
for (int j=0;j<shape[1];j++) { |
1448 |
assert(dataView2(i,j)==dataView1.index(i,j)); |
1449 |
} |
1450 |
} |
1451 |
|
1452 |
if (p<npoints-1) { |
1453 |
dataView1.incrOffset(); |
1454 |
dataView2.incrOffset(); |
1455 |
} |
1456 |
|
1457 |
} |
1458 |
|
1459 |
} |
1460 |
|
1461 |
{ |
1462 |
cout << endl; |
1463 |
cout << "\tTest DataArrayView copy method - shape (2,3,8,9)."; |
1464 |
|
1465 |
// define the shape for the DataArrayViews |
1466 |
DataArrayView::ShapeType shape; |
1467 |
shape.push_back(2); |
1468 |
shape.push_back(3); |
1469 |
shape.push_back(8); |
1470 |
shape.push_back(9); |
1471 |
|
1472 |
// allocate the data for the DataArrayViews |
1473 |
int npoints=10; |
1474 |
DataArrayView::ValueType data1(DataArrayView::noValues(shape)*npoints,0); |
1475 |
DataArrayView::ValueType data2(DataArrayView::noValues(shape)*npoints,0); |
1476 |
|
1477 |
// construct two views |
1478 |
DataArrayView dataView1(data1,shape); |
1479 |
DataArrayView dataView2(data2,shape); |
1480 |
|
1481 |
// step the views along each block of this shape in the underlying data arrays |
1482 |
for (int p=0;p<npoints;p++) { |
1483 |
|
1484 |
// assign values to the data underlying the first view |
1485 |
for (int i=0;i<shape[0];i++) { |
1486 |
for (int j=0;j<shape[1];j++) { |
1487 |
for (int k=0;k<shape[2];k++) { |
1488 |
for (int l=0;l<shape[3];l++) { |
1489 |
dataView1(i,j,k,l)=dataView1.index(i,j,k,l); |
1490 |
} |
1491 |
} |
1492 |
} |
1493 |
} |
1494 |
|
1495 |
// copy data from the first view to the second |
1496 |
dataView2.copy(dataView1); |
1497 |
|
1498 |
// check the data underlying the second view |
1499 |
for (int i=0;i<shape[0];i++) { |
1500 |
for (int j=0;j<shape[1];j++) { |
1501 |
for (int k=0;k<shape[2];k++) { |
1502 |
for (int l=0;l<shape[3];l++) { |
1503 |
assert(dataView2(i,j,k,l)==dataView1.index(i,j,k,l)); |
1504 |
} |
1505 |
} |
1506 |
} |
1507 |
} |
1508 |
|
1509 |
if (p<npoints-1) { |
1510 |
dataView1.incrOffset(); |
1511 |
dataView2.incrOffset(); |
1512 |
} |
1513 |
|
1514 |
} |
1515 |
|
1516 |
} |
1517 |
|
1518 |
{ |
1519 |
cout << endl; |
1520 |
cout << "\tTest DataArrayView copy with offset method - shape (2,3)."; |
1521 |
|
1522 |
// define the shape for the DataArrayViews |
1523 |
DataArrayView::ShapeType shape; |
1524 |
shape.push_back(2); |
1525 |
shape.push_back(3); |
1526 |
|
1527 |
// allocate the data for the DataArrayViews |
1528 |
int npoints=4; |
1529 |
DataArrayView::ValueType data1(DataArrayView::noValues(shape)*npoints,0); |
1530 |
DataArrayView::ValueType data2(DataArrayView::noValues(shape)*npoints,0); |
1531 |
|
1532 |
// construct two views |
1533 |
DataArrayView dataView1(data1,shape); |
1534 |
DataArrayView dataView2(data2,shape); |
1535 |
|
1536 |
// assign values to the data underlying the first view |
1537 |
for (int i=0;i<shape[0];i++) { |
1538 |
for (int j=0;j<shape[1];j++) { |
1539 |
dataView1(i,j)=dataView1.index(i,j); |
1540 |
} |
1541 |
} |
1542 |
|
1543 |
// copy data from the first view to the second at an offset |
1544 |
dataView2.copy(12,dataView1,0); |
1545 |
|
1546 |
// check the data underlying the second view |
1547 |
dataView2.setOffset(12); |
1548 |
for (int i=0;i<shape[0];i++) { |
1549 |
for (int j=0;j<shape[1];j++) { |
1550 |
assert(dataView2(i,j)==dataView1.index(i,j)); |
1551 |
} |
1552 |
} |
1553 |
|
1554 |
} |
1555 |
|
1556 |
{ |
1557 |
cout << endl; |
1558 |
cout << "\tTest DataArrayView copy with value method - shape (5,8)."; |
1559 |
|
1560 |
// define the shape for the DataArrayView |
1561 |
DataArrayView::ShapeType shape; |
1562 |
shape.push_back(5); |
1563 |
shape.push_back(8); |
1564 |
|
1565 |
// allocate the data for the DataArrayView |
1566 |
int npoints=4; |
1567 |
DataArrayView::ValueType data(DataArrayView::noValues(shape)*npoints,0); |
1568 |
|
1569 |
// construct view |
1570 |
DataArrayView dataView(data,shape); |
1571 |
|
1572 |
// copy a value to the view at an offset |
1573 |
dataView.copy(80,5); |
1574 |
|
1575 |
// check the data underlying the second view |
1576 |
dataView.setOffset(80); |
1577 |
for (int i=0;i<shape[0];i++) { |
1578 |
for (int j=0;j<shape[1];j++) { |
1579 |
assert(dataView(i,j)==5); |
1580 |
} |
1581 |
} |
1582 |
|
1583 |
} |
1584 |
|
1585 |
#if defined DOASSERT |
1586 |
{ |
1587 |
cout << endl; |
1588 |
cout << "\tTest too many indices for shape exception."; |
1589 |
|
1590 |
DataArrayView::ShapeType shape; |
1591 |
DataArrayView::ValueType data(DataArrayView::noValues(shape),0); |
1592 |
DataArrayView dataView(data,shape); |
1593 |
|
1594 |
// Should be a scalar |
1595 |
dataView()=1; |
1596 |
|
1597 |
try { |
1598 |
dataView(1)=1; |
1599 |
assert(false); |
1600 |
} |
1601 |
catch (EsysException& e) { |
1602 |
assert(true); |
1603 |
} |
1604 |
|
1605 |
try { |
1606 |
dataView(1,1)=1; |
1607 |
assert(false); |
1608 |
} |
1609 |
catch (EsysException& e) { |
1610 |
assert(true); |
1611 |
} |
1612 |
|
1613 |
try { |
1614 |
dataView(1,1,1)=1; |
1615 |
assert(false); |
1616 |
} |
1617 |
catch (EsysException& e) { |
1618 |
assert(true); |
1619 |
} |
1620 |
|
1621 |
try { |
1622 |
dataView(1,1,1,1)=1; |
1623 |
assert(false); |
1624 |
} |
1625 |
catch (EsysException& e) { |
1626 |
assert(true); |
1627 |
} |
1628 |
|
1629 |
} |
1630 |
#endif |
1631 |
|
1632 |
#if defined DOASSERT |
1633 |
{ |
1634 |
cout << endl; |
1635 |
cout << "\tTest invalid index exception."; |
1636 |
|
1637 |
DataArrayView::ShapeType shape; |
1638 |
shape.push_back(4); |
1639 |
DataArrayView::ValueType data(DataArrayView::noValues(shape),0); |
1640 |
DataArrayView dataView(data,shape); |
1641 |
|
1642 |
try { |
1643 |
dataView(4000)=1; |
1644 |
assert(false); |
1645 |
} |
1646 |
catch (EsysException& e) { |
1647 |
assert(true); |
1648 |
} |
1649 |
} |
1650 |
#endif |
1651 |
|
1652 |
{ |
1653 |
cout << endl; |
1654 |
cout << "\tTest insufficient data exception."; |
1655 |
|
1656 |
DataArrayView::ShapeType shape; |
1657 |
DataArrayView::ValueType data; |
1658 |
|
1659 |
try { |
1660 |
DataArrayView dataView(data,shape); |
1661 |
assert(false); |
1662 |
} |
1663 |
catch (EsysException& e) { |
1664 |
assert(true); |
1665 |
} |
1666 |
} |
1667 |
|
1668 |
cout << endl; |
1669 |
|
1670 |
} |
1671 |
|
1672 |
void DataArrayViewTestCase::testMatMult() |
1673 |
{ |
1674 |
|
1675 |
{ |
1676 |
cout << endl; |
1677 |
cout << "\tTest result shape." << endl; |
1678 |
|
1679 |
DataArrayView::ShapeType leftShape; |
1680 |
leftShape.push_back(1); |
1681 |
leftShape.push_back(3); |
1682 |
DataArrayView::ValueType leftData(DataArrayView::noValues(leftShape),0); |
1683 |
DataArrayView leftDataView(leftData,leftShape); |
1684 |
|
1685 |
DataArrayView::ShapeType rightShape; |
1686 |
rightShape.push_back(3); |
1687 |
rightShape.push_back(2); |
1688 |
DataArrayView::ValueType rightData(DataArrayView::noValues(rightShape),0); |
1689 |
DataArrayView rightDataView(rightData,rightShape); |
1690 |
|
1691 |
DataArrayView::ShapeType resultShape=DataArrayView::determineResultShape(leftDataView,rightDataView); |
1692 |
|
1693 |
assert(resultShape.size()==2); |
1694 |
assert(resultShape[0]==1); |
1695 |
assert(resultShape[1]==2); |
1696 |
|
1697 |
DataArrayView::ValueType resultData(DataArrayView::noValues(resultShape),0); |
1698 |
DataArrayView resultDataView(resultData,resultShape); |
1699 |
|
1700 |
cout << "\tTest matrix multiplication."; |
1701 |
double aValue=0.0; |
1702 |
for (int i=0;i<leftShape[0];i++) { |
1703 |
for (int j=0;j<leftShape[1];j++) { |
1704 |
leftDataView(i,j)=++aValue; |
1705 |
} |
1706 |
} |
1707 |
aValue=0.0; |
1708 |
for (int i=0;i<rightShape[0];i++) { |
1709 |
for (int j=0;j<rightShape[1];j++) { |
1710 |
rightDataView(i,j)=++aValue; |
1711 |
} |
1712 |
} |
1713 |
|
1714 |
DataArrayView::matMult(leftDataView,rightDataView,resultDataView); |
1715 |
} |
1716 |
|
1717 |
cout << endl; |
1718 |
|
1719 |
} |
1720 |
|
1721 |
void DataArrayViewTestCase::testUnaryOp() |
1722 |
{ |
1723 |
|
1724 |
// This typedef allows function names to be cast to pointers |
1725 |
// to unary functions of the appropriate type. |
1726 |
typedef double (*UnaryDFunPtr)(double); |
1727 |
|
1728 |
{ |
1729 |
cout << endl; |
1730 |
cout << "\tTest unaryOp on scalar DataArrayView."; |
1731 |
|
1732 |
// define the shape for the DataArrayView |
1733 |
DataArrayView::ShapeType shape; |
1734 |
|
1735 |
// allocate the data for the DataArrayView |
1736 |
int npoints=4; |
1737 |
DataArrayView::ValueType data(DataArrayView::noValues(shape)*npoints,0); |
1738 |
|
1739 |
// constructor |
1740 |
DataArrayView dataView(data,shape); |
1741 |
|
1742 |
double tmp; |
1743 |
// step the view along each data point in the underlying data |
1744 |
for (int p=0;p<npoints;p++) { |
1745 |
|
1746 |
// assign values to the data point |
1747 |
dataView()=p; |
1748 |
|
1749 |
// apply a unary operation to this data point |
1750 |
dataView.unaryOp((UnaryDFunPtr)std::sin); |
1751 |
|
1752 |
// check the results |
1753 |
tmp = std::sin((double)p); |
1754 |
assert(std::abs(dataView()-tmp)<=REL_TOL*std::abs(tmp)); |
1755 |
|
1756 |
if (p<npoints-1) { |
1757 |
dataView.incrOffset(); |
1758 |
} |
1759 |
|
1760 |
} |
1761 |
|
1762 |
} |
1763 |
|
1764 |
{ |
1765 |
cout << endl; |
1766 |
cout << "\tTest unaryOp on shape (2,3) DataArrayView."; |
1767 |
|
1768 |
// define the shape for the DataArrayView |
1769 |
DataArrayView::ShapeType shape; |
1770 |
shape.push_back(2); |
1771 |
shape.push_back(3); |
1772 |
|
1773 |
// allocate the data for the DataArrayView |
1774 |
int npoints=4; |
1775 |
DataArrayView::ValueType data(DataArrayView::noValues(shape)*npoints,0); |
1776 |
|
1777 |
// constructor |
1778 |
DataArrayView dataView(data,shape); |
1779 |
|
1780 |
// step the view along each data point in the underlying data |
1781 |
for (int p=0;p<npoints;p++) { |
1782 |
|
1783 |
// assign values to the data point |
1784 |
for (int i=0;i<shape[0];i++) { |
1785 |
for (int j=0;j<shape[1];j++) { |
1786 |
dataView(i,j)=dataView.index(i,j); |
1787 |
} |
1788 |
} |
1789 |
|
1790 |
// apply a unary operation to this data point |
1791 |
dataView.unaryOp((UnaryDFunPtr)std::sqrt); |
1792 |
|
1793 |
// check the results |
1794 |
for (int i=0;i<shape[0];i++) { |
1795 |
for (int j=0;j<shape[1];j++) { |
1796 |
assert(std::abs(dataView(i,j)-std::sqrt((double)dataView.index(i,j)))<=REL_TOL*std::sqrt((double)dataView.index(i,j))); |
1797 |
} |
1798 |
} |
1799 |
|
1800 |
if (p<npoints-1) { |
1801 |
dataView.incrOffset(); |
1802 |
} |
1803 |
|
1804 |
} |
1805 |
|
1806 |
} |
1807 |
|
1808 |
{ |
1809 |
cout << endl; |
1810 |
cout << "\tTest unaryOp on shape (9,8,5,11) DataArrayView."; |
1811 |
|
1812 |
// define the shape for the DataArrayView |
1813 |
DataArrayView::ShapeType shape; |
1814 |
shape.push_back(9); |
1815 |
shape.push_back(8); |
1816 |
shape.push_back(5); |
1817 |
shape.push_back(11); |
1818 |
|
1819 |
// allocate the data for the DataArrayView |
1820 |
int npoints=4; |
1821 |
DataArrayView::ValueType data(DataArrayView::noValues(shape)*npoints,0); |
1822 |
|
1823 |
// constructor |
1824 |
DataArrayView dataView(data,shape); |
1825 |
|
1826 |
// step the view along each data point in the underlying data |
1827 |
for (int p=0;p<npoints;p++) { |
1828 |
|
1829 |
// assign values to the data point |
1830 |
for (int i=0;i<shape[0];i++) { |
1831 |
for (int j=0;j<shape[1];j++) { |
1832 |
for (int k=0;k<shape[2];k++) { |
1833 |
for (int l=0;l<shape[3];l++) { |
1834 |
dataView(i,j,k,l)=dataView.index(i,j,k,l)+1; |
1835 |
} |
1836 |
} |
1837 |
} |
1838 |
} |
1839 |
|
1840 |
// apply a unary operation to this data point |
1841 |
dataView.unaryOp((UnaryDFunPtr)std::log); |
1842 |
|
1843 |
// check the results |
1844 |
for (int i=0;i<shape[0];i++) { |
1845 |
for (int j=0;j<shape[1];j++) { |
1846 |
for (int k=0;k<shape[2];k++) { |
1847 |
for (int l=0;l<shape[3];l++) { |
1848 |
assert(std::abs(dataView(i,j,k,l)-std::log(1+(double)dataView.index(i,j,k,l)))<=REL_TOL*std::abs(std::log(1+(double)dataView.index(i,j,k,l)))); |
1849 |
} |
1850 |
} |
1851 |
} |
1852 |
} |
1853 |
|
1854 |
if (p<npoints-1) { |
1855 |
dataView.incrOffset(); |
1856 |
} |
1857 |
|
1858 |
} |
1859 |
|
1860 |
} |
1861 |
|
1862 |
cout << endl; |
1863 |
|
1864 |
} |
1865 |
|
1866 |
void DataArrayViewTestCase::testBinaryOp() |
1867 |
{ |
1868 |
|
1869 |
// This typedef allows function names to be cast to pointers |
1870 |
// to binary functions of the appropriate type. |
1871 |
typedef double (*BinaryDFunPtr)(double,double); |
1872 |
|
1873 |
{ |
1874 |
cout << endl; |
1875 |
cout << "\tTest binaryOp on scalar DataArrayViews."; |
1876 |
|
1877 |
// define the shape for the DataArrayViews |
1878 |
DataArrayView::ShapeType shape; |
1879 |
|
1880 |
// allocate the data for the DataArrayViews |
1881 |
int npoints=4; |
1882 |
DataArrayView::ValueType data1(DataArrayView::noValues(shape)*npoints,0); |
1883 |
DataArrayView::ValueType data2(DataArrayView::noValues(shape)*npoints,0); |
1884 |
|
1885 |
// constructor |
1886 |
DataArrayView dataView1(data1,shape); |
1887 |
DataArrayView dataView2(data2,shape); |
1888 |
|
1889 |
// step the views along each data point in the underlying data |
1890 |
for (int p=0;p<npoints;p++) { |
1891 |
|
1892 |
// assign values to the data points |
1893 |
dataView1()=p; |
1894 |
dataView2()=p; |
1895 |
|
1896 |
// apply a binary operation to these data points |
1897 |
dataView1.binaryOp(dataView2,plus<double>()); |
1898 |
|
1899 |
// check the results |
1900 |
assert(dataView1()==p+p); |
1901 |
|
1902 |
if (p<npoints-1) { |
1903 |
dataView1.incrOffset(); |
1904 |
dataView2.incrOffset(); |
1905 |
} |
1906 |
|
1907 |
} |
1908 |
|
1909 |
} |
1910 |
|
1911 |
{ |
1912 |
cout << endl; |
1913 |
cout << "\tTest binaryOp on shape (2,3) DataArrayViews."; |
1914 |
|
1915 |
// define the shape for the DataArrayViews |
1916 |
DataArrayView::ShapeType shape; |
1917 |
shape.push_back(2); |
1918 |
shape.push_back(3); |
1919 |
|
1920 |
// allocate the data for the DataArrayViews |
1921 |
int npoints=4; |
1922 |
DataArrayView::ValueType data1(DataArrayView::noValues(shape)*npoints,0); |
1923 |
DataArrayView::ValueType data2(DataArrayView::noValues(shape)*npoints,0); |
1924 |
|
1925 |
// constructor |
1926 |
DataArrayView dataView1(data1,shape); |
1927 |
DataArrayView dataView2(data2,shape); |
1928 |
|
1929 |
// step the views along each data point in the underlying data |
1930 |
for (int p=0;p<npoints;p++) { |
1931 |
|
1932 |
// assign values to the data points |
1933 |
for (int i=0;i<shape[0];i++) { |
1934 |
for (int j=0;j<shape[1];j++) { |
1935 |
dataView1(i,j)=dataView1.index(i,j); |
1936 |
dataView2(i,j)=dataView2.index(i,j); |
1937 |
} |
1938 |
} |
1939 |
|
1940 |
// apply a binary operation to these data points |
1941 |
dataView1.binaryOp(dataView2,multiplies<double>()); |
1942 |
|
1943 |
// check the results |
1944 |
for (int i=0;i<shape[0];i++) { |
1945 |
for (int j=0;j<shape[1];j++) { |
1946 |
assert(dataView1(i,j)==dataView1.index(i,j)*dataView2.index(i,j)); |
1947 |
} |
1948 |
} |
1949 |
|
1950 |
if (p<npoints-1) { |
1951 |
dataView1.incrOffset(); |
1952 |
dataView2.incrOffset(); |
1953 |
} |
1954 |
|
1955 |
} |
1956 |
|
1957 |
} |
1958 |
|
1959 |
{ |
1960 |
cout << endl; |
1961 |
cout << "\tTest binaryOp on shape (9,8,5,11) DataArrayViews."; |
1962 |
|
1963 |
// define the shape for the DataArrayViews |
1964 |
DataArrayView::ShapeType shape; |
1965 |
shape.push_back(9); |
1966 |
shape.push_back(8); |
1967 |
shape.push_back(5); |
1968 |
shape.push_back(11); |
1969 |
|
1970 |
// allocate the data for the DataArrayViews |
1971 |
int npoints=4; |
1972 |
DataArrayView::ValueType data1(DataArrayView::noValues(shape)*npoints,0); |
1973 |
DataArrayView::ValueType data2(DataArrayView::noValues(shape)*npoints,0); |
1974 |
|
1975 |
// constructor |
1976 |
DataArrayView dataView1(data1,shape); |
1977 |
DataArrayView dataView2(data2,shape); |
1978 |
|
1979 |
// step the views along each data point in the underlying data |
1980 |
for (int p=0;p<npoints;p++) { |
1981 |
|
1982 |
// assign values to the data points |
1983 |
for (int i=0;i<shape[0];i++) { |
1984 |
for (int j=0;j<shape[1];j++) { |
1985 |
for (int k=0;k<shape[2];k++) { |
1986 |
for (int l=0;l<shape[3];l++) { |
1987 |
dataView1(i,j,k,l)=dataView1.index(i,j,k,l); |
1988 |
dataView2(i,j,k,l)=dataView2.index(i,j,k,l); |
1989 |
} |
1990 |
} |
1991 |
} |
1992 |
} |
1993 |
|
1994 |
// apply a binary operation to these data points |
1995 |
dataView1.binaryOp(dataView2,multiplies<double>()); |
1996 |
|
1997 |
// check the results |
1998 |
for (int i=0;i<shape[0];i++) { |
1999 |
for (int j=0;j<shape[1];j++) { |
2000 |
for (int k=0;k<shape[2];k++) { |
2001 |
for (int l=0;l<shape[3];l++) { |
2002 |
assert(dataView1(i,j,k,l)==dataView1.index(i,j,k,l)*dataView2.index(i,j,k,l)); |
2003 |
} |
2004 |
} |
2005 |
} |
2006 |
} |
2007 |
|
2008 |
if (p<npoints-1) { |
2009 |
dataView1.incrOffset(); |
2010 |
dataView2.incrOffset(); |
2011 |
} |
2012 |
|
2013 |
} |
2014 |
|
2015 |
} |
2016 |
|
2017 |
{ |
2018 |
cout << endl; |
2019 |
cout << "\tTest binaryOp on scalar DataArrayView and single value."; |
2020 |
|
2021 |
// define the shape for the DataArrayView |
2022 |
DataArrayView::ShapeType shape; |
2023 |
|
2024 |
// allocate the data for the DataArrayView |
2025 |
int npoints=4; |
2026 |
DataArrayView::ValueType data(DataArrayView::noValues(shape)*npoints,0); |
2027 |
|
2028 |
// constructor |
2029 |
DataArrayView dataView(data,shape); |
2030 |
|
2031 |
// step the view along each data point in the underlying data |
2032 |
for (int p=0;p<npoints;p++) { |
2033 |
|
2034 |
// assign values to the data point |
2035 |
dataView()=p; |
2036 |
|
2037 |
// apply a binary operation to this data point |
2038 |
dataView.binaryOp(4.9,plus<double>()); |
2039 |
|
2040 |
// check the results |
2041 |
assert(dataView()==4.9+p); |
2042 |
|
2043 |
if (p<npoints-1) { |
2044 |
dataView.incrOffset(); |
2045 |
} |
2046 |
|
2047 |
} |
2048 |
|
2049 |
} |
2050 |
|
2051 |
{ |
2052 |
cout << endl; |
2053 |
cout << "\tTest binaryOp on shape (2,3) DataArrayView and single value."; |
2054 |
|
2055 |
// define the shape for the DataArrayView |
2056 |
DataArrayView::ShapeType shape; |
2057 |
shape.push_back(2); |
2058 |
shape.push_back(3); |
2059 |
|
2060 |
// allocate the data for the DataArrayView |
2061 |
int npoints=4; |
2062 |
DataArrayView::ValueType data(DataArrayView::noValues(shape)*npoints,0); |
2063 |
|
2064 |
// constructor |
2065 |
DataArrayView dataView(data,shape); |
2066 |
|
2067 |
// step the view along each data point in the underlying data |
2068 |
for (int p=0;p<npoints;p++) { |
2069 |
|
2070 |
// assign values to the data point |
2071 |
for (int i=0;i<shape[0];i++) { |
2072 |
for (int j=0;j<shape[1];j++) { |
2073 |
dataView(i,j)=dataView.index(i,j); |
2074 |
} |
2075 |
} |
2076 |
|
2077 |
// apply a binary operation to the data point |
2078 |
dataView.binaryOp(5.8,multiplies<double>()); |
2079 |
|
2080 |
double tmp; |
2081 |
// check the results |
2082 |
for (int i=0;i<shape[0];i++) { |
2083 |
for (int j=0;j<shape[1];j++) { |
2084 |
tmp=5.8*dataView.index(i,j); |
2085 |
assert(std::abs(dataView(i,j)-tmp)<=REL_TOL*std::abs(tmp)); |
2086 |
} |
2087 |
} |
2088 |
|
2089 |
if (p<npoints-1) { |
2090 |
dataView.incrOffset(); |
2091 |
} |
2092 |
|
2093 |
} |
2094 |
|
2095 |
} |
2096 |
|
2097 |
{ |
2098 |
cout << endl; |
2099 |
cout << "\tTest binaryOp on shape (9,8,5,11) DataArrayView and single value."; |
2100 |
|
2101 |
// define the shape for the DataArrayView |
2102 |
DataArrayView::ShapeType shape; |
2103 |
shape.push_back(9); |
2104 |
shape.push_back(8); |
2105 |
shape.push_back(5); |
2106 |
shape.push_back(11); |
2107 |
|
2108 |
// allocate the data for the DataArrayView |
2109 |
int npoints=4; |
2110 |
DataArrayView::ValueType data(DataArrayView::noValues(shape)*npoints,0); |
2111 |
|
2112 |
// constructor |
2113 |
DataArrayView dataView(data,shape); |
2114 |
|
2115 |
// step the view along each data point in the underlying data |
2116 |
for (int p=0;p<npoints;p++) { |
2117 |
|
2118 |
// assign values to the data point |
2119 |
for (int i=0;i<shape[0];i++) { |
2120 |
for (int j=0;j<shape[1];j++) { |
2121 |
for (int k=0;k<shape[2];k++) { |
2122 |
for (int l=0;l<shape[3];l++) { |
2123 |
dataView(i,j,k,l)=dataView.index(i,j,k,l); |
2124 |
} |
2125 |
} |
2126 |
} |
2127 |
} |
2128 |
|
2129 |
// apply a binary operation to the data point |
2130 |
dataView.binaryOp(5.4,multiplies<double>()); |
2131 |
|
2132 |
double tmp; |
2133 |
// check the results |
2134 |
for (int i=0;i<shape[0];i++) { |
2135 |
for (int j=0;j<shape[1];j++) { |
2136 |
for (int k=0;k<shape[2];k++) { |
2137 |
for (int l=0;l<shape[3];l++) { |
2138 |
tmp=5.4*dataView.index(i,j,k,l); |
2139 |
assert(std::abs(dataView(i,j,k,l)-tmp)<=REL_TOL*std::abs(tmp)); |
2140 |
} |
2141 |
} |
2142 |
} |
2143 |
} |
2144 |
|
2145 |
if (p<npoints-1) { |
2146 |
dataView.incrOffset(); |
2147 |
} |
2148 |
|
2149 |
} |
2150 |
|
2151 |
} |
2152 |
|
2153 |
cout << endl; |
2154 |
|
2155 |
} |
2156 |
|
2157 |
void DataArrayViewTestCase::testReductionOp() |
2158 |
{ |
2159 |
|
2160 |
{ |
2161 |
cout << endl; |
2162 |
cout << "\tTest reductionOp on scalar DataArrayView."; |
2163 |
|
2164 |
// define the shape for the DataArrayView |
2165 |
DataArrayView::ShapeType shape; |
2166 |
|
2167 |
// allocate the data for the DataArrayView |
2168 |
int npoints=4; |
2169 |
DataArrayView::ValueType data(DataArrayView::noValues(shape)*npoints,0); |
2170 |
|
2171 |
// constructor |
2172 |
DataArrayView dataView(data,shape); |
2173 |
|
2174 |
// step the view along each data point in the underlying data |
2175 |
for (int p=0;p<npoints;p++) { |
2176 |
|
2177 |
// assign values to the data point |
2178 |
dataView()=p; |
2179 |
|
2180 |
// apply a reduction operation to this data point and check the results |
2181 |
FMax fmax_func; |
2182 |
assert(std::abs(dataView.reductionOp(fmax_func,numeric_limits<double>::max()*-1)-p)<=REL_TOL*p); |
2183 |
|
2184 |
if (p<npoints-1) { |
2185 |
dataView.incrOffset(); |
2186 |
} |
2187 |
|
2188 |
} |
2189 |
|
2190 |
} |
2191 |
|
2192 |
{ |
2193 |
cout << endl; |
2194 |
cout << "\tTest reductionOp on shape (2,3) DataArrayView."; |
2195 |
|
2196 |
// define the shape for the DataArrayView |
2197 |
DataArrayView::ShapeType shape; |
2198 |
shape.push_back(2); |
2199 |
shape.push_back(3); |
2200 |
|
2201 |
// allocate the data for the DataArrayView |
2202 |
int npoints=4; |
2203 |
DataArrayView::ValueType data(DataArrayView::noValues(shape)*npoints,0); |
2204 |
|
2205 |
// constructor |
2206 |
DataArrayView dataView(data,shape); |
2207 |
|
2208 |
// step the view along each data point in the underlying data |
2209 |
for (int p=0;p<npoints;p++) { |
2210 |
|
2211 |
// assign values to the data point |
2212 |
for (int i=0;i<shape[0];i++) { |
2213 |
for (int j=0;j<shape[1];j++) { |
2214 |
dataView(i,j)=dataView.index(i,j); |
2215 |
} |
2216 |
} |
2217 |
|
2218 |
// apply a reduction operation to this data point and check the results |
2219 |
FMin fmin_func; |
2220 |
assert(std::abs(dataView.reductionOp(fmin_func,numeric_limits<double>::max())-dataView.index(0,0))<=REL_TOL*std::abs(dataView.index(0,0))); |
2221 |
|
2222 |
if (p<npoints-1) { |
2223 |
dataView.incrOffset(); |
2224 |
} |
2225 |
|
2226 |
} |
2227 |
|
2228 |
} |
2229 |
|
2230 |
{ |
2231 |
cout << endl; |
2232 |
cout << "\tTest reductionOp on shape (9,8,5,11) DataArrayView."; |
2233 |
|
2234 |
// define the shape for the DataArrayView |
2235 |
DataArrayView::ShapeType shape; |
2236 |
shape.push_back(9); |
2237 |
shape.push_back(8); |
2238 |
shape.push_back(5); |
2239 |
shape.push_back(11); |
2240 |
|
2241 |
// allocate the data for the DataArrayView |
2242 |
int npoints=4; |
2243 |
DataArrayView::ValueType data(DataArrayView::noValues(shape)*npoints,0); |
2244 |
|
2245 |
// constructor |
2246 |
DataArrayView dataView(data,shape); |
2247 |
|
2248 |
// step the view along each data point in the underlying data |
2249 |
for (int p=0;p<npoints;p++) { |
2250 |
|
2251 |
// assign values to the data point |
2252 |
for (int i=0;i<shape[0];i++) { |
2253 |
for (int j=0;j<shape[1];j++) { |
2254 |
for (int k=0;k<shape[2];k++) { |
2255 |
for (int l=0;l<shape[3];l++) { |
2256 |
dataView(i,j,k,l)=dataView.index(i,j,k,l); |
2257 |
} |
2258 |
} |
2259 |
} |
2260 |
} |
2261 |
|
2262 |
// apply a reduction operation to this data point and check the results |
2263 |
AbsMax absmax_func; |
2264 |
assert(dataView.reductionOp(absmax_func,0)==dataView.index(8,7,4,10)); |
2265 |
|
2266 |
if (p<npoints-1) { |
2267 |
dataView.incrOffset(); |
2268 |
} |
2269 |
|
2270 |
} |
2271 |
|
2272 |
} |
2273 |
|
2274 |
cout << endl; |
2275 |
|
2276 |
} |
2277 |
|
2278 |
TestSuite* DataArrayViewTestCase::suite () |
2279 |
{ |
2280 |
// |
2281 |
// create the suite of tests to perform. |
2282 |
TestSuite *testSuite = new TestSuite ("DataArrayViewTestCase"); |
2283 |
|
2284 |
testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testAll",&DataArrayViewTestCase::testAll)); |
2285 |
testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testShapeToString",&DataArrayViewTestCase::testShapeToString)); |
2286 |
testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testScalarView",&DataArrayViewTestCase::testScalarView)); |
2287 |
testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testResultSliceShape",&DataArrayViewTestCase::testResultSliceShape)); |
2288 |
testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testSlicing",&DataArrayViewTestCase::testSlicing)); |
2289 |
testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testUnaryOp",&DataArrayViewTestCase::testUnaryOp)); |
2290 |
testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testBinaryOp",&DataArrayViewTestCase::testBinaryOp)); |
2291 |
testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testReductionOp",&DataArrayViewTestCase::testReductionOp)); |
2292 |
testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testMatMult",&DataArrayViewTestCase::testMatMult)); |
2293 |
return testSuite; |
2294 |
} |