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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2147 - (hide annotations)
Wed Dec 10 04:41:26 2008 UTC (11 years, 2 months ago) by jfenwick
File size: 35247 byte(s)
Made some changes to c++ unit tests to accomodate AUTOLAZY.
whereZero and whereNonZero can now work with lazy data.
There are some double frees if AUTOLAZY is turned on so don't use it yet.
1 ksteube 1312
2     /*******************************************************
3 ksteube 1811 *
4     * Copyright (c) 2003-2008 by University of Queensland
5     * Earth Systems Science Computational Center (ESSCC)
6     * http://www.uq.edu.au/esscc
7     *
8     * Primary Business: Queensland, Australia
9     * Licensed under the Open Software License version 3.0
10     * http://www.opensource.org/licenses/osl-3.0.php
11     *
12     *******************************************************/
13 ksteube 1312
14 ksteube 1811
15 woo409 757 #include <iostream>
16 phornby 2051 #if defined (_WIN32) && defined (__INTEL_COMPILER)
17 woo409 757 #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 jfenwick 2005 #include "escript/DataLazy.h"
29 jfenwick 2147 #include "escript/EscriptParams.h"
30 jgs 537
31 jfenwick 2147 #define AUTOLAZYON setEscriptParamInt("AUTOLAZY",1);
32     #define AUTOLAZYOFF setEscriptParamInt("AUTOLAZY",0);
33     #define CHECKAUTOLAZY (getEscriptParamInt("AUTOLAZY")==1)
34     #define SAVELAZYSTATE int LAZYSTATE=getEscriptParamInt("AUTOLAZY");
35     #define RESTORELAZYSTATE setEscriptParamInt("AUTOLAZY",LAZYSTATE);
36 jgs 82
37 jfenwick 2147
38    
39 jgs 82 using namespace std;
40     using namespace CppUnitTest;
41     using namespace escript;
42     using namespace esysUtils;
43 jfenwick 1796 using namespace escript::DataTypes;
44 jgs 82
45     void DataTestCase::setUp() {
46     //
47     // This is called before each test is run
48     }
49    
50     void DataTestCase::tearDown() {
51     //
52     // This is called after each test has been run
53     }
54    
55 jfenwick 1796
56    
57     namespace
58     {
59    
60     inline
61     DataTypes::ValueType::reference
62     getRef(Data& d,int s1, int p1, int x, int y)
63     {
64     return d.getDataAtOffset(d.getDataOffset(s1,p1)+getRelIndex(d.getDataPointShape(),x,y));
65     }
66    
67     inline
68     DataTypes::ValueType::reference
69     getRef(Data& d, int x, int y)
70     {
71     return d.getDataAtOffset(getRelIndex(d.getDataPointShape(),x,y));
72     }
73    
74     }
75    
76 jfenwick 2005
77     void DataTestCase::testCopyingWorker(bool delayed)
78 jfenwick 1799 {
79 jfenwick 2005
80 jfenwick 1799 using namespace escript::DataTypes;
81     cout << endl;
82 jfenwick 2005
83     DataTypes::ShapeType shape;
84     shape.push_back(2);
85     shape.push_back(3);
86     DataTypes::ValueType data(DataTypes::noValues(shape),1);
87     const int NUMDATS=3;
88     Data* dats[NUMDATS];
89     const char* strs[]={"DataConstant", "DataTagged", "DataExpanded"};
90     dats[0]=new Data(new DataConstant(FunctionSpace(),shape,data));
91     dats[1]=new Data(new DataTagged(FunctionSpace(),shape,data));
92     dats[2]=new Data(new DataExpanded(FunctionSpace(),shape,data));
93     if (delayed)
94 jfenwick 1799 {
95 jfenwick 2005 dats[0]->delaySelf();
96     dats[1]->delaySelf();
97     dats[2]->delaySelf();
98 jfenwick 1799 }
99    
100 jfenwick 2005 for (int k=0;k<NUMDATS;++k)
101 jfenwick 1799 {
102 jfenwick 2005 cout << "\tTest deep copy " << strs[k] << endl;
103     Data* d=dats[k];
104 jfenwick 2105 Data deep=d->copySelf(); // test self copy
105 jfenwick 2005 if (delayed)
106 jfenwick 1799 {
107 jfenwick 2105 assert(deep.isLazy());
108 jfenwick 1799 }
109     for (int i=0;i<DataTypes::noValues(shape);++i)
110     {
111 jfenwick 2105 if (d->getDataAtOffset(i)!=deep.getDataAtOffset(i))
112 jfenwick 1799 assert(false);
113     }
114 jfenwick 2005 if (delayed)
115 jfenwick 1799 {
116 jfenwick 2005 d->delaySelf();
117 jfenwick 1799 }
118 jfenwick 2005 d->setToZero();
119     if (delayed)
120 jfenwick 1799 {
121 jfenwick 2005 assert(d->isLazy());
122 jfenwick 1799 }
123     for (int i=0;i<DataTypes::noValues(shape);++i)
124     {
125 jfenwick 2105 if (d->getDataAtOffset(i)==deep.getDataAtOffset(i))
126 jfenwick 1799 assert(false);
127     }
128 jfenwick 2005 if (delayed)
129 jfenwick 1799 {
130 jfenwick 2005 d->delaySelf();
131 jfenwick 2105 deep.delaySelf();
132 jfenwick 1799 }
133 jfenwick 2105 d->copy(deep); // test copy from object
134 jfenwick 2005 if (delayed)
135     {
136     assert(d->isLazy());
137     }
138 jfenwick 1799 for (int i=0;i<DataTypes::noValues(shape);++i)
139     {
140 jfenwick 2105 if (d->getDataAtOffset(i)!=deep.getDataAtOffset(i))
141 jfenwick 1799 assert(false);
142     }
143 jfenwick 2005 d->setToZero();
144 jfenwick 1799 for (int i=0;i<DataTypes::noValues(shape);++i)
145     {
146 jfenwick 2105 if (d->getDataAtOffset(i)==deep.getDataAtOffset(i))
147 jfenwick 1799 assert(false);
148     }
149 jfenwick 2005 delete dats[k];
150 jfenwick 1799 }
151    
152    
153 jgs 82
154    
155 jgs 537
156    
157 jfenwick 2005 }
158 jgs 537
159    
160 jfenwick 2005 void DataTestCase::testSlicingWorker(bool delayed)
161     {
162 jgs 537
163 jfenwick 2005 using namespace escript::DataTypes;
164     cout << endl;
165     {
166     DataTypes::ShapeType viewShape;
167     viewShape.push_back(2);
168     viewShape.push_back(3);
169 jgs 537
170 jfenwick 2005 const int NUMDATS=3;
171     const char* strs[]={"DataConstant", "DataTagged","DataExpanded"};
172     bool tags[]={false,true,false}; // is the slice of this data supposed to be tagged
173     Data* dats[NUMDATS];
174     for (int k=0;k<NUMDATS;++k)
175     {
176     dats[k]=new Data(1.3, viewShape);
177     }
178     dats[1]->tag();
179     dats[2]->expand();
180     for (int k=0;k<NUMDATS;++k)
181     {
182     Data* temp=dats[k];
183     dats[k]=new Data(dats[k]->delay());
184     delete temp;
185     }
186     for (int k=0;k<NUMDATS;++k)
187     {
188     cout << "\t\tTest get-slicing " << strs[k] << endl;
189     dats[k]->getDataAtOffset(dats[k]->getDataOffset(0,0)+getRelIndex(viewShape,0,0))=1.0;
190     dats[k]->getDataAtOffset(dats[k]->getDataOffset(0,0)+getRelIndex(viewShape,1,1))=2.0;
191 jgs 537
192 jfenwick 2005 DataTypes::RegionType region;
193     region.push_back(DataTypes::RegionType::value_type(0,0));
194     region.push_back(DataTypes::RegionType::value_type(0,0));
195 jgs 537
196 jfenwick 2005 Data slice1(dats[k]->getSlice(region));
197 jgs 537
198 jfenwick 2005 if (tags[k]) {assert(slice1.isTagged());}
199     assert(slice1.getDataPointRank()==0);
200     assert(slice1.getDataPoint(0,0)==1.0);
201 jgs 537
202 jfenwick 2005 //
203     // create a rank 2 slice with one value
204    
205     region.clear();
206     region.push_back(DataTypes::RegionType::value_type(0,1));
207     region.push_back(DataTypes::RegionType::value_type(0,1));
208    
209     Data slice2(dats[k]->getSlice(region));
210    
211     //cout << slice2.toString() << endl;
212    
213     if (tags[k]) {assert(slice2.isTagged());}
214     assert(slice2.getDataPointRank()==2);
215    
216     assert(slice2.getDataAtOffset(slice2.getDataOffset(0,0)+getRelIndex(slice2.getDataPointShape(),0,0))==1.0);
217 jgs 537
218 jfenwick 2005 //
219     // create a rank 2 slice with four values
220    
221     region.clear();
222     region.push_back(DataTypes::RegionType::value_type(0,2));
223     region.push_back(DataTypes::RegionType::value_type(0,2));
224    
225     Data slice3(dats[k]->getSlice(region));
226    
227     //cout << slice3.toString() << endl;
228    
229     if (tags[k]) {assert(slice3.isTagged());}
230     assert(slice3.getDataPointRank()==2);
231     assert(getRef(slice3,0,0,0,0)==1.0);
232     assert(getRef(slice3,0,0,0,1)==1.3);
233     assert(getRef(slice3,0,0,1,0)==1.3);
234     assert(getRef(slice3,0,0,1,1)==2.0);
235     }
236 jgs 537
237 jfenwick 2005 // now some extra tests for tagged data (dats[1])
238 jgs 537
239 jfenwick 2005 //
240     // add a value for tag "1"
241 jgs 537
242 jfenwick 2005 DataTypes::ValueType viewData(6);
243     for (int i=0;i<viewData.size();i++) {
244     viewData[i]=i;
245     }
246     dats[1]->setTaggedValueFromCPP(1, viewShape, viewData);
247 jgs 537
248 jfenwick 2005 //
249     // create a full slice
250 jgs 537
251 jfenwick 2005 DataTypes::RegionType region;
252     region.push_back(DataTypes::RegionType::value_type(0,2));
253     region.push_back(DataTypes::RegionType::value_type(0,3));
254 jgs 82
255 jfenwick 2005 Data slice4(dats[1]->getSlice(region));
256 jgs 537
257 jfenwick 2005 assert(slice4.isTagged());
258     assert(slice4.getDataPointRank()==2);
259     assert(getRef(slice4,0,0,0,0)==0);
260     assert(getRef(slice4,0,0,0,1)==2);
261     assert(getRef(slice4,0,0,0,2)==4);
262     assert(getRef(slice4,0,0,1,0)==1);
263     assert(getRef(slice4,0,0,1,1)==3);
264     assert(getRef(slice4,0,0,1,2)==5);
265 jgs 537
266 jfenwick 2005 for (int k=0;k<NUMDATS;++k)
267     {
268     delete dats[k];
269     }
270     }
271 jgs 537
272 jfenwick 2005 {
273     DataTypes::ShapeType viewShape;
274     viewShape.push_back(2);
275     viewShape.push_back(3);
276 jgs 537
277 jfenwick 2005 const int NUMDATS=3;
278     const char* strs[]={"DataConstant", "DataTagged","DataExpanded"};
279     Data* dats[NUMDATS];
280     Data* src[NUMDATS];
281     for (int k=0;k<NUMDATS;++k)
282     {
283     dats[k]=new Data(1.3, viewShape);
284     src[k]=new Data(10,DataTypes::scalarShape);
285 jgs 537 }
286 jfenwick 2005 dats[1]->tag();
287     src[1]->tag();
288     dats[2]->expand();
289     src[2]->expand();
290     if (delayed)
291 jgs 537 {
292 jfenwick 2005 for(int k=0;k<NUMDATS;++k)
293     {
294     if (delayed)
295     {
296     Data* temp=dats[k];
297     dats[k]=new Data(dats[k]->delay()); // coz delay returns an object not a pointer
298     delete temp;
299     temp=src[k];
300     src[k]=new Data(src[k]->delay());
301     delete temp;
302     }
303 jgs 537 }
304 jgs 82 }
305 jfenwick 2005 for (int k=0;k<NUMDATS;++k)
306 jgs 82 {
307 jfenwick 2005 cout << "\t\tTest set-slicing " << strs[k] << endl;
308     Data target(1.3,viewShape);
309     if (k==2) {target.expand();}
310     DataTypes::RegionType region;
311     region.push_back(DataTypes::RegionType::value_type(1,1));
312     region.push_back(DataTypes::RegionType::value_type(1,1));
313     target.setSlice(*(src[k]),region);
314     assert(getRef(target,0,0,1,1)==src[k]->getDataPoint(0,0));
315     }
316    
317     // some extra tests on tagged data
318 jgs 537
319 jfenwick 2005 //
320     // add a value for tag "1" to target
321 jgs 537
322 jfenwick 2005 DataTypes::ValueType viewData(6);
323     for (int i=0;i<viewData.size();i++) {
324     viewData[i]=i;
325     }
326 jgs 537
327 jfenwick 2005 Data target(1.3,viewShape,FunctionSpace(),false);
328     target.tag();
329     target.setTaggedValueFromCPP(1, viewShape, viewData);
330 jgs 537
331 jgs 549 //cout << "target:\n" << target.toString() << endl;
332 jgs 537
333 jgs 82 //
334 jgs 537 // set a slice in target from source
335    
336 jfenwick 2005 DataTypes::RegionType region;
337     region.push_back(DataTypes::RegionType::value_type(0,0));
338     region.push_back(DataTypes::RegionType::value_type(1,1));
339 jgs 537
340 jfenwick 2005 target.setSlice(*src[1],region);
341 jgs 537
342 jfenwick 2005 assert(target.isTagged());
343     assert(target.getDataPointRank()==2);
344     assert(getRef(target,0,0,0,0)==0);
345     assert(getRef(target,0,0,0,1)==src[1]->getDataPoint(0,0));
346     assert(getRef(target,0,0,0,2)==4);
347     assert(getRef(target,0,0,1,0)==1);
348     assert(getRef(target,0,0,1,1)==3);
349     assert(getRef(target,0,0,1,2)==5);
350 jgs 537
351 jfenwick 2005 //
352     // add a value for tag "2" to source
353 jgs 537
354 jfenwick 2005 DataTypes::ShapeType viewShape2;
355     DataTypes::ValueType viewData2(1);
356     viewData2[0]=6;
357     src[1]->setTaggedValueFromCPP(2, viewShape2, viewData2);
358 jgs 537
359 jfenwick 2005 region.clear();
360     region.push_back(DataTypes::RegionType::value_type(0,0));
361     region.push_back(DataTypes::RegionType::value_type(1,1));
362 jgs 537
363 jfenwick 2005 target.setSlice(*src[1],region);
364 jgs 549
365 jfenwick 2005 assert(target.isTagged());
366     assert(target.getDataPointRank()==2);
367 jgs 537
368 jgs 549 // use a non-existant tag so we get a pointer to the default value
369     // ie: the first element in the data array
370 jfenwick 2005 DataAbstract::ValueType::value_type* targetData=target.getSampleDataByTag(9);
371     for (int i=0; i<target.getLength(); i++) {
372 jgs 549 assert(targetData[i]>=0);
373 jgs 537 }
374 jfenwick 2005 assert(targetData[0]==1.3);
375     assert(targetData[1]==1.3);
376     assert(targetData[2]==10);
377     assert(targetData[3]==1.3);
378     assert(targetData[4]==1.3);
379     assert(targetData[5]==1.3);
380     assert(targetData[6]==0);
381     assert(targetData[7]==1);
382     assert(targetData[8]==10);
383     assert(targetData[9]==3);
384     assert(targetData[10]==4);
385     assert(targetData[11]==5);
386     assert(targetData[12]==1.3);
387     assert(targetData[13]==1.3);
388     assert(targetData[14]==6);
389     assert(targetData[15]==1.3);
390     assert(targetData[16]==1.3);
391     assert(targetData[17]==1.3);
392 jgs 537
393 jfenwick 2005
394     for (int k=0;k<NUMDATS;++k)
395 jgs 537 {
396 jfenwick 2005 delete dats[k];
397     delete src[k];
398 jgs 82 }
399    
400 jfenwick 2005 }
401 jgs 82
402 jfenwick 2005 }
403 jgs 537
404 jfenwick 2005 // This is to test new copy routines, existing tests should remain where they are
405     void DataTestCase::testCopying()
406     {
407     cout << "\n\tReadyData." << endl;
408     testCopyingWorker(false);
409     cout << "\n\tLazyData." << endl;
410     testCopyingWorker(true);
411 jgs 82 }
412    
413 jfenwick 2005 void DataTestCase::testSlicing() {
414     cout << "\n\tReadyData." << endl;
415     testSlicingWorker(false);
416     cout << "\n\tLazyData." << endl;
417     testSlicingWorker(true);
418 jgs 82 }
419    
420    
421 jfenwick 2147 void DataTestCase::testSomeDriver(bool autolazy)
422     {
423 jgs 82 cout << endl;
424 jfenwick 2147 SAVELAZYSTATE
425     if (autolazy)
426     {
427     AUTOLAZYON
428     cout << "\tNow testing using autolazy." << endl;
429     }
430 jfenwick 2005 cout << "\tCreate a Data object." << endl;
431 jgs 82
432 jfenwick 1796 DataTypes::ShapeType viewShape;
433 jgs 82 viewShape.push_back(3);
434 jfenwick 1796 DataTypes::ValueType viewData(3);
435 jgs 82 for (int i=0;i<viewShape[0];++i) {
436 jgs 121 viewData[i]=i;
437 jgs 82 }
438    
439     bool expanded=true;
440 jfenwick 1796 Data exData(viewData,viewShape,FunctionSpace(),expanded);
441     Data cData(viewData,viewShape);
442 jgs 82 Data result;
443 jgs 123
444 jgs 82 assert(exData.isExpanded());
445     assert(cData.isConstant());
446     assert(result.isEmpty());
447    
448     cout << "\tTest some basic operations" << endl;
449     result=exData*cData;
450 jfenwick 2147 cout << CHECKAUTOLAZY << " " << result.isLazy() << " " << result.isExpanded()<< endl;
451     assert(CHECKAUTOLAZY?result.isLazy():result.isExpanded());
452 jgs 82
453 jgs 538 assert(result.Lsup()==4);
454     assert(result.sup()==4);
455     assert(result.inf()==0);
456    
457     result=exData+cData;
458     result=exData-cData;
459     result=exData/cData;
460    
461     cout << "\tExercise wherePositive method" << endl;
462     assert(!exData.wherePositive().isEmpty());
463    
464     cout << "\tExercise copyWithMask method" << endl;
465     exData.copyWithMask(result, exData.wherePositive());
466     assert(!exData.wherePositive().isEmpty());
467 jfenwick 2147 RESTORELAZYSTATE
468 jgs 538
469 jgs 82 }
470    
471 jfenwick 2147 void DataTestCase::testSome() {
472     testSomeDriver(false);
473     testSomeDriver(true);
474     }
475 jfenwick 2005
476    
477 jfenwick 2147
478 jfenwick 2005 // This method tests to see if resolve() produces results of the correct type
479     void DataTestCase::testResolveType()
480     {
481     cout << endl;
482     cout << "\tTesting resolve()\n";
483     DataTypes::ShapeType viewShape;
484     viewShape.push_back(2);
485     viewShape.push_back(3);
486     viewShape.push_back(4);
487     DataTypes::ValueType viewData(2*3*4);
488     for (int i=0;i<DataTypes::noValues(viewShape);++i) {
489     viewData[i]=i;
490     }
491     Data c1(viewData,viewShape);
492     Data t1(viewData,viewShape);
493     Data e1(viewData,viewShape);
494     t1.tag();
495     e1.expand();
496     c1.delaySelf();
497     t1.delaySelf();
498     e1.delaySelf();
499     Data d1=c1+c1;
500     assert(d1.isLazy());
501     assert((d1.resolve(),d1.isConstant()));
502     d1=c1+t1;
503     assert(d1.isLazy());
504     assert((d1.resolve(),d1.isTagged()));
505     d1=t1+c1;
506     assert(d1.isLazy());
507     assert((d1.resolve(),d1.isTagged()));
508     d1=t1+t1;
509     assert(d1.isLazy());
510     assert((d1.resolve(),d1.isTagged()));
511     d1=c1+e1;
512     assert(d1.isLazy());
513     assert((d1.resolve(),d1.isExpanded()));
514     d1=e1+c1;
515     assert(d1.isLazy());
516     assert((d1.resolve(),d1.isExpanded()));
517     d1=e1+t1;
518     assert(d1.isLazy());
519     assert((d1.resolve(),d1.isExpanded()));
520     d1=t1+e1;
521     assert(d1.isLazy());
522     assert((d1.resolve(),d1.isExpanded()));
523     d1=e1+e1;
524     assert(d1.isLazy());
525     assert((d1.resolve(),d1.isExpanded()));
526     cout << "\tTesting tag()\n";
527     c1.tag();
528     assert(c1.isTagged());
529     t1.tag();
530     assert(t1.isTagged());
531     try
532     {
533     e1.tag();
534     assert(false); // this should have thrown
535     } catch(...) {}
536     cout << "\tTesting expand()\n";
537     Data c2(viewData,viewShape);
538     Data t2(viewData,viewShape);
539     Data e2(viewData,viewShape);
540     t2.tag();
541     e2.expand();
542     c2.delaySelf();
543     t2.delaySelf();
544     e2.delaySelf();
545     c2.expand();
546     assert(c2.isExpanded());
547     t2.expand();
548     assert(t2.isExpanded());
549     e2.expand();
550     assert(e2.isExpanded());
551     }
552    
553 jgs 82 void DataTestCase::testDataConstant() {
554    
555     cout << endl;
556    
557 jfenwick 2005 cout << "\tCreate a DataConstant object." << endl;
558 jgs 82
559 jfenwick 1796 DataTypes::ShapeType viewShape;
560 jgs 82 viewShape.push_back(2);
561     viewShape.push_back(3);
562     viewShape.push_back(4);
563 jfenwick 1796 DataTypes::ValueType viewData(2*3*4);
564     for (int i=0;i<DataTypes::noValues(viewShape);++i) {
565 jgs 121 viewData[i]=i;
566 jgs 82 }
567    
568 jfenwick 1796 Data left(viewData,viewShape);
569     Data right(viewData,viewShape);
570 jgs 82 Data result;
571    
572     cout << "\tTest some basic operations" << endl;
573    
574     result=left-right;
575    
576     assert(left.isConstant());
577     assert(right.isConstant());
578     assert(result.isConstant());
579    
580     result=left+right;
581    
582     assert(left.isConstant());
583     assert(right.isConstant());
584     assert(result.isConstant());
585    
586     assert(!result.isExpanded());
587     assert(!result.isTagged());
588    
589     }
590    
591     void DataTestCase::testDataTagged() {
592    
593     cout << endl;
594    
595 jgs 539 {
596 jgs 82
597 jgs 539 cout << "\tCreate a DataTagged object with a default value only." << endl;
598 jgs 511
599 jfenwick 1796 DataTypes::ShapeType viewShape;
600 jgs 539 viewShape.push_back(3);
601 jgs 82
602 jfenwick 1796 DataTypes::ValueType viewData(3);
603 jgs 539 for (int i=0;i<viewShape[0];i++) {
604     viewData[i]=i;
605     }
606 jfenwick 1796 int arr[1]={1}; // iso c++ does not like empty arrays
607     DataTagged* dt=new DataTagged(FunctionSpace(),viewShape,arr,viewData);
608     Data myData(dt);
609 jgs 511
610 jgs 539 assert(!myData.isEmpty());
611     assert(myData.isTagged());
612     assert(myData.getTagNumber(0)==1);
613     assert(myData.getDataPointRank()==1);
614     assert(myData.getLength()==3);
615 jfenwick 1796
616     assert(myData.getNoValues()==3);
617     assert(myData.getDataAtOffset(0)==0.0);
618     assert(myData.getDataAtOffset(1)==1.0);
619     assert(myData.getDataAtOffset(2)==2.0);
620 jgs 82
621 jgs 539 double* sampleData=myData.getSampleData(0);
622 jfenwick 1796 for (int i=0; i<myData.getNoValues(); i++) {
623 jgs 539 assert(sampleData[i]==i);
624     }
625     // use a non-existent tag so we get a pointer to
626     // the first element of the data array
627     sampleData=myData.getSampleDataByTag(9);
628     for (int i=0; i<myData.getLength(); i++) {
629     assert(sampleData[i]==i);
630     }
631 jgs 511
632 jgs 539 cout << "\tTest setting of a tag and associated value." << endl;
633 jgs 511
634 jgs 539 // value for tag "1"
635 jfenwick 1796 DataTypes::ValueType eTwoData(viewData);
636     // DataArrayView eTwoView(eTwoData, viewShape);
637     for (int i=0;i<viewShape[0];i++) {
638     eTwoData[i]=i+2.0;
639 jgs 539 }
640    
641 jfenwick 1796 myData.setTaggedValueFromCPP(1,viewShape, eTwoData);
642 jgs 539
643     assert(myData.getLength()==6);
644    
645 jfenwick 1796 int offset=myData.getDataOffset(0,0);
646     assert(offset==3);
647     assert(myData.getDataPointRank()==1);
648     assert(myData.getNoValues()==3);
649 jgs 539
650 jfenwick 1796 assert(myData.getDataAtOffset(offset+0)==2);
651     assert(myData.getDataAtOffset(offset+1)==3);
652     assert(myData.getDataAtOffset(offset+2)==4);
653    
654 jgs 539 sampleData=myData.getSampleDataByTag(1);
655 jfenwick 1796 for (int i=0; i<myData.getNoValues(); i++) {
656 jgs 539 assert(sampleData[i]==i+2);
657     }
658    
659 jgs 511 }
660    
661 jgs 539 {
662 jgs 511
663 jgs 539 cout << "\tCreate a DataTagged object via tag() method." << endl;
664 jgs 511
665 jfenwick 1796 DataTypes::ShapeType viewShape;
666 jgs 539 viewShape.push_back(2);
667     viewShape.push_back(3);
668     Data myData(1.3,viewShape,FunctionSpace(),false);
669     myData.tag();
670 jgs 511
671 jgs 539 assert(!myData.isEmpty());
672     assert(myData.isTagged());
673     assert(myData.getTagNumber(0)==1);
674     assert(myData.getDataPointRank()==2);
675     assert(myData.getLength()==6);
676    
677     // check default value
678 jfenwick 1796 assert(!myData.isEmpty());
679     assert(myData.getDataPointRank()==2);
680     assert(myData.getNoValues()==6);
681     assert(myData.getDataPointShape().size()==2);
682     assert(getRef(myData,0,0)==1.3);
683     assert(getRef(myData,0,1)==1.3);
684     assert(getRef(myData,0,2)==1.3);
685     assert(getRef(myData,1,0)==1.3);
686     assert(getRef(myData,1,1)==1.3);
687     assert(getRef(myData,1,2)==1.3);
688 jgs 539
689     // check value for data-point (0,0).
690 jfenwick 1796 // myDataView = myData.getDataPoint(0,0);
691     assert(!myData.isEmpty());
692     // assert(myDataView.getOffset()==0);
693     assert(myData.getDataPointRank()==2);
694     assert(myData.getNoValues()==6);
695     assert(myData.getDataPointShape().size()==2);
696     assert(getRef(myData,0,0)==1.3);
697     assert(getRef(myData,0,1)==1.3);
698     assert(getRef(myData,0,2)==1.3);
699     assert(getRef(myData,1,0)==1.3);
700     assert(getRef(myData,1,1)==1.3);
701     assert(getRef(myData,1,2)==1.3);
702 jgs 539
703 jgs 511 }
704    
705 jgs 82 }
706    
707 jgs 538 void DataTestCase::testDataTaggedExceptions() {
708    
709     cout << endl;
710    
711     cout << "\tTest DataTagged exceptions." << endl;
712    
713     Data myData;
714    
715     try {
716     myData.getSampleDataByTag(0);;
717     assert(false);
718     }
719 phornby 1455 catch (EsysException&) {
720 jgs 538 //cout << e.what() << endl;
721     assert(true);
722     }
723    
724     try {
725 jfenwick 1796 myData.setTaggedValueFromCPP(0,DataTypes::ShapeType(), DataTypes::ValueType());;
726 jgs 538 assert(false);
727     }
728 phornby 1455 catch (EsysException&) {
729 jgs 538 //cout << e.what() << endl;
730     assert(true);
731     }
732    
733     }
734    
735 jgs 82 void DataTestCase::testConstructors() {
736    
737     cout << endl;
738    
739 jfenwick 1796 DataTypes::ShapeType viewShape;
740 jgs 82 {
741     cout << "\tCreate an Empty Data object" << endl;
742     Data temp(1.3,viewShape,FunctionSpace(),false);
743     }
744     {
745     cout << "\tCreate a rank 2 Data object" << endl;
746     viewShape.push_back(2);
747     viewShape.push_back(3);
748     Data temp(1.3,viewShape,FunctionSpace(),false);
749     }
750     }
751    
752 jgs 113
753 jfenwick 2005 void DataTestCase::testOperations()
754     {
755    
756 jgs 82 cout << endl;
757    
758 jfenwick 2005 // define the shape for the test data
759 jfenwick 1796 DataTypes::ShapeType shape;
760 jgs 113 shape.push_back(2);
761     shape.push_back(3);
762 jgs 82
763 jfenwick 2005 // allocate the data
764 jfenwick 1796 DataTypes::ValueType data(DataTypes::noValues(shape),0);
765 jgs 82
766 jgs 113 // assign values to the data
767     for (int i=0;i<shape[0];i++) {
768     for (int j=0;j<shape[1];j++) {
769 jfenwick 1796 data[getRelIndex(shape,i,j)]=getRelIndex(shape,i,j);
770 jgs 113 }
771     }
772    
773 jfenwick 2005
774    
775     Data dats[]={Data(data,shape,FunctionSpace(),false),
776     Data(data,shape,FunctionSpace(),false),
777     Data(data,shape,FunctionSpace(),true),
778     Data(data,shape,FunctionSpace(),false),
779     Data(data,shape,FunctionSpace(),false),
780     Data(data,shape,FunctionSpace(),true)};
781     const int NUMDATS=6;
782     const int LAZY=3; // where do the lazy objects start?
783    
784     // Data baseEx(data,shape,FunctionSpace(),true);
785     // Data baseCon(data,shape,FunctionSpace(),false);
786     // Data baseTag(data,shape,FunctionSpace(),false);
787     Data& baseCon=dats[0];
788     Data& baseTag=dats[1];
789     Data& baseEx=dats[2];
790 jgs 563 baseTag.tag();
791 jfenwick 2005 dats[4].tag();
792     dats[3].delaySelf();
793     dats[4].delaySelf();
794     dats[5].delaySelf();
795 jgs 113
796 jgs 563 assert(baseEx.isExpanded());
797     assert(baseCon.isConstant());
798     assert(baseTag.isTagged());
799    
800 jfenwick 2005 Data results[NUMDATS];
801 jfenwick 2037 // Data& resultEx=results[0];
802     // Data& resultCon=results[1];
803     // Data& resultTag=results[2];
804 jgs 563
805 jfenwick 2005 // create 0 <= smalldata <= 1 for testing trig functions
806    
807     DataTypes::ValueType smalldata(DataTypes::noValues(shape),0);
808    
809     // assign values to the data
810     for (int i=0;i<shape[0];i++) {
811     for (int j=0;j<shape[1];j++) {
812     smalldata[getRelIndex(shape,i,j)]=(i==0 && j==0)?0:1.0/(getRelIndex(shape,i,j)+1);
813     }
814     }
815     Data sdats[]={Data(smalldata,shape,FunctionSpace(),false),
816     Data(smalldata,shape,FunctionSpace(),false),
817     Data(smalldata,shape,FunctionSpace(),true),
818     Data(smalldata,shape,FunctionSpace(),false),
819     Data(smalldata,shape,FunctionSpace(),false),
820     Data(smalldata,shape,FunctionSpace(),true)};
821     sdats[1].tag();
822     sdats[4].tag();
823     sdats[3].delaySelf();
824     sdats[4].delaySelf();
825     sdats[5].delaySelf();
826    
827    
828    
829 jgs 113 // test unary operations
830    
831 gross 713 double tmp;
832 jgs 113 cout << "\tTest Data::pow." << endl;
833     Data power(3.0,shape,FunctionSpace(),true);
834 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
835     {
836     results[z].copy(dats[z].powD(power));
837     if (z>=LAZY)
838     {
839     assert(results[z].isLazy());
840     }
841     }
842 jgs 113 for (int i=0;i<shape[0];i++) {
843     for (int j=0;j<shape[1];j++) {
844 jfenwick 1796 tmp=pow((double)data[getRelIndex(shape,i,j)],(double)3.0);
845 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
846     {
847     assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
848     }
849 jgs 113 }
850     }
851 jgs 82
852 jgs 113 cout << "\tTest Data::sin." << endl;
853 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
854     {
855     results[z].copy(dats[z].sin());
856     if (z>=LAZY)
857     {
858     assert(results[z].isLazy());
859     }
860     }
861 gross 713 for (int i=0;i<shape[0];i++) {
862     for (int j=0;j<shape[1];j++) {
863 jfenwick 1796 tmp=sin((double)data[getRelIndex(shape,i,j)]);
864 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
865     {
866     assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
867     }
868 gross 713 }
869     }
870 jgs 102
871 jgs 113 cout << "\tTest Data::cos." << endl;
872 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
873     {
874     results[z].copy(dats[z].cos());
875     if (z>=LAZY)
876     {
877     assert(results[z].isLazy());
878     }
879     }
880 gross 713 for (int i=0;i<shape[0];i++) {
881     for (int j=0;j<shape[1];j++) {
882 jfenwick 1796 tmp=cos((double)data[getRelIndex(shape,i,j)]);
883 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
884     {
885     assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
886     }
887 gross 713 }
888     }
889 jgs 102
890 jgs 113 cout << "\tTest Data::tan." << endl;
891 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
892     {
893     results[z].copy(dats[z].tan());
894     if (z>=LAZY)
895     {
896     assert(results[z].isLazy());
897     }
898     }
899 gross 713 for (int i=0;i<shape[0];i++) {
900     for (int j=0;j<shape[1];j++) {
901 jfenwick 1796 tmp=tan((double)data[getRelIndex(shape,i,j)]);
902 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
903     {
904     assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
905     }
906 gross 713 }
907     }
908 jgs 102
909 jgs 150 cout << "\tTest Data::asin." << endl;
910 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
911     {
912     results[z].copy(sdats[z].asin());
913     if (z>=LAZY)
914     {
915     assert(results[z].isLazy());
916     }
917     }
918     for (int i=0;i<shape[0];i++) {
919     for (int j=0;j<shape[1];j++) {
920     tmp=asin((double)smalldata[getRelIndex(shape,i,j)]);
921     for (int z=0;z<NUMDATS;++z)
922     {
923     assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
924     }
925     }
926     }
927 jgs 150
928     cout << "\tTest Data::acos." << endl;
929 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
930     {
931     results[z].copy(sdats[z].acos());
932     if (z>=LAZY)
933     {
934     assert(results[z].isLazy());
935     }
936     }
937     for (int i=0;i<shape[0];i++) {
938     for (int j=0;j<shape[1];j++) {
939     tmp=acos((double)smalldata[getRelIndex(shape,i,j)]);
940     for (int z=0;z<NUMDATS;++z)
941     {
942     assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
943     }
944     }
945     }
946 jgs 150
947     cout << "\tTest Data::atan." << endl;
948 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
949     {
950     results[z].copy(sdats[z].atan());
951     if (z>=LAZY)
952     {
953     assert(results[z].isLazy());
954     }
955     }
956 gross 713 for (int i=0;i<shape[0];i++) {
957     for (int j=0;j<shape[1];j++) {
958 jfenwick 2005 tmp=atan((double)smalldata[getRelIndex(shape,i,j)]);
959     for (int z=0;z<NUMDATS;++z)
960     {
961     assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
962     }
963 gross 713 }
964     }
965 jgs 150
966     cout << "\tTest Data::sinh." << endl;
967 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
968     {
969     results[z].copy(dats[z].sinh());
970     if (z>=LAZY)
971     {
972     assert(results[z].isLazy());
973     }
974     }
975 gross 713 for (int i=0;i<shape[0];i++) {
976     for (int j=0;j<shape[1];j++) {
977 jfenwick 1796 tmp=sinh((double)data[getRelIndex(shape,i,j)]);
978 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
979     {
980     assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
981     }
982 gross 713 }
983     }
984 jgs 150
985     cout << "\tTest Data::cosh." << endl;
986 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
987     {
988     results[z].copy(dats[z].cosh());
989     if (z>=LAZY)
990     {
991     assert(results[z].isLazy());
992     }
993     }
994 gross 713 for (int i=0;i<shape[0];i++) {
995     for (int j=0;j<shape[1];j++) {
996 jfenwick 1796 tmp=cosh((double)data[getRelIndex(shape,i,j)]);
997 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
998     {
999     assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1000     }
1001 gross 713 }
1002     }
1003 jgs 150
1004     cout << "\tTest Data::tanh." << endl;
1005 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
1006     {
1007     results[z].copy(dats[z].tanh());
1008     if (z>=LAZY)
1009     {
1010     assert(results[z].isLazy());
1011     }
1012     }
1013 gross 713 for (int i=0;i<shape[0];i++) {
1014     for (int j=0;j<shape[1];j++) {
1015 jfenwick 1796 tmp=tanh((double)data[getRelIndex(shape,i,j)]);
1016 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
1017     {
1018     assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1019     }
1020 gross 713 }
1021     }
1022 jgs 150
1023 jfenwick 2005 // rather than accomodate the different windows operations directly I'll just use inverse functions
1024 jgs 150 cout << "\tTest Data::asinh." << endl;
1025 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
1026     {
1027     results[z].copy(dats[z].asinh().sinh());
1028     if (z>=LAZY)
1029     {
1030     assert(results[z].isLazy());
1031     }
1032     }
1033     for (int i=0;i<shape[0];i++) {
1034     for (int j=0;j<shape[1];j++) {
1035     tmp=data[getRelIndex(shape,i,j)];
1036     for (int z=0;z<NUMDATS;++z)
1037     {
1038     assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1039     }
1040     }
1041     }
1042 jgs 150
1043     cout << "\tTest Data::acosh." << endl;
1044 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
1045     {
1046     results[z].copy(dats[z].acosh().cosh());
1047     if (z>=LAZY)
1048     {
1049     assert(results[z].isLazy());
1050     }
1051     }
1052     for (int i=0;i<shape[0];i++) {
1053     for (int j=0;j<shape[1];j++) {
1054     if (i==0 && j==0) break;
1055     tmp=data[getRelIndex(shape,i,j)];
1056     for (int z=0;z<NUMDATS;++z)
1057     {
1058     assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1059     }
1060     }
1061     }
1062 jgs 150
1063     cout << "\tTest Data::atanh." << endl;
1064 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
1065     {
1066     results[z].copy(dats[z].tanh().atanh()); // if these are the other way around the results are
1067     if (z>=LAZY) // undefined
1068     {
1069     assert(results[z].isLazy());
1070     }
1071     }
1072     for (int i=0;i<shape[0];i++) {
1073     for (int j=0;j<shape[1];j++) {
1074     tmp=data[getRelIndex(shape,i,j)];
1075     for (int z=0;z<NUMDATS;++z)
1076     {
1077     assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1078     }
1079     }
1080     }
1081 jgs 150
1082 jgs 113 cout << "\tTest Data::log." << endl;
1083 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
1084     {
1085     results[z].copy(dats[z].log());
1086     if (z>=LAZY)
1087     {
1088     assert(results[z].isLazy());
1089     }
1090     }
1091     for (int i=0;i<shape[0];i++) {
1092     for (int j=0;j<shape[1];j++) {
1093     if (i==0 && j==0) break;
1094     tmp=log((double)data[getRelIndex(shape,i,j)]);
1095     for (int z=0;z<NUMDATS;++z)
1096     {
1097     assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1098     }
1099     }
1100     }
1101 jgs 102
1102 jfenwick 2005 cout << "\tTest Data::log10." << endl;
1103     for (int z=0;z<NUMDATS;++z)
1104     {
1105     results[z].copy(dats[z].log10());
1106     if (z>=LAZY)
1107     {
1108     assert(results[z].isLazy());
1109     }
1110     }
1111     for (int i=0;i<shape[0];i++) {
1112     for (int j=0;j<shape[1];j++) {
1113     if (i==0 && j==0) break;
1114     tmp=log10((double)data[getRelIndex(shape,i,j)]);
1115     for (int z=0;z<NUMDATS;++z)
1116     {
1117     assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1118     }
1119     }
1120     }
1121 phornby 2051 #if defined (_WIN32) && !defined (__INTEL_COMPILER)
1122 phornby 2053 cout << "\tSkip test Data::erf on windows with MSVC compiler." << endl;
1123     #else
1124 jfenwick 2005 cout << "\tTest Data::erf." << endl;
1125     for (int z=0;z<NUMDATS;++z)
1126     {
1127     results[z].copy(dats[z].erf());
1128     if (z>=LAZY)
1129     {
1130     assert(results[z].isLazy());
1131     }
1132     }
1133     for (int i=0;i<shape[0];i++) {
1134     for (int j=0;j<shape[1];j++) {
1135     if (i==0 && j==0) break;
1136     tmp=erf((double)data[getRelIndex(shape,i,j)]);
1137     for (int z=0;z<NUMDATS;++z)
1138     {
1139     assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1140     }
1141     }
1142     }
1143     #endif
1144    
1145    
1146 jgs 113 cout << "\tTest Data::abs." << endl;
1147 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
1148     {
1149     results[z].copy(dats[z].abs());
1150     if (z>=LAZY)
1151     {
1152     assert(results[z].isLazy());
1153     }
1154     }
1155 gross 713 for (int i=0;i<shape[0];i++) {
1156     for (int j=0;j<shape[1];j++) {
1157 jfenwick 1796 tmp=abs((double)data[getRelIndex(shape,i,j)]);
1158 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
1159     {
1160     assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1161     }
1162 gross 713 }
1163     }
1164 jgs 102
1165 jfenwick 2005 cout << "\tTest Data::sign (positive)." << endl;
1166     for (int z=0;z<NUMDATS;++z)
1167     {
1168     results[z].copy(dats[z].sign());
1169     if (z>=LAZY)
1170     {
1171     assert(results[z].isLazy());
1172     }
1173     }
1174     for (int i=0;i<shape[0];i++) {
1175     for (int j=0;j<shape[1];j++) {
1176     tmp=(i==0 && j==0)?0:1;
1177     for (int z=0;z<NUMDATS;++z)
1178     {
1179     assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1180     }
1181     }
1182     }
1183 jgs 102
1184 jfenwick 2005 cout << "\tTest Data::sign (negative)." << endl;
1185     for (int z=0;z<NUMDATS;++z)
1186     {
1187     results[z].copy(dats[z].neg().sign());
1188     if (z>=LAZY)
1189     {
1190     assert(results[z].isLazy());
1191     }
1192     }
1193     for (int i=0;i<shape[0];i++) {
1194     for (int j=0;j<shape[1];j++) {
1195     tmp=(i==0 && j==0)?0:-1;
1196     for (int z=0;z<NUMDATS;++z)
1197     {
1198     assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1199     }
1200     }
1201     }
1202    
1203    
1204 jgs 113 cout << "\tTest Data::exp." << endl;
1205 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
1206     {
1207     results[z].copy(dats[z].exp());
1208     if (z>=LAZY)
1209     {
1210     assert(results[z].isLazy());
1211     }
1212     }
1213 gross 713 for (int i=0;i<shape[0];i++) {
1214     for (int j=0;j<shape[1];j++) {
1215 jfenwick 1796 tmp=exp((double)data[getRelIndex(shape,i,j)]);
1216 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
1217     {
1218     assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1219     }
1220 gross 713 }
1221     }
1222 jgs 102
1223 jgs 113 cout << "\tTest Data::sqrt." << endl;
1224 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
1225     {
1226     results[z].copy(dats[z].sqrt());
1227     if (z>=LAZY)
1228     {
1229     assert(results[z].isLazy());
1230     }
1231     }
1232 gross 713 for (int i=0;i<shape[0];i++) {
1233     for (int j=0;j<shape[1];j++) {
1234 jfenwick 1796 tmp=sqrt((double)data[getRelIndex(shape,i,j)]);
1235 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
1236     {
1237     assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1238     }
1239 gross 713 }
1240     }
1241 jgs 102
1242 jgs 113 cout << "\tTest Data::neg." << endl;
1243 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
1244     {
1245     results[z].copy(dats[z].neg());
1246     if (z>=LAZY)
1247     {
1248     assert(results[z].isLazy());
1249     }
1250     }
1251     for (int i=0;i<shape[0];i++) {
1252     for (int j=0;j<shape[1];j++) {
1253     tmp=-data[getRelIndex(shape,i,j)];
1254     for (int z=0;z<NUMDATS;++z)
1255     {
1256     assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1257     }
1258     }
1259     }
1260 jgs 102
1261 jgs 113 cout << "\tTest Data::pos." << endl;
1262 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
1263     {
1264     results[z].copy(dats[z].pos());
1265     if (z>=LAZY)
1266     {
1267     assert(results[z].isLazy());
1268     }
1269     }
1270 jgs 113 for (int i=0;i<shape[0];i++) {
1271     for (int j=0;j<shape[1];j++) {
1272 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
1273     {
1274     assert(std::abs(getRef(results[z],i,j) - getRelIndex(shape,i,j)) <= REL_TOL*std::abs(data[getRelIndex(shape,i,j)]));
1275     }
1276 jgs 113 }
1277     }
1278 jgs 102
1279 jgs 113 // test reduction operations
1280    
1281     cout << "\tTest Data::Lsup." << endl;
1282 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
1283     {
1284     assert(std::abs(dats[z].Lsup() - 5) <= REL_TOL*5);
1285     }
1286 jgs 113
1287     cout << "\tTest Data::sup." << endl;
1288 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
1289     {
1290     assert(std::abs(dats[z].sup() - 5) <= REL_TOL*5);
1291     }
1292 jgs 113
1293     cout << "\tTest Data::inf." << endl;
1294 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
1295     {
1296     assert(std::abs(dats[z].inf() - 0) <= REL_TOL*0);
1297     }
1298 jgs 113
1299     // test data-point reduction operations
1300    
1301     cout << "\tTest Data::minval." << endl;
1302 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
1303     {
1304     results[z].copy(dats[z].minval());
1305     }
1306     for (int z=0;z<NUMDATS;++z)
1307     {
1308     assert(std::abs(results[z].getDataAtOffset(0) - 0) <= REL_TOL*0);
1309     }
1310    
1311 jgs 102
1312 jgs 113 cout << "\tTest Data::maxval." << endl;
1313 jfenwick 2005 for (int z=0;z<NUMDATS;++z)
1314     {
1315     results[z].copy(dats[z].maxval());
1316     }
1317     for (int z=0;z<NUMDATS;++z)
1318     {
1319     assert(std::abs(results[z].getDataAtOffset(0) - 5) <= REL_TOL*5);
1320     }
1321 jgs 102
1322 jgs 82 }
1323    
1324 jgs 110
1325 jfenwick 2005 // Here we test the binary operators in complex expressions
1326     void DataTestCase::testBinary()
1327     {
1328    
1329     cout << endl;
1330    
1331     // define the shape for the test data
1332     DataTypes::ShapeType shape;
1333     shape.push_back(2);
1334     shape.push_back(3);
1335    
1336     // allocate the data
1337     DataTypes::ValueType data(DataTypes::noValues(shape),0);
1338    
1339     // assign values to the data
1340     for (int i=0;i<shape[0];i++) {
1341     for (int j=0;j<shape[1];j++) {
1342     data[getRelIndex(shape,i,j)]=getRelIndex(shape,i,j)+2; // so we get no zeros
1343     }
1344     }
1345    
1346    
1347     Data one(1.0,DataTypes::scalarShape,FunctionSpace());
1348     Data two(2.0,DataTypes::scalarShape,FunctionSpace());
1349     Data dats[]={Data(data,shape,FunctionSpace(),false),
1350     Data(data,shape,FunctionSpace(),false),
1351     Data(data,shape,FunctionSpace(),true),
1352     Data(data,shape,FunctionSpace(),false),
1353     Data(data,shape,FunctionSpace(),false),
1354     Data(data,shape,FunctionSpace(),true)};
1355     dats[1].tag();
1356     dats[4].tag();
1357     const int NUMDATS=6;
1358     const int LAZY=3;
1359     dats[3].delaySelf();
1360     dats[4].delaySelf();
1361     dats[5].delaySelf();
1362     for (int z=0;z<NUMDATS;++z)
1363     {
1364     Data& a=dats[z];
1365     Data r1=(((a+a)/two)+a-a)*one; // scalar/*, matrix+-
1366     Data r2=(((a*a)/a+one)-one); // scalar+-, matrix*/
1367     Data r3=(a.powD(two)/a.powD(one)); // scalar power
1368     Data r4=a.powD(a); // matrix power
1369     if (z>LAZY)
1370     {
1371     assert(r1.isLazy() && r2.isLazy() && r3.isLazy() && r4.isLazy());
1372     }
1373     for (int i=0;i<DataTypes::noValues(shape);++i)
1374     {
1375     assert(std::abs(r1.getDataAtOffset(i)-data[i]) <= REL_TOL*data[i]);
1376     assert(std::abs(r2.getDataAtOffset(i)-data[i]) <= REL_TOL*data[i]);
1377     assert(std::abs(r3.getDataAtOffset(i)-data[i]) <= REL_TOL*data[i]);
1378     assert(std::abs(r4.getDataAtOffset(i)-pow(data[i],i)) <=REL_TOL*pow(data[i],i));
1379     }
1380     }
1381     }
1382    
1383    
1384 jgs 151 void DataTestCase::testMemAlloc() {
1385    
1386     //
1387     // Simple little sanity check for the memory allocator
1388    
1389     cout << endl;
1390    
1391     Data *testData;
1392     for (int i=0; i<1000; i++) {
1393 jfenwick 1796 testData = new Data(0.0, DataTypes::ShapeType(), FunctionSpace(), true);
1394 jgs 151 delete testData;
1395     }
1396    
1397 jfenwick 1796 DataTypes::ShapeType viewShape;
1398 jgs 151 viewShape.push_back(10);
1399     viewShape.push_back(10);
1400     viewShape.push_back(10);
1401    
1402     Data *testData2;
1403     Data *testData3 = new Data(0.0, viewShape, FunctionSpace(), true);
1404     for (int i=0; i<1000; i++) {
1405     testData2 = new Data(0.0, viewShape, FunctionSpace(), true);
1406     delete testData2;
1407     }
1408     delete testData3;
1409    
1410     }
1411    
1412 jgs 82 TestSuite* DataTestCase::suite ()
1413     {
1414     //
1415     // create the suite of tests to perform.
1416     TestSuite *testSuite = new TestSuite ("DataTestCase");
1417 jfenwick 1799 testSuite->addTest (new TestCaller< DataTestCase>("testCopying",&DataTestCase::testCopying));
1418 jfenwick 2005 testSuite->addTest (new TestCaller< DataTestCase>("testSome",&DataTestCase::testSome));
1419 jgs 82 testSuite->addTest (new TestCaller< DataTestCase>("testDataConstant",&DataTestCase::testDataConstant));
1420     testSuite->addTest (new TestCaller< DataTestCase>("testDataTagged",&DataTestCase::testDataTagged));
1421     testSuite->addTest (new TestCaller< DataTestCase>("testDataTaggedExceptions",&DataTestCase::testDataTaggedExceptions));
1422     testSuite->addTest (new TestCaller< DataTestCase>("testConstructors",&DataTestCase::testConstructors));
1423     testSuite->addTest (new TestCaller< DataTestCase>("testSlicing",&DataTestCase::testSlicing));
1424     testSuite->addTest (new TestCaller< DataTestCase>("testOperations",&DataTestCase::testOperations));
1425 jgs 110 //testSuite->addTest (new TestCaller< DataTestCase>("testRefValue",&DataTestCase::testRefValue));
1426 jgs 151 testSuite->addTest (new TestCaller< DataTestCase>("testMemAlloc",&DataTestCase::testMemAlloc));
1427 jfenwick 2005 testSuite->addTest (new TestCaller< DataTestCase>("Resolving",&DataTestCase::testResolveType));
1428 jgs 82
1429     return testSuite;
1430     }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26