/[escript]/trunk/esys2/escript/test/DataArrayView/DataArrayViewTestCase.cpp
ViewVC logotype

Contents of /trunk/esys2/escript/test/DataArrayView/DataArrayViewTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 108 - (show annotations)
Thu Jan 27 06:21:59 2005 UTC (14 years, 8 months ago) by jgs
File size: 61565 byte(s)
*** empty log message ***

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

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26