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

Annotation of /trunk/escript/test/DataTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 757 - (hide annotations)
Mon Jun 26 13:12:56 2006 UTC (13 years ago) by woo409
File size: 32750 byte(s)
+ Merge of intelc_win32 branch (revision 741:755) with trunk. Tested on iVEC altix (run_tests and py_tests all pass)

1 jgs 82 // $Id$
2     /*
3     *****************************************************************************
4     * *
5     * COPYRIGHT ACcESS - All Rights Reserved *
6     * *
7     * This software is the property of ACcESS. No part of this code *
8     * may be copied in any form or by any means without the expressed written *
9     * consent of ACcESS. Copying, use or modification of this software *
10     * by any unauthorised person is illegal unless that person has a software *
11     * license agreement with ACcESS. *
12     * *
13     *****************************************************************************
14     */
15 woo409 757 #include <iostream>
16     #ifdef _WIN32 && __INTEL_COMPILER
17     #include <mathimf.h>
18     #else
19     #include <math.h>
20     #endif
21 jgs 511
22     #include "DataTestCase.h"
23    
24 robwdcock 670 #include "escript/FunctionSpace.h"
25 robwdcock 638 #include "esysUtils/EsysException.h"
26 jgs 82
27 robwdcock 670 #include "escript/Data.h"
28 jgs 537
29 jgs 82
30     using namespace std;
31     using namespace CppUnitTest;
32     using namespace escript;
33     using namespace esysUtils;
34    
35     void DataTestCase::setUp() {
36     //
37     // This is called before each test is run
38     }
39    
40     void DataTestCase::tearDown() {
41     //
42     // This is called after each test has been run
43     }
44    
45     void DataTestCase::testSlicing() {
46    
47     cout << endl;
48    
49     {
50 jgs 537
51     cout << "\tTest get-slicing DataConstant" << endl;
52    
53 jgs 82 DataArrayView::ShapeType viewShape;
54     viewShape.push_back(2);
55     viewShape.push_back(3);
56 jgs 537 Data data(1.3,viewShape,FunctionSpace(),false);
57    
58     //cout << data.toString() << endl;
59    
60 jgs 82 DataArrayView::RegionType region;
61     region.push_back(DataArrayView::RegionType::value_type(0,0));
62     region.push_back(DataArrayView::RegionType::value_type(0,0));
63 jgs 537
64     Data slice1(data.getSlice(region));
65    
66     //cout << slice1.toString() << endl;
67    
68     assert(slice1.getDataPointRank()==0);
69     assert(slice1.getDataPoint(0,0)()==1.3);
70    
71 jgs 82 region.clear();
72     region.push_back(DataArrayView::RegionType::value_type(0,1));
73     region.push_back(DataArrayView::RegionType::value_type(0,1));
74 jgs 537
75     Data slice2(data.getSlice(region));
76    
77     //cout << slice2.toString() << endl;
78    
79     assert(slice2.getDataPointRank()==2);
80     assert(slice2.getDataPoint(0,0)(0,0)==1.3);
81    
82     region.clear();
83     region.push_back(DataArrayView::RegionType::value_type(0,1));
84     region.push_back(DataArrayView::RegionType::value_type(0,2));
85    
86     Data slice3(data.getSlice(region));
87    
88     //cout << slice3.toString() << endl;
89    
90     assert(slice3.getDataPointRank()==2);
91     assert(slice3.getDataPoint(0,0)(0,0)==1.3);
92     assert(slice3.getDataPoint(0,0)(0,1)==1.3);
93    
94 jgs 82 }
95    
96     {
97 jgs 537
98     cout << "\tTest set-slicing DataConstant" << endl;
99    
100 jgs 82 DataArrayView::ShapeType viewShape;
101 jgs 537 Data source(10.0,viewShape,FunctionSpace(),false);
102    
103     //cout << source.toString() << endl;
104    
105     viewShape.push_back(2);
106     viewShape.push_back(3);
107     Data target(1.3,viewShape,FunctionSpace(),false);
108    
109     //cout << target.toString() << endl;
110    
111     DataArrayView::RegionType region;
112     region.push_back(DataArrayView::RegionType::value_type(0,0));
113     region.push_back(DataArrayView::RegionType::value_type(0,0));
114    
115     target.setSlice(source,region);
116    
117     //cout << target.toString() << endl;
118    
119     assert(target.getDataPoint(0,0)(0,0)==source.getDataPoint(0,0)());
120    
121     }
122    
123     {
124    
125     cout << "\tTest get-slicing DataTagged" << endl;
126    
127 jgs 82 //
128 jgs 537 // create a DataTagged with a default value only
129    
130     DataArrayView::ShapeType viewShape;
131 jgs 82 viewShape.push_back(2);
132     viewShape.push_back(3);
133 jgs 537 Data data(1.3,viewShape,FunctionSpace(),false);
134     data.tag();
135     data.getDataPoint(0,0)(0,0)=1.0;
136     data.getDataPoint(0,0)(1,1)=2.0;
137    
138     //cout << data.toString() << endl;
139    
140     //
141     // create a scalar slice
142    
143 jgs 82 DataArrayView::RegionType region;
144     region.push_back(DataArrayView::RegionType::value_type(0,0));
145     region.push_back(DataArrayView::RegionType::value_type(0,0));
146 jgs 537
147     Data slice1(data.getSlice(region));
148    
149     //cout << slice1.toString() << endl;
150    
151     assert(slice1.isTagged());
152     assert(slice1.getDataPointRank()==0);
153     assert(slice1.getDataPoint(0,0)()==1.0);
154    
155 jgs 82 //
156 jgs 537 // create a rank 2 slice with one value
157    
158 jgs 82 region.clear();
159     region.push_back(DataArrayView::RegionType::value_type(0,1));
160     region.push_back(DataArrayView::RegionType::value_type(0,1));
161 jgs 537
162     Data slice2(data.getSlice(region));
163    
164     //cout << slice2.toString() << endl;
165    
166     assert(slice2.isTagged());
167     assert(slice2.getDataPointRank()==2);
168     assert(slice2.getDataPoint(0,0)(0,0)==1.0);
169    
170     //
171     // create a rank 2 slice with four values
172    
173 jgs 82 region.clear();
174     region.push_back(DataArrayView::RegionType::value_type(0,2));
175     region.push_back(DataArrayView::RegionType::value_type(0,2));
176 jgs 537
177     Data slice3(data.getSlice(region));
178    
179     //cout << slice3.toString() << endl;
180    
181     assert(slice3.isTagged());
182     assert(slice3.getDataPointRank()==2);
183     assert(slice3.getDataPoint(0,0)(0,0)==1.0);
184     assert(slice3.getDataPoint(0,0)(0,1)==1.3);
185     assert(slice3.getDataPoint(0,0)(1,0)==1.3);
186     assert(slice3.getDataPoint(0,0)(1,1)==2.0);
187    
188     //
189     // add a value for tag "1"
190    
191     DataArrayView::ValueType viewData(6);
192     for (int i=0;i<viewData.size();i++) {
193     viewData[i]=i;
194     }
195     DataArrayView dataView(viewData,viewShape);
196    
197     data.setTaggedValueFromCPP(1, dataView);
198    
199     //
200     // create a full slice
201    
202     region.clear();
203     region.push_back(DataArrayView::RegionType::value_type(0,2));
204     region.push_back(DataArrayView::RegionType::value_type(0,3));
205    
206     Data slice4(data.getSlice(region));
207    
208     //cout << slice4.toString() << endl;
209    
210     assert(slice4.isTagged());
211     assert(slice4.getDataPointRank()==2);
212     assert(slice4.getDataPoint(0,0)(0,0)==0);
213     assert(slice4.getDataPoint(0,0)(0,1)==2);
214     assert(slice4.getDataPoint(0,0)(0,2)==4);
215     assert(slice4.getDataPoint(0,0)(1,0)==1);
216     assert(slice4.getDataPoint(0,0)(1,1)==3);
217     assert(slice4.getDataPoint(0,0)(1,2)==5);
218    
219 jgs 82 }
220    
221     {
222 jgs 537
223     cout << "\tTest set-slicing DataTagged" << endl;
224    
225     //
226 jgs 549 // create a source DataTagged with a scalar default value only
227 jgs 537
228 jgs 82 DataArrayView::ShapeType viewShape;
229 jgs 537 Data source(10.0,viewShape,FunctionSpace(),false);
230     source.tag();
231    
232 jgs 549 //cout << "source:\n" << source.toString() << endl;
233 jgs 537
234 jgs 82 //
235 jgs 549 // create a target DataTagged with a rank 2 default value only
236 jgs 537
237 jgs 82 viewShape.push_back(2);
238     viewShape.push_back(3);
239 jgs 537 Data target(1.3,viewShape,FunctionSpace(),false);
240     target.tag();
241    
242 jgs 549 //cout << "target:\n" << target.toString() << endl;
243 jgs 537
244 jgs 82 //
245 jgs 537 // set a slice in target from source
246    
247     DataArrayView::RegionType region;
248     region.push_back(DataArrayView::RegionType::value_type(1,1));
249     region.push_back(DataArrayView::RegionType::value_type(1,1));
250    
251     target.setSlice(source,region);
252    
253 jgs 549 //cout << "target:\n" << target.toString() << endl;
254 jgs 537
255     assert(target.isTagged());
256     assert(target.getDataPointRank()==2);
257     assert(target.getDataPoint(0,0)(0,0)==1.3);
258     assert(target.getDataPoint(0,0)(0,1)==1.3);
259     assert(target.getDataPoint(0,0)(0,2)==1.3);
260     assert(target.getDataPoint(0,0)(1,0)==1.3);
261     assert(target.getDataPoint(0,0)(1,1)==source.getDataPoint(0,0)());
262     assert(target.getDataPoint(0,0)(1,2)==1.3);
263    
264     //
265 jgs 549 // add a value for tag "1" to target
266 jgs 537
267     DataArrayView::ValueType viewData(6);
268     for (int i=0;i<viewData.size();i++) {
269     viewData[i]=i;
270     }
271     DataArrayView dataView(viewData,viewShape);
272    
273     target.setTaggedValueFromCPP(1, dataView);
274    
275 jgs 549 //cout << "target:\n" << target.toString() << endl;
276    
277 jgs 537 //
278     // set a slice in target from source
279    
280     region.clear();
281     region.push_back(DataArrayView::RegionType::value_type(0,0));
282     region.push_back(DataArrayView::RegionType::value_type(1,1));
283    
284     target.setSlice(source,region);
285    
286 jgs 549 //cout << "target:\n" << target.toString() << endl;
287 jgs 537
288     assert(target.isTagged());
289     assert(target.getDataPointRank()==2);
290     assert(target.getDataPoint(0,0)(0,0)==0);
291     assert(target.getDataPoint(0,0)(0,1)==source.getDataPoint(0,0)());
292     assert(target.getDataPoint(0,0)(0,2)==4);
293     assert(target.getDataPoint(0,0)(1,0)==1);
294     assert(target.getDataPoint(0,0)(1,1)==3);
295     assert(target.getDataPoint(0,0)(1,2)==5);
296    
297 jgs 549 //
298     // add a value for tag "2" to source
299    
300     DataArrayView::ShapeType viewShape2;
301     DataArrayView::ValueType viewData2(1);
302     viewData2[0]=6;
303     DataArrayView dataView2(viewData2,viewShape2);
304    
305     source.setTaggedValueFromCPP(2, dataView2);
306    
307     //cout << "source:\n" << source.toString() << endl;
308    
309     //
310     // set a slice in target from source
311    
312     region.clear();
313     region.push_back(DataArrayView::RegionType::value_type(0,0));
314     region.push_back(DataArrayView::RegionType::value_type(1,1));
315    
316     target.setSlice(source,region);
317    
318     //cout << "target:\n" << target.toString() << endl;
319    
320     assert(target.isTagged());
321     assert(target.getDataPointRank()==2);
322    
323     // use a non-existant tag so we get a pointer to the default value
324     // ie: the first element in the data array
325     DataAbstract::ValueType::value_type* targetData=target.getSampleDataByTag(9);
326     for (int i=0; i<target.getLength(); i++) {
327     assert(targetData[i]>=0);
328     }
329     assert(targetData[0]==1.3);
330     assert(targetData[1]==1.3);
331     assert(targetData[2]==10);
332     assert(targetData[3]==10);
333     assert(targetData[4]==1.3);
334     assert(targetData[5]==1.3);
335     assert(targetData[6]==0);
336     assert(targetData[7]==1);
337     assert(targetData[8]==10);
338     assert(targetData[9]==3);
339     assert(targetData[10]==4);
340     assert(targetData[11]==5);
341     assert(targetData[12]==1.3);
342     assert(targetData[13]==1.3);
343     assert(targetData[14]==6);
344     assert(targetData[15]==10);
345     assert(targetData[16]==1.3);
346     assert(targetData[17]==1.3);
347    
348 jgs 537 }
349    
350     {
351    
352     cout << "\tTest get-slicing DataExpanded" << endl;
353    
354     DataArrayView::ShapeType viewShape;
355     viewShape.push_back(2);
356     viewShape.push_back(3);
357     Data temp(1.3,viewShape,FunctionSpace(),true);
358    
359 jgs 82 temp.getDataPoint(0,0)(0,0)=0.0;
360     temp.getDataPoint(0,0)(1,1)=1.0;
361 jgs 537
362 jgs 82 DataArrayView::RegionType region;
363     region.push_back(DataArrayView::RegionType::value_type(0,0));
364     region.push_back(DataArrayView::RegionType::value_type(0,0));
365 jgs 537
366 jgs 82 Data slice(temp.getSlice(region));
367 jgs 537
368 jgs 82 assert(slice.getDataPointRank()==0);
369     assert(slice.getDataPoint(0,0)()==0.0);
370 jgs 537
371 jgs 82 region.clear();
372     region.push_back(DataArrayView::RegionType::value_type(0,1));
373     region.push_back(DataArrayView::RegionType::value_type(0,1));
374 jgs 537
375 jgs 82 slice=temp.getSlice(region);
376 jgs 537
377 jgs 82 assert(slice.getDataPointRank()==2);
378     assert(slice.getDataPoint(0,0)(0,0)==0.0);
379 jgs 537
380 jgs 82 region.clear();
381     region.push_back(DataArrayView::RegionType::value_type(0,2));
382     region.push_back(DataArrayView::RegionType::value_type(0,2));
383 jgs 537
384 jgs 82 slice=temp.getSlice(region);
385 jgs 537
386 jgs 82 assert(slice.getDataPoint(0,0)(0,0)==0.0);
387     assert(slice.getDataPoint(0,0)(1,1)==1.0);
388 jgs 537
389 jgs 82 }
390    
391     {
392    
393 jgs 537 cout << "\tTest set-slicing DataExpanded" << endl;
394    
395 jgs 82 DataArrayView::ShapeType viewShape;
396     Data source(10.0,viewShape,FunctionSpace(),true);
397 jgs 537
398 jgs 82 viewShape.push_back(2);
399     viewShape.push_back(3);
400     Data target(1.3,viewShape,FunctionSpace(),true);
401 jgs 537
402 jgs 82 DataArrayView::RegionType region;
403     region.push_back(DataArrayView::RegionType::value_type(0,0));
404     region.push_back(DataArrayView::RegionType::value_type(0,0));
405 jgs 537
406 jgs 82 target.setSlice(source,region);
407 jgs 537
408 jgs 82 assert(target.getDataPoint(0,0)(0,0)==source.getDataPoint(0,0)());
409    
410     }
411    
412     }
413    
414 jgs 538 void DataTestCase::testAll() {
415 jgs 82
416     cout << endl;
417    
418     cout << "\tCreate a Data object from a DataArrayView" << endl;
419    
420     DataArrayView::ShapeType viewShape;
421     viewShape.push_back(3);
422 jgs 121 DataArrayView::ValueType viewData(3);
423 jgs 82 for (int i=0;i<viewShape[0];++i) {
424 jgs 121 viewData[i]=i;
425 jgs 82 }
426     DataArrayView myView(viewData,viewShape);
427    
428     bool expanded=true;
429     Data exData(myView,FunctionSpace(),expanded);
430     Data cData(myView);
431     Data result;
432    
433     assert(exData.isExpanded());
434     assert(cData.isConstant());
435     assert(result.isEmpty());
436    
437     cout << "\tTest some basic operations" << endl;
438     result=exData*cData;
439     assert(result.isExpanded());
440    
441     }
442    
443 jgs 538 void DataTestCase::testMore() {
444 jgs 82
445     cout << endl;
446    
447     cout << "\tCreate a Data object from a DataArrayView" << endl;
448    
449     DataArrayView::ShapeType viewShape;
450     viewShape.push_back(3);
451 jgs 121 DataArrayView::ValueType viewData(3);
452 jgs 82 for (int i=0;i<viewShape[0];++i) {
453 jgs 121 viewData[i]=i;
454 jgs 82 }
455     DataArrayView myView(viewData,viewShape);
456    
457     bool expanded=true;
458     Data exData(myView,FunctionSpace(),expanded);
459     Data cData(myView);
460     Data result;
461 jgs 123
462 jgs 82 assert(exData.isExpanded());
463     assert(cData.isConstant());
464     assert(result.isEmpty());
465    
466     cout << "\tTest some basic operations" << endl;
467     result=exData*cData;
468     assert(result.isExpanded());
469    
470 jgs 538 assert(result.Lsup()==4);
471     assert(result.sup()==4);
472     assert(result.inf()==0);
473    
474     result=exData+cData;
475     result=exData-cData;
476     result=exData/cData;
477    
478     cout << "\tExercise wherePositive method" << endl;
479     assert(!exData.wherePositive().isEmpty());
480    
481     cout << "\tExercise copyWithMask method" << endl;
482     exData.copyWithMask(result, exData.wherePositive());
483     assert(!exData.wherePositive().isEmpty());
484    
485 jgs 82 }
486    
487     void DataTestCase::testDataConstant() {
488    
489     cout << endl;
490    
491     cout << "\tCreate a DataConstant object from a DataArrayView" << endl;
492    
493     DataArrayView::ShapeType viewShape;
494     viewShape.push_back(2);
495     viewShape.push_back(3);
496     viewShape.push_back(4);
497 jgs 121 DataArrayView::ValueType viewData(2*3*4);
498 jgs 82 for (int i=0;i<DataArrayView::noValues(viewShape);++i) {
499 jgs 121 viewData[i]=i;
500 jgs 82 }
501     DataArrayView myView(viewData,viewShape);
502    
503     Data left(myView);
504     Data right(myView);
505     Data result;
506    
507     cout << "\tTest some basic operations" << endl;
508    
509     result=left-right;
510    
511     assert(left.isConstant());
512     assert(right.isConstant());
513     assert(result.isConstant());
514    
515     result=left+right;
516    
517     assert(left.isConstant());
518     assert(right.isConstant());
519     assert(result.isConstant());
520    
521     assert(!result.isExpanded());
522     assert(!result.isTagged());
523    
524     }
525    
526     void DataTestCase::testDataTagged() {
527    
528     cout << endl;
529    
530 jgs 539 {
531 jgs 82
532 jgs 539 cout << "\tCreate a DataTagged object with a default value only." << endl;
533 jgs 511
534 jgs 539 DataTagged::TagListType keys;
535 jgs 511
536 jgs 539 DataTagged::ValueListType values;
537 jgs 511
538 jgs 539 DataArrayView::ShapeType viewShape;
539     viewShape.push_back(3);
540 jgs 82
541 jgs 539 DataArrayView::ValueType viewData(3);
542     for (int i=0;i<viewShape[0];i++) {
543     viewData[i]=i;
544     }
545     DataArrayView defaultValue(viewData,viewShape);
546 jgs 82
547 jgs 539 bool expanded=false;
548    
549     Data myData(keys,values,defaultValue,FunctionSpace(),expanded);
550 jgs 511
551 jgs 539 // cout << myData.toString() << endl;
552 jgs 511
553 jgs 539 assert(!myData.isEmpty());
554     assert(myData.isTagged());
555     assert(myData.getTagNumber(0)==1);
556     assert(myData.getDataPointRank()==1);
557     assert(myData.getLength()==3);
558 jgs 82
559 jgs 539 DataArrayView myDataView = myData.getPointDataView();
560     assert(!myDataView.isEmpty());
561     assert(myDataView.getOffset()==0);
562     assert(myDataView.getRank()==1);
563     assert(myDataView.noValues()==3);
564     assert(myDataView.getShape().size()==1);
565     assert(myDataView(0)==0.0);
566     assert(myDataView(1)==1.0);
567     assert(myDataView(2)==2.0);
568 jgs 82
569 jgs 539 myDataView = myData.getDataPoint(0,0);
570     assert(!myDataView.isEmpty());
571     assert(myDataView.getOffset()==0);
572     assert(myDataView.getRank()==1);
573     assert(myDataView.noValues()==3);
574     assert(myDataView.getShape().size()==1);
575     assert(myDataView(0)==0.0);
576     assert(myDataView(1)==1.0);
577     assert(myDataView(2)==2.0);
578 jgs 82
579 jgs 539 double* sampleData=myData.getSampleData(0);
580     for (int i=0; i<myDataView.noValues(); i++) {
581     assert(sampleData[i]==i);
582     }
583     // use a non-existent tag so we get a pointer to
584     // the first element of the data array
585     sampleData=myData.getSampleDataByTag(9);
586     for (int i=0; i<myData.getLength(); i++) {
587     assert(sampleData[i]==i);
588     }
589 jgs 511
590 jgs 539 cout << "\tTest setting of a tag and associated value." << endl;
591 jgs 511
592 jgs 539 // value for tag "1"
593     DataArray eTwo(defaultValue);
594     for (int i=0;i<eTwo.getView().getShape()[0];i++) {
595     eTwo.getView()(i)=i+2.0;
596     }
597    
598     myData.setTaggedValueFromCPP(1,eTwo.getView());
599    
600     assert(myData.getLength()==6);
601    
602     myDataView = myData.getDataPoint(0,0);
603     assert(myDataView==eTwo.getView());
604     assert(!myDataView.isEmpty());
605     assert(myDataView.getOffset()==3);
606     assert(myDataView.getRank()==1);
607     assert(myDataView.noValues()==3);
608     assert(myDataView.getShape().size()==1);
609     assert(myDataView(0)==2);
610     assert(myDataView(1)==3);
611     assert(myDataView(2)==4);
612    
613     sampleData=myData.getSampleDataByTag(1);
614     for (int i=0; i<myDataView.noValues(); i++) {
615     assert(sampleData[i]==i+2);
616     }
617    
618 jgs 511 }
619    
620 jgs 539 {
621 jgs 511
622 jgs 539 cout << "\tCreate a DataTagged object via tag() method." << endl;
623 jgs 511
624 jgs 539 DataArrayView::ShapeType viewShape;
625     viewShape.push_back(2);
626     viewShape.push_back(3);
627     Data myData(1.3,viewShape,FunctionSpace(),false);
628     myData.tag();
629 jgs 511
630 jgs 539 //cout << myData.toString() << endl;
631    
632     assert(!myData.isEmpty());
633     assert(myData.isTagged());
634     assert(myData.getTagNumber(0)==1);
635     assert(myData.getDataPointRank()==2);
636     assert(myData.getLength()==6);
637    
638     // check default value
639     DataArrayView myDataView = myData.getPointDataView();
640     assert(!myDataView.isEmpty());
641     assert(myDataView.getOffset()==0);
642     assert(myDataView.getRank()==2);
643     assert(myDataView.noValues()==6);
644     assert(myDataView.getShape().size()==2);
645     assert(myDataView(0,0)==1.3);
646     assert(myDataView(0,1)==1.3);
647     assert(myDataView(0,2)==1.3);
648     assert(myDataView(1,0)==1.3);
649     assert(myDataView(1,1)==1.3);
650     assert(myDataView(1,2)==1.3);
651    
652     // check value for data-point (0,0).
653     myDataView = myData.getDataPoint(0,0);
654     assert(!myDataView.isEmpty());
655     assert(myDataView.getOffset()==0);
656     assert(myDataView.getRank()==2);
657     assert(myDataView.noValues()==6);
658     assert(myDataView.getShape().size()==2);
659     assert(myDataView(0,0)==1.3);
660     assert(myDataView(0,1)==1.3);
661     assert(myDataView(0,2)==1.3);
662     assert(myDataView(1,0)==1.3);
663     assert(myDataView(1,1)==1.3);
664     assert(myDataView(1,2)==1.3);
665    
666 jgs 511 }
667    
668 jgs 82 }
669    
670 jgs 538 void DataTestCase::testDataTaggedExceptions() {
671    
672     cout << endl;
673    
674     cout << "\tTest DataTagged exceptions." << endl;
675    
676     Data myData;
677     DataArrayView myView;
678    
679     try {
680     myData.getSampleDataByTag(0);;
681     assert(false);
682     }
683     catch (EsysException& e) {
684     //cout << e.what() << endl;
685     assert(true);
686     }
687    
688     try {
689     myData.setTaggedValueFromCPP(0,myView);;
690     assert(false);
691     }
692     catch (EsysException& e) {
693     //cout << e.what() << endl;
694     assert(true);
695     }
696    
697     }
698    
699 jgs 82 void DataTestCase::testConstructors() {
700    
701     cout << endl;
702    
703     DataArrayView::ShapeType viewShape;
704     {
705     cout << "\tCreate an Empty Data object" << endl;
706     Data temp(1.3,viewShape,FunctionSpace(),false);
707     }
708     {
709     cout << "\tCreate a rank 2 Data object" << endl;
710     viewShape.push_back(2);
711     viewShape.push_back(3);
712     Data temp(1.3,viewShape,FunctionSpace(),false);
713     }
714     }
715    
716     void DataTestCase::testOperations() {
717 jgs 113
718 jgs 82 cout << endl;
719    
720 jgs 113 // define the shape for the DataArrayView test data
721     DataArrayView::ShapeType shape;
722     shape.push_back(2);
723     shape.push_back(3);
724 jgs 82
725 jgs 113 // allocate the data for the DataArrayView
726     DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
727 jgs 82
728 jgs 113 // construct DataArrayView
729     DataArrayView dataView(data,shape);
730 jgs 82
731 jgs 113 // assign values to the data
732     for (int i=0;i<shape[0];i++) {
733     for (int j=0;j<shape[1];j++) {
734     dataView(i,j)=dataView.index(i,j);
735     }
736     }
737    
738 jgs 563 Data baseEx(dataView,FunctionSpace(),true);
739     Data baseCon(dataView,FunctionSpace(),false);
740     Data baseTag(dataView,FunctionSpace(),false);
741     baseTag.tag();
742 jgs 113
743 jgs 563 assert(baseEx.isExpanded());
744     assert(baseCon.isConstant());
745     assert(baseTag.isTagged());
746    
747     Data resultEx;
748     Data resultCon;
749     Data resultTag;
750    
751 jgs 113 // test unary operations
752    
753 gross 713 double tmp;
754 jgs 113 cout << "\tTest Data::pow." << endl;
755     Data power(3.0,shape,FunctionSpace(),true);
756 jgs 563 resultEx.copy(baseEx.powD(power));
757     resultCon.copy(baseCon.powD(power));
758     resultTag.copy(baseTag.powD(power));
759 jgs 113 for (int i=0;i<shape[0];i++) {
760     for (int j=0;j<shape[1];j++) {
761 gross 713 tmp=pow(dataView.index(i,j),3.0);
762     assert(std::abs(resultEx.getPointDataView()(i,j) - tmp) <= REL_TOL*std::abs(tmp));
763     assert(std::abs(resultCon.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
764     assert(std::abs(resultTag.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
765 jgs 113 }
766     }
767 jgs 82
768 jgs 113 cout << "\tTest Data::sin." << endl;
769 jgs 563 resultEx.copy(baseEx.sin());
770     resultCon.copy(baseCon.sin());
771     resultTag.copy(baseTag.sin());
772 gross 713 for (int i=0;i<shape[0];i++) {
773     for (int j=0;j<shape[1];j++) {
774     tmp=sin((double)dataView.index(i,j));
775     assert(std::abs(resultEx.getPointDataView()(i,j) - tmp) <= REL_TOL*std::abs(tmp));
776     assert(std::abs(resultCon.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
777     assert(std::abs(resultTag.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
778     }
779     }
780 jgs 102
781 jgs 113 cout << "\tTest Data::cos." << endl;
782 jgs 563 resultEx.copy(baseEx.cos());
783     resultCon.copy(baseCon.cos());
784     resultTag.copy(baseTag.cos());
785 gross 713 for (int i=0;i<shape[0];i++) {
786     for (int j=0;j<shape[1];j++) {
787     tmp=cos((double)dataView.index(i,j));
788     assert(std::abs(resultEx.getPointDataView()(i,j) - tmp) <= REL_TOL*std::abs(tmp));
789     assert(std::abs(resultCon.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
790     assert(std::abs(resultTag.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
791     }
792     }
793 jgs 102
794 jgs 113 cout << "\tTest Data::tan." << endl;
795 jgs 563 resultEx.copy(baseEx.tan());
796     resultCon.copy(baseCon.tan());
797     resultTag.copy(baseTag.tan());
798 gross 713 for (int i=0;i<shape[0];i++) {
799     for (int j=0;j<shape[1];j++) {
800     tmp=tan((double)dataView.index(i,j));
801     assert(std::abs(resultEx.getPointDataView()(i,j) - tmp) <= REL_TOL*std::abs(tmp));
802     assert(std::abs(resultCon.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
803     assert(std::abs(resultTag.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
804     }
805     }
806 jgs 102
807 jgs 150 cout << "\tTest Data::asin." << endl;
808 jgs 563 resultEx.copy(baseEx.asin());
809     resultCon.copy(baseCon.asin());
810     resultTag.copy(baseTag.asin());
811 jgs 150 assert(true);
812    
813     cout << "\tTest Data::acos." << endl;
814 jgs 563 resultEx.copy(baseEx.acos());
815     resultCon.copy(baseCon.acos());
816     resultTag.copy(baseTag.acos());
817 jgs 150 assert(true);
818    
819     cout << "\tTest Data::atan." << endl;
820 jgs 563 resultEx.copy(baseEx.atan());
821     resultCon.copy(baseCon.atan());
822     resultTag.copy(baseTag.atan());
823 gross 713 for (int i=0;i<shape[0];i++) {
824     for (int j=0;j<shape[1];j++) {
825     tmp=atan((double)dataView.index(i,j));
826     assert(std::abs(resultEx.getPointDataView()(i,j) - tmp) <= REL_TOL*std::abs(tmp));
827     assert(std::abs(resultCon.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
828     assert(std::abs(resultTag.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
829     }
830     }
831 jgs 150
832     cout << "\tTest Data::sinh." << endl;
833 jgs 563 resultEx.copy(baseEx.sinh());
834     resultCon.copy(baseCon.sinh());
835     resultTag.copy(baseTag.sinh());
836 gross 713 for (int i=0;i<shape[0];i++) {
837     for (int j=0;j<shape[1];j++) {
838     tmp=sinh((double)dataView.index(i,j));
839     assert(std::abs(resultEx.getPointDataView()(i,j) - tmp) <= REL_TOL*std::abs(tmp));
840     assert(std::abs(resultCon.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
841     assert(std::abs(resultTag.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
842     }
843     }
844 jgs 150
845     cout << "\tTest Data::cosh." << endl;
846 jgs 563 resultEx.copy(baseEx.cosh());
847     resultCon.copy(baseCon.cosh());
848     resultTag.copy(baseTag.cosh());
849 gross 713 for (int i=0;i<shape[0];i++) {
850     for (int j=0;j<shape[1];j++) {
851     tmp=cosh((double)dataView.index(i,j));
852     assert(std::abs(resultEx.getPointDataView()(i,j) - tmp) <= REL_TOL*std::abs(tmp));
853     assert(std::abs(resultCon.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
854     assert(std::abs(resultTag.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
855     }
856     }
857 jgs 150
858     cout << "\tTest Data::tanh." << endl;
859 jgs 563 resultEx.copy(baseEx.tanh());
860     resultCon.copy(baseCon.tanh());
861     resultTag.copy(baseTag.tanh());
862 gross 713 for (int i=0;i<shape[0];i++) {
863     for (int j=0;j<shape[1];j++) {
864     tmp=tanh((double)dataView.index(i,j));
865     assert(std::abs(resultEx.getPointDataView()(i,j) - tmp) <= REL_TOL*std::abs(tmp));
866     assert(std::abs(resultCon.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
867     assert(std::abs(resultTag.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
868     }
869     }
870 jgs 150
871     cout << "\tTest Data::asinh." << endl;
872 jgs 563 resultEx.copy(baseEx.asinh());
873     resultCon.copy(baseCon.asinh());
874     resultTag.copy(baseTag.asinh());
875 jgs 150 assert(true);
876    
877     cout << "\tTest Data::acosh." << endl;
878 jgs 563 resultEx.copy(baseEx.acosh());
879     resultCon.copy(baseCon.acosh());
880     resultTag.copy(baseTag.acosh());
881 jgs 150 assert(true);
882    
883     cout << "\tTest Data::atanh." << endl;
884 jgs 563 resultEx.copy(baseEx.atanh());
885     resultCon.copy(baseCon.atanh());
886     resultTag.copy(baseTag.atanh());
887 jgs 150 assert(true);
888    
889 jgs 113 cout << "\tTest Data::log." << endl;
890 jgs 563 resultEx.copy(baseEx.log());
891     resultCon.copy(baseCon.log());
892     resultTag.copy(baseTag.log());
893 jgs 102 assert(true);
894    
895 jgs 113 cout << "\tTest Data::abs." << endl;
896 jgs 563 resultEx.copy(baseEx.abs());
897     resultCon.copy(baseCon.abs());
898     resultTag.copy(baseTag.abs());
899 gross 713 for (int i=0;i<shape[0];i++) {
900     for (int j=0;j<shape[1];j++) {
901     tmp=abs((double)dataView.index(i,j));
902     assert(std::abs(resultEx.getPointDataView()(i,j) - tmp) <= REL_TOL*std::abs(tmp));
903     assert(std::abs(resultCon.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
904     assert(std::abs(resultTag.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
905     }
906     }
907 jgs 102
908 jgs 113 cout << "\tTest Data::sign." << endl;
909 jgs 563 resultEx.copy(baseEx.sign());
910     resultCon.copy(baseCon.sign());
911     resultTag.copy(baseTag.sign());
912 jgs 102 assert(true);
913    
914 jgs 113 cout << "\tTest Data::exp." << endl;
915 jgs 563 resultEx.copy(baseEx.exp());
916     resultCon.copy(baseCon.exp());
917     resultTag.copy(baseTag.exp());
918 gross 713 for (int i=0;i<shape[0];i++) {
919     for (int j=0;j<shape[1];j++) {
920     tmp=exp((double)dataView.index(i,j));
921     assert(std::abs(resultEx.getPointDataView()(i,j) - tmp) <= REL_TOL*std::abs(tmp));
922     assert(std::abs(resultCon.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
923     assert(std::abs(resultTag.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
924     }
925     }
926 jgs 102
927 jgs 113 cout << "\tTest Data::sqrt." << endl;
928 jgs 563 resultEx.copy(baseEx.sqrt());
929     resultCon.copy(baseCon.sqrt());
930     resultTag.copy(baseTag.sqrt());
931 gross 713 for (int i=0;i<shape[0];i++) {
932     for (int j=0;j<shape[1];j++) {
933     tmp=sqrt((double)dataView.index(i,j));
934     assert(std::abs(resultEx.getPointDataView()(i,j) - tmp) <= REL_TOL*std::abs(tmp));
935     assert(std::abs(resultCon.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
936     assert(std::abs(resultTag.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
937     }
938     }
939 jgs 102
940 jgs 113 cout << "\tTest Data::neg." << endl;
941 jgs 563 resultEx.copy(baseEx.neg());
942     resultCon.copy(baseCon.neg());
943     resultTag.copy(baseTag.neg());
944 jgs 102 assert(true);
945    
946 jgs 113 cout << "\tTest Data::pos." << endl;
947 jgs 563 resultEx.copy(baseEx.pos());
948     resultCon.copy(baseCon.pos());
949     resultTag.copy(baseTag.pos());
950 jgs 113 for (int i=0;i<shape[0];i++) {
951     for (int j=0;j<shape[1];j++) {
952 gross 713 assert(std::abs(resultEx.getPointDataView()(i,j) - dataView.index(i,j)) <= REL_TOL*std::abs(dataView.index(i,j)));
953     assert(std::abs(resultCon.getPointDataView()(i,j) - dataView.index(i,j)) <= REL_TOL*std::abs(dataView.index(i,j)));
954     assert(std::abs(resultTag.getPointDataView()(i,j) - dataView.index(i,j)) <= REL_TOL*std::abs(dataView.index(i,j)));
955 jgs 113 }
956     }
957 jgs 102
958 jgs 113 // test reduction operations
959    
960     cout << "\tTest Data::Lsup." << endl;
961 gross 713 assert(std::abs(baseEx.Lsup() - 5) <= REL_TOL*5);
962     assert(std::abs(baseCon.Lsup() - 5) <= REL_TOL*5);
963     assert(std::abs(baseTag.Lsup() - 5) <= REL_TOL*5);
964 jgs 113
965     cout << "\tTest Data::sup." << endl;
966 gross 713 assert(std::abs(baseEx.sup() - 5) <= REL_TOL*5);
967     assert(std::abs(baseCon.sup() - 5) <= REL_TOL*5);
968     assert(std::abs(baseTag.sup() - 5) <= REL_TOL*5);
969 jgs 113
970     cout << "\tTest Data::inf." << endl;
971 gross 713 assert(std::abs(baseEx.inf() - 0) <= REL_TOL*0);
972     assert(std::abs(baseCon.inf() - 0) <= REL_TOL*0);
973     assert(std::abs(baseTag.inf() - 0) <= REL_TOL*0);
974 jgs 113
975     // test data-point reduction operations
976    
977     cout << "\tTest Data::minval." << endl;
978 jgs 563 resultEx.copy(baseEx.minval());
979     resultCon.copy(baseCon.minval());
980     resultTag.copy(baseTag.minval());
981 gross 713 assert(std::abs(resultEx.getPointDataView()() - 0) <= REL_TOL*0);
982     assert(std::abs(resultCon.getPointDataView()() - 0) <= REL_TOL*0);
983     assert(std::abs(resultTag.getPointDataView()() - 0) <= REL_TOL*0);
984 jgs 102
985 jgs 113 cout << "\tTest Data::maxval." << endl;
986 jgs 563 resultEx.copy(baseEx.maxval());
987     resultCon.copy(baseCon.maxval());
988     resultTag.copy(baseTag.maxval());
989 gross 713 assert(std::abs(resultEx.getPointDataView()() - 5) <= REL_TOL*5);
990     assert(std::abs(resultCon.getPointDataView()() - 5) <= REL_TOL*5);
991     assert(std::abs(resultTag.getPointDataView()() - 5) <= REL_TOL*5);
992 jgs 102
993 jgs 113 cout << "\tTest Data::trace." << endl;
994 jgs 563 resultEx.copy(baseEx.trace());
995     resultCon.copy(baseCon.trace());
996     resultTag.copy(baseTag.trace());
997 gross 713 assert(std::abs(resultEx.getPointDataView()() - 15) <= REL_TOL*15);
998     assert(std::abs(resultCon.getPointDataView()() - 15) <= REL_TOL*15);
999     assert(std::abs(resultTag.getPointDataView()() - 15) <= REL_TOL*15);
1000 jgs 102
1001 jgs 82 }
1002    
1003 jgs 110 void DataTestCase::testRefValue() {
1004    
1005     //
1006     // Note - this test can't be run as boost::python::numeric::array
1007 jgs 113 // objects can only be created and used from within a python thread!
1008 jgs 110 //
1009    
1010     cout << endl;
1011    
1012     cout << "\tTest Data object RefValue methods." << endl;
1013    
1014     // Create three Data object - DataExpanded, DataConstant and DataEmpty
1015     DataArrayView::ShapeType viewShape;
1016     viewShape.push_back(3);
1017 jgs 121 DataArrayView::ValueType viewData(3);
1018 jgs 110 for (int i=0;i<viewShape[0];++i) {
1019 jgs 121 viewData[i]=i;
1020 jgs 110 }
1021     DataArrayView myView(viewData,viewShape);
1022    
1023     bool expanded=true;
1024    
1025     Data expandedData(myView,FunctionSpace(),expanded);
1026     Data constantData(myView);
1027     Data emptyData;
1028    
1029     assert(expandedData.isExpanded());
1030     assert(constantData.isConstant());
1031     assert(emptyData.isEmpty());
1032    
1033     // Check assertions are thrown for RefValue methods on DataEmpty
1034    
1035     int ref = 0;
1036     boost::python::numeric::array num_array(1.0);
1037    
1038     try {
1039     emptyData.getRefValue(ref,num_array);
1040     assert(false);
1041     }
1042     catch (EsysException& e) {
1043     assert(true);
1044     }
1045     try {
1046     emptyData.setRefValue(ref,num_array);
1047     assert(false);
1048     }
1049     catch (EsysException& e) {
1050     assert(true);
1051     }
1052    
1053     // Check assertions are thrown for RefValue methods on DataConstant
1054     try {
1055     constantData.getRefValue(ref,num_array);
1056     assert(false);
1057     }
1058     catch (EsysException& e) {
1059     assert(true);
1060     }
1061     try {
1062     constantData.setRefValue(ref,num_array);
1063     assert(false);
1064     }
1065     catch (EsysException& e) {
1066     assert(true);
1067     }
1068    
1069     // Check calls to RefValue methods on DataExpanded
1070     expandedData.getRefValue(ref,num_array);
1071     expandedData.setRefValue(ref,num_array);
1072    
1073     }
1074    
1075 jgs 151 void DataTestCase::testMemAlloc() {
1076    
1077     //
1078     // Simple little sanity check for the memory allocator
1079    
1080     cout << endl;
1081    
1082     Data *testData;
1083     for (int i=0; i<1000; i++) {
1084     testData = new Data(0.0, DataArrayView::ShapeType(), FunctionSpace(), true);
1085     delete testData;
1086     }
1087    
1088     DataArrayView::ShapeType viewShape;
1089     viewShape.push_back(10);
1090     viewShape.push_back(10);
1091     viewShape.push_back(10);
1092    
1093     Data *testData2;
1094     Data *testData3 = new Data(0.0, viewShape, FunctionSpace(), true);
1095     for (int i=0; i<1000; i++) {
1096     testData2 = new Data(0.0, viewShape, FunctionSpace(), true);
1097     delete testData2;
1098     }
1099     delete testData3;
1100    
1101     }
1102    
1103 jgs 82 TestSuite* DataTestCase::suite ()
1104     {
1105     //
1106     // create the suite of tests to perform.
1107     TestSuite *testSuite = new TestSuite ("DataTestCase");
1108    
1109     testSuite->addTest (new TestCaller< DataTestCase>("testAll",&DataTestCase::testAll));
1110     testSuite->addTest (new TestCaller< DataTestCase>("testMore",&DataTestCase::testMore));
1111     testSuite->addTest (new TestCaller< DataTestCase>("testDataConstant",&DataTestCase::testDataConstant));
1112     testSuite->addTest (new TestCaller< DataTestCase>("testDataTagged",&DataTestCase::testDataTagged));
1113     testSuite->addTest (new TestCaller< DataTestCase>("testDataTaggedExceptions",&DataTestCase::testDataTaggedExceptions));
1114     testSuite->addTest (new TestCaller< DataTestCase>("testConstructors",&DataTestCase::testConstructors));
1115     testSuite->addTest (new TestCaller< DataTestCase>("testSlicing",&DataTestCase::testSlicing));
1116     testSuite->addTest (new TestCaller< DataTestCase>("testOperations",&DataTestCase::testOperations));
1117 jgs 110 //testSuite->addTest (new TestCaller< DataTestCase>("testRefValue",&DataTestCase::testRefValue));
1118 jgs 151 testSuite->addTest (new TestCaller< DataTestCase>("testMemAlloc",&DataTestCase::testMemAlloc));
1119 jgs 82
1120     return testSuite;
1121     }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26