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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26