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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1796 - (show annotations)
Wed Sep 17 01:45:46 2008 UTC (10 years, 7 months ago) by jfenwick
File size: 31817 byte(s)
Merged noarrayview branch onto trunk.


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[1]={1}; // iso c++ does not like empty arrays
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
723 try {
724 myData.getSampleDataByTag(0);;
725 assert(false);
726 }
727 catch (EsysException&) {
728 //cout << e.what() << endl;
729 assert(true);
730 }
731
732 try {
733 myData.setTaggedValueFromCPP(0,DataTypes::ShapeType(), DataTypes::ValueType());;
734 assert(false);
735 }
736 catch (EsysException&) {
737 //cout << e.what() << endl;
738 assert(true);
739 }
740
741 }
742
743 void DataTestCase::testConstructors() {
744
745 cout << endl;
746
747 DataTypes::ShapeType viewShape;
748 {
749 cout << "\tCreate an Empty Data object" << endl;
750 Data temp(1.3,viewShape,FunctionSpace(),false);
751 }
752 {
753 cout << "\tCreate a rank 2 Data object" << endl;
754 viewShape.push_back(2);
755 viewShape.push_back(3);
756 Data temp(1.3,viewShape,FunctionSpace(),false);
757 }
758 }
759
760 void DataTestCase::testOperations() {
761
762 cout << endl;
763
764 // define the shape for the DataArrayView test data
765 DataTypes::ShapeType shape;
766 shape.push_back(2);
767 shape.push_back(3);
768
769 // allocate the data for the DataArrayView
770 DataTypes::ValueType data(DataTypes::noValues(shape),0);
771
772 // construct DataArrayView
773 // DataArrayView dataView(data,shape);
774
775 // assign values to the data
776 for (int i=0;i<shape[0];i++) {
777 for (int j=0;j<shape[1];j++) {
778 data[getRelIndex(shape,i,j)]=getRelIndex(shape,i,j);
779 }
780 }
781
782 Data baseEx(data,shape,FunctionSpace(),true);
783 Data baseCon(data,shape,FunctionSpace(),false);
784 Data baseTag(data,shape,FunctionSpace(),false);
785 baseTag.tag();
786
787 assert(baseEx.isExpanded());
788 assert(baseCon.isConstant());
789 assert(baseTag.isTagged());
790
791 Data resultEx;
792 Data resultCon;
793 Data resultTag;
794
795 // test unary operations
796
797 double tmp;
798 cout << "\tTest Data::pow." << endl;
799 Data power(3.0,shape,FunctionSpace(),true);
800 resultEx.copy(baseEx.powD(power));
801 resultCon.copy(baseCon.powD(power));
802 resultTag.copy(baseTag.powD(power));
803 for (int i=0;i<shape[0];i++) {
804 for (int j=0;j<shape[1];j++) {
805 tmp=pow((double)data[getRelIndex(shape,i,j)],(double)3.0);
806 assert(std::abs(getRef(resultEx,i,j) - tmp) <= REL_TOL*std::abs(tmp));
807 assert(std::abs(getRef(resultCon,i,j) - tmp) <= REL_TOL*std::abs(tmp));
808 assert(std::abs(getRef(resultTag,i,j) - tmp) <= REL_TOL*std::abs(tmp));
809 }
810 }
811
812 cout << "\tTest Data::sin." << endl;
813 resultEx.copy(baseEx.sin());
814 resultCon.copy(baseCon.sin());
815 resultTag.copy(baseTag.sin());
816 for (int i=0;i<shape[0];i++) {
817 for (int j=0;j<shape[1];j++) {
818 tmp=sin((double)data[getRelIndex(shape,i,j)]);
819 assert(std::abs(getRef(resultEx,i,j) - tmp) <= REL_TOL*std::abs(tmp));
820 assert(std::abs(getRef(resultCon,i,j) - tmp) <= REL_TOL*std::abs(tmp));
821 assert(std::abs(getRef(resultTag,i,j) - tmp) <= REL_TOL*std::abs(tmp));
822 }
823 }
824
825 cout << "\tTest Data::cos." << endl;
826 resultEx.copy(baseEx.cos());
827 resultCon.copy(baseCon.cos());
828 resultTag.copy(baseTag.cos());
829 for (int i=0;i<shape[0];i++) {
830 for (int j=0;j<shape[1];j++) {
831 tmp=cos((double)data[getRelIndex(shape,i,j)]);
832 assert(std::abs(getRef(resultEx,i,j) - tmp) <= REL_TOL*std::abs(tmp));
833 assert(std::abs(getRef(resultCon,i,j) - tmp) <= REL_TOL*std::abs(tmp));
834 assert(std::abs(getRef(resultTag,i,j) - tmp) <= REL_TOL*std::abs(tmp));
835 }
836 }
837
838 cout << "\tTest Data::tan." << endl;
839 resultEx.copy(baseEx.tan());
840 resultCon.copy(baseCon.tan());
841 resultTag.copy(baseTag.tan());
842 for (int i=0;i<shape[0];i++) {
843 for (int j=0;j<shape[1];j++) {
844 tmp=tan((double)data[getRelIndex(shape,i,j)]);
845 assert(std::abs(getRef(resultEx,i,j) - tmp) <= REL_TOL*std::abs(tmp));
846 assert(std::abs(getRef(resultCon,i,j)- tmp) <= REL_TOL*std::abs(tmp));
847 assert(std::abs(getRef(resultTag,i,j)- tmp) <= REL_TOL*std::abs(tmp));
848 }
849 }
850
851 cout << "\tTest Data::asin." << endl;
852 resultEx.copy(baseEx.asin());
853 resultCon.copy(baseCon.asin());
854 resultTag.copy(baseTag.asin());
855 assert(true);
856
857 cout << "\tTest Data::acos." << endl;
858 resultEx.copy(baseEx.acos());
859 resultCon.copy(baseCon.acos());
860 resultTag.copy(baseTag.acos());
861 assert(true);
862
863 cout << "\tTest Data::atan." << endl;
864 resultEx.copy(baseEx.atan());
865 resultCon.copy(baseCon.atan());
866 resultTag.copy(baseTag.atan());
867 for (int i=0;i<shape[0];i++) {
868 for (int j=0;j<shape[1];j++) {
869 tmp=atan((double)data[getRelIndex(shape,i,j)]);
870 assert(std::abs(getRef(resultEx,i,j) - tmp) <= REL_TOL*std::abs(tmp));
871 assert(std::abs(getRef(resultCon,i,j)- tmp) <= REL_TOL*std::abs(tmp));
872 assert(std::abs(getRef(resultTag,i,j)- tmp) <= REL_TOL*std::abs(tmp));
873 }
874 }
875
876 cout << "\tTest Data::sinh." << endl;
877 resultEx.copy(baseEx.sinh());
878 resultCon.copy(baseCon.sinh());
879 resultTag.copy(baseTag.sinh());
880 for (int i=0;i<shape[0];i++) {
881 for (int j=0;j<shape[1];j++) {
882 tmp=sinh((double)data[getRelIndex(shape,i,j)]);
883 assert(std::abs(getRef(resultEx,i,j) - tmp) <= REL_TOL*std::abs(tmp));
884 assert(std::abs(getRef(resultCon,i,j)- tmp) <= REL_TOL*std::abs(tmp));
885 assert(std::abs(getRef(resultTag,i,j)- tmp) <= REL_TOL*std::abs(tmp));
886 }
887 }
888
889 cout << "\tTest Data::cosh." << endl;
890 resultEx.copy(baseEx.cosh());
891 resultCon.copy(baseCon.cosh());
892 resultTag.copy(baseTag.cosh());
893 for (int i=0;i<shape[0];i++) {
894 for (int j=0;j<shape[1];j++) {
895 tmp=cosh((double)data[getRelIndex(shape,i,j)]);
896 assert(std::abs(getRef(resultEx,i,j) - tmp) <= REL_TOL*std::abs(tmp));
897 assert(std::abs(getRef(resultCon,i,j)- tmp) <= REL_TOL*std::abs(tmp));
898 assert(std::abs(getRef(resultTag,i,j)- tmp) <= REL_TOL*std::abs(tmp));
899 }
900 }
901
902 cout << "\tTest Data::tanh." << endl;
903 resultEx.copy(baseEx.tanh());
904 resultCon.copy(baseCon.tanh());
905 resultTag.copy(baseTag.tanh());
906 for (int i=0;i<shape[0];i++) {
907 for (int j=0;j<shape[1];j++) {
908 tmp=tanh((double)data[getRelIndex(shape,i,j)]);
909 assert(std::abs(getRef(resultEx,i,j) - tmp) <= REL_TOL*std::abs(tmp));
910 assert(std::abs(getRef(resultCon,i,j)- tmp) <= REL_TOL*std::abs(tmp));
911 assert(std::abs(getRef(resultTag,i,j)- tmp) <= REL_TOL*std::abs(tmp));
912 }
913 }
914
915 cout << "\tTest Data::asinh." << endl;
916 resultEx.copy(baseEx.asinh());
917 resultCon.copy(baseCon.asinh());
918 resultTag.copy(baseTag.asinh());
919 assert(true);
920
921 cout << "\tTest Data::acosh." << endl;
922 resultEx.copy(baseEx.acosh());
923 resultCon.copy(baseCon.acosh());
924 resultTag.copy(baseTag.acosh());
925 assert(true);
926
927 cout << "\tTest Data::atanh." << endl;
928 resultEx.copy(baseEx.atanh());
929 resultCon.copy(baseCon.atanh());
930 resultTag.copy(baseTag.atanh());
931 assert(true);
932
933 cout << "\tTest Data::log." << endl;
934 resultEx.copy(baseEx.log());
935 resultCon.copy(baseCon.log());
936 resultTag.copy(baseTag.log());
937 assert(true);
938
939 cout << "\tTest Data::abs." << endl;
940 resultEx.copy(baseEx.abs());
941 resultCon.copy(baseCon.abs());
942 resultTag.copy(baseTag.abs());
943 for (int i=0;i<shape[0];i++) {
944 for (int j=0;j<shape[1];j++) {
945 tmp=abs((double)data[getRelIndex(shape,i,j)]);
946 assert(std::abs(getRef(resultEx,i,j) - tmp) <= REL_TOL*std::abs(tmp));
947 assert(std::abs(getRef(resultCon,i,j)- tmp) <= REL_TOL*std::abs(tmp));
948 assert(std::abs(getRef(resultTag,i,j)- tmp) <= REL_TOL*std::abs(tmp));
949 }
950 }
951
952 cout << "\tTest Data::sign." << endl;
953 resultEx.copy(baseEx.sign());
954 resultCon.copy(baseCon.sign());
955 resultTag.copy(baseTag.sign());
956 assert(true);
957
958 cout << "\tTest Data::exp." << endl;
959 resultEx.copy(baseEx.exp());
960 resultCon.copy(baseCon.exp());
961 resultTag.copy(baseTag.exp());
962 for (int i=0;i<shape[0];i++) {
963 for (int j=0;j<shape[1];j++) {
964 tmp=exp((double)data[getRelIndex(shape,i,j)]);
965 assert(std::abs(getRef(resultEx,i,j) - tmp) <= REL_TOL*std::abs(tmp));
966 assert(std::abs(getRef(resultCon,i,j)- tmp) <= REL_TOL*std::abs(tmp));
967 assert(std::abs(getRef(resultTag,i,j)- tmp) <= REL_TOL*std::abs(tmp));
968 }
969 }
970
971 cout << "\tTest Data::sqrt." << endl;
972 resultEx.copy(baseEx.sqrt());
973 resultCon.copy(baseCon.sqrt());
974 resultTag.copy(baseTag.sqrt());
975 for (int i=0;i<shape[0];i++) {
976 for (int j=0;j<shape[1];j++) {
977 tmp=sqrt((double)data[getRelIndex(shape,i,j)]);
978 assert(std::abs(getRef(resultEx,i,j) - tmp) <= REL_TOL*std::abs(tmp));
979 assert(std::abs(getRef(resultCon,i,j)- tmp) <= REL_TOL*std::abs(tmp));
980 assert(std::abs(getRef(resultTag,i,j)- tmp) <= REL_TOL*std::abs(tmp));
981 }
982 }
983
984 cout << "\tTest Data::neg." << endl;
985 resultEx.copy(baseEx.neg());
986 resultCon.copy(baseCon.neg());
987 resultTag.copy(baseTag.neg());
988 assert(true);
989
990 cout << "\tTest Data::pos." << endl;
991 resultEx.copy(baseEx.pos());
992 resultCon.copy(baseCon.pos());
993 resultTag.copy(baseTag.pos());
994 for (int i=0;i<shape[0];i++) {
995 for (int j=0;j<shape[1];j++) {
996 assert(std::abs(getRef(resultEx,i,j) - getRelIndex(shape,i,j)) <= REL_TOL*std::abs(data[getRelIndex(shape,i,j)]));
997 assert(std::abs(getRef(resultCon,i,j) - getRelIndex(shape,i,j)) <= REL_TOL*std::abs(data[getRelIndex(shape,i,j)]));
998 assert(std::abs(getRef(resultTag,i,j) - getRelIndex(shape,i,j)) <= REL_TOL*std::abs(data[getRelIndex(shape,i,j)]));
999 }
1000 }
1001
1002 // test reduction operations
1003
1004 cout << "\tTest Data::Lsup." << endl;
1005 assert(std::abs(baseEx.Lsup() - 5) <= REL_TOL*5);
1006 assert(std::abs(baseCon.Lsup() - 5) <= REL_TOL*5);
1007 assert(std::abs(baseTag.Lsup() - 5) <= REL_TOL*5);
1008
1009 cout << "\tTest Data::sup." << endl;
1010 assert(std::abs(baseEx.sup() - 5) <= REL_TOL*5);
1011 assert(std::abs(baseCon.sup() - 5) <= REL_TOL*5);
1012 assert(std::abs(baseTag.sup() - 5) <= REL_TOL*5);
1013
1014 cout << "\tTest Data::inf." << endl;
1015 assert(std::abs(baseEx.inf() - 0) <= REL_TOL*0);
1016 assert(std::abs(baseCon.inf() - 0) <= REL_TOL*0);
1017 assert(std::abs(baseTag.inf() - 0) <= REL_TOL*0);
1018
1019 // test data-point reduction operations
1020
1021 cout << "\tTest Data::minval." << endl;
1022 resultEx.copy(baseEx.minval());
1023 resultCon.copy(baseCon.minval());
1024 resultTag.copy(baseTag.minval());
1025 // assert(std::abs(resultEx.getPointDataView()() - 0) <= REL_TOL*0);
1026 // assert(std::abs(resultCon.getPointDataView()() - 0) <= REL_TOL*0);
1027 // assert(std::abs(resultTag.getPointDataView()() - 0) <= REL_TOL*0);
1028 assert(std::abs(resultEx.getDataAtOffset(0) - 0) <= REL_TOL*0);
1029 assert(std::abs(resultCon.getDataAtOffset(0) - 0) <= REL_TOL*0);
1030 assert(std::abs(resultTag.getDataAtOffset(0) - 0) <= REL_TOL*0);
1031
1032 cout << "\tTest Data::maxval." << endl;
1033 resultEx.copy(baseEx.maxval());
1034 resultCon.copy(baseCon.maxval());
1035 resultTag.copy(baseTag.maxval());
1036 assert(std::abs(resultEx.getDataAtOffset(0) - 5) <= REL_TOL*5);
1037 assert(std::abs(resultCon.getDataAtOffset(0) - 5) <= REL_TOL*5);
1038 assert(std::abs(resultTag.getDataAtOffset(0) - 5) <= REL_TOL*5);
1039
1040 }
1041
1042
1043 void DataTestCase::testMemAlloc() {
1044
1045 //
1046 // Simple little sanity check for the memory allocator
1047
1048 cout << endl;
1049
1050 Data *testData;
1051 for (int i=0; i<1000; i++) {
1052 testData = new Data(0.0, DataTypes::ShapeType(), FunctionSpace(), true);
1053 delete testData;
1054 }
1055
1056 DataTypes::ShapeType viewShape;
1057 viewShape.push_back(10);
1058 viewShape.push_back(10);
1059 viewShape.push_back(10);
1060
1061 Data *testData2;
1062 Data *testData3 = new Data(0.0, viewShape, FunctionSpace(), true);
1063 for (int i=0; i<1000; i++) {
1064 testData2 = new Data(0.0, viewShape, FunctionSpace(), true);
1065 delete testData2;
1066 }
1067 delete testData3;
1068
1069 }
1070
1071 TestSuite* DataTestCase::suite ()
1072 {
1073 //
1074 // create the suite of tests to perform.
1075 TestSuite *testSuite = new TestSuite ("DataTestCase");
1076
1077 testSuite->addTest (new TestCaller< DataTestCase>("testAll",&DataTestCase::testAll));
1078 testSuite->addTest (new TestCaller< DataTestCase>("testMore",&DataTestCase::testMore));
1079 testSuite->addTest (new TestCaller< DataTestCase>("testDataConstant",&DataTestCase::testDataConstant));
1080 testSuite->addTest (new TestCaller< DataTestCase>("testDataTagged",&DataTestCase::testDataTagged));
1081 testSuite->addTest (new TestCaller< DataTestCase>("testDataTaggedExceptions",&DataTestCase::testDataTaggedExceptions));
1082 testSuite->addTest (new TestCaller< DataTestCase>("testConstructors",&DataTestCase::testConstructors));
1083 testSuite->addTest (new TestCaller< DataTestCase>("testSlicing",&DataTestCase::testSlicing));
1084 testSuite->addTest (new TestCaller< DataTestCase>("testOperations",&DataTestCase::testOperations));
1085 //testSuite->addTest (new TestCaller< DataTestCase>("testRefValue",&DataTestCase::testRefValue));
1086 testSuite->addTest (new TestCaller< DataTestCase>("testMemAlloc",&DataTestCase::testMemAlloc));
1087
1088 return testSuite;
1089 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26