/[escript]/branches/schroedinger/escript/test/DataTestCase.cpp
ViewVC logotype

Contents of /branches/schroedinger/escript/test/DataTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1938 - (show annotations)
Tue Oct 28 01:51:08 2008 UTC (12 years, 5 months ago) by jfenwick
File size: 34532 byte(s)
Branch commit.
Cleaned up DataTestCase and added tests for LazyData.

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26