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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2881 - (show annotations)
Thu Jan 28 02:03:15 2010 UTC (9 years, 8 months ago) by jfenwick
File size: 38428 byte(s)
Don't panic.
Updating copyright stamps

1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2010 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 <iostream>
16 #if defined (_WIN32) && defined (__INTEL_COMPILER)
17 #include <mathimf.h>
18 #else
19 #include <math.h>
20 #endif
21
22 #include "DataTestCase.h"
23
24 #include "escript/FunctionSpace.h"
25 #include "esysUtils/EsysException.h"
26
27 #include "escript/Data.h"
28 #include "escript/DataLazy.h"
29 #include "escript/EscriptParams.h"
30
31 #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
37
38
39 using namespace std;
40 using namespace CppUnitTest;
41 using namespace escript;
42 using namespace esysUtils;
43 using namespace escript::DataTypes;
44
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
56
57 namespace
58 {
59
60 inline
61 DataTypes::ValueType::const_reference
62 getRef(Data& d,int s1, int p1, int x, int y)
63 {
64 return d.getDataAtOffsetRO(d.getDataOffset(s1,p1)+getRelIndex(d.getDataPointShape(),x,y));
65 }
66
67 inline
68 DataTypes::ValueType::const_reference
69 getRef(Data& d, int x, int y)
70 {
71 return d.getDataAtOffsetRO(getRelIndex(d.getDataPointShape(),x,y));
72 }
73
74 }
75
76
77 void DataTestCase::testCopyingWorker(bool delayed)
78 {
79
80 using namespace escript::DataTypes;
81 cout << endl;
82
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 {
95 dats[0]->delaySelf();
96 dats[1]->delaySelf();
97 dats[2]->delaySelf();
98 }
99
100 for (int k=0;k<NUMDATS;++k)
101 {
102 cout << "\tTest deep copy " << strs[k] << endl;
103 Data* d=dats[k];
104 Data deep=d->copySelf(); // test self copy
105 if (delayed)
106 {
107 assert(deep.isLazy());
108 }
109 for (int i=0;i<DataTypes::noValues(shape);++i)
110 {
111 if (d->getDataAtOffsetRO(i)!=deep.getDataAtOffsetRO(i))
112 assert(false);
113 }
114 if (delayed)
115 {
116 d->delaySelf();
117 }
118 d->setToZero();
119 if (delayed)
120 {
121 assert(d->isLazy());
122 }
123 for (int i=0;i<DataTypes::noValues(shape);++i)
124 {
125 if (d->getDataAtOffsetRO(i)==deep.getDataAtOffsetRO(i))
126 assert(false);
127 }
128 if (delayed)
129 {
130 d->delaySelf();
131 deep.delaySelf();
132 }
133 d->copy(deep); // test copy from object
134 if (delayed)
135 {
136 assert(d->isLazy());
137 }
138 for (int i=0;i<DataTypes::noValues(shape);++i)
139 {
140 if (d->getDataAtOffsetRO(i)!=deep.getDataAtOffsetRO(i))
141 assert(false);
142 }
143 d->setToZero();
144 for (int i=0;i<DataTypes::noValues(shape);++i)
145 {
146 if (d->getDataAtOffsetRO(i)==deep.getDataAtOffsetRO(i))
147 assert(false);
148 }
149 delete dats[k];
150 }
151
152
153
154
155
156
157 }
158
159
160 void DataTestCase::testSlicingWorker(bool delayed)
161 {
162
163 using namespace escript::DataTypes;
164 cout << endl;
165 {
166 DataTypes::ShapeType viewShape;
167 viewShape.push_back(2);
168 viewShape.push_back(3);
169
170 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]->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
193 DataTypes::RegionType region;
194 region.push_back(DataTypes::RegionType::value_type(0,0));
195 region.push_back(DataTypes::RegionType::value_type(0,0));
196
197 Data slice1(dats[k]->getSlice(region));
198
199 if (tags[k]) {assert(slice1.isTagged());}
200 assert(slice1.getDataPointRank()==0);
201 assert(slice1.getDataPointRO(0,0)==1.0);
202
203 //
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 assert(slice2.getDataAtOffsetRO(slice2.getDataOffset(0,0)+getRelIndex(slice2.getDataPointShape(),0,0))==1.0);
218
219 //
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
238 // now some extra tests for tagged data (dats[1])
239
240 //
241 // add a value for tag "1"
242
243 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
249 //
250 // create a full slice
251
252 DataTypes::RegionType region;
253 region.push_back(DataTypes::RegionType::value_type(0,2));
254 region.push_back(DataTypes::RegionType::value_type(0,3));
255
256 Data slice4(dats[1]->getSlice(region));
257
258 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
267 for (int k=0;k<NUMDATS;++k)
268 {
269 delete dats[k];
270 }
271 }
272
273 {
274 DataTypes::ShapeType viewShape;
275 viewShape.push_back(2);
276 viewShape.push_back(3);
277
278 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 }
287 dats[1]->tag();
288 src[1]->tag();
289 dats[2]->expand();
290 src[2]->expand();
291 if (delayed)
292 {
293 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 }
305 }
306 for (int k=0;k<NUMDATS;++k)
307 {
308 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 assert(getRef(target,0,0,1,1)==src[k]->getDataPointRO(0,0));
316 }
317
318 // some extra tests on tagged data
319
320 //
321 // add a value for tag "1" to target
322
323 DataTypes::ValueType viewData(6);
324 for (int i=0;i<viewData.size();i++) {
325 viewData[i]=i;
326 }
327
328 Data target(1.3,viewShape,FunctionSpace(),false);
329 target.tag();
330 target.setTaggedValueFromCPP(1, viewShape, viewData);
331
332 //cout << "target:\n" << target.toString() << endl;
333
334 //
335 // set a slice in target from source
336
337 DataTypes::RegionType region;
338 region.push_back(DataTypes::RegionType::value_type(0,0));
339 region.push_back(DataTypes::RegionType::value_type(1,1));
340
341 target.setSlice(*src[1],region);
342
343 assert(target.isTagged());
344 assert(target.getDataPointRank()==2);
345 assert(getRef(target,0,0,0,0)==0);
346 assert(getRef(target,0,0,0,1)==src[1]->getDataPointRO(0,0));
347 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
352 //
353 // add a value for tag "2" to source
354
355 DataTypes::ShapeType viewShape2;
356 DataTypes::ValueType viewData2(1);
357 viewData2[0]=6;
358 src[1]->setTaggedValueFromCPP(2, viewShape2, viewData2);
359
360 region.clear();
361 region.push_back(DataTypes::RegionType::value_type(0,0));
362 region.push_back(DataTypes::RegionType::value_type(1,1));
363
364 target.setSlice(*src[1],region);
365
366 assert(target.isTagged());
367 assert(target.getDataPointRank()==2);
368
369 // use a non-existant tag so we get a pointer to the default value
370 // ie: the first element in the data array
371 DataAbstract::ValueType::value_type* targetData=target.getSampleDataByTag(9);
372 for (int i=0; i<target.getLength(); i++) {
373 assert(targetData[i]>=0);
374 }
375 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
394
395 for (int k=0;k<NUMDATS;++k)
396 {
397 delete dats[k];
398 delete src[k];
399 }
400
401 }
402
403 }
404
405 // 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 }
413
414 void DataTestCase::testSlicing() {
415 cout << "\n\tReadyData." << endl;
416 testSlicingWorker(false);
417 cout << "\n\tLazyData." << endl;
418 testSlicingWorker(true);
419 }
420
421
422 void DataTestCase::testSomeDriver(bool autolazy)
423 {
424 cout << endl;
425 SAVELAZYSTATE
426 if (autolazy)
427 {
428 AUTOLAZYON
429 cout << "\tNow testing using autolazy." << endl;
430 }
431 cout << "\tCreate a Data object." << endl;
432
433 DataTypes::ShapeType viewShape;
434 viewShape.push_back(3);
435 DataTypes::ValueType viewData(3);
436 for (int i=0;i<viewShape[0];++i) {
437 viewData[i]=i;
438 }
439
440 bool expanded=true;
441 Data exData(viewData,viewShape,FunctionSpace(),expanded);
442 Data cData(viewData,viewShape);
443 Data result;
444
445 assert(exData.isExpanded());
446 assert(cData.isConstant());
447 assert(result.isEmpty());
448
449 cout << "\tTest some basic operations" << endl;
450 result=exData*cData;
451 cout << CHECKAUTOLAZY << " " << result.isLazy() << " " << result.isExpanded()<< endl;
452 assert(CHECKAUTOLAZY?result.isLazy():result.isExpanded());
453
454 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 RESTORELAZYSTATE
469
470 }
471
472 void DataTestCase::testSome() {
473 testSomeDriver(false);
474 testSomeDriver(true);
475 }
476
477
478
479 // 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 void DataTestCase::testDataConstant() {
555
556 cout << endl;
557
558 cout << "\tCreate a DataConstant object." << endl;
559
560 DataTypes::ShapeType viewShape;
561 viewShape.push_back(2);
562 viewShape.push_back(3);
563 viewShape.push_back(4);
564 DataTypes::ValueType viewData(2*3*4);
565 for (int i=0;i<DataTypes::noValues(viewShape);++i) {
566 viewData[i]=i;
567 }
568
569 Data left(viewData,viewShape);
570 Data right(viewData,viewShape);
571 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 {
597
598 cout << "\tCreate a DataTagged object with a default value only." << endl;
599
600 DataTypes::ShapeType viewShape;
601 viewShape.push_back(3);
602
603 DataTypes::ValueType viewData(3);
604 for (int i=0;i<viewShape[0];i++) {
605 viewData[i]=i;
606 }
607 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
611 assert(!myData.isEmpty());
612 assert(myData.isTagged());
613 assert(myData.getTagNumber(0)==1);
614 assert(myData.getDataPointRank()==1);
615 assert(myData.getLength()==3);
616
617 assert(myData.getNoValues()==3);
618 assert(myData.getDataAtOffsetRO(0)==0.0);
619 assert(myData.getDataAtOffsetRO(1)==1.0);
620 assert(myData.getDataAtOffsetRO(2)==2.0);
621
622 double* sampleData=myData.getSampleDataRW(0);
623 for (int i=0; i<myData.getNoValues(); i++) {
624 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
633 cout << "\tTest setting of a tag and associated value." << endl;
634
635 // value for tag "1"
636 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 }
641
642 myData.setTaggedValueFromCPP(1,viewShape, eTwoData);
643
644 assert(myData.getLength()==6);
645
646 int offset=myData.getDataOffset(0,0);
647 assert(offset==3);
648 assert(myData.getDataPointRank()==1);
649 assert(myData.getNoValues()==3);
650
651 assert(myData.getDataAtOffsetRO(offset+0)==2);
652 assert(myData.getDataAtOffsetRO(offset+1)==3);
653 assert(myData.getDataAtOffsetRO(offset+2)==4);
654
655 sampleData=myData.getSampleDataByTag(1);
656 for (int i=0; i<myData.getNoValues(); i++) {
657 assert(sampleData[i]==i+2);
658 }
659
660 }
661
662 {
663
664 cout << "\tCreate a DataTagged object via tag() method." << endl;
665
666 DataTypes::ShapeType viewShape;
667 viewShape.push_back(2);
668 viewShape.push_back(3);
669 Data myData(1.3,viewShape,FunctionSpace(),false);
670 myData.tag();
671
672 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 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
690 // check value for data-point (0,0).
691 // 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
704 }
705
706 }
707
708 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 catch (EsysException&) {
721 //cout << e.what() << endl;
722 assert(true);
723 }
724
725 try {
726 myData.setTaggedValueFromCPP(0,DataTypes::ShapeType(), DataTypes::ValueType());;
727 assert(false);
728 }
729 catch (EsysException&) {
730 //cout << e.what() << endl;
731 assert(true);
732 }
733
734 }
735
736 void DataTestCase::testConstructors() {
737
738 cout << endl;
739
740 DataTypes::ShapeType viewShape;
741 {
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 void DataTestCase::testMoreOperations()
754 {
755 cout << endl;
756 DataTypes::ShapeType shape;
757 shape.push_back(3);
758 shape.push_back(3);
759
760 // allocate the data
761 DataTypes::ValueType data(DataTypes::noValues(shape),0);
762
763 // assign values to the data
764 for (int i=0;i<shape[0];i++) {
765 for (int j=0;j<shape[1];j++) {
766 data[getRelIndex(shape,i,j)]=getRelIndex(shape,i,j);
767 }
768 }
769
770
771
772 Data dats[]={Data(data,shape,FunctionSpace(),false),
773 Data(data,shape,FunctionSpace(),false),
774 Data(data,shape,FunctionSpace(),true),
775 Data(data,shape,FunctionSpace(),false),
776 Data(data,shape,FunctionSpace(),false),
777 Data(data,shape,FunctionSpace(),true)};
778 const int NUMDATS=6;
779 // const int LAZY=3; // where do the lazy objects start?
780
781 // Data baseEx(data,shape,FunctionSpace(),true);
782 // Data baseCon(data,shape,FunctionSpace(),false);
783 // Data baseTag(data,shape,FunctionSpace(),false);
784 Data& baseCon=dats[0];
785 Data& baseTag=dats[1];
786 Data& baseEx=dats[2];
787 baseTag.tag();
788 dats[4].tag();
789 dats[3].delaySelf();
790 dats[4].delaySelf();
791 dats[5].delaySelf();
792
793 assert(baseEx.isExpanded());
794 assert(baseCon.isConstant());
795 assert(baseTag.isTagged());
796
797 Data results[NUMDATS];
798 double tmp;
799 cout << "\tTest Data::trace(0)." << endl;
800 for (int z=0;z<NUMDATS;++z)
801 {
802 results[z].copy(dats[z].trace(0));
803 }
804 for (int z=0;z<NUMDATS;++z)
805 {
806 tmp=0;
807 for (int i=0;i<shape[0];++i)
808 {
809 tmp+=getRef(dats[z],i,i);
810 }
811 assert(std::abs(results[z].getDataAtOffsetRO(0) - tmp) <= REL_TOL*std::abs(tmp));
812 }
813
814
815 }
816
817 void DataTestCase::testOperations()
818 {
819
820 cout << endl;
821
822 // define the shape for the test data
823 DataTypes::ShapeType shape;
824 shape.push_back(2);
825 shape.push_back(3);
826
827 // allocate the data
828 DataTypes::ValueType data(DataTypes::noValues(shape),0);
829
830 // assign values to the data
831 for (int i=0;i<shape[0];i++) {
832 for (int j=0;j<shape[1];j++) {
833 data[getRelIndex(shape,i,j)]=getRelIndex(shape,i,j);
834 }
835 }
836
837
838
839 Data dats[]={Data(data,shape,FunctionSpace(),false),
840 Data(data,shape,FunctionSpace(),false),
841 Data(data,shape,FunctionSpace(),true),
842 Data(data,shape,FunctionSpace(),false),
843 Data(data,shape,FunctionSpace(),false),
844 Data(data,shape,FunctionSpace(),true)};
845 const int NUMDATS=6;
846 const int LAZY=3; // where do the lazy objects start?
847
848 // Data baseEx(data,shape,FunctionSpace(),true);
849 // Data baseCon(data,shape,FunctionSpace(),false);
850 // Data baseTag(data,shape,FunctionSpace(),false);
851 Data& baseCon=dats[0];
852 Data& baseTag=dats[1];
853 Data& baseEx=dats[2];
854 baseTag.tag();
855 dats[4].tag();
856 dats[3].delaySelf();
857 dats[4].delaySelf();
858 dats[5].delaySelf();
859
860 assert(baseEx.isExpanded());
861 assert(baseCon.isConstant());
862 assert(baseTag.isTagged());
863
864 Data results[NUMDATS];
865 // Data& resultEx=results[0];
866 // Data& resultCon=results[1];
867 // Data& resultTag=results[2];
868
869 // create 0 <= smalldata <= 1 for testing trig functions
870
871 DataTypes::ValueType smalldata(DataTypes::noValues(shape),0);
872
873 // assign values to the data
874 for (int i=0;i<shape[0];i++) {
875 for (int j=0;j<shape[1];j++) {
876 smalldata[getRelIndex(shape,i,j)]=(i==0 && j==0)?0:1.0/(getRelIndex(shape,i,j)+1);
877 }
878 }
879 Data sdats[]={Data(smalldata,shape,FunctionSpace(),false),
880 Data(smalldata,shape,FunctionSpace(),false),
881 Data(smalldata,shape,FunctionSpace(),true),
882 Data(smalldata,shape,FunctionSpace(),false),
883 Data(smalldata,shape,FunctionSpace(),false),
884 Data(smalldata,shape,FunctionSpace(),true)};
885 sdats[1].tag();
886 sdats[4].tag();
887 sdats[3].delaySelf(); // 3 is a lazy constant
888 sdats[4].delaySelf(); // 4 is a lazy tagged
889 sdats[5].delaySelf(); // 5 is a lazy expanded
890
891
892
893 // test unary operations
894
895 double tmp;
896 cout << "\tTest Data::pow." << endl;
897 Data power(3.0,shape,FunctionSpace(),true);
898 for (int z=0;z<NUMDATS;++z)
899 {
900 results[z].copy(dats[z].powD(power));
901 if (z>=LAZY)
902 {
903 assert(results[z].isLazy());
904 }
905 }
906 for (int i=0;i<shape[0];i++) {
907 for (int j=0;j<shape[1];j++) {
908 tmp=pow((double)data[getRelIndex(shape,i,j)],(double)3.0);
909 for (int z=0;z<NUMDATS;++z)
910 {
911 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
912 }
913 }
914 }
915
916 cout << "\tTest Data::sin." << endl;
917 for (int z=0;z<NUMDATS;++z)
918 {
919 results[z].copy(dats[z].sin());
920 if (z>=LAZY)
921 {
922 assert(results[z].isLazy());
923 }
924 }
925 for (int i=0;i<shape[0];i++) {
926 for (int j=0;j<shape[1];j++) {
927 tmp=sin((double)data[getRelIndex(shape,i,j)]);
928 for (int z=0;z<NUMDATS;++z)
929 {
930 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
931 }
932 }
933 }
934
935 cout << "\tTest Data::cos." << endl;
936 for (int z=0;z<NUMDATS;++z)
937 {
938 results[z].copy(dats[z].cos());
939 if (z>=LAZY)
940 {
941 assert(results[z].isLazy());
942 }
943 }
944 for (int i=0;i<shape[0];i++) {
945 for (int j=0;j<shape[1];j++) {
946 tmp=cos((double)data[getRelIndex(shape,i,j)]);
947 for (int z=0;z<NUMDATS;++z)
948 {
949 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
950 }
951 }
952 }
953
954 cout << "\tTest Data::tan." << endl;
955 for (int z=0;z<NUMDATS;++z)
956 {
957 results[z].copy(dats[z].tan());
958 if (z>=LAZY)
959 {
960 assert(results[z].isLazy());
961 }
962 }
963 for (int i=0;i<shape[0];i++) {
964 for (int j=0;j<shape[1];j++) {
965 tmp=tan((double)data[getRelIndex(shape,i,j)]);
966 for (int z=0;z<NUMDATS;++z)
967 {
968 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
969 }
970 }
971 }
972
973 cout << "\tTest Data::asin." << endl;
974 for (int z=0;z<NUMDATS;++z)
975 {
976 results[z].copy(sdats[z].asin());
977 if (z>=LAZY)
978 {
979 assert(results[z].isLazy());
980 }
981 }
982 for (int i=0;i<shape[0];i++) {
983 for (int j=0;j<shape[1];j++) {
984 tmp=asin((double)smalldata[getRelIndex(shape,i,j)]);
985 for (int z=0;z<NUMDATS;++z)
986 {
987 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
988 }
989 }
990 }
991
992 cout << "\tTest Data::acos." << endl;
993 for (int z=0;z<NUMDATS;++z)
994 {
995 results[z].copy(sdats[z].acos());
996 if (z>=LAZY)
997 {
998 assert(results[z].isLazy());
999 }
1000 }
1001 for (int i=0;i<shape[0];i++) {
1002 for (int j=0;j<shape[1];j++) {
1003 tmp=acos((double)smalldata[getRelIndex(shape,i,j)]);
1004 for (int z=0;z<NUMDATS;++z)
1005 {
1006 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1007 }
1008 }
1009 }
1010
1011 cout << "\tTest Data::atan." << endl;
1012 for (int z=0;z<NUMDATS;++z)
1013 {
1014 results[z].copy(sdats[z].atan());
1015 if (z>=LAZY)
1016 {
1017 assert(results[z].isLazy());
1018 }
1019 }
1020 for (int i=0;i<shape[0];i++) {
1021 for (int j=0;j<shape[1];j++) {
1022 tmp=atan((double)smalldata[getRelIndex(shape,i,j)]);
1023 for (int z=0;z<NUMDATS;++z)
1024 {
1025 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1026 }
1027 }
1028 }
1029
1030 cout << "\tTest Data::sinh." << endl;
1031 for (int z=0;z<NUMDATS;++z)
1032 {
1033 results[z].copy(dats[z].sinh());
1034 if (z>=LAZY)
1035 {
1036 assert(results[z].isLazy());
1037 }
1038 }
1039 for (int i=0;i<shape[0];i++) {
1040 for (int j=0;j<shape[1];j++) {
1041 tmp=sinh((double)data[getRelIndex(shape,i,j)]);
1042 for (int z=0;z<NUMDATS;++z)
1043 {
1044 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1045 }
1046 }
1047 }
1048
1049 cout << "\tTest Data::cosh." << endl;
1050 for (int z=0;z<NUMDATS;++z)
1051 {
1052 results[z].copy(dats[z].cosh());
1053 if (z>=LAZY)
1054 {
1055 assert(results[z].isLazy());
1056 }
1057 }
1058 for (int i=0;i<shape[0];i++) {
1059 for (int j=0;j<shape[1];j++) {
1060 tmp=cosh((double)data[getRelIndex(shape,i,j)]);
1061 for (int z=0;z<NUMDATS;++z)
1062 {
1063 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1064 }
1065 }
1066 }
1067
1068 cout << "\tTest Data::tanh." << endl;
1069 for (int z=0;z<NUMDATS;++z)
1070 {
1071 results[z].copy(dats[z].tanh());
1072 if (z>=LAZY)
1073 {
1074 assert(results[z].isLazy());
1075 }
1076 }
1077 for (int i=0;i<shape[0];i++) {
1078 for (int j=0;j<shape[1];j++) {
1079 tmp=tanh((double)data[getRelIndex(shape,i,j)]);
1080 for (int z=0;z<NUMDATS;++z)
1081 {
1082 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1083 }
1084 }
1085 }
1086
1087 // rather than accomodate the different windows operations directly I'll just use inverse functions
1088 cout << "\tTest Data::asinh." << endl;
1089 for (int z=0;z<NUMDATS;++z)
1090 {
1091 results[z].copy(dats[z].asinh().sinh());
1092 if (z>=LAZY)
1093 {
1094 assert(results[z].isLazy());
1095 }
1096 }
1097 for (int i=0;i<shape[0];i++) {
1098 for (int j=0;j<shape[1];j++) {
1099 tmp=data[getRelIndex(shape,i,j)];
1100 for (int z=0;z<NUMDATS;++z)
1101 {
1102 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1103 }
1104 }
1105 }
1106
1107 cout << "\tTest Data::acosh." << endl;
1108 for (int z=0;z<NUMDATS;++z)
1109 {
1110 results[z].copy(dats[z].acosh().cosh());
1111 if (z>=LAZY)
1112 {
1113 assert(results[z].isLazy());
1114 }
1115 }
1116 for (int i=0;i<shape[0];i++) {
1117 for (int j=0;j<shape[1];j++) {
1118 if (i==0 && j==0) break;
1119 tmp=data[getRelIndex(shape,i,j)];
1120 for (int z=0;z<NUMDATS;++z)
1121 {
1122 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1123 }
1124 }
1125 }
1126
1127 cout << "\tTest Data::atanh." << endl;
1128 for (int z=0;z<NUMDATS;++z)
1129 {
1130 results[z].copy(dats[z].tanh().atanh()); // if these are the other way around the results are
1131 if (z>=LAZY) // undefined
1132 {
1133 assert(results[z].isLazy());
1134 }
1135 }
1136 for (int i=0;i<shape[0];i++) {
1137 for (int j=0;j<shape[1];j++) {
1138 tmp=data[getRelIndex(shape,i,j)];
1139 for (int z=0;z<NUMDATS;++z)
1140 {
1141 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1142 }
1143 }
1144 }
1145
1146 cout << "\tTest Data::log." << endl;
1147 for (int z=0;z<NUMDATS;++z)
1148 {
1149 results[z].copy(dats[z].log());
1150 if (z>=LAZY)
1151 {
1152 assert(results[z].isLazy());
1153 }
1154 }
1155 for (int i=0;i<shape[0];i++) {
1156 for (int j=0;j<shape[1];j++) {
1157 if (i==0 && j==0) break;
1158 tmp=log((double)data[getRelIndex(shape,i,j)]);
1159 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 }
1164 }
1165
1166 cout << "\tTest Data::log10." << endl;
1167 for (int z=0;z<NUMDATS;++z)
1168 {
1169 results[z].copy(dats[z].log10());
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 if (i==0 && j==0) break;
1178 tmp=log10((double)data[getRelIndex(shape,i,j)]);
1179 for (int z=0;z<NUMDATS;++z)
1180 {
1181 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1182 }
1183 }
1184 }
1185 #if defined (_WIN32) && !defined (__INTEL_COMPILER)
1186 cout << "\tSkip test Data::erf on windows with MSVC compiler." << endl;
1187 #else
1188 cout << "\tTest Data::erf." << endl;
1189 for (int z=0;z<NUMDATS;++z)
1190 {
1191 results[z].copy(dats[z].erf());
1192 if (z>=LAZY)
1193 {
1194 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 if (i==0 && j==0) break;
1200 tmp=erf((double)data[getRelIndex(shape,i,j)]);
1201 for (int z=0;z<NUMDATS;++z)
1202 {
1203 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1204 }
1205 }
1206 }
1207 #endif
1208
1209
1210 cout << "\tTest Data::abs." << endl;
1211 for (int z=0;z<NUMDATS;++z)
1212 {
1213 results[z].copy(dats[z].abs());
1214 if (z>=LAZY)
1215 {
1216 assert(results[z].isLazy());
1217 }
1218 }
1219 for (int i=0;i<shape[0];i++) {
1220 for (int j=0;j<shape[1];j++) {
1221 tmp=abs((double)data[getRelIndex(shape,i,j)]);
1222 for (int z=0;z<NUMDATS;++z)
1223 {
1224 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1225 }
1226 }
1227 }
1228
1229 cout << "\tTest Data::sign (positive)." << endl;
1230 for (int z=0;z<NUMDATS;++z)
1231 {
1232 results[z].copy(dats[z].sign());
1233 if (z>=LAZY)
1234 {
1235 assert(results[z].isLazy());
1236 }
1237 }
1238 for (int i=0;i<shape[0];i++) {
1239 for (int j=0;j<shape[1];j++) {
1240 tmp=(i==0 && j==0)?0:1;
1241 for (int z=0;z<NUMDATS;++z)
1242 {
1243 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1244 }
1245 }
1246 }
1247
1248 cout << "\tTest Data::sign (negative)." << endl;
1249 for (int z=0;z<NUMDATS;++z)
1250 {
1251 results[z].copy(dats[z].neg().sign());
1252 if (z>=LAZY)
1253 {
1254 assert(results[z].isLazy());
1255 }
1256 }
1257 for (int i=0;i<shape[0];i++) {
1258 for (int j=0;j<shape[1];j++) {
1259 tmp=(i==0 && j==0)?0:-1;
1260 for (int z=0;z<NUMDATS;++z)
1261 {
1262 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1263 }
1264 }
1265 }
1266
1267
1268 cout << "\tTest Data::exp." << endl;
1269 for (int z=0;z<NUMDATS;++z)
1270 {
1271 results[z].copy(dats[z].exp());
1272 if (z>=LAZY)
1273 {
1274 assert(results[z].isLazy());
1275 }
1276 }
1277 for (int i=0;i<shape[0];i++) {
1278 for (int j=0;j<shape[1];j++) {
1279 tmp=exp((double)data[getRelIndex(shape,i,j)]);
1280 for (int z=0;z<NUMDATS;++z)
1281 {
1282 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1283 }
1284 }
1285 }
1286
1287 cout << "\tTest Data::sqrt." << endl;
1288 for (int z=0;z<NUMDATS;++z)
1289 {
1290 results[z].copy(dats[z].sqrt());
1291 if (z>=LAZY)
1292 {
1293 assert(results[z].isLazy());
1294 }
1295 }
1296 for (int i=0;i<shape[0];i++) {
1297 for (int j=0;j<shape[1];j++) {
1298 tmp=sqrt((double)data[getRelIndex(shape,i,j)]);
1299 for (int z=0;z<NUMDATS;++z)
1300 {
1301 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1302 }
1303 }
1304 }
1305
1306 cout << "\tTest Data::neg." << endl;
1307 for (int z=0;z<NUMDATS;++z)
1308 {
1309 results[z].copy(dats[z].neg());
1310 if (z>=LAZY)
1311 {
1312 assert(results[z].isLazy());
1313 }
1314 }
1315 for (int i=0;i<shape[0];i++) {
1316 for (int j=0;j<shape[1];j++) {
1317 tmp=-data[getRelIndex(shape,i,j)];
1318 for (int z=0;z<NUMDATS;++z)
1319 {
1320 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1321 }
1322 }
1323 }
1324
1325 cout << "\tTest Data::pos." << endl;
1326 for (int z=0;z<NUMDATS;++z)
1327 {
1328 results[z].copy(dats[z].pos());
1329 if (z>=LAZY)
1330 {
1331 assert(results[z].isLazy());
1332 }
1333 }
1334 for (int i=0;i<shape[0];i++) {
1335 for (int j=0;j<shape[1];j++) {
1336 for (int z=0;z<NUMDATS;++z)
1337 {
1338 assert(std::abs(getRef(results[z],i,j) - getRelIndex(shape,i,j)) <= REL_TOL*std::abs(data[getRelIndex(shape,i,j)]));
1339 }
1340 }
1341 }
1342
1343 // test reduction operations
1344
1345 cout << "\tTest Data::Lsup." << endl;
1346 for (int z=0;z<NUMDATS;++z)
1347 {
1348 assert(std::abs(dats[z].Lsup() - 5) <= REL_TOL*5);
1349 }
1350
1351 cout << "\tTest Data::sup." << endl;
1352 for (int z=0;z<NUMDATS;++z)
1353 {
1354 assert(std::abs(dats[z].sup() - 5) <= REL_TOL*5);
1355 }
1356
1357 cout << "\tTest Data::inf." << endl;
1358 for (int z=0;z<NUMDATS;++z)
1359 {
1360 assert(std::abs(dats[z].inf() - 0) <= REL_TOL*0);
1361 }
1362
1363 // test data-point reduction operations
1364
1365 cout << "\tTest Data::minval." << endl;
1366 for (int z=0;z<NUMDATS;++z)
1367 {
1368 results[z].copy(dats[z].minval());
1369 }
1370 for (int z=0;z<NUMDATS;++z)
1371 {
1372 assert(std::abs(results[z].getDataAtOffsetRO(0) - 0) <= REL_TOL*0);
1373 }
1374
1375
1376 cout << "\tTest Data::maxval." << endl;
1377 for (int z=0;z<NUMDATS;++z)
1378 {
1379 results[z].copy(dats[z].maxval());
1380 }
1381 for (int z=0;z<NUMDATS;++z)
1382 {
1383 assert(std::abs(results[z].getDataAtOffsetRO(0) - 5) <= REL_TOL*5);
1384 }
1385
1386 cout << "\tTest Data::whereZero." << endl;
1387 for (int z=0;z<NUMDATS;++z)
1388 {
1389 results[z].copy(dats[z].whereZero(2));
1390 }
1391 for (int i=0;i<shape[0];i++) {
1392 for (int j=0;j<shape[1];j++) {
1393 tmp=(getRelIndex(shape,i,j)<=2);
1394 for (int z=0;z<NUMDATS;++z)
1395 {
1396 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1397 }
1398 }
1399 }
1400
1401 cout << "\tTest Data::whereNonZero." << endl;
1402 for (int z=0;z<NUMDATS;++z)
1403 {
1404 results[z].copy(dats[z].whereNonZero(2));
1405 }
1406 for (int i=0;i<shape[0];i++) {
1407 for (int j=0;j<shape[1];j++) {
1408 tmp=!(getRelIndex(shape,i,j)<=2);
1409 for (int z=0;z<NUMDATS;++z)
1410 {
1411 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1412 }
1413 }
1414 }
1415
1416 cout << "\tTest Data::transpose(1)." << endl;
1417 for (int z=0;z<NUMDATS;++z)
1418 {
1419 results[z].copy(dats[z].transpose(1));
1420 }
1421 for (int i=0;i<shape[0];i++) {
1422 for (int j=0;j<shape[1];j++) {
1423 for (int z=0;z<NUMDATS;++z)
1424 {
1425 tmp=getRef(dats[z],i,j);
1426 assert(std::abs(getRef(results[z],j,i) - tmp) <= REL_TOL*std::abs(tmp));
1427 }
1428 }
1429 }
1430
1431 cout << "\tTest Data::swapaxes(0,1)." << endl;
1432 for (int z=0;z<NUMDATS;++z)
1433 {
1434 results[z].copy(dats[z].swapaxes(0,1));
1435 }
1436 for (int i=0;i<shape[0];i++) {
1437 for (int j=0;j<shape[1];j++) {
1438 for (int z=0;z<NUMDATS;++z)
1439 {
1440 tmp=getRef(dats[z],i,j);
1441 assert(std::abs(getRef(results[z],j,i) - tmp) <= REL_TOL*std::abs(tmp));
1442 }
1443 }
1444 }
1445 }
1446
1447
1448 // Here we test the binary operators in complex expressions
1449 void DataTestCase::testBinary()
1450 {
1451
1452 cout << endl;
1453
1454 // define the shape for the test data
1455 DataTypes::ShapeType shape;
1456 shape.push_back(2);
1457 shape.push_back(3);
1458
1459 // allocate the data
1460 DataTypes::ValueType data(DataTypes::noValues(shape),0);
1461
1462 // assign values to the data
1463 for (int i=0;i<shape[0];i++) {
1464 for (int j=0;j<shape[1];j++) {
1465 data[getRelIndex(shape,i,j)]=getRelIndex(shape,i,j)+2; // so we get no zeros
1466 }
1467 }
1468
1469
1470 Data one(1.0,DataTypes::scalarShape,FunctionSpace());
1471 Data two(2.0,DataTypes::scalarShape,FunctionSpace());
1472 Data dats[]={Data(data,shape,FunctionSpace(),false),
1473 Data(data,shape,FunctionSpace(),false),
1474 Data(data,shape,FunctionSpace(),true),
1475 Data(data,shape,FunctionSpace(),false),
1476 Data(data,shape,FunctionSpace(),false),
1477 Data(data,shape,FunctionSpace(),true)};
1478 dats[1].tag();
1479 dats[4].tag();
1480 const int NUMDATS=6;
1481 const int LAZY=3;
1482 dats[3].delaySelf();
1483 dats[4].delaySelf();
1484 dats[5].delaySelf();
1485 for (int z=0;z<NUMDATS;++z)
1486 {
1487 Data& a=dats[z];
1488 Data r1=(((a+a)/two)+a-a)*one; // scalar/*, matrix+-
1489 Data r2=(((a*a)/a+one)-one); // scalar+-, matrix*/
1490 Data r3=(a.powD(two)/a.powD(one)); // scalar power
1491 Data r4=a.powD(a); // matrix power
1492 if (z>LAZY)
1493 {
1494 assert(r1.isLazy() && r2.isLazy() && r3.isLazy() && r4.isLazy());
1495 }
1496 for (int i=0;i<DataTypes::noValues(shape);++i)
1497 {
1498 assert(std::abs(r1.getDataAtOffsetRO(i)-data[i]) <= REL_TOL*data[i]);
1499 assert(std::abs(r2.getDataAtOffsetRO(i)-data[i]) <= REL_TOL*data[i]);
1500 assert(std::abs(r3.getDataAtOffsetRO(i)-data[i]) <= REL_TOL*data[i]);
1501 assert(std::abs(r4.getDataAtOffsetRO(i)-pow(data[i],i)) <=REL_TOL*pow(data[i],i));
1502 }
1503 }
1504 }
1505
1506
1507 void DataTestCase::testMemAlloc() {
1508
1509 //
1510 // Simple little sanity check for the memory allocator
1511
1512 cout << endl;
1513
1514 Data *testData;
1515 for (int i=0; i<1000; i++) {
1516 testData = new Data(0.0, DataTypes::ShapeType(), FunctionSpace(), true);
1517 delete testData;
1518 }
1519
1520 DataTypes::ShapeType viewShape;
1521 viewShape.push_back(10);
1522 viewShape.push_back(10);
1523 viewShape.push_back(10);
1524
1525 Data *testData2;
1526 Data *testData3 = new Data(0.0, viewShape, FunctionSpace(), true);
1527 for (int i=0; i<1000; i++) {
1528 testData2 = new Data(0.0, viewShape, FunctionSpace(), true);
1529 delete testData2;
1530 }
1531 delete testData3;
1532
1533 }
1534
1535 TestSuite* DataTestCase::suite ()
1536 {
1537 //
1538 // create the suite of tests to perform.
1539 TestSuite *testSuite = new TestSuite ("DataTestCase");
1540 testSuite->addTest (new TestCaller< DataTestCase>("testCopying",&DataTestCase::testCopying));
1541 testSuite->addTest (new TestCaller< DataTestCase>("testSome",&DataTestCase::testSome));
1542 testSuite->addTest (new TestCaller< DataTestCase>("testDataConstant",&DataTestCase::testDataConstant));
1543 testSuite->addTest (new TestCaller< DataTestCase>("testDataTagged",&DataTestCase::testDataTagged));
1544 testSuite->addTest (new TestCaller< DataTestCase>("testDataTaggedExceptions",&DataTestCase::testDataTaggedExceptions));
1545 testSuite->addTest (new TestCaller< DataTestCase>("testConstructors",&DataTestCase::testConstructors));
1546 testSuite->addTest (new TestCaller< DataTestCase>("testSlicing",&DataTestCase::testSlicing));
1547 testSuite->addTest (new TestCaller< DataTestCase>("testOperations",&DataTestCase::testOperations));
1548 testSuite->addTest (new TestCaller< DataTestCase>("testMoreOperations",&DataTestCase::testMoreOperations));
1549 testSuite->addTest (new TestCaller< DataTestCase>("testMemAlloc",&DataTestCase::testMemAlloc));
1550 testSuite->addTest (new TestCaller< DataTestCase>("Resolving",&DataTestCase::testResolveType));
1551
1552 return testSuite;
1553 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26