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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26