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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 801 - (show annotations)
Wed Aug 9 00:49:56 2006 UTC (13 years ago) by ksteube
File size: 32393 byte(s)
Removed outdated tests that use trace()


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26