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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1786 - (hide annotations)
Fri Sep 12 03:16:28 2008 UTC (13 years, 11 months ago) by jfenwick
File size: 31817 byte(s)
Fixed some memory leaks in unit_tests.
Fixed an empty array which was giving iso-c++ errors.
Fixed typo which meant scons build_tests didn't build unit tests.


1 ksteube 1312
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 woo409 757 #include <iostream>
17 phornby 1026 #if (defined _WIN32) && (defined __INTEL_COMPILER)
18 woo409 757 #include <mathimf.h>
19     #else
20     #include <math.h>
21     #endif
22 jgs 511
23     #include "DataTestCase.h"
24    
25 robwdcock 670 #include "escript/FunctionSpace.h"
26 robwdcock 638 #include "esysUtils/EsysException.h"
27 jgs 82
28 robwdcock 670 #include "escript/Data.h"
29 jgs 537
30 jgs 82
31     using namespace std;
32     using namespace CppUnitTest;
33     using namespace escript;
34     using namespace esysUtils;
35 jfenwick 1747 using namespace escript::DataTypes;
36 jgs 82
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 jfenwick 1747
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 jgs 82 void DataTestCase::testSlicing() {
69    
70 jfenwick 1747 using namespace escript::DataTypes;
71 jgs 82 cout << endl;
72    
73     {
74 jgs 537
75     cout << "\tTest get-slicing DataConstant" << endl;
76    
77 jfenwick 1697 DataTypes::ShapeType viewShape;
78 jgs 82 viewShape.push_back(2);
79     viewShape.push_back(3);
80 jgs 537 Data data(1.3,viewShape,FunctionSpace(),false);
81    
82     //cout << data.toString() << endl;
83    
84 jfenwick 1697 DataTypes::RegionType region;
85     region.push_back(DataTypes::RegionType::value_type(0,0));
86     region.push_back(DataTypes::RegionType::value_type(0,0));
87 jgs 537
88     Data slice1(data.getSlice(region));
89    
90     //cout << slice1.toString() << endl;
91    
92     assert(slice1.getDataPointRank()==0);
93 jfenwick 1747 assert(slice1.getDataPoint(0,0)==1.3);
94 jgs 537
95 jgs 82 region.clear();
96 jfenwick 1697 region.push_back(DataTypes::RegionType::value_type(0,1));
97     region.push_back(DataTypes::RegionType::value_type(0,1));
98 jgs 537
99     Data slice2(data.getSlice(region));
100    
101     //cout << slice2.toString() << endl;
102    
103     assert(slice2.getDataPointRank()==2);
104 jfenwick 1747 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 jgs 537
108     region.clear();
109 jfenwick 1697 region.push_back(DataTypes::RegionType::value_type(0,1));
110     region.push_back(DataTypes::RegionType::value_type(0,2));
111 jgs 537
112     Data slice3(data.getSlice(region));
113    
114     //cout << slice3.toString() << endl;
115    
116     assert(slice3.getDataPointRank()==2);
117 jfenwick 1747 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 jgs 537
123 jgs 82 }
124    
125     {
126 jgs 537
127     cout << "\tTest set-slicing DataConstant" << endl;
128    
129 jfenwick 1697 DataTypes::ShapeType viewShape;
130 jgs 537 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 jfenwick 1697 DataTypes::RegionType region;
141     region.push_back(DataTypes::RegionType::value_type(0,0));
142     region.push_back(DataTypes::RegionType::value_type(0,0));
143 jgs 537
144     target.setSlice(source,region);
145    
146     //cout << target.toString() << endl;
147    
148 jfenwick 1747 int off1=target.getDataOffset(0,0);
149     assert(target.getDataAtOffset(off1+getRelIndex(target.getDataPointShape(),0,0)==source.getDataPoint(0,0)));
150 jgs 537
151     }
152    
153     {
154    
155     cout << "\tTest get-slicing DataTagged" << endl;
156 jgs 82 //
157 jgs 537 // create a DataTagged with a default value only
158    
159 jfenwick 1697 DataTypes::ShapeType viewShape;
160 jgs 82 viewShape.push_back(2);
161     viewShape.push_back(3);
162 jgs 537 Data data(1.3,viewShape,FunctionSpace(),false);
163     data.tag();
164 jfenwick 1747 data.getDataAtOffset(data.getDataOffset(0,0)+getRelIndex(viewShape,0,0))=1.0;
165 jfenwick 1752 data.getDataAtOffset(data.getDataOffset(0,0)+getRelIndex(viewShape,1,1))=2.0;
166 jfenwick 1747 // data.getDataPoint(0,0)(0,0)=1.0;
167     // data.getDataPoint(0,0)(1,1)=2.0;
168 jgs 537
169     //cout << data.toString() << endl;
170     //
171     // create a scalar slice
172    
173 jfenwick 1697 DataTypes::RegionType region;
174     region.push_back(DataTypes::RegionType::value_type(0,0));
175     region.push_back(DataTypes::RegionType::value_type(0,0));
176 jgs 537
177     Data slice1(data.getSlice(region));
178     //cout << slice1.toString() << endl;
179    
180     assert(slice1.isTagged());
181     assert(slice1.getDataPointRank()==0);
182 jfenwick 1747 assert(slice1.getDataPoint(0,0)==1.0);
183 jgs 82 //
184 jgs 537 // create a rank 2 slice with one value
185    
186 jgs 82 region.clear();
187 jfenwick 1697 region.push_back(DataTypes::RegionType::value_type(0,1));
188     region.push_back(DataTypes::RegionType::value_type(0,1));
189 jgs 537
190     Data slice2(data.getSlice(region));
191    
192     //cout << slice2.toString() << endl;
193    
194     assert(slice2.isTagged());
195     assert(slice2.getDataPointRank()==2);
196    
197 jfenwick 1752 assert(slice2.getDataAtOffset(slice2.getDataOffset(0,0)+getRelIndex(slice2.getDataPointShape(),0,0))==1.0);
198    
199 jgs 537 //
200     // create a rank 2 slice with four values
201    
202 jgs 82 region.clear();
203 jfenwick 1697 region.push_back(DataTypes::RegionType::value_type(0,2));
204     region.push_back(DataTypes::RegionType::value_type(0,2));
205 jgs 537
206     Data slice3(data.getSlice(region));
207    
208     //cout << slice3.toString() << endl;
209    
210     assert(slice3.isTagged());
211     assert(slice3.getDataPointRank()==2);
212 jfenwick 1747 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 jgs 537
217     //
218     // add a value for tag "1"
219    
220 jfenwick 1697 DataTypes::ValueType viewData(6);
221 jgs 537 for (int i=0;i<viewData.size();i++) {
222     viewData[i]=i;
223     }
224 jfenwick 1721 // DataArrayView dataView(viewData,viewShape);
225 jgs 537
226 jfenwick 1721 // data.setTaggedValueFromCPP(1, dataView);
227     data.setTaggedValueFromCPP(1, viewShape, viewData);
228 jgs 537
229 jfenwick 1721
230 jgs 537 //
231     // create a full slice
232    
233     region.clear();
234 jfenwick 1697 region.push_back(DataTypes::RegionType::value_type(0,2));
235     region.push_back(DataTypes::RegionType::value_type(0,3));
236 jgs 537
237     Data slice4(data.getSlice(region));
238    
239     //cout << slice4.toString() << endl;
240    
241     assert(slice4.isTagged());
242     assert(slice4.getDataPointRank()==2);
243 jfenwick 1747 assert(getRef(slice4,0,0,0,0)==0);
244 jfenwick 1752 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 jgs 537
250 jgs 82 }
251    
252     {
253 jgs 537
254     cout << "\tTest set-slicing DataTagged" << endl;
255    
256     //
257 jgs 549 // create a source DataTagged with a scalar default value only
258 jgs 537
259 jfenwick 1697 DataTypes::ShapeType viewShape;
260 jgs 537 Data source(10.0,viewShape,FunctionSpace(),false);
261     source.tag();
262    
263 jgs 549 //cout << "source:\n" << source.toString() << endl;
264 jgs 537
265 jgs 82 //
266 jgs 549 // create a target DataTagged with a rank 2 default value only
267 jgs 537
268 jgs 82 viewShape.push_back(2);
269     viewShape.push_back(3);
270 jgs 537 Data target(1.3,viewShape,FunctionSpace(),false);
271     target.tag();
272    
273 jgs 549 //cout << "target:\n" << target.toString() << endl;
274 jgs 537
275 jgs 82 //
276 jgs 537 // set a slice in target from source
277    
278 jfenwick 1697 DataTypes::RegionType region;
279     region.push_back(DataTypes::RegionType::value_type(1,1));
280     region.push_back(DataTypes::RegionType::value_type(1,1));
281 jgs 537
282     target.setSlice(source,region);
283    
284 jgs 549 //cout << "target:\n" << target.toString() << endl;
285 jgs 537
286     assert(target.isTagged());
287     assert(target.getDataPointRank()==2);
288 jfenwick 1747 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 jgs 537
295     //
296 jgs 549 // add a value for tag "1" to target
297 jgs 537
298 jfenwick 1697 DataTypes::ValueType viewData(6);
299 jgs 537 for (int i=0;i<viewData.size();i++) {
300     viewData[i]=i;
301     }
302 jfenwick 1721 // DataArrayView dataView(viewData,viewShape);
303     //
304     // target.setTaggedValueFromCPP(1, dataView);
305     target.setTaggedValueFromCPP(1, viewShape, viewData);
306 jgs 537
307 jgs 549 //cout << "target:\n" << target.toString() << endl;
308    
309 jgs 537 //
310     // set a slice in target from source
311    
312     region.clear();
313 jfenwick 1697 region.push_back(DataTypes::RegionType::value_type(0,0));
314     region.push_back(DataTypes::RegionType::value_type(1,1));
315 jgs 537
316     target.setSlice(source,region);
317    
318 jgs 549 //cout << "target:\n" << target.toString() << endl;
319 jgs 537
320     assert(target.isTagged());
321     assert(target.getDataPointRank()==2);
322 jfenwick 1747 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 jgs 537
329 jgs 549 //
330     // add a value for tag "2" to source
331    
332 jfenwick 1697 DataTypes::ShapeType viewShape2;
333     DataTypes::ValueType viewData2(1);
334 jgs 549 viewData2[0]=6;
335 jfenwick 1721 // DataArrayView dataView2(viewData2,viewShape2);
336     //
337     // source.setTaggedValueFromCPP(2, dataView2);
338     source.setTaggedValueFromCPP(2, viewShape2, viewData2);
339 jgs 549
340     //cout << "source:\n" << source.toString() << endl;
341    
342     //
343     // set a slice in target from source
344    
345     region.clear();
346 jfenwick 1697 region.push_back(DataTypes::RegionType::value_type(0,0));
347     region.push_back(DataTypes::RegionType::value_type(1,1));
348 jgs 549
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 jgs 537 }
382    
383     {
384    
385     cout << "\tTest get-slicing DataExpanded" << endl;
386    
387 jfenwick 1697 DataTypes::ShapeType viewShape;
388 jgs 537 viewShape.push_back(2);
389     viewShape.push_back(3);
390     Data temp(1.3,viewShape,FunctionSpace(),true);
391    
392 jfenwick 1747 getRef(temp,0,0,0,0)=0.0;
393     getRef(temp,0,0,1,1)=1.0;
394 jgs 537
395 jfenwick 1697 DataTypes::RegionType region;
396     region.push_back(DataTypes::RegionType::value_type(0,0));
397     region.push_back(DataTypes::RegionType::value_type(0,0));
398 jgs 537
399 jgs 82 Data slice(temp.getSlice(region));
400 jgs 537
401 jgs 82 assert(slice.getDataPointRank()==0);
402 jfenwick 1747 assert(slice.getDataPoint(0,0)==0.0);
403 jgs 537
404 jgs 82 region.clear();
405 jfenwick 1697 region.push_back(DataTypes::RegionType::value_type(0,1));
406     region.push_back(DataTypes::RegionType::value_type(0,1));
407 jgs 537
408 jgs 82 slice=temp.getSlice(region);
409 jgs 537
410 jgs 82 assert(slice.getDataPointRank()==2);
411 jfenwick 1747 assert(getRef(slice,0,0,0,0)==0.0);
412 jgs 537
413 jgs 82 region.clear();
414 jfenwick 1697 region.push_back(DataTypes::RegionType::value_type(0,2));
415     region.push_back(DataTypes::RegionType::value_type(0,2));
416 jgs 537
417 jgs 82 slice=temp.getSlice(region);
418 jgs 537
419 jfenwick 1747 assert(getRef(slice,0,0,0,0)==0.0);
420     assert(getRef(slice,0,0,1,1)==1.0);
421 jgs 537
422 jgs 82 }
423    
424     {
425    
426 jgs 537 cout << "\tTest set-slicing DataExpanded" << endl;
427    
428 jfenwick 1697 DataTypes::ShapeType viewShape;
429 jgs 82 Data source(10.0,viewShape,FunctionSpace(),true);
430 jgs 537
431 jgs 82 viewShape.push_back(2);
432     viewShape.push_back(3);
433     Data target(1.3,viewShape,FunctionSpace(),true);
434 jgs 537
435 jfenwick 1697 DataTypes::RegionType region;
436     region.push_back(DataTypes::RegionType::value_type(0,0));
437     region.push_back(DataTypes::RegionType::value_type(0,0));
438 jgs 537
439 jgs 82 target.setSlice(source,region);
440 jgs 537
441 jfenwick 1747 assert(getRef(target,0,0,0,0)==source.getDataPoint(0,0));
442 jgs 82
443     }
444    
445     }
446    
447 jgs 538 void DataTestCase::testAll() {
448 jgs 82
449     cout << endl;
450    
451     cout << "\tCreate a Data object from a DataArrayView" << endl;
452    
453 jfenwick 1697 DataTypes::ShapeType viewShape;
454 jgs 82 viewShape.push_back(3);
455 jfenwick 1697 DataTypes::ValueType viewData(3);
456 jgs 82 for (int i=0;i<viewShape[0];++i) {
457 jgs 121 viewData[i]=i;
458 jgs 82 }
459 jfenwick 1752 // DataArrayView myView(viewData,viewShape);
460 jgs 82
461     bool expanded=true;
462 jfenwick 1752 Data exData(viewData,viewShape,FunctionSpace(),expanded);
463     // Data cData(myView);
464     Data cData(viewData,viewShape,FunctionSpace());
465 jgs 82 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 jgs 538 void DataTestCase::testMore() {
478 jgs 82
479     cout << endl;
480    
481     cout << "\tCreate a Data object from a DataArrayView" << endl;
482    
483 jfenwick 1697 DataTypes::ShapeType viewShape;
484 jgs 82 viewShape.push_back(3);
485 jfenwick 1697 DataTypes::ValueType viewData(3);
486 jgs 82 for (int i=0;i<viewShape[0];++i) {
487 jgs 121 viewData[i]=i;
488 jgs 82 }
489 jfenwick 1752 // DataArrayView myView(viewData,viewShape);
490 jgs 82
491     bool expanded=true;
492 jfenwick 1752 Data exData(viewData,viewShape,FunctionSpace(),expanded);
493     Data cData(viewData,viewShape);
494 jgs 82 Data result;
495 jgs 123
496 jgs 82 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 jgs 538 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 jgs 82 }
520    
521     void DataTestCase::testDataConstant() {
522    
523     cout << endl;
524    
525     cout << "\tCreate a DataConstant object from a DataArrayView" << endl;
526    
527 jfenwick 1697 DataTypes::ShapeType viewShape;
528 jgs 82 viewShape.push_back(2);
529     viewShape.push_back(3);
530     viewShape.push_back(4);
531 jfenwick 1697 DataTypes::ValueType viewData(2*3*4);
532 jfenwick 1698 for (int i=0;i<DataTypes::noValues(viewShape);++i) {
533 jgs 121 viewData[i]=i;
534 jgs 82 }
535 jfenwick 1752 // DataArrayView myView(viewData,viewShape);
536 jgs 82
537 jfenwick 1752 Data left(viewData,viewShape);
538     Data right(viewData,viewShape);
539 jgs 82 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 jgs 539 {
565 jgs 82
566 jgs 539 cout << "\tCreate a DataTagged object with a default value only." << endl;
567 jgs 511
568 jfenwick 1747 // DataTagged::TagListType keys;
569 jgs 511
570 jfenwick 1747 // DataTagged::ValueListType values;
571 jgs 511
572 jfenwick 1697 DataTypes::ShapeType viewShape;
573 jgs 539 viewShape.push_back(3);
574 jgs 82
575 jfenwick 1697 DataTypes::ValueType viewData(3);
576 jgs 539 for (int i=0;i<viewShape[0];i++) {
577     viewData[i]=i;
578     }
579 jfenwick 1747 /* DataArrayView defaultValue(viewData,viewShape);
580 jgs 82
581 jgs 539 bool expanded=false;
582 matt 1325
583 jfenwick 1747 Data myData(keys,values,defaultValue,FunctionSpace(),expanded);*/
584 jfenwick 1786 int arr[1]={1}; // iso c++ does not like empty arrays
585 jfenwick 1747 DataTagged* dt=new DataTagged(FunctionSpace(),viewShape,arr,viewData);
586     Data myData(dt);
587 jgs 511
588 jgs 539 // cout << myData.toString() << endl;
589 jgs 511
590 jgs 539 assert(!myData.isEmpty());
591     assert(myData.isTagged());
592     assert(myData.getTagNumber(0)==1);
593     assert(myData.getDataPointRank()==1);
594     assert(myData.getLength()==3);
595 jfenwick 1747
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 jgs 82
601 jfenwick 1747 // 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 jgs 82
611 jfenwick 1747 // 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 jgs 82
621 jgs 539 double* sampleData=myData.getSampleData(0);
622 jfenwick 1747 for (int i=0; i<myData.getNoValues(); i++) {
623 jgs 539 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 jgs 511
632 jgs 539 cout << "\tTest setting of a tag and associated value." << endl;
633 jgs 511
634 jgs 539 // value for tag "1"
635 jfenwick 1697 DataTypes::ValueType eTwoData(viewData);
636 jfenwick 1747 // DataArrayView eTwoView(eTwoData, viewShape);
637     for (int i=0;i<viewShape[0];i++) {
638     eTwoData[i]=i+2.0;
639 jgs 539 }
640    
641 jfenwick 1747 myData.setTaggedValueFromCPP(1,viewShape, eTwoData);
642 jgs 539
643     assert(myData.getLength()==6);
644    
645 jfenwick 1752 int offset=myData.getDataOffset(0,0);
646 jfenwick 1747 // myDataView = myData.getDataPoint(0,0);
647     // assert(myDataView==eTwoView);
648     // assert(!myDataView.isEmpty());
649 jfenwick 1752 assert(offset==3);
650 jfenwick 1747 assert(myData.getDataPointRank()==1);
651     assert(myData.getNoValues()==3);
652     // assert(myDataView.getShape().size()==1);
653 jgs 539
654 jfenwick 1752 assert(myData.getDataAtOffset(offset+0)==2);
655     assert(myData.getDataAtOffset(offset+1)==3);
656     assert(myData.getDataAtOffset(offset+2)==4);
657    
658 jgs 539 sampleData=myData.getSampleDataByTag(1);
659 jfenwick 1747 for (int i=0; i<myData.getNoValues(); i++) {
660 jgs 539 assert(sampleData[i]==i+2);
661     }
662    
663 jgs 511 }
664    
665 jgs 539 {
666 jgs 511
667 jgs 539 cout << "\tCreate a DataTagged object via tag() method." << endl;
668 jgs 511
669 jfenwick 1697 DataTypes::ShapeType viewShape;
670 jgs 539 viewShape.push_back(2);
671     viewShape.push_back(3);
672     Data myData(1.3,viewShape,FunctionSpace(),false);
673     myData.tag();
674 jgs 511
675 jgs 539 //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 jfenwick 1747 // 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 jgs 539
697     // check value for data-point (0,0).
698 jfenwick 1747 // 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 jgs 539
711 jgs 511 }
712    
713 jgs 82 }
714    
715 jgs 538 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 phornby 1455 catch (EsysException&) {
728 jgs 538 //cout << e.what() << endl;
729     assert(true);
730     }
731    
732     try {
733 jfenwick 1721 myData.setTaggedValueFromCPP(0,DataTypes::ShapeType(), DataTypes::ValueType());;
734 jgs 538 assert(false);
735     }
736 phornby 1455 catch (EsysException&) {
737 jgs 538 //cout << e.what() << endl;
738     assert(true);
739     }
740    
741     }
742    
743 jgs 82 void DataTestCase::testConstructors() {
744    
745     cout << endl;
746    
747 jfenwick 1697 DataTypes::ShapeType viewShape;
748 jgs 82 {
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 jgs 113
762 jgs 82 cout << endl;
763    
764 jgs 113 // define the shape for the DataArrayView test data
765 jfenwick 1697 DataTypes::ShapeType shape;
766 jgs 113 shape.push_back(2);
767     shape.push_back(3);
768 jgs 82
769 jgs 113 // allocate the data for the DataArrayView
770 jfenwick 1698 DataTypes::ValueType data(DataTypes::noValues(shape),0);
771 jgs 82
772 jgs 113 // construct DataArrayView
773 jfenwick 1752 // DataArrayView dataView(data,shape);
774 jgs 82
775 jgs 113 // assign values to the data
776     for (int i=0;i<shape[0];i++) {
777     for (int j=0;j<shape[1];j++) {
778 jfenwick 1752 data[getRelIndex(shape,i,j)]=getRelIndex(shape,i,j);
779 jgs 113 }
780     }
781    
782 jfenwick 1752 Data baseEx(data,shape,FunctionSpace(),true);
783     Data baseCon(data,shape,FunctionSpace(),false);
784     Data baseTag(data,shape,FunctionSpace(),false);
785 jgs 563 baseTag.tag();
786 jgs 113
787 jgs 563 assert(baseEx.isExpanded());
788     assert(baseCon.isConstant());
789     assert(baseTag.isTagged());
790    
791     Data resultEx;
792     Data resultCon;
793     Data resultTag;
794    
795 jgs 113 // test unary operations
796    
797 gross 713 double tmp;
798 jgs 113 cout << "\tTest Data::pow." << endl;
799     Data power(3.0,shape,FunctionSpace(),true);
800 jgs 563 resultEx.copy(baseEx.powD(power));
801     resultCon.copy(baseCon.powD(power));
802     resultTag.copy(baseTag.powD(power));
803 jgs 113 for (int i=0;i<shape[0];i++) {
804     for (int j=0;j<shape[1];j++) {
805 jfenwick 1752 tmp=pow((double)data[getRelIndex(shape,i,j)],(double)3.0);
806 jfenwick 1747 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 jgs 113 }
810     }
811 jgs 82
812 jgs 113 cout << "\tTest Data::sin." << endl;
813 jgs 563 resultEx.copy(baseEx.sin());
814     resultCon.copy(baseCon.sin());
815     resultTag.copy(baseTag.sin());
816 gross 713 for (int i=0;i<shape[0];i++) {
817     for (int j=0;j<shape[1];j++) {
818 jfenwick 1752 tmp=sin((double)data[getRelIndex(shape,i,j)]);
819 jfenwick 1747 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 gross 713 }
823     }
824 jgs 102
825 jgs 113 cout << "\tTest Data::cos." << endl;
826 jgs 563 resultEx.copy(baseEx.cos());
827     resultCon.copy(baseCon.cos());
828     resultTag.copy(baseTag.cos());
829 gross 713 for (int i=0;i<shape[0];i++) {
830     for (int j=0;j<shape[1];j++) {
831 jfenwick 1752 tmp=cos((double)data[getRelIndex(shape,i,j)]);
832 jfenwick 1747 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 gross 713 }
836     }
837 jgs 102
838 jgs 113 cout << "\tTest Data::tan." << endl;
839 jgs 563 resultEx.copy(baseEx.tan());
840     resultCon.copy(baseCon.tan());
841     resultTag.copy(baseTag.tan());
842 gross 713 for (int i=0;i<shape[0];i++) {
843     for (int j=0;j<shape[1];j++) {
844 jfenwick 1752 tmp=tan((double)data[getRelIndex(shape,i,j)]);
845 jfenwick 1747 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 gross 713 }
849     }
850 jgs 102
851 jgs 150 cout << "\tTest Data::asin." << endl;
852 jgs 563 resultEx.copy(baseEx.asin());
853     resultCon.copy(baseCon.asin());
854     resultTag.copy(baseTag.asin());
855 jgs 150 assert(true);
856    
857     cout << "\tTest Data::acos." << endl;
858 jgs 563 resultEx.copy(baseEx.acos());
859     resultCon.copy(baseCon.acos());
860     resultTag.copy(baseTag.acos());
861 jgs 150 assert(true);
862    
863     cout << "\tTest Data::atan." << endl;
864 jgs 563 resultEx.copy(baseEx.atan());
865     resultCon.copy(baseCon.atan());
866     resultTag.copy(baseTag.atan());
867 gross 713 for (int i=0;i<shape[0];i++) {
868     for (int j=0;j<shape[1];j++) {
869 jfenwick 1752 tmp=atan((double)data[getRelIndex(shape,i,j)]);
870 jfenwick 1747 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 gross 713 }
874     }
875 jgs 150
876     cout << "\tTest Data::sinh." << endl;
877 jgs 563 resultEx.copy(baseEx.sinh());
878     resultCon.copy(baseCon.sinh());
879     resultTag.copy(baseTag.sinh());
880 gross 713 for (int i=0;i<shape[0];i++) {
881     for (int j=0;j<shape[1];j++) {
882 jfenwick 1752 tmp=sinh((double)data[getRelIndex(shape,i,j)]);
883 jfenwick 1747 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 gross 713 }
887     }
888 jgs 150
889     cout << "\tTest Data::cosh." << endl;
890 jgs 563 resultEx.copy(baseEx.cosh());
891     resultCon.copy(baseCon.cosh());
892     resultTag.copy(baseTag.cosh());
893 gross 713 for (int i=0;i<shape[0];i++) {
894     for (int j=0;j<shape[1];j++) {
895 jfenwick 1752 tmp=cosh((double)data[getRelIndex(shape,i,j)]);
896 jfenwick 1747 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 gross 713 }
900     }
901 jgs 150
902     cout << "\tTest Data::tanh." << endl;
903 jgs 563 resultEx.copy(baseEx.tanh());
904     resultCon.copy(baseCon.tanh());
905     resultTag.copy(baseTag.tanh());
906 gross 713 for (int i=0;i<shape[0];i++) {
907     for (int j=0;j<shape[1];j++) {
908 jfenwick 1752 tmp=tanh((double)data[getRelIndex(shape,i,j)]);
909 jfenwick 1747 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 gross 713 }
913     }
914 jgs 150
915     cout << "\tTest Data::asinh." << endl;
916 jgs 563 resultEx.copy(baseEx.asinh());
917     resultCon.copy(baseCon.asinh());
918     resultTag.copy(baseTag.asinh());
919 jgs 150 assert(true);
920    
921     cout << "\tTest Data::acosh." << endl;
922 jgs 563 resultEx.copy(baseEx.acosh());
923     resultCon.copy(baseCon.acosh());
924     resultTag.copy(baseTag.acosh());
925 jgs 150 assert(true);
926    
927     cout << "\tTest Data::atanh." << endl;
928 jgs 563 resultEx.copy(baseEx.atanh());
929     resultCon.copy(baseCon.atanh());
930     resultTag.copy(baseTag.atanh());
931 jgs 150 assert(true);
932    
933 jgs 113 cout << "\tTest Data::log." << endl;
934 jgs 563 resultEx.copy(baseEx.log());
935     resultCon.copy(baseCon.log());
936     resultTag.copy(baseTag.log());
937 jgs 102 assert(true);
938    
939 jgs 113 cout << "\tTest Data::abs." << endl;
940 jgs 563 resultEx.copy(baseEx.abs());
941     resultCon.copy(baseCon.abs());
942     resultTag.copy(baseTag.abs());
943 gross 713 for (int i=0;i<shape[0];i++) {
944     for (int j=0;j<shape[1];j++) {
945 jfenwick 1752 tmp=abs((double)data[getRelIndex(shape,i,j)]);
946 jfenwick 1747 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 gross 713 }
950     }
951 jgs 102
952 jgs 113 cout << "\tTest Data::sign." << endl;
953 jgs 563 resultEx.copy(baseEx.sign());
954     resultCon.copy(baseCon.sign());
955     resultTag.copy(baseTag.sign());
956 jgs 102 assert(true);
957    
958 jgs 113 cout << "\tTest Data::exp." << endl;
959 jgs 563 resultEx.copy(baseEx.exp());
960     resultCon.copy(baseCon.exp());
961     resultTag.copy(baseTag.exp());
962 gross 713 for (int i=0;i<shape[0];i++) {
963     for (int j=0;j<shape[1];j++) {
964 jfenwick 1752 tmp=exp((double)data[getRelIndex(shape,i,j)]);
965 jfenwick 1747 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 gross 713 }
969     }
970 jgs 102
971 jgs 113 cout << "\tTest Data::sqrt." << endl;
972 jgs 563 resultEx.copy(baseEx.sqrt());
973     resultCon.copy(baseCon.sqrt());
974     resultTag.copy(baseTag.sqrt());
975 gross 713 for (int i=0;i<shape[0];i++) {
976     for (int j=0;j<shape[1];j++) {
977 jfenwick 1752 tmp=sqrt((double)data[getRelIndex(shape,i,j)]);
978 jfenwick 1747 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 gross 713 }
982     }
983 jgs 102
984 jgs 113 cout << "\tTest Data::neg." << endl;
985 jgs 563 resultEx.copy(baseEx.neg());
986     resultCon.copy(baseCon.neg());
987     resultTag.copy(baseTag.neg());
988 jgs 102 assert(true);
989    
990 jgs 113 cout << "\tTest Data::pos." << endl;
991 jgs 563 resultEx.copy(baseEx.pos());
992     resultCon.copy(baseCon.pos());
993     resultTag.copy(baseTag.pos());
994 jgs 113 for (int i=0;i<shape[0];i++) {
995     for (int j=0;j<shape[1];j++) {
996 jfenwick 1752 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 jgs 113 }
1000     }
1001 jgs 102
1002 jgs 113 // test reduction operations
1003    
1004     cout << "\tTest Data::Lsup." << endl;
1005 gross 713 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 jgs 113
1009     cout << "\tTest Data::sup." << endl;
1010 gross 713 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 jgs 113
1014     cout << "\tTest Data::inf." << endl;
1015 gross 713 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 jgs 113
1019     // test data-point reduction operations
1020    
1021     cout << "\tTest Data::minval." << endl;
1022 jgs 563 resultEx.copy(baseEx.minval());
1023     resultCon.copy(baseCon.minval());
1024     resultTag.copy(baseTag.minval());
1025 jfenwick 1747 // 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 jgs 102
1032 jgs 113 cout << "\tTest Data::maxval." << endl;
1033 jgs 563 resultEx.copy(baseEx.maxval());
1034     resultCon.copy(baseCon.maxval());
1035     resultTag.copy(baseTag.maxval());
1036 jfenwick 1747 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 jgs 102
1040 jgs 82 }
1041    
1042 jgs 110
1043 jgs 151 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 jfenwick 1697 testData = new Data(0.0, DataTypes::ShapeType(), FunctionSpace(), true);
1053 jgs 151 delete testData;
1054     }
1055    
1056 jfenwick 1697 DataTypes::ShapeType viewShape;
1057 jgs 151 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 jgs 82 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 jgs 110 //testSuite->addTest (new TestCaller< DataTestCase>("testRefValue",&DataTestCase::testRefValue));
1086 jgs 151 testSuite->addTest (new TestCaller< DataTestCase>("testMemAlloc",&DataTestCase::testMemAlloc));
1087 jgs 82
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