/[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 1935 - (show annotations)
Mon Oct 27 06:06:39 2008 UTC (10 years, 6 months ago) by jfenwick
File size: 27129 byte(s)
Branch commit
More cleanup of DataTestCase - still don't have all the LazyTests in 
there yet.
Added tests to make sure the resolve() operation does what it is 
supposed to.
Added non-constant versions of getPointOffset to DataAbstract 
classes.
Fixed a bug in deepCopy on DataLazy.
Changed setToZero to not check the type of the data.

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 void DataTestCase::testOperations() {
734
735 cout << endl;
736
737 // define the shape for the test data
738 DataTypes::ShapeType shape;
739 shape.push_back(2);
740 shape.push_back(3);
741
742 // allocate the data
743 DataTypes::ValueType data(DataTypes::noValues(shape),0);
744
745 // assign values to the data
746 for (int i=0;i<shape[0];i++) {
747 for (int j=0;j<shape[1];j++) {
748 data[getRelIndex(shape,i,j)]=getRelIndex(shape,i,j);
749 }
750 }
751
752 Data dats[]={Data(data,shape,FunctionSpace(),false),
753 Data(data,shape,FunctionSpace(),false),
754 Data(data,shape,FunctionSpace(),true)};
755 const int NUMDATS=3;
756
757 // Data baseEx(data,shape,FunctionSpace(),true);
758 // Data baseCon(data,shape,FunctionSpace(),false);
759 // Data baseTag(data,shape,FunctionSpace(),false);
760 Data& baseCon=dats[0];
761 Data& baseTag=dats[1];
762 Data& baseEx=dats[2];
763 baseTag.tag();
764
765 assert(baseEx.isExpanded());
766 assert(baseCon.isConstant());
767 assert(baseTag.isTagged());
768
769 Data results[NUMDATS];
770 Data& resultEx=results[0];
771 Data& resultCon=results[1];
772 Data& resultTag=results[2];
773
774 // test unary operations
775
776 double tmp;
777 cout << "\tTest Data::pow." << endl;
778 Data power(3.0,shape,FunctionSpace(),true);
779 for (int z=0;z<NUMDATS;++z)
780 {
781 results[z].copy(dats[z].powD(power));
782 }
783 for (int i=0;i<shape[0];i++) {
784 for (int j=0;j<shape[1];j++) {
785 tmp=pow((double)data[getRelIndex(shape,i,j)],(double)3.0);
786 for (int z=0;z<NUMDATS;++z)
787 {
788 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
789 }
790 }
791 }
792
793 cout << "\tTest Data::sin." << endl;
794 for (int z=0;z<NUMDATS;++z)
795 {
796 results[z].copy(dats[z].sin());
797 }
798 for (int i=0;i<shape[0];i++) {
799 for (int j=0;j<shape[1];j++) {
800 tmp=sin((double)data[getRelIndex(shape,i,j)]);
801 for (int z=0;z<NUMDATS;++z)
802 {
803 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
804 }
805 }
806 }
807
808 cout << "\tTest Data::cos." << endl;
809 for (int z=0;z<NUMDATS;++z)
810 {
811 results[z].copy(dats[z].cos());
812 }
813 for (int i=0;i<shape[0];i++) {
814 for (int j=0;j<shape[1];j++) {
815 tmp=cos((double)data[getRelIndex(shape,i,j)]);
816 for (int z=0;z<NUMDATS;++z)
817 {
818 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
819 }
820 }
821 }
822
823 cout << "\tTest Data::tan." << endl;
824 for (int z=0;z<NUMDATS;++z)
825 {
826 results[z].copy(dats[z].tan());
827 }
828 for (int i=0;i<shape[0];i++) {
829 for (int j=0;j<shape[1];j++) {
830 tmp=tan((double)data[getRelIndex(shape,i,j)]);
831 for (int z=0;z<NUMDATS;++z)
832 {
833 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
834 }
835 }
836 }
837
838 cout << "\tTest Data::asin." << endl;
839 for (int z=0;z<NUMDATS;++z)
840 {
841 results[z].copy(dats[z].asin());
842 }
843 assert(true);
844
845 cout << "\tTest Data::acos." << endl;
846 for (int z=0;z<NUMDATS;++z)
847 {
848 results[z].copy(dats[z].asin());
849 }
850 assert(true);
851
852 cout << "\tTest Data::atan." << endl;
853 for (int z=0;z<NUMDATS;++z)
854 {
855 results[z].copy(dats[z].atan());
856 }
857 for (int i=0;i<shape[0];i++) {
858 for (int j=0;j<shape[1];j++) {
859 tmp=atan((double)data[getRelIndex(shape,i,j)]);
860 for (int z=0;z<NUMDATS;++z)
861 {
862 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
863 }
864 }
865 }
866
867 cout << "\tTest Data::sinh." << endl;
868 for (int z=0;z<NUMDATS;++z)
869 {
870 results[z].copy(dats[z].sinh());
871 }
872 for (int i=0;i<shape[0];i++) {
873 for (int j=0;j<shape[1];j++) {
874 tmp=sinh((double)data[getRelIndex(shape,i,j)]);
875 for (int z=0;z<NUMDATS;++z)
876 {
877 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
878 }
879 }
880 }
881
882 cout << "\tTest Data::cosh." << endl;
883 for (int z=0;z<NUMDATS;++z)
884 {
885 results[z].copy(dats[z].cosh());
886 }
887 for (int i=0;i<shape[0];i++) {
888 for (int j=0;j<shape[1];j++) {
889 tmp=cosh((double)data[getRelIndex(shape,i,j)]);
890 for (int z=0;z<NUMDATS;++z)
891 {
892 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
893 }
894 }
895 }
896
897 cout << "\tTest Data::tanh." << endl;
898 for (int z=0;z<NUMDATS;++z)
899 {
900 results[z].copy(dats[z].tanh());
901 }
902 for (int i=0;i<shape[0];i++) {
903 for (int j=0;j<shape[1];j++) {
904 tmp=tanh((double)data[getRelIndex(shape,i,j)]);
905 for (int z=0;z<NUMDATS;++z)
906 {
907 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
908 }
909 }
910 }
911
912 cout << "\tTest Data::asinh." << endl;
913 for (int z=0;z<NUMDATS;++z)
914 {
915 results[z].copy(dats[z].asinh());
916 }
917 assert(true);
918
919 cout << "\tTest Data::acosh." << endl;
920 for (int z=0;z<NUMDATS;++z)
921 {
922 results[z].copy(dats[z].acosh());
923 }
924 assert(true);
925
926 cout << "\tTest Data::atanh." << endl;
927 for (int z=0;z<NUMDATS;++z)
928 {
929 results[z].copy(dats[z].atanh());
930 }
931 assert(true);
932
933 cout << "\tTest Data::log." << endl;
934 for (int z=0;z<NUMDATS;++z)
935 {
936 results[z].copy(dats[z].log());
937 }
938 assert(true);
939
940 cout << "\tTest Data::abs." << endl;
941 for (int z=0;z<NUMDATS;++z)
942 {
943 results[z].copy(dats[z].abs());
944 }
945 for (int i=0;i<shape[0];i++) {
946 for (int j=0;j<shape[1];j++) {
947 tmp=abs((double)data[getRelIndex(shape,i,j)]);
948 for (int z=0;z<NUMDATS;++z)
949 {
950 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
951 }
952 }
953 }
954
955 cout << "\tTest Data::sign." << endl;
956 for (int z=0;z<NUMDATS;++z)
957 {
958 results[z].copy(dats[z].sign());
959 }
960 assert(true);
961
962 cout << "\tTest Data::exp." << endl;
963 for (int z=0;z<NUMDATS;++z)
964 {
965 results[z].copy(dats[z].exp());
966 }
967 for (int i=0;i<shape[0];i++) {
968 for (int j=0;j<shape[1];j++) {
969 tmp=exp((double)data[getRelIndex(shape,i,j)]);
970 for (int z=0;z<NUMDATS;++z)
971 {
972 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
973 }
974 }
975 }
976
977 cout << "\tTest Data::sqrt." << endl;
978 for (int z=0;z<NUMDATS;++z)
979 {
980 results[z].copy(dats[z].sqrt());
981 }
982 for (int i=0;i<shape[0];i++) {
983 for (int j=0;j<shape[1];j++) {
984 tmp=sqrt((double)data[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::neg." << endl;
993 for (int z=0;z<NUMDATS;++z)
994 {
995 results[z].copy(dats[z].neg());
996 }
997 assert(true);
998
999 cout << "\tTest Data::pos." << endl;
1000 for (int z=0;z<NUMDATS;++z)
1001 {
1002 results[z].copy(dats[z].pos());
1003 }
1004 for (int i=0;i<shape[0];i++) {
1005 for (int j=0;j<shape[1];j++) {
1006 for (int z=0;z<NUMDATS;++z)
1007 {
1008 assert(std::abs(getRef(results[z],i,j) - getRelIndex(shape,i,j)) <= REL_TOL*std::abs(data[getRelIndex(shape,i,j)]));
1009 }
1010 }
1011 }
1012
1013 // test reduction operations
1014
1015 cout << "\tTest Data::Lsup." << endl;
1016 for (int z=0;z<NUMDATS;++z)
1017 {
1018 assert(std::abs(dats[z].Lsup() - 5) <= REL_TOL*5);
1019 }
1020
1021 cout << "\tTest Data::sup." << endl;
1022 for (int z=0;z<NUMDATS;++z)
1023 {
1024 assert(std::abs(dats[z].sup() - 5) <= REL_TOL*5);
1025 }
1026
1027 cout << "\tTest Data::inf." << endl;
1028 for (int z=0;z<NUMDATS;++z)
1029 {
1030 assert(std::abs(dats[z].inf() - 0) <= REL_TOL*0);
1031 }
1032
1033 // test data-point reduction operations
1034
1035 cout << "\tTest Data::minval." << endl;
1036 for (int z=0;z<NUMDATS;++z)
1037 {
1038 results[z].copy(dats[z].minval());
1039 }
1040 for (int z=0;z<NUMDATS;++z)
1041 {
1042 assert(std::abs(results[z].getDataAtOffset(0) - 0) <= REL_TOL*0);
1043 }
1044
1045
1046 cout << "\tTest Data::maxval." << endl;
1047 for (int z=0;z<NUMDATS;++z)
1048 {
1049 results[z].copy(dats[z].maxval());
1050 }
1051 for (int z=0;z<NUMDATS;++z)
1052 {
1053 assert(std::abs(results[z].getDataAtOffset(0) - 5) <= REL_TOL*5);
1054 }
1055
1056 }
1057
1058
1059 void DataTestCase::testMemAlloc() {
1060
1061 //
1062 // Simple little sanity check for the memory allocator
1063
1064 cout << endl;
1065
1066 Data *testData;
1067 for (int i=0; i<1000; i++) {
1068 testData = new Data(0.0, DataTypes::ShapeType(), FunctionSpace(), true);
1069 delete testData;
1070 }
1071
1072 DataTypes::ShapeType viewShape;
1073 viewShape.push_back(10);
1074 viewShape.push_back(10);
1075 viewShape.push_back(10);
1076
1077 Data *testData2;
1078 Data *testData3 = new Data(0.0, viewShape, FunctionSpace(), true);
1079 for (int i=0; i<1000; i++) {
1080 testData2 = new Data(0.0, viewShape, FunctionSpace(), true);
1081 delete testData2;
1082 }
1083 delete testData3;
1084
1085 }
1086
1087 TestSuite* DataTestCase::suite ()
1088 {
1089 //
1090 // create the suite of tests to perform.
1091 TestSuite *testSuite = new TestSuite ("DataTestCase");
1092 testSuite->addTest (new TestCaller< DataTestCase>("testCopying",&DataTestCase::testCopying));
1093 testSuite->addTest (new TestCaller< DataTestCase>("testSome",&DataTestCase::testSome));
1094 testSuite->addTest (new TestCaller< DataTestCase>("testDataConstant",&DataTestCase::testDataConstant));
1095 testSuite->addTest (new TestCaller< DataTestCase>("testDataTagged",&DataTestCase::testDataTagged));
1096 testSuite->addTest (new TestCaller< DataTestCase>("testDataTaggedExceptions",&DataTestCase::testDataTaggedExceptions));
1097 testSuite->addTest (new TestCaller< DataTestCase>("testConstructors",&DataTestCase::testConstructors));
1098 testSuite->addTest (new TestCaller< DataTestCase>("testSlicing",&DataTestCase::testSlicing));
1099 testSuite->addTest (new TestCaller< DataTestCase>("testOperations",&DataTestCase::testOperations));
1100 //testSuite->addTest (new TestCaller< DataTestCase>("testRefValue",&DataTestCase::testRefValue));
1101 testSuite->addTest (new TestCaller< DataTestCase>("testMemAlloc",&DataTestCase::testMemAlloc));
1102 testSuite->addTest (new TestCaller< DataTestCase>("Resolving",&DataTestCase::testResolveType));
1103
1104 return testSuite;
1105 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26