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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1811 - (show annotations)
Thu Sep 25 23:11:13 2008 UTC (11 years, 3 months ago) by ksteube
File size: 34864 byte(s)
Copyright updated in all files

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26