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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1455 - (show annotations)
Thu Feb 28 17:19:44 2008 UTC (11 years, 6 months ago) by phornby
File size: 66591 byte(s)
Merge of branches/windows_from_1431_trunk.

Revamp of the exception system.
Fix unused vars and signed/unsigned comparisons.
defined a macro THROW(ARG) in the system_dep.h's to
deal with the expectations of declarations on different architectures.

Details in the logs of branches/windows_from_1431_trunk.

pre-merge snapshot of the trunk in tags/trunk_at_1452


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26