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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

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

Details in the logs of branches/windows_from_1431_trunk.

pre-merge snapshot of the trunk in tags/trunk_at_1452


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26