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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2271 - (hide annotations)
Mon Feb 16 05:08:29 2009 UTC (10 years, 8 months ago) by jfenwick
File size: 35317 byte(s)
Merging version 2269 to trunk

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