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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1799 - (show annotations)
Wed Sep 17 06:33:18 2008 UTC (11 years ago) by jfenwick
File size: 34899 byte(s)
Added Data::copySelf() [Note: this is exposed as copy() in python].
This method returns a pointer to a deep copy of the target.
There are c++ tests but no python tests for this yet.

All DataAbstracts now have a deepCopy() which simplifies the 
implementation of the compy methods.


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26