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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2105 - (show annotations)
Fri Nov 28 01:52:12 2008 UTC (10 years, 10 months ago) by jfenwick
File size: 34569 byte(s)
Data::copySelf() now returns an object instead of a pointer.
Fixed a bug in copyFromArray relating to expanded 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 const char* strs[]={"DataConstant", "DataTagged", "DataExpanded"};
82 dats[0]=new Data(new DataConstant(FunctionSpace(),shape,data));
83 dats[1]=new Data(new DataTagged(FunctionSpace(),shape,data));
84 dats[2]=new Data(new DataExpanded(FunctionSpace(),shape,data));
85 if (delayed)
86 {
87 dats[0]->delaySelf();
88 dats[1]->delaySelf();
89 dats[2]->delaySelf();
90 }
91
92 for (int k=0;k<NUMDATS;++k)
93 {
94 cout << "\tTest deep copy " << strs[k] << endl;
95 Data* d=dats[k];
96 Data deep=d->copySelf(); // test self copy
97 if (delayed)
98 {
99 assert(deep.isLazy());
100 }
101 for (int i=0;i<DataTypes::noValues(shape);++i)
102 {
103 if (d->getDataAtOffset(i)!=deep.getDataAtOffset(i))
104 assert(false);
105 }
106 if (delayed)
107 {
108 d->delaySelf();
109 }
110 d->setToZero();
111 if (delayed)
112 {
113 assert(d->isLazy());
114 }
115 for (int i=0;i<DataTypes::noValues(shape);++i)
116 {
117 if (d->getDataAtOffset(i)==deep.getDataAtOffset(i))
118 assert(false);
119 }
120 if (delayed)
121 {
122 d->delaySelf();
123 deep.delaySelf();
124 }
125 d->copy(deep); // test copy from object
126 if (delayed)
127 {
128 assert(d->isLazy());
129 }
130 for (int i=0;i<DataTypes::noValues(shape);++i)
131 {
132 if (d->getDataAtOffset(i)!=deep.getDataAtOffset(i))
133 assert(false);
134 }
135 d->setToZero();
136 for (int i=0;i<DataTypes::noValues(shape);++i)
137 {
138 if (d->getDataAtOffset(i)==deep.getDataAtOffset(i))
139 assert(false);
140 }
141 delete dats[k];
142 }
143
144
145
146
147
148
149 }
150
151
152 void DataTestCase::testSlicingWorker(bool delayed)
153 {
154
155 using namespace escript::DataTypes;
156 cout << endl;
157 {
158 DataTypes::ShapeType viewShape;
159 viewShape.push_back(2);
160 viewShape.push_back(3);
161
162 const int NUMDATS=3;
163 const char* strs[]={"DataConstant", "DataTagged","DataExpanded"};
164 bool tags[]={false,true,false}; // is the slice of this data supposed to be tagged
165 Data* dats[NUMDATS];
166 for (int k=0;k<NUMDATS;++k)
167 {
168 dats[k]=new Data(1.3, viewShape);
169 }
170 dats[1]->tag();
171 dats[2]->expand();
172 for (int k=0;k<NUMDATS;++k)
173 {
174 Data* temp=dats[k];
175 dats[k]=new Data(dats[k]->delay());
176 delete temp;
177 }
178 for (int k=0;k<NUMDATS;++k)
179 {
180 cout << "\t\tTest get-slicing " << strs[k] << endl;
181 dats[k]->getDataAtOffset(dats[k]->getDataOffset(0,0)+getRelIndex(viewShape,0,0))=1.0;
182 dats[k]->getDataAtOffset(dats[k]->getDataOffset(0,0)+getRelIndex(viewShape,1,1))=2.0;
183
184 DataTypes::RegionType region;
185 region.push_back(DataTypes::RegionType::value_type(0,0));
186 region.push_back(DataTypes::RegionType::value_type(0,0));
187
188 Data slice1(dats[k]->getSlice(region));
189
190 if (tags[k]) {assert(slice1.isTagged());}
191 assert(slice1.getDataPointRank()==0);
192 assert(slice1.getDataPoint(0,0)==1.0);
193
194 //
195 // create a rank 2 slice with one value
196
197 region.clear();
198 region.push_back(DataTypes::RegionType::value_type(0,1));
199 region.push_back(DataTypes::RegionType::value_type(0,1));
200
201 Data slice2(dats[k]->getSlice(region));
202
203 //cout << slice2.toString() << endl;
204
205 if (tags[k]) {assert(slice2.isTagged());}
206 assert(slice2.getDataPointRank()==2);
207
208 assert(slice2.getDataAtOffset(slice2.getDataOffset(0,0)+getRelIndex(slice2.getDataPointShape(),0,0))==1.0);
209
210 //
211 // create a rank 2 slice with four values
212
213 region.clear();
214 region.push_back(DataTypes::RegionType::value_type(0,2));
215 region.push_back(DataTypes::RegionType::value_type(0,2));
216
217 Data slice3(dats[k]->getSlice(region));
218
219 //cout << slice3.toString() << endl;
220
221 if (tags[k]) {assert(slice3.isTagged());}
222 assert(slice3.getDataPointRank()==2);
223 assert(getRef(slice3,0,0,0,0)==1.0);
224 assert(getRef(slice3,0,0,0,1)==1.3);
225 assert(getRef(slice3,0,0,1,0)==1.3);
226 assert(getRef(slice3,0,0,1,1)==2.0);
227 }
228
229 // now some extra tests for tagged data (dats[1])
230
231 //
232 // add a value for tag "1"
233
234 DataTypes::ValueType viewData(6);
235 for (int i=0;i<viewData.size();i++) {
236 viewData[i]=i;
237 }
238 dats[1]->setTaggedValueFromCPP(1, viewShape, viewData);
239
240 //
241 // create a full slice
242
243 DataTypes::RegionType region;
244 region.push_back(DataTypes::RegionType::value_type(0,2));
245 region.push_back(DataTypes::RegionType::value_type(0,3));
246
247 Data slice4(dats[1]->getSlice(region));
248
249 assert(slice4.isTagged());
250 assert(slice4.getDataPointRank()==2);
251 assert(getRef(slice4,0,0,0,0)==0);
252 assert(getRef(slice4,0,0,0,1)==2);
253 assert(getRef(slice4,0,0,0,2)==4);
254 assert(getRef(slice4,0,0,1,0)==1);
255 assert(getRef(slice4,0,0,1,1)==3);
256 assert(getRef(slice4,0,0,1,2)==5);
257
258 for (int k=0;k<NUMDATS;++k)
259 {
260 delete dats[k];
261 }
262 }
263
264 {
265 DataTypes::ShapeType viewShape;
266 viewShape.push_back(2);
267 viewShape.push_back(3);
268
269 const int NUMDATS=3;
270 const char* strs[]={"DataConstant", "DataTagged","DataExpanded"};
271 Data* dats[NUMDATS];
272 Data* src[NUMDATS];
273 for (int k=0;k<NUMDATS;++k)
274 {
275 dats[k]=new Data(1.3, viewShape);
276 src[k]=new Data(10,DataTypes::scalarShape);
277 }
278 dats[1]->tag();
279 src[1]->tag();
280 dats[2]->expand();
281 src[2]->expand();
282 if (delayed)
283 {
284 for(int k=0;k<NUMDATS;++k)
285 {
286 if (delayed)
287 {
288 Data* temp=dats[k];
289 dats[k]=new Data(dats[k]->delay()); // coz delay returns an object not a pointer
290 delete temp;
291 temp=src[k];
292 src[k]=new Data(src[k]->delay());
293 delete temp;
294 }
295 }
296 }
297 for (int k=0;k<NUMDATS;++k)
298 {
299 cout << "\t\tTest set-slicing " << strs[k] << endl;
300 Data target(1.3,viewShape);
301 if (k==2) {target.expand();}
302 DataTypes::RegionType region;
303 region.push_back(DataTypes::RegionType::value_type(1,1));
304 region.push_back(DataTypes::RegionType::value_type(1,1));
305 target.setSlice(*(src[k]),region);
306 assert(getRef(target,0,0,1,1)==src[k]->getDataPoint(0,0));
307 }
308
309 // some extra tests on tagged data
310
311 //
312 // add a value for tag "1" to target
313
314 DataTypes::ValueType viewData(6);
315 for (int i=0;i<viewData.size();i++) {
316 viewData[i]=i;
317 }
318
319 Data target(1.3,viewShape,FunctionSpace(),false);
320 target.tag();
321 target.setTaggedValueFromCPP(1, viewShape, viewData);
322
323 //cout << "target:\n" << target.toString() << endl;
324
325 //
326 // set a slice in target from source
327
328 DataTypes::RegionType region;
329 region.push_back(DataTypes::RegionType::value_type(0,0));
330 region.push_back(DataTypes::RegionType::value_type(1,1));
331
332 target.setSlice(*src[1],region);
333
334 assert(target.isTagged());
335 assert(target.getDataPointRank()==2);
336 assert(getRef(target,0,0,0,0)==0);
337 assert(getRef(target,0,0,0,1)==src[1]->getDataPoint(0,0));
338 assert(getRef(target,0,0,0,2)==4);
339 assert(getRef(target,0,0,1,0)==1);
340 assert(getRef(target,0,0,1,1)==3);
341 assert(getRef(target,0,0,1,2)==5);
342
343 //
344 // add a value for tag "2" to source
345
346 DataTypes::ShapeType viewShape2;
347 DataTypes::ValueType viewData2(1);
348 viewData2[0]=6;
349 src[1]->setTaggedValueFromCPP(2, viewShape2, viewData2);
350
351 region.clear();
352 region.push_back(DataTypes::RegionType::value_type(0,0));
353 region.push_back(DataTypes::RegionType::value_type(1,1));
354
355 target.setSlice(*src[1],region);
356
357 assert(target.isTagged());
358 assert(target.getDataPointRank()==2);
359
360 // use a non-existant tag so we get a pointer to the default value
361 // ie: the first element in the data array
362 DataAbstract::ValueType::value_type* targetData=target.getSampleDataByTag(9);
363 for (int i=0; i<target.getLength(); i++) {
364 assert(targetData[i]>=0);
365 }
366 assert(targetData[0]==1.3);
367 assert(targetData[1]==1.3);
368 assert(targetData[2]==10);
369 assert(targetData[3]==1.3);
370 assert(targetData[4]==1.3);
371 assert(targetData[5]==1.3);
372 assert(targetData[6]==0);
373 assert(targetData[7]==1);
374 assert(targetData[8]==10);
375 assert(targetData[9]==3);
376 assert(targetData[10]==4);
377 assert(targetData[11]==5);
378 assert(targetData[12]==1.3);
379 assert(targetData[13]==1.3);
380 assert(targetData[14]==6);
381 assert(targetData[15]==1.3);
382 assert(targetData[16]==1.3);
383 assert(targetData[17]==1.3);
384
385
386 for (int k=0;k<NUMDATS;++k)
387 {
388 delete dats[k];
389 delete src[k];
390 }
391
392 }
393
394 }
395
396 // This is to test new copy routines, existing tests should remain where they are
397 void DataTestCase::testCopying()
398 {
399 cout << "\n\tReadyData." << endl;
400 testCopyingWorker(false);
401 cout << "\n\tLazyData." << endl;
402 testCopyingWorker(true);
403 }
404
405 void DataTestCase::testSlicing() {
406 cout << "\n\tReadyData." << endl;
407 testSlicingWorker(false);
408 cout << "\n\tLazyData." << endl;
409 testSlicingWorker(true);
410 }
411
412 void DataTestCase::testSome() {
413
414 cout << endl;
415
416 cout << "\tCreate a Data object." << endl;
417
418 DataTypes::ShapeType viewShape;
419 viewShape.push_back(3);
420 DataTypes::ValueType viewData(3);
421 for (int i=0;i<viewShape[0];++i) {
422 viewData[i]=i;
423 }
424
425 bool expanded=true;
426 Data exData(viewData,viewShape,FunctionSpace(),expanded);
427 Data cData(viewData,viewShape);
428 Data result;
429
430 assert(exData.isExpanded());
431 assert(cData.isConstant());
432 assert(result.isEmpty());
433
434 cout << "\tTest some basic operations" << endl;
435 result=exData*cData;
436 assert(result.isExpanded());
437
438 assert(result.Lsup()==4);
439 assert(result.sup()==4);
440 assert(result.inf()==0);
441
442 result=exData+cData;
443 result=exData-cData;
444 result=exData/cData;
445
446 cout << "\tExercise wherePositive method" << endl;
447 assert(!exData.wherePositive().isEmpty());
448
449 cout << "\tExercise copyWithMask method" << endl;
450 exData.copyWithMask(result, exData.wherePositive());
451 assert(!exData.wherePositive().isEmpty());
452
453 }
454
455
456
457 // This method tests to see if resolve() produces results of the correct type
458 void DataTestCase::testResolveType()
459 {
460 cout << endl;
461 cout << "\tTesting resolve()\n";
462 DataTypes::ShapeType viewShape;
463 viewShape.push_back(2);
464 viewShape.push_back(3);
465 viewShape.push_back(4);
466 DataTypes::ValueType viewData(2*3*4);
467 for (int i=0;i<DataTypes::noValues(viewShape);++i) {
468 viewData[i]=i;
469 }
470 Data c1(viewData,viewShape);
471 Data t1(viewData,viewShape);
472 Data e1(viewData,viewShape);
473 t1.tag();
474 e1.expand();
475 c1.delaySelf();
476 t1.delaySelf();
477 e1.delaySelf();
478 Data d1=c1+c1;
479 assert(d1.isLazy());
480 assert((d1.resolve(),d1.isConstant()));
481 d1=c1+t1;
482 assert(d1.isLazy());
483 assert((d1.resolve(),d1.isTagged()));
484 d1=t1+c1;
485 assert(d1.isLazy());
486 assert((d1.resolve(),d1.isTagged()));
487 d1=t1+t1;
488 assert(d1.isLazy());
489 assert((d1.resolve(),d1.isTagged()));
490 d1=c1+e1;
491 assert(d1.isLazy());
492 assert((d1.resolve(),d1.isExpanded()));
493 d1=e1+c1;
494 assert(d1.isLazy());
495 assert((d1.resolve(),d1.isExpanded()));
496 d1=e1+t1;
497 assert(d1.isLazy());
498 assert((d1.resolve(),d1.isExpanded()));
499 d1=t1+e1;
500 assert(d1.isLazy());
501 assert((d1.resolve(),d1.isExpanded()));
502 d1=e1+e1;
503 assert(d1.isLazy());
504 assert((d1.resolve(),d1.isExpanded()));
505 cout << "\tTesting tag()\n";
506 c1.tag();
507 assert(c1.isTagged());
508 t1.tag();
509 assert(t1.isTagged());
510 try
511 {
512 e1.tag();
513 assert(false); // this should have thrown
514 } catch(...) {}
515 cout << "\tTesting expand()\n";
516 Data c2(viewData,viewShape);
517 Data t2(viewData,viewShape);
518 Data e2(viewData,viewShape);
519 t2.tag();
520 e2.expand();
521 c2.delaySelf();
522 t2.delaySelf();
523 e2.delaySelf();
524 c2.expand();
525 assert(c2.isExpanded());
526 t2.expand();
527 assert(t2.isExpanded());
528 e2.expand();
529 assert(e2.isExpanded());
530 }
531
532 void DataTestCase::testDataConstant() {
533
534 cout << endl;
535
536 cout << "\tCreate a DataConstant object." << endl;
537
538 DataTypes::ShapeType viewShape;
539 viewShape.push_back(2);
540 viewShape.push_back(3);
541 viewShape.push_back(4);
542 DataTypes::ValueType viewData(2*3*4);
543 for (int i=0;i<DataTypes::noValues(viewShape);++i) {
544 viewData[i]=i;
545 }
546
547 Data left(viewData,viewShape);
548 Data right(viewData,viewShape);
549 Data result;
550
551 cout << "\tTest some basic operations" << endl;
552
553 result=left-right;
554
555 assert(left.isConstant());
556 assert(right.isConstant());
557 assert(result.isConstant());
558
559 result=left+right;
560
561 assert(left.isConstant());
562 assert(right.isConstant());
563 assert(result.isConstant());
564
565 assert(!result.isExpanded());
566 assert(!result.isTagged());
567
568 }
569
570 void DataTestCase::testDataTagged() {
571
572 cout << endl;
573
574 {
575
576 cout << "\tCreate a DataTagged object with a default value only." << endl;
577
578 DataTypes::ShapeType viewShape;
579 viewShape.push_back(3);
580
581 DataTypes::ValueType viewData(3);
582 for (int i=0;i<viewShape[0];i++) {
583 viewData[i]=i;
584 }
585 int arr[1]={1}; // iso c++ does not like empty arrays
586 DataTagged* dt=new DataTagged(FunctionSpace(),viewShape,arr,viewData);
587 Data myData(dt);
588
589 assert(!myData.isEmpty());
590 assert(myData.isTagged());
591 assert(myData.getTagNumber(0)==1);
592 assert(myData.getDataPointRank()==1);
593 assert(myData.getLength()==3);
594
595 assert(myData.getNoValues()==3);
596 assert(myData.getDataAtOffset(0)==0.0);
597 assert(myData.getDataAtOffset(1)==1.0);
598 assert(myData.getDataAtOffset(2)==2.0);
599
600 double* sampleData=myData.getSampleData(0);
601 for (int i=0; i<myData.getNoValues(); i++) {
602 assert(sampleData[i]==i);
603 }
604 // use a non-existent tag so we get a pointer to
605 // the first element of the data array
606 sampleData=myData.getSampleDataByTag(9);
607 for (int i=0; i<myData.getLength(); i++) {
608 assert(sampleData[i]==i);
609 }
610
611 cout << "\tTest setting of a tag and associated value." << endl;
612
613 // value for tag "1"
614 DataTypes::ValueType eTwoData(viewData);
615 // DataArrayView eTwoView(eTwoData, viewShape);
616 for (int i=0;i<viewShape[0];i++) {
617 eTwoData[i]=i+2.0;
618 }
619
620 myData.setTaggedValueFromCPP(1,viewShape, eTwoData);
621
622 assert(myData.getLength()==6);
623
624 int offset=myData.getDataOffset(0,0);
625 assert(offset==3);
626 assert(myData.getDataPointRank()==1);
627 assert(myData.getNoValues()==3);
628
629 assert(myData.getDataAtOffset(offset+0)==2);
630 assert(myData.getDataAtOffset(offset+1)==3);
631 assert(myData.getDataAtOffset(offset+2)==4);
632
633 sampleData=myData.getSampleDataByTag(1);
634 for (int i=0; i<myData.getNoValues(); i++) {
635 assert(sampleData[i]==i+2);
636 }
637
638 }
639
640 {
641
642 cout << "\tCreate a DataTagged object via tag() method." << endl;
643
644 DataTypes::ShapeType viewShape;
645 viewShape.push_back(2);
646 viewShape.push_back(3);
647 Data myData(1.3,viewShape,FunctionSpace(),false);
648 myData.tag();
649
650 assert(!myData.isEmpty());
651 assert(myData.isTagged());
652 assert(myData.getTagNumber(0)==1);
653 assert(myData.getDataPointRank()==2);
654 assert(myData.getLength()==6);
655
656 // check default value
657 assert(!myData.isEmpty());
658 assert(myData.getDataPointRank()==2);
659 assert(myData.getNoValues()==6);
660 assert(myData.getDataPointShape().size()==2);
661 assert(getRef(myData,0,0)==1.3);
662 assert(getRef(myData,0,1)==1.3);
663 assert(getRef(myData,0,2)==1.3);
664 assert(getRef(myData,1,0)==1.3);
665 assert(getRef(myData,1,1)==1.3);
666 assert(getRef(myData,1,2)==1.3);
667
668 // check value for data-point (0,0).
669 // myDataView = myData.getDataPoint(0,0);
670 assert(!myData.isEmpty());
671 // assert(myDataView.getOffset()==0);
672 assert(myData.getDataPointRank()==2);
673 assert(myData.getNoValues()==6);
674 assert(myData.getDataPointShape().size()==2);
675 assert(getRef(myData,0,0)==1.3);
676 assert(getRef(myData,0,1)==1.3);
677 assert(getRef(myData,0,2)==1.3);
678 assert(getRef(myData,1,0)==1.3);
679 assert(getRef(myData,1,1)==1.3);
680 assert(getRef(myData,1,2)==1.3);
681
682 }
683
684 }
685
686 void DataTestCase::testDataTaggedExceptions() {
687
688 cout << endl;
689
690 cout << "\tTest DataTagged exceptions." << endl;
691
692 Data myData;
693
694 try {
695 myData.getSampleDataByTag(0);;
696 assert(false);
697 }
698 catch (EsysException&) {
699 //cout << e.what() << endl;
700 assert(true);
701 }
702
703 try {
704 myData.setTaggedValueFromCPP(0,DataTypes::ShapeType(), DataTypes::ValueType());;
705 assert(false);
706 }
707 catch (EsysException&) {
708 //cout << e.what() << endl;
709 assert(true);
710 }
711
712 }
713
714 void DataTestCase::testConstructors() {
715
716 cout << endl;
717
718 DataTypes::ShapeType viewShape;
719 {
720 cout << "\tCreate an Empty Data object" << endl;
721 Data temp(1.3,viewShape,FunctionSpace(),false);
722 }
723 {
724 cout << "\tCreate a rank 2 Data object" << endl;
725 viewShape.push_back(2);
726 viewShape.push_back(3);
727 Data temp(1.3,viewShape,FunctionSpace(),false);
728 }
729 }
730
731
732 void DataTestCase::testOperations()
733 {
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
753
754 Data dats[]={Data(data,shape,FunctionSpace(),false),
755 Data(data,shape,FunctionSpace(),false),
756 Data(data,shape,FunctionSpace(),true),
757 Data(data,shape,FunctionSpace(),false),
758 Data(data,shape,FunctionSpace(),false),
759 Data(data,shape,FunctionSpace(),true)};
760 const int NUMDATS=6;
761 const int LAZY=3; // where do the lazy objects start?
762
763 // Data baseEx(data,shape,FunctionSpace(),true);
764 // Data baseCon(data,shape,FunctionSpace(),false);
765 // Data baseTag(data,shape,FunctionSpace(),false);
766 Data& baseCon=dats[0];
767 Data& baseTag=dats[1];
768 Data& baseEx=dats[2];
769 baseTag.tag();
770 dats[4].tag();
771 dats[3].delaySelf();
772 dats[4].delaySelf();
773 dats[5].delaySelf();
774
775 assert(baseEx.isExpanded());
776 assert(baseCon.isConstant());
777 assert(baseTag.isTagged());
778
779 Data results[NUMDATS];
780 // Data& resultEx=results[0];
781 // Data& resultCon=results[1];
782 // Data& resultTag=results[2];
783
784 // create 0 <= smalldata <= 1 for testing trig functions
785
786 DataTypes::ValueType smalldata(DataTypes::noValues(shape),0);
787
788 // assign values to the data
789 for (int i=0;i<shape[0];i++) {
790 for (int j=0;j<shape[1];j++) {
791 smalldata[getRelIndex(shape,i,j)]=(i==0 && j==0)?0:1.0/(getRelIndex(shape,i,j)+1);
792 }
793 }
794 Data sdats[]={Data(smalldata,shape,FunctionSpace(),false),
795 Data(smalldata,shape,FunctionSpace(),false),
796 Data(smalldata,shape,FunctionSpace(),true),
797 Data(smalldata,shape,FunctionSpace(),false),
798 Data(smalldata,shape,FunctionSpace(),false),
799 Data(smalldata,shape,FunctionSpace(),true)};
800 sdats[1].tag();
801 sdats[4].tag();
802 sdats[3].delaySelf();
803 sdats[4].delaySelf();
804 sdats[5].delaySelf();
805
806
807
808 // test unary operations
809
810 double tmp;
811 cout << "\tTest Data::pow." << endl;
812 Data power(3.0,shape,FunctionSpace(),true);
813 for (int z=0;z<NUMDATS;++z)
814 {
815 results[z].copy(dats[z].powD(power));
816 if (z>=LAZY)
817 {
818 assert(results[z].isLazy());
819 }
820 }
821 for (int i=0;i<shape[0];i++) {
822 for (int j=0;j<shape[1];j++) {
823 tmp=pow((double)data[getRelIndex(shape,i,j)],(double)3.0);
824 for (int z=0;z<NUMDATS;++z)
825 {
826 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
827 }
828 }
829 }
830
831 cout << "\tTest Data::sin." << endl;
832 for (int z=0;z<NUMDATS;++z)
833 {
834 results[z].copy(dats[z].sin());
835 if (z>=LAZY)
836 {
837 assert(results[z].isLazy());
838 }
839 }
840 for (int i=0;i<shape[0];i++) {
841 for (int j=0;j<shape[1];j++) {
842 tmp=sin((double)data[getRelIndex(shape,i,j)]);
843 for (int z=0;z<NUMDATS;++z)
844 {
845 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
846 }
847 }
848 }
849
850 cout << "\tTest Data::cos." << endl;
851 for (int z=0;z<NUMDATS;++z)
852 {
853 results[z].copy(dats[z].cos());
854 if (z>=LAZY)
855 {
856 assert(results[z].isLazy());
857 }
858 }
859 for (int i=0;i<shape[0];i++) {
860 for (int j=0;j<shape[1];j++) {
861 tmp=cos((double)data[getRelIndex(shape,i,j)]);
862 for (int z=0;z<NUMDATS;++z)
863 {
864 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
865 }
866 }
867 }
868
869 cout << "\tTest Data::tan." << endl;
870 for (int z=0;z<NUMDATS;++z)
871 {
872 results[z].copy(dats[z].tan());
873 if (z>=LAZY)
874 {
875 assert(results[z].isLazy());
876 }
877 }
878 for (int i=0;i<shape[0];i++) {
879 for (int j=0;j<shape[1];j++) {
880 tmp=tan((double)data[getRelIndex(shape,i,j)]);
881 for (int z=0;z<NUMDATS;++z)
882 {
883 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
884 }
885 }
886 }
887
888 cout << "\tTest Data::asin." << endl;
889 for (int z=0;z<NUMDATS;++z)
890 {
891 results[z].copy(sdats[z].asin());
892 if (z>=LAZY)
893 {
894 assert(results[z].isLazy());
895 }
896 }
897 for (int i=0;i<shape[0];i++) {
898 for (int j=0;j<shape[1];j++) {
899 tmp=asin((double)smalldata[getRelIndex(shape,i,j)]);
900 for (int z=0;z<NUMDATS;++z)
901 {
902 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
903 }
904 }
905 }
906
907 cout << "\tTest Data::acos." << endl;
908 for (int z=0;z<NUMDATS;++z)
909 {
910 results[z].copy(sdats[z].acos());
911 if (z>=LAZY)
912 {
913 assert(results[z].isLazy());
914 }
915 }
916 for (int i=0;i<shape[0];i++) {
917 for (int j=0;j<shape[1];j++) {
918 tmp=acos((double)smalldata[getRelIndex(shape,i,j)]);
919 for (int z=0;z<NUMDATS;++z)
920 {
921 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
922 }
923 }
924 }
925
926 cout << "\tTest Data::atan." << endl;
927 for (int z=0;z<NUMDATS;++z)
928 {
929 results[z].copy(sdats[z].atan());
930 if (z>=LAZY)
931 {
932 assert(results[z].isLazy());
933 }
934 }
935 for (int i=0;i<shape[0];i++) {
936 for (int j=0;j<shape[1];j++) {
937 tmp=atan((double)smalldata[getRelIndex(shape,i,j)]);
938 for (int z=0;z<NUMDATS;++z)
939 {
940 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
941 }
942 }
943 }
944
945 cout << "\tTest Data::sinh." << endl;
946 for (int z=0;z<NUMDATS;++z)
947 {
948 results[z].copy(dats[z].sinh());
949 if (z>=LAZY)
950 {
951 assert(results[z].isLazy());
952 }
953 }
954 for (int i=0;i<shape[0];i++) {
955 for (int j=0;j<shape[1];j++) {
956 tmp=sinh((double)data[getRelIndex(shape,i,j)]);
957 for (int z=0;z<NUMDATS;++z)
958 {
959 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
960 }
961 }
962 }
963
964 cout << "\tTest Data::cosh." << endl;
965 for (int z=0;z<NUMDATS;++z)
966 {
967 results[z].copy(dats[z].cosh());
968 if (z>=LAZY)
969 {
970 assert(results[z].isLazy());
971 }
972 }
973 for (int i=0;i<shape[0];i++) {
974 for (int j=0;j<shape[1];j++) {
975 tmp=cosh((double)data[getRelIndex(shape,i,j)]);
976 for (int z=0;z<NUMDATS;++z)
977 {
978 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
979 }
980 }
981 }
982
983 cout << "\tTest Data::tanh." << endl;
984 for (int z=0;z<NUMDATS;++z)
985 {
986 results[z].copy(dats[z].tanh());
987 if (z>=LAZY)
988 {
989 assert(results[z].isLazy());
990 }
991 }
992 for (int i=0;i<shape[0];i++) {
993 for (int j=0;j<shape[1];j++) {
994 tmp=tanh((double)data[getRelIndex(shape,i,j)]);
995 for (int z=0;z<NUMDATS;++z)
996 {
997 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
998 }
999 }
1000 }
1001
1002 // rather than accomodate the different windows operations directly I'll just use inverse functions
1003 cout << "\tTest Data::asinh." << endl;
1004 for (int z=0;z<NUMDATS;++z)
1005 {
1006 results[z].copy(dats[z].asinh().sinh());
1007 if (z>=LAZY)
1008 {
1009 assert(results[z].isLazy());
1010 }
1011 }
1012 for (int i=0;i<shape[0];i++) {
1013 for (int j=0;j<shape[1];j++) {
1014 tmp=data[getRelIndex(shape,i,j)];
1015 for (int z=0;z<NUMDATS;++z)
1016 {
1017 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1018 }
1019 }
1020 }
1021
1022 cout << "\tTest Data::acosh." << endl;
1023 for (int z=0;z<NUMDATS;++z)
1024 {
1025 results[z].copy(dats[z].acosh().cosh());
1026 if (z>=LAZY)
1027 {
1028 assert(results[z].isLazy());
1029 }
1030 }
1031 for (int i=0;i<shape[0];i++) {
1032 for (int j=0;j<shape[1];j++) {
1033 if (i==0 && j==0) break;
1034 tmp=data[getRelIndex(shape,i,j)];
1035 for (int z=0;z<NUMDATS;++z)
1036 {
1037 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1038 }
1039 }
1040 }
1041
1042 cout << "\tTest Data::atanh." << endl;
1043 for (int z=0;z<NUMDATS;++z)
1044 {
1045 results[z].copy(dats[z].tanh().atanh()); // if these are the other way around the results are
1046 if (z>=LAZY) // undefined
1047 {
1048 assert(results[z].isLazy());
1049 }
1050 }
1051 for (int i=0;i<shape[0];i++) {
1052 for (int j=0;j<shape[1];j++) {
1053 tmp=data[getRelIndex(shape,i,j)];
1054 for (int z=0;z<NUMDATS;++z)
1055 {
1056 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1057 }
1058 }
1059 }
1060
1061 cout << "\tTest Data::log." << endl;
1062 for (int z=0;z<NUMDATS;++z)
1063 {
1064 results[z].copy(dats[z].log());
1065 if (z>=LAZY)
1066 {
1067 assert(results[z].isLazy());
1068 }
1069 }
1070 for (int i=0;i<shape[0];i++) {
1071 for (int j=0;j<shape[1];j++) {
1072 if (i==0 && j==0) break;
1073 tmp=log((double)data[getRelIndex(shape,i,j)]);
1074 for (int z=0;z<NUMDATS;++z)
1075 {
1076 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1077 }
1078 }
1079 }
1080
1081 cout << "\tTest Data::log10." << endl;
1082 for (int z=0;z<NUMDATS;++z)
1083 {
1084 results[z].copy(dats[z].log10());
1085 if (z>=LAZY)
1086 {
1087 assert(results[z].isLazy());
1088 }
1089 }
1090 for (int i=0;i<shape[0];i++) {
1091 for (int j=0;j<shape[1];j++) {
1092 if (i==0 && j==0) break;
1093 tmp=log10((double)data[getRelIndex(shape,i,j)]);
1094 for (int z=0;z<NUMDATS;++z)
1095 {
1096 assert(std::abs(getRef(results[z],i,j) - tmp) <= REL_TOL*std::abs(tmp));
1097 }
1098 }
1099 }
1100 #if defined (_WIN32) && !defined (__INTEL_COMPILER)
1101 cout << "\tSkip test Data::erf on windows with MSVC compiler." << endl;
1102 #else
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