/[escript]/branches/arrayview_from_1695_trunk/escript/test/DataArrayViewTestCase.cpp
ViewVC logotype

Annotation of /branches/arrayview_from_1695_trunk/escript/test/DataArrayViewTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1714 - (hide annotations)
Thu Aug 21 00:01:55 2008 UTC (11 years, 1 month ago) by jfenwick
File size: 65422 byte(s)
Branch commit

Moved getSliceRegion() and getSliceRange() into DataTypes
Data.cpp - modified not to rely on operator() from DataArrayView
         - Used more const& to avoid copies


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