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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26