/[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 1926 - (show annotations)
Fri Oct 24 05:54:08 2008 UTC (12 years, 3 months ago) by jfenwick
File size: 28654 byte(s)
Branch commit.
Changed some text so it doesn't mention DataArrayView.
Added some tests for DataLazy.
Factored common code in the Data tests (will add tests against lazy 
later).


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
29
30 using namespace std;
31 using namespace CppUnitTest;
32 using namespace escript;
33 using namespace esysUtils;
34 using namespace escript::DataTypes;
35
36 void DataTestCase::setUp() {
37 //
38 // This is called before each test is run
39 }
40
41 void DataTestCase::tearDown() {
42 //
43 // This is called after each test has been run
44 }
45
46
47
48 namespace
49 {
50
51 inline
52 DataTypes::ValueType::reference
53 getRef(Data& d,int s1, int p1, int x, int y)
54 {
55 return d.getDataAtOffset(d.getDataOffset(s1,p1)+getRelIndex(d.getDataPointShape(),x,y));
56 }
57
58 inline
59 DataTypes::ValueType::reference
60 getRef(Data& d, int x, int y)
61 {
62 return d.getDataAtOffset(getRelIndex(d.getDataPointShape(),x,y));
63 }
64
65 }
66
67 // This is to test new copy routines, existing tests should remain where they are
68 void DataTestCase::testCopying()
69 {
70 using namespace escript::DataTypes;
71 cout << endl;
72
73 DataTypes::ShapeType shape;
74 shape.push_back(2);
75 shape.push_back(3);
76 DataTypes::ValueType data(DataTypes::noValues(shape),1);
77 const int NUMDATS=3;
78 Data* dats[NUMDATS];
79 char* strs[]={"DataConstant", "DataTagged", "DataExpanded"};
80 dats[0]=new Data(new DataConstant(FunctionSpace(),shape,data));
81 dats[1]=new Data(new DataTagged(FunctionSpace(),shape,data));
82 dats[2]=new Data(new DataExpanded(FunctionSpace(),shape,data));
83
84 for (int k=0;k<NUMDATS;++k)
85 {
86 cout << "\tTest deep copy " << strs[k] << endl;
87 Data* d=dats[k];
88 Data* deep=d->copySelf(); // test self copy
89 for (int i=0;i<DataTypes::noValues(shape);++i)
90 {
91 if (d->getDataAtOffset(i)!=deep->getDataAtOffset(i))
92 assert(false);
93 }
94 d->setToZero();
95 for (int i=0;i<DataTypes::noValues(shape);++i)
96 {
97 if (d->getDataAtOffset(i)==deep->getDataAtOffset(i))
98 assert(false);
99 }
100 d->copy(*deep); // test copy from object
101 for (int i=0;i<DataTypes::noValues(shape);++i)
102 {
103 if (d->getDataAtOffset(i)!=deep->getDataAtOffset(i))
104 assert(false);
105 }
106 d->setToZero();
107 for (int i=0;i<DataTypes::noValues(shape);++i)
108 {
109 if (d->getDataAtOffset(i)==deep->getDataAtOffset(i))
110 assert(false);
111 }
112 delete deep;
113 delete dats[k];
114 }
115
116 }
117
118 void DataTestCase::testSlicing() {
119
120 using namespace escript::DataTypes;
121 cout << endl;
122 {
123 DataTypes::ShapeType viewShape;
124 viewShape.push_back(2);
125 viewShape.push_back(3);
126
127 const int NUMDATS=3;
128 char* strs[]={"DataConstant", "DataTagged","DataExpanded"};
129 bool tags[]={false,true,false}; // is the slice of this data supposed to be tagged
130 Data* dats[NUMDATS];
131 for (int k=0;k<NUMDATS;++k)
132 {
133 dats[k]=new Data(1.3, viewShape);
134 }
135 dats[1]->tag();
136 dats[2]->expand();
137 for (int k=0;k<NUMDATS;++k)
138 {
139 cout << "\tTest get-slicing " << strs[k] << endl;
140 dats[k]->getDataAtOffset(dats[k]->getDataOffset(0,0)+getRelIndex(viewShape,0,0))=1.0;
141 dats[k]->getDataAtOffset(dats[k]->getDataOffset(0,0)+getRelIndex(viewShape,1,1))=2.0;
142
143 DataTypes::RegionType region;
144 region.push_back(DataTypes::RegionType::value_type(0,0));
145 region.push_back(DataTypes::RegionType::value_type(0,0));
146
147 Data slice1(dats[k]->getSlice(region));
148
149 if (tags[k]) {assert(slice1.isTagged());}
150 assert(slice1.getDataPointRank()==0);
151 assert(slice1.getDataPoint(0,0)==1.0);
152
153 //
154 // create a rank 2 slice with one value
155
156 region.clear();
157 region.push_back(DataTypes::RegionType::value_type(0,1));
158 region.push_back(DataTypes::RegionType::value_type(0,1));
159
160 Data slice2(dats[k]->getSlice(region));
161
162 //cout << slice2.toString() << endl;
163
164 if (tags[k]) {assert(slice2.isTagged());}
165 assert(slice2.getDataPointRank()==2);
166
167 assert(slice2.getDataAtOffset(slice2.getDataOffset(0,0)+getRelIndex(slice2.getDataPointShape(),0,0))==1.0);
168
169 //
170 // create a rank 2 slice with four values
171
172 region.clear();
173 region.push_back(DataTypes::RegionType::value_type(0,2));
174 region.push_back(DataTypes::RegionType::value_type(0,2));
175
176 Data slice3(dats[k]->getSlice(region));
177
178 //cout << slice3.toString() << endl;
179
180 if (tags[k]) {assert(slice3.isTagged());}
181 assert(slice3.getDataPointRank()==2);
182 assert(getRef(slice3,0,0,0,0)==1.0);
183 assert(getRef(slice3,0,0,0,1)==1.3);
184 assert(getRef(slice3,0,0,1,0)==1.3);
185 assert(getRef(slice3,0,0,1,1)==2.0);
186 }
187
188 // now some extra tests for tagged data (dats[1])
189
190 //
191 // add a value for tag "1"
192
193 DataTypes::ValueType viewData(6);
194 for (int i=0;i<viewData.size();i++) {
195 viewData[i]=i;
196 }
197 dats[1]->setTaggedValueFromCPP(1, viewShape, viewData);
198
199 //
200 // create a full slice
201
202 DataTypes::RegionType region;
203 region.push_back(DataTypes::RegionType::value_type(0,2));
204 region.push_back(DataTypes::RegionType::value_type(0,3));
205
206 Data slice4(dats[1]->getSlice(region));
207
208 assert(slice4.isTagged());
209 assert(slice4.getDataPointRank()==2);
210 assert(getRef(slice4,0,0,0,0)==0);
211 assert(getRef(slice4,0,0,0,1)==2);
212 assert(getRef(slice4,0,0,0,2)==4);
213 assert(getRef(slice4,0,0,1,0)==1);
214 assert(getRef(slice4,0,0,1,1)==3);
215 assert(getRef(slice4,0,0,1,2)==5);
216
217 for (int k=0;k<NUMDATS;++k)
218 {
219 delete dats[k];
220 }
221 }
222
223 {
224 DataTypes::ShapeType viewShape;
225 viewShape.push_back(2);
226 viewShape.push_back(3);
227
228 const int NUMDATS=3;
229 char* strs[]={"DataConstant", "DataTagged","DataExpanded"};
230 bool tags[]={false,true,false}; // is the slice of this data supposed to be tagged
231 Data* dats[NUMDATS];
232 Data* src[NUMDATS];
233 for (int k=0;k<NUMDATS;++k)
234 {
235 dats[k]=new Data(1.3, viewShape);
236 src[k]=new Data(10,DataTypes::scalarShape);
237 }
238 dats[1]->tag();
239 src[1]->tag();
240 dats[2]->expand();
241 src[2]->expand();
242
243 for (int k=0;k<NUMDATS;++k)
244 {
245 cout << "\tTest set-slicing " << strs[k] << endl;
246 Data target(1.3,viewShape);
247 if (k==2) {target.expand();}
248 DataTypes::RegionType region;
249 region.push_back(DataTypes::RegionType::value_type(1,1));
250 region.push_back(DataTypes::RegionType::value_type(1,1));
251 target.setSlice(*(src[k]),region);
252 assert(getRef(target,0,0,1,1)==src[k]->getDataPoint(0,0));
253 }
254
255 // some extra tests on tagged data
256
257 //
258 // add a value for tag "1" to target
259
260 DataTypes::ValueType viewData(6);
261 for (int i=0;i<viewData.size();i++) {
262 viewData[i]=i;
263 }
264
265 Data target(1.3,viewShape,FunctionSpace(),false);
266 target.tag();
267 target.setTaggedValueFromCPP(1, viewShape, viewData);
268
269 //cout << "target:\n" << target.toString() << endl;
270
271 //
272 // set a slice in target from source
273
274 DataTypes::RegionType region;
275 region.push_back(DataTypes::RegionType::value_type(0,0));
276 region.push_back(DataTypes::RegionType::value_type(1,1));
277
278 target.setSlice(*src[1],region);
279
280 assert(target.isTagged());
281 assert(target.getDataPointRank()==2);
282 assert(getRef(target,0,0,0,0)==0);
283 assert(getRef(target,0,0,0,1)==src[1]->getDataPoint(0,0));
284 assert(getRef(target,0,0,0,2)==4);
285 assert(getRef(target,0,0,1,0)==1);
286 assert(getRef(target,0,0,1,1)==3);
287 assert(getRef(target,0,0,1,2)==5);
288
289 //
290 // add a value for tag "2" to source
291
292 DataTypes::ShapeType viewShape2;
293 DataTypes::ValueType viewData2(1);
294 viewData2[0]=6;
295 src[1]->setTaggedValueFromCPP(2, viewShape2, viewData2);
296
297 region.clear();
298 region.push_back(DataTypes::RegionType::value_type(0,0));
299 region.push_back(DataTypes::RegionType::value_type(1,1));
300
301 target.setSlice(*src[1],region);
302
303 assert(target.isTagged());
304 assert(target.getDataPointRank()==2);
305
306 // use a non-existant tag so we get a pointer to the default value
307 // ie: the first element in the data array
308 DataAbstract::ValueType::value_type* targetData=target.getSampleDataByTag(9);
309 for (int i=0; i<target.getLength(); i++) {
310 assert(targetData[i]>=0);
311 }
312 assert(targetData[0]==1.3);
313 assert(targetData[1]==1.3);
314 assert(targetData[2]==10);
315 assert(targetData[3]==1.3);
316 assert(targetData[4]==1.3);
317 assert(targetData[5]==1.3);
318 assert(targetData[6]==0);
319 assert(targetData[7]==1);
320 assert(targetData[8]==10);
321 assert(targetData[9]==3);
322 assert(targetData[10]==4);
323 assert(targetData[11]==5);
324 assert(targetData[12]==1.3);
325 assert(targetData[13]==1.3);
326 assert(targetData[14]==6);
327 assert(targetData[15]==1.3);
328 assert(targetData[16]==1.3);
329 assert(targetData[17]==1.3);
330
331
332 for (int k=0;k<NUMDATS;++k)
333 {
334 delete dats[k];
335 delete src[k];
336 }
337
338 }
339 }
340
341 void DataTestCase::testAll() {
342
343 cout << endl;
344
345 cout << "\tCreate a Data object from a DataArrayView" << endl;
346
347 DataTypes::ShapeType viewShape;
348 viewShape.push_back(3);
349 DataTypes::ValueType viewData(3);
350 for (int i=0;i<viewShape[0];++i) {
351 viewData[i]=i;
352 }
353 // DataArrayView myView(viewData,viewShape);
354
355 bool expanded=true;
356 Data exData(viewData,viewShape,FunctionSpace(),expanded);
357 // Data cData(myView);
358 Data cData(viewData,viewShape,FunctionSpace());
359 Data result;
360
361 assert(exData.isExpanded());
362 assert(cData.isConstant());
363 assert(result.isEmpty());
364
365 cout << "\tTest some basic operations" << endl;
366 result=exData*cData;
367 assert(result.isExpanded());
368
369 }
370
371 void DataTestCase::testMore() {
372
373 cout << endl;
374
375 cout << "\tCreate a Data object from a DataArrayView" << endl;
376
377 DataTypes::ShapeType viewShape;
378 viewShape.push_back(3);
379 DataTypes::ValueType viewData(3);
380 for (int i=0;i<viewShape[0];++i) {
381 viewData[i]=i;
382 }
383 // DataArrayView myView(viewData,viewShape);
384
385 bool expanded=true;
386 Data exData(viewData,viewShape,FunctionSpace(),expanded);
387 Data cData(viewData,viewShape);
388 Data result;
389
390 assert(exData.isExpanded());
391 assert(cData.isConstant());
392 assert(result.isEmpty());
393
394 cout << "\tTest some basic operations" << endl;
395 result=exData*cData;
396 assert(result.isExpanded());
397
398 assert(result.Lsup()==4);
399 assert(result.sup()==4);
400 assert(result.inf()==0);
401
402 result=exData+cData;
403 result=exData-cData;
404 result=exData/cData;
405
406 cout << "\tExercise wherePositive method" << endl;
407 assert(!exData.wherePositive().isEmpty());
408
409 cout << "\tExercise copyWithMask method" << endl;
410 exData.copyWithMask(result, exData.wherePositive());
411 assert(!exData.wherePositive().isEmpty());
412
413 }
414
415 void DataTestCase::testDataConstant() {
416
417 cout << endl;
418
419 cout << "\tCreate a DataConstant object from a DataArrayView" << endl;
420
421 DataTypes::ShapeType viewShape;
422 viewShape.push_back(2);
423 viewShape.push_back(3);
424 viewShape.push_back(4);
425 DataTypes::ValueType viewData(2*3*4);
426 for (int i=0;i<DataTypes::noValues(viewShape);++i) {
427 viewData[i]=i;
428 }
429 // DataArrayView myView(viewData,viewShape);
430
431 Data left(viewData,viewShape);
432 Data right(viewData,viewShape);
433 Data result;
434
435 cout << "\tTest some basic operations" << endl;
436
437 result=left-right;
438
439 assert(left.isConstant());
440 assert(right.isConstant());
441 assert(result.isConstant());
442
443 result=left+right;
444
445 assert(left.isConstant());
446 assert(right.isConstant());
447 assert(result.isConstant());
448
449 assert(!result.isExpanded());
450 assert(!result.isTagged());
451
452 }
453
454 void DataTestCase::testDataTagged() {
455
456 cout << endl;
457
458 {
459
460 cout << "\tCreate a DataTagged object with a default value only." << endl;
461
462 // DataTagged::TagListType keys;
463
464 // DataTagged::ValueListType values;
465
466 DataTypes::ShapeType viewShape;
467 viewShape.push_back(3);
468
469 DataTypes::ValueType viewData(3);
470 for (int i=0;i<viewShape[0];i++) {
471 viewData[i]=i;
472 }
473 /* DataArrayView defaultValue(viewData,viewShape);
474
475 bool expanded=false;
476
477 Data myData(keys,values,defaultValue,FunctionSpace(),expanded);*/
478 int arr[1]={1}; // iso c++ does not like empty arrays
479 DataTagged* dt=new DataTagged(FunctionSpace(),viewShape,arr,viewData);
480 Data myData(dt);
481
482 // cout << myData.toString() << endl;
483
484 assert(!myData.isEmpty());
485 assert(myData.isTagged());
486 assert(myData.getTagNumber(0)==1);
487 assert(myData.getDataPointRank()==1);
488 assert(myData.getLength()==3);
489
490 assert(myData.getNoValues()==3);
491 assert(myData.getDataAtOffset(0)==0.0);
492 assert(myData.getDataAtOffset(1)==1.0);
493 assert(myData.getDataAtOffset(2)==2.0);
494
495 // DataArrayView myDataView = myData.getPointDataView();
496 // assert(!myDataView.isEmpty());
497 // assert(myDataView.getOffset()==0);
498 // assert(myDataView.getRank()==1);
499 // assert(myDataView.noValues()==3);
500 // assert(myDataView.getShape().size()==1);
501 // assert(myDataView(0)==0.0);
502 // assert(myDataView(1)==1.0);
503 // assert(myDataView(2)==2.0);
504
505 // myDataView = myData.getDataPoint(0,0);
506 // assert(!myDataView.isEmpty());
507 // assert(myDataView.getOffset()==0);
508 // assert(myDataView.getRank()==1);
509 // assert(myDataView.noValues()==3);
510 // assert(myDataView.getShape().size()==1);
511 // assert(myDataView(0)==0.0);
512 // assert(myDataView(1)==1.0);
513 // assert(myDataView(2)==2.0);
514
515 double* sampleData=myData.getSampleData(0);
516 for (int i=0; i<myData.getNoValues(); i++) {
517 assert(sampleData[i]==i);
518 }
519 // use a non-existent tag so we get a pointer to
520 // the first element of the data array
521 sampleData=myData.getSampleDataByTag(9);
522 for (int i=0; i<myData.getLength(); i++) {
523 assert(sampleData[i]==i);
524 }
525
526 cout << "\tTest setting of a tag and associated value." << endl;
527
528 // value for tag "1"
529 DataTypes::ValueType eTwoData(viewData);
530 // DataArrayView eTwoView(eTwoData, viewShape);
531 for (int i=0;i<viewShape[0];i++) {
532 eTwoData[i]=i+2.0;
533 }
534
535 myData.setTaggedValueFromCPP(1,viewShape, eTwoData);
536
537 assert(myData.getLength()==6);
538
539 int offset=myData.getDataOffset(0,0);
540 // myDataView = myData.getDataPoint(0,0);
541 // assert(myDataView==eTwoView);
542 // assert(!myDataView.isEmpty());
543 assert(offset==3);
544 assert(myData.getDataPointRank()==1);
545 assert(myData.getNoValues()==3);
546 // assert(myDataView.getShape().size()==1);
547
548 assert(myData.getDataAtOffset(offset+0)==2);
549 assert(myData.getDataAtOffset(offset+1)==3);
550 assert(myData.getDataAtOffset(offset+2)==4);
551
552 sampleData=myData.getSampleDataByTag(1);
553 for (int i=0; i<myData.getNoValues(); i++) {
554 assert(sampleData[i]==i+2);
555 }
556
557 }
558
559 {
560
561 cout << "\tCreate a DataTagged object via tag() method." << endl;
562
563 DataTypes::ShapeType viewShape;
564 viewShape.push_back(2);
565 viewShape.push_back(3);
566 Data myData(1.3,viewShape,FunctionSpace(),false);
567 myData.tag();
568
569 //cout << myData.toString() << endl;
570
571 assert(!myData.isEmpty());
572 assert(myData.isTagged());
573 assert(myData.getTagNumber(0)==1);
574 assert(myData.getDataPointRank()==2);
575 assert(myData.getLength()==6);
576
577 // check default value
578 // DataArrayView myDataView = myData.getPointDataView();
579 assert(!myData.isEmpty());
580 // assert(myDataView.getOffset()==0);
581 assert(myData.getDataPointRank()==2);
582 assert(myData.getNoValues()==6);
583 assert(myData.getDataPointShape().size()==2);
584 assert(getRef(myData,0,0)==1.3);
585 assert(getRef(myData,0,1)==1.3);
586 assert(getRef(myData,0,2)==1.3);
587 assert(getRef(myData,1,0)==1.3);
588 assert(getRef(myData,1,1)==1.3);
589 assert(getRef(myData,1,2)==1.3);
590
591 // check value for data-point (0,0).
592 // myDataView = myData.getDataPoint(0,0);
593 assert(!myData.isEmpty());
594 // assert(myDataView.getOffset()==0);
595 assert(myData.getDataPointRank()==2);
596 assert(myData.getNoValues()==6);
597 assert(myData.getDataPointShape().size()==2);
598 assert(getRef(myData,0,0)==1.3);
599 assert(getRef(myData,0,1)==1.3);
600 assert(getRef(myData,0,2)==1.3);
601 assert(getRef(myData,1,0)==1.3);
602 assert(getRef(myData,1,1)==1.3);
603 assert(getRef(myData,1,2)==1.3);
604
605 }
606
607 }
608
609 void DataTestCase::testDataTaggedExceptions() {
610
611 cout << endl;
612
613 cout << "\tTest DataTagged exceptions." << endl;
614
615 Data myData;
616
617 try {
618 myData.getSampleDataByTag(0);;
619 assert(false);
620 }
621 catch (EsysException&) {
622 //cout << e.what() << endl;
623 assert(true);
624 }
625
626 try {
627 myData.setTaggedValueFromCPP(0,DataTypes::ShapeType(), DataTypes::ValueType());;
628 assert(false);
629 }
630 catch (EsysException&) {
631 //cout << e.what() << endl;
632 assert(true);
633 }
634
635 }
636
637 void DataTestCase::testConstructors() {
638
639 cout << endl;
640
641 DataTypes::ShapeType viewShape;
642 {
643 cout << "\tCreate an Empty Data object" << endl;
644 Data temp(1.3,viewShape,FunctionSpace(),false);
645 }
646 {
647 cout << "\tCreate a rank 2 Data object" << endl;
648 viewShape.push_back(2);
649 viewShape.push_back(3);
650 Data temp(1.3,viewShape,FunctionSpace(),false);
651 }
652 }
653
654 void DataTestCase::testOperations() {
655
656 cout << endl;
657
658 // define the shape for the DataArrayView test data
659 DataTypes::ShapeType shape;
660 shape.push_back(2);
661 shape.push_back(3);
662
663 // allocate the data for the DataArrayView
664 DataTypes::ValueType data(DataTypes::noValues(shape),0);
665
666 // construct DataArrayView
667 // DataArrayView dataView(data,shape);
668
669 // assign values to the data
670 for (int i=0;i<shape[0];i++) {
671 for (int j=0;j<shape[1];j++) {
672 data[getRelIndex(shape,i,j)]=getRelIndex(shape,i,j);
673 }
674 }
675
676 Data baseEx(data,shape,FunctionSpace(),true);
677 Data baseCon(data,shape,FunctionSpace(),false);
678 Data baseTag(data,shape,FunctionSpace(),false);
679 baseTag.tag();
680
681 assert(baseEx.isExpanded());
682 assert(baseCon.isConstant());
683 assert(baseTag.isTagged());
684
685 Data resultEx;
686 Data resultCon;
687 Data resultTag;
688
689 // test unary operations
690
691 double tmp;
692 cout << "\tTest Data::pow." << endl;
693 Data power(3.0,shape,FunctionSpace(),true);
694 resultEx.copy(baseEx.powD(power));
695 resultCon.copy(baseCon.powD(power));
696 resultTag.copy(baseTag.powD(power));
697 for (int i=0;i<shape[0];i++) {
698 for (int j=0;j<shape[1];j++) {
699 tmp=pow((double)data[getRelIndex(shape,i,j)],(double)3.0);
700 assert(std::abs(getRef(resultEx,i,j) - tmp) <= REL_TOL*std::abs(tmp));
701 assert(std::abs(getRef(resultCon,i,j) - tmp) <= REL_TOL*std::abs(tmp));
702 assert(std::abs(getRef(resultTag,i,j) - tmp) <= REL_TOL*std::abs(tmp));
703 }
704 }
705
706 cout << "\tTest Data::sin." << endl;
707 resultEx.copy(baseEx.sin());
708 resultCon.copy(baseCon.sin());
709 resultTag.copy(baseTag.sin());
710 for (int i=0;i<shape[0];i++) {
711 for (int j=0;j<shape[1];j++) {
712 tmp=sin((double)data[getRelIndex(shape,i,j)]);
713 assert(std::abs(getRef(resultEx,i,j) - tmp) <= REL_TOL*std::abs(tmp));
714 assert(std::abs(getRef(resultCon,i,j) - tmp) <= REL_TOL*std::abs(tmp));
715 assert(std::abs(getRef(resultTag,i,j) - tmp) <= REL_TOL*std::abs(tmp));
716 }
717 }
718
719 cout << "\tTest Data::cos." << endl;
720 resultEx.copy(baseEx.cos());
721 resultCon.copy(baseCon.cos());
722 resultTag.copy(baseTag.cos());
723 for (int i=0;i<shape[0];i++) {
724 for (int j=0;j<shape[1];j++) {
725 tmp=cos((double)data[getRelIndex(shape,i,j)]);
726 assert(std::abs(getRef(resultEx,i,j) - tmp) <= REL_TOL*std::abs(tmp));
727 assert(std::abs(getRef(resultCon,i,j) - tmp) <= REL_TOL*std::abs(tmp));
728 assert(std::abs(getRef(resultTag,i,j) - tmp) <= REL_TOL*std::abs(tmp));
729 }
730 }
731
732 cout << "\tTest Data::tan." << endl;
733 resultEx.copy(baseEx.tan());
734 resultCon.copy(baseCon.tan());
735 resultTag.copy(baseTag.tan());
736 for (int i=0;i<shape[0];i++) {
737 for (int j=0;j<shape[1];j++) {
738 tmp=tan((double)data[getRelIndex(shape,i,j)]);
739 assert(std::abs(getRef(resultEx,i,j) - tmp) <= REL_TOL*std::abs(tmp));
740 assert(std::abs(getRef(resultCon,i,j)- tmp) <= REL_TOL*std::abs(tmp));
741 assert(std::abs(getRef(resultTag,i,j)- tmp) <= REL_TOL*std::abs(tmp));
742 }
743 }
744
745 cout << "\tTest Data::asin." << endl;
746 resultEx.copy(baseEx.asin());
747 resultCon.copy(baseCon.asin());
748 resultTag.copy(baseTag.asin());
749 assert(true);
750
751 cout << "\tTest Data::acos." << endl;
752 resultEx.copy(baseEx.acos());
753 resultCon.copy(baseCon.acos());
754 resultTag.copy(baseTag.acos());
755 assert(true);
756
757 cout << "\tTest Data::atan." << endl;
758 resultEx.copy(baseEx.atan());
759 resultCon.copy(baseCon.atan());
760 resultTag.copy(baseTag.atan());
761 for (int i=0;i<shape[0];i++) {
762 for (int j=0;j<shape[1];j++) {
763 tmp=atan((double)data[getRelIndex(shape,i,j)]);
764 assert(std::abs(getRef(resultEx,i,j) - tmp) <= REL_TOL*std::abs(tmp));
765 assert(std::abs(getRef(resultCon,i,j)- tmp) <= REL_TOL*std::abs(tmp));
766 assert(std::abs(getRef(resultTag,i,j)- tmp) <= REL_TOL*std::abs(tmp));
767 }
768 }
769
770 cout << "\tTest Data::sinh." << endl;
771 resultEx.copy(baseEx.sinh());
772 resultCon.copy(baseCon.sinh());
773 resultTag.copy(baseTag.sinh());
774 for (int i=0;i<shape[0];i++) {
775 for (int j=0;j<shape[1];j++) {
776 tmp=sinh((double)data[getRelIndex(shape,i,j)]);
777 assert(std::abs(getRef(resultEx,i,j) - tmp) <= REL_TOL*std::abs(tmp));
778 assert(std::abs(getRef(resultCon,i,j)- tmp) <= REL_TOL*std::abs(tmp));
779 assert(std::abs(getRef(resultTag,i,j)- tmp) <= REL_TOL*std::abs(tmp));
780 }
781 }
782
783 cout << "\tTest Data::cosh." << endl;
784 resultEx.copy(baseEx.cosh());
785 resultCon.copy(baseCon.cosh());
786 resultTag.copy(baseTag.cosh());
787 for (int i=0;i<shape[0];i++) {
788 for (int j=0;j<shape[1];j++) {
789 tmp=cosh((double)data[getRelIndex(shape,i,j)]);
790 assert(std::abs(getRef(resultEx,i,j) - tmp) <= REL_TOL*std::abs(tmp));
791 assert(std::abs(getRef(resultCon,i,j)- tmp) <= REL_TOL*std::abs(tmp));
792 assert(std::abs(getRef(resultTag,i,j)- tmp) <= REL_TOL*std::abs(tmp));
793 }
794 }
795
796 cout << "\tTest Data::tanh." << endl;
797 resultEx.copy(baseEx.tanh());
798 resultCon.copy(baseCon.tanh());
799 resultTag.copy(baseTag.tanh());
800 for (int i=0;i<shape[0];i++) {
801 for (int j=0;j<shape[1];j++) {
802 tmp=tanh((double)data[getRelIndex(shape,i,j)]);
803 assert(std::abs(getRef(resultEx,i,j) - tmp) <= REL_TOL*std::abs(tmp));
804 assert(std::abs(getRef(resultCon,i,j)- tmp) <= REL_TOL*std::abs(tmp));
805 assert(std::abs(getRef(resultTag,i,j)- tmp) <= REL_TOL*std::abs(tmp));
806 }
807 }
808
809 cout << "\tTest Data::asinh." << endl;
810 resultEx.copy(baseEx.asinh());
811 resultCon.copy(baseCon.asinh());
812 resultTag.copy(baseTag.asinh());
813 assert(true);
814
815 cout << "\tTest Data::acosh." << endl;
816 resultEx.copy(baseEx.acosh());
817 resultCon.copy(baseCon.acosh());
818 resultTag.copy(baseTag.acosh());
819 assert(true);
820
821 cout << "\tTest Data::atanh." << endl;
822 resultEx.copy(baseEx.atanh());
823 resultCon.copy(baseCon.atanh());
824 resultTag.copy(baseTag.atanh());
825 assert(true);
826
827 cout << "\tTest Data::log." << endl;
828 resultEx.copy(baseEx.log());
829 resultCon.copy(baseCon.log());
830 resultTag.copy(baseTag.log());
831 assert(true);
832
833 cout << "\tTest Data::abs." << endl;
834 resultEx.copy(baseEx.abs());
835 resultCon.copy(baseCon.abs());
836 resultTag.copy(baseTag.abs());
837 for (int i=0;i<shape[0];i++) {
838 for (int j=0;j<shape[1];j++) {
839 tmp=abs((double)data[getRelIndex(shape,i,j)]);
840 assert(std::abs(getRef(resultEx,i,j) - tmp) <= REL_TOL*std::abs(tmp));
841 assert(std::abs(getRef(resultCon,i,j)- tmp) <= REL_TOL*std::abs(tmp));
842 assert(std::abs(getRef(resultTag,i,j)- tmp) <= REL_TOL*std::abs(tmp));
843 }
844 }
845
846 cout << "\tTest Data::sign." << endl;
847 resultEx.copy(baseEx.sign());
848 resultCon.copy(baseCon.sign());
849 resultTag.copy(baseTag.sign());
850 assert(true);
851
852 cout << "\tTest Data::exp." << endl;
853 resultEx.copy(baseEx.exp());
854 resultCon.copy(baseCon.exp());
855 resultTag.copy(baseTag.exp());
856 for (int i=0;i<shape[0];i++) {
857 for (int j=0;j<shape[1];j++) {
858 tmp=exp((double)data[getRelIndex(shape,i,j)]);
859 assert(std::abs(getRef(resultEx,i,j) - tmp) <= REL_TOL*std::abs(tmp));
860 assert(std::abs(getRef(resultCon,i,j)- tmp) <= REL_TOL*std::abs(tmp));
861 assert(std::abs(getRef(resultTag,i,j)- tmp) <= REL_TOL*std::abs(tmp));
862 }
863 }
864
865 cout << "\tTest Data::sqrt." << endl;
866 resultEx.copy(baseEx.sqrt());
867 resultCon.copy(baseCon.sqrt());
868 resultTag.copy(baseTag.sqrt());
869 for (int i=0;i<shape[0];i++) {
870 for (int j=0;j<shape[1];j++) {
871 tmp=sqrt((double)data[getRelIndex(shape,i,j)]);
872 assert(std::abs(getRef(resultEx,i,j) - tmp) <= REL_TOL*std::abs(tmp));
873 assert(std::abs(getRef(resultCon,i,j)- tmp) <= REL_TOL*std::abs(tmp));
874 assert(std::abs(getRef(resultTag,i,j)- tmp) <= REL_TOL*std::abs(tmp));
875 }
876 }
877
878 cout << "\tTest Data::neg." << endl;
879 resultEx.copy(baseEx.neg());
880 resultCon.copy(baseCon.neg());
881 resultTag.copy(baseTag.neg());
882 assert(true);
883
884 cout << "\tTest Data::pos." << endl;
885 resultEx.copy(baseEx.pos());
886 resultCon.copy(baseCon.pos());
887 resultTag.copy(baseTag.pos());
888 for (int i=0;i<shape[0];i++) {
889 for (int j=0;j<shape[1];j++) {
890 assert(std::abs(getRef(resultEx,i,j) - getRelIndex(shape,i,j)) <= REL_TOL*std::abs(data[getRelIndex(shape,i,j)]));
891 assert(std::abs(getRef(resultCon,i,j) - getRelIndex(shape,i,j)) <= REL_TOL*std::abs(data[getRelIndex(shape,i,j)]));
892 assert(std::abs(getRef(resultTag,i,j) - getRelIndex(shape,i,j)) <= REL_TOL*std::abs(data[getRelIndex(shape,i,j)]));
893 }
894 }
895
896 // test reduction operations
897
898 cout << "\tTest Data::Lsup." << endl;
899 assert(std::abs(baseEx.Lsup() - 5) <= REL_TOL*5);
900 assert(std::abs(baseCon.Lsup() - 5) <= REL_TOL*5);
901 assert(std::abs(baseTag.Lsup() - 5) <= REL_TOL*5);
902
903 cout << "\tTest Data::sup." << endl;
904 assert(std::abs(baseEx.sup() - 5) <= REL_TOL*5);
905 assert(std::abs(baseCon.sup() - 5) <= REL_TOL*5);
906 assert(std::abs(baseTag.sup() - 5) <= REL_TOL*5);
907
908 cout << "\tTest Data::inf." << endl;
909 assert(std::abs(baseEx.inf() - 0) <= REL_TOL*0);
910 assert(std::abs(baseCon.inf() - 0) <= REL_TOL*0);
911 assert(std::abs(baseTag.inf() - 0) <= REL_TOL*0);
912
913 // test data-point reduction operations
914
915 cout << "\tTest Data::minval." << endl;
916 resultEx.copy(baseEx.minval());
917 resultCon.copy(baseCon.minval());
918 resultTag.copy(baseTag.minval());
919 // assert(std::abs(resultEx.getPointDataView()() - 0) <= REL_TOL*0);
920 // assert(std::abs(resultCon.getPointDataView()() - 0) <= REL_TOL*0);
921 // assert(std::abs(resultTag.getPointDataView()() - 0) <= REL_TOL*0);
922 assert(std::abs(resultEx.getDataAtOffset(0) - 0) <= REL_TOL*0);
923 assert(std::abs(resultCon.getDataAtOffset(0) - 0) <= REL_TOL*0);
924 assert(std::abs(resultTag.getDataAtOffset(0) - 0) <= REL_TOL*0);
925
926 cout << "\tTest Data::maxval." << endl;
927 resultEx.copy(baseEx.maxval());
928 resultCon.copy(baseCon.maxval());
929 resultTag.copy(baseTag.maxval());
930 assert(std::abs(resultEx.getDataAtOffset(0) - 5) <= REL_TOL*5);
931 assert(std::abs(resultCon.getDataAtOffset(0) - 5) <= REL_TOL*5);
932 assert(std::abs(resultTag.getDataAtOffset(0) - 5) <= REL_TOL*5);
933
934 }
935
936
937 void DataTestCase::testMemAlloc() {
938
939 //
940 // Simple little sanity check for the memory allocator
941
942 cout << endl;
943
944 Data *testData;
945 for (int i=0; i<1000; i++) {
946 testData = new Data(0.0, DataTypes::ShapeType(), FunctionSpace(), true);
947 delete testData;
948 }
949
950 DataTypes::ShapeType viewShape;
951 viewShape.push_back(10);
952 viewShape.push_back(10);
953 viewShape.push_back(10);
954
955 Data *testData2;
956 Data *testData3 = new Data(0.0, viewShape, FunctionSpace(), true);
957 for (int i=0; i<1000; i++) {
958 testData2 = new Data(0.0, viewShape, FunctionSpace(), true);
959 delete testData2;
960 }
961 delete testData3;
962
963 }
964
965 TestSuite* DataTestCase::suite ()
966 {
967 //
968 // create the suite of tests to perform.
969 TestSuite *testSuite = new TestSuite ("DataTestCase");
970 testSuite->addTest (new TestCaller< DataTestCase>("testCopying",&DataTestCase::testCopying));
971 testSuite->addTest (new TestCaller< DataTestCase>("testAll",&DataTestCase::testAll));
972 testSuite->addTest (new TestCaller< DataTestCase>("testMore",&DataTestCase::testMore));
973 testSuite->addTest (new TestCaller< DataTestCase>("testDataConstant",&DataTestCase::testDataConstant));
974 testSuite->addTest (new TestCaller< DataTestCase>("testDataTagged",&DataTestCase::testDataTagged));
975 testSuite->addTest (new TestCaller< DataTestCase>("testDataTaggedExceptions",&DataTestCase::testDataTaggedExceptions));
976 testSuite->addTest (new TestCaller< DataTestCase>("testConstructors",&DataTestCase::testConstructors));
977 testSuite->addTest (new TestCaller< DataTestCase>("testSlicing",&DataTestCase::testSlicing));
978 testSuite->addTest (new TestCaller< DataTestCase>("testOperations",&DataTestCase::testOperations));
979 //testSuite->addTest (new TestCaller< DataTestCase>("testRefValue",&DataTestCase::testRefValue));
980 testSuite->addTest (new TestCaller< DataTestCase>("testMemAlloc",&DataTestCase::testMemAlloc));
981
982 return testSuite;
983 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26