/[escript]/branches/arrayview_from_1695_trunk/escript/test/DataTestCase.cpp
ViewVC logotype

Contents of /branches/arrayview_from_1695_trunk/escript/test/DataTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1752 - (show annotations)
Fri Sep 5 06:26:29 2008 UTC (11 years, 11 months ago) by jfenwick
File size: 31801 byte(s)
Branch commit

Now passes all c++ unit tests but fails some python ones.


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26