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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2051 - (show annotations)
Mon Nov 17 09:24:02 2008 UTC (10 years, 11 months ago) by phornby
File size: 34511 byte(s)
Now add the erf() test back for windows under the intelc compiler.


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26