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

Annotation of /trunk/escript/test/DataTaggedTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 499 - (hide annotations)
Tue Feb 7 04:40:44 2006 UTC (13 years, 10 months ago) by jgs
Original Path: trunk/escript/test/DataTagged/DataTaggedTestCase.cpp
File size: 19590 byte(s)
add comprehensive testing for DataTagged with (1) default value only, (2) one tag value and (3) three tag values
1 jgs 82 // $Id$
2     /*
3     *****************************************************************************
4     * *
5     * COPYRIGHT ACcESS - All Rights Reserved *
6     * *
7     * This software is the property of ACcESS. No part of this code *
8     * may be copied in any form or by any means without the expressed written *
9     * consent of ACcESS. Copying, use or modification of this software *
10     * by any unauthorised person is illegal unless that person has a software *
11     * license agreement with ACcESS. *
12     * *
13     *****************************************************************************
14     */
15 jgs 497
16     #include "EsysException.h"
17    
18 jgs 474 #include "DataTagged.h"
19 jgs 497
20     #include "DataTaggedTestCase.h"
21    
22 jgs 474 #include "BinaryOp.h"
23     #include "UnaryOp.h"
24     #include "FunctionSpaceFactory.h"
25     #include "DataFactory.h"
26 jgs 82
27     #include <iostream>
28     #include <functional>
29     #include <algorithm>
30    
31     using namespace CppUnitTest;
32     using namespace escript;
33     using namespace esysUtils;
34     using namespace std;
35    
36     void DataTaggedTestCase::setUp() {
37     //
38     // This is called before each test is run
39    
40     }
41    
42     void DataTaggedTestCase::tearDown() {
43     //
44     // This is called after each test has been run
45    
46     }
47    
48     void DataTaggedTestCase::testReshape() {
49    
50     cout << endl;
51    
52     {
53     cout << "\tTest reshape of default constructed DataTagged to rank 1." << endl;
54     DataTagged value;
55     value.getPointDataView()()=1.0;
56     DataArrayView::ShapeType shape;
57     shape.push_back(2);
58     value.reshapeDataPoint(shape);
59     for (int i=0;i<shape[0];++i) {
60     assert(value.getDefaultValue()(i)==1);
61     }
62     }
63     {
64     cout << "\tTest reshape of default constructed DataTagged to rank 2." << endl;
65     DataTagged value;
66     value.getPointDataView()()=0.0;
67     DataArray vOne(1.0);
68     DataArray vTwo(2.0);
69     value.addTaggedValue(1,vOne.getView());
70     value.addTaggedValue(2,vTwo.getView());
71     DataArrayView::ShapeType shape;
72     shape.push_back(2);
73     shape.push_back(5);
74     value.reshapeDataPoint(shape);
75     for (int j=0;j<shape[1];++j) {
76     for (int i=0;i<shape[0];++i) {
77     assert(value.getDefaultValue()(i,j)==0.0);
78     assert(value.getDataPointByTag(1)(i,j)==vOne.getView()());
79     assert(value.getDataPointByTag(2)(i,j)==vTwo.getView()());
80     }
81     }
82     }
83     }
84    
85     void DataTaggedTestCase::testOperations() {
86    
87     cout << endl;
88    
89     {
90     DataTagged left;
91     DataTagged right;
92 jgs 121
93     cout << "\tTest default DataTagged contains only a default value." << endl;
94 jgs 82 binaryOp(left,right,plus<double>());
95     assert(left.getPointDataView()()==0);
96     assert(right.getPointDataView()()==0);
97 jgs 121
98     cout << "\tTest binaryOp(plus)." << endl;
99 jgs 82 DataArray vOne(1.0);
100     DataArray vTwo(2.0);
101     right.addTaggedValue(1,vOne.getView());
102     right.addTaggedValue(2,vTwo.getView());
103     binaryOp(left,right,plus<double>());
104     assert(left.getPointDataView()()==0);
105     assert(left.getDataPointByTag(1)==vOne.getView());
106     assert(left.getDataPointByTag(2)==vTwo.getView());
107 jgs 121
108     cout << "\tTest binaryOp(multiplies)." << endl;
109 jgs 82 DataArray vZero(0.0);
110     right.setTaggedValue(1,vZero.getView());
111     right.setTaggedValue(2,vZero.getView());
112     binaryOp(left,right,multiplies<double>());
113     assert(left.getPointDataView()()==0);
114     assert(left.getDataPointByTag(1)==vZero.getView());
115     assert(left.getDataPointByTag(2)==vZero.getView());
116     }
117     {
118     DataArrayView::ShapeType viewShape;
119     viewShape.push_back(3);
120 jgs 121 DataArrayView::ValueType viewData(3);
121 jgs 82 DataTagged::TagListType keys;
122     DataTagged::ValueListType values;
123     for (int i=0;i<viewShape[0];++i) {
124 jgs 121 viewData[i]=i;
125 jgs 82 }
126     DataArrayView myView(viewData,viewShape);
127     cout << "\tCreate tagged data with no tag values just a default." << endl;
128     DataTagged left(keys,values,myView,FunctionSpace());
129     DataTagged right(keys,values,myView,FunctionSpace());
130     binaryOp(left,right,minus<double>());
131     for (int i=0;i<viewShape[0];++i) {
132     assert(left.getDefaultValue()(i)==0);
133     }
134     double mVal=10.0;
135     for (int i=0;i<viewShape[0];++i) {
136     viewData[i]=i*mVal;
137     }
138     cout << "\tTest binaryOp(minus) with the right hand side as a view." << endl;
139     binaryOp(left,myView,minus<double>());
140     for (int i=0;i<viewShape[0];++i) {
141     assert(left.getDefaultValue()(i)==-(i*mVal));
142     }
143     }
144     {
145     cout << "\tTest unaryOp(negate) on default DataTagged." << endl;
146     DataTagged data;
147     unaryOp(data,negate<double>());
148     assert(data.getDefaultValue()()==0);
149     DataArray vOne(1);
150     binaryOp(data,vOne.getView(),plus<double>());
151     assert(data.getDefaultValue()()==1);
152     unaryOp(data,negate<double>());
153     assert(data.getDefaultValue()()==-1);
154     }
155     {
156     cout << "\tTest unaryOp(negate) on DataTagged with 3 tags." << endl;
157     DataArrayView::ShapeType vShape;
158     vShape.push_back(3);
159     vShape.push_back(2);
160     vShape.push_back(1);
161     DataArray defData(vShape,0.0);
162     DataArrayView& defView=defData.getView();
163     DataArray tOneData(vShape,1.0);
164     DataArrayView& tOneView=tOneData.getView();
165     DataArray tTwoData(vShape,2.0);
166     DataArrayView& tTwoView=tTwoData.getView();
167     DataArray tThreeData(vShape,3.0);
168     DataArrayView& tThreeView=tThreeData.getView();
169     DataTagged::TagListType keys;
170     DataTagged::ValueListType values;
171     keys.push_back(1);
172     keys.push_back(2);
173     keys.push_back(3);
174     values.push_back(tOneView);
175     values.push_back(tTwoView);
176     values.push_back(tThreeView);
177     DataTagged tData(keys,values,defView,FunctionSpace());
178     unaryOp(tData,negate<double>());
179     unaryOp(tData,negate<double>());
180     assert(tData.getDataPointByTag(1)==tOneView);
181     assert(tData.getDataPointByTag(2)==tTwoView);
182     assert(tData.getDataPointByTag(3)==tThreeView);
183     }
184     }
185    
186     void DataTaggedTestCase::testAll() {
187 jgs 121
188 jgs 82 cout << endl;
189 jgs 121
190 jgs 82 {
191 jgs 497
192     cout << "\tTest default DataTagged." << endl;
193 jgs 82 DataTagged myData;
194 jgs 497
195 jgs 499 //cout << myData.toString() << endl;
196    
197 jgs 497 assert(myData.getNumSamples()==1);
198 jgs 82 assert(myData.getNumDPPSample()==1);
199 jgs 497
200     assert(myData.validSamplePointNo(0));
201     assert(myData.validSampleNo(0));
202     assert(!myData.validSamplePointNo(1));
203     assert(!myData.validSampleNo(1));
204    
205     assert(myData.getTagNumber(0)==1);
206    
207     assert(!myData.isCurrentTag(1));
208    
209     assert(myData.getTagLookup().size()==0);
210    
211     assert(myData.getLength()==1);
212    
213     assert(myData.getPointOffset(0,0)==0);
214    
215     DataArrayView myDataView = myData.getDataPoint(0,0);
216     assert(!myDataView.isEmpty());
217     assert(myDataView.getOffset()==0);
218     assert(myDataView.getRank()==0);
219     assert(myDataView.noValues()==1);
220     assert(myDataView.getShape().size()==0);
221     assert(myDataView()==0.0);
222    
223 jgs 499 // Test non-existent tag returns the default value.
224 jgs 497 myDataView = myData.getDataPointByTag(1);
225     assert(!myDataView.isEmpty());
226     assert(myDataView.getOffset()==0);
227     assert(myDataView.getRank()==0);
228     assert(myDataView.noValues()==1);
229     assert(myDataView.getShape().size()==0);
230     assert(myDataView()==0.0);
231    
232     myDataView = myData.getDefaultValue();
233     assert(!myDataView.isEmpty());
234     assert(myDataView.getOffset()==0);
235     assert(myDataView.getRank()==0);
236     assert(myDataView.noValues()==1);
237     assert(myDataView.getShape().size()==0);
238     assert(myDataView()==0.0);
239    
240     //cout << "\tTest adding two keys with empty value list." << endl;
241     //DataTagged::TagListType keys;
242     //DataTagged::ValueListType values;
243     //keys.push_back(1);
244     //keys.push_back(2);
245     //myData.addTaggedValues(keys,values);
246     //for (int i=0;i<keys.size();++i) {
247     // assert(myData.getPointDataView()()==0);
248     //}
249 jgs 499
250 jgs 82 }
251 jgs 497
252 jgs 82 {
253 jgs 497
254 jgs 499 cout << "\tTest DataTagged with default value only." << endl;
255    
256     DataArrayView::ShapeType viewShape;
257     viewShape.push_back(3);
258    
259     DataTagged::TagListType keys;
260    
261     DataTagged::ValueListType values;
262    
263     DataArrayView::ValueType viewData(3);
264     for (int i=0;i<viewShape[0];i++) {
265     viewData[i]=i;
266     }
267     DataArrayView myView(viewData,viewShape);
268    
269     DataTagged myData(keys,values,myView,FunctionSpace());
270    
271     //cout << myData.toString() << endl;
272    
273     assert(myData.getNumSamples()==1);
274     assert(myData.getNumDPPSample()==1);
275    
276     assert(myData.validSamplePointNo(0));
277     assert(myData.validSampleNo(0));
278     assert(!myData.validSamplePointNo(1));
279     assert(!myData.validSampleNo(1));
280    
281     assert(myData.getTagNumber(0)==1);
282    
283     assert(!myData.isCurrentTag(1));
284    
285     assert(myData.getTagLookup().size()==0);
286    
287     assert(myData.getLength()==3);
288    
289     assert(myData.getPointOffset(0,0)==0);
290    
291     DataArrayView myDataView = myData.getDataPoint(0,0);
292     assert(myDataView==myView);
293     assert(!myDataView.isEmpty());
294     assert(myDataView.getOffset()==0);
295     assert(myDataView.getRank()==1);
296     assert(myDataView.noValues()==3);
297     assert(myDataView.getShape().size()==1);
298     assert(myDataView(0)==0);
299     assert(myDataView(1)==1);
300     assert(myDataView(2)==2);
301    
302     // Test non-existent tag returns the default value.
303     myDataView = myData.getDataPointByTag(1);
304     assert(myDataView==myView);
305     assert(!myDataView.isEmpty());
306     assert(myDataView.getOffset()==0);
307     assert(myDataView.getRank()==1);
308     assert(myDataView.noValues()==3);
309     assert(myDataView.getShape().size()==1);
310     assert(myDataView(0)==0);
311     assert(myDataView(1)==1);
312     assert(myDataView(2)==2);
313    
314     myDataView = myData.getDefaultValue();
315     assert(myDataView==myView);
316     assert(!myDataView.isEmpty());
317     assert(myDataView.getOffset()==0);
318     assert(myDataView.getRank()==1);
319     assert(myDataView.noValues()==3);
320     assert(myDataView.getShape().size()==1);
321     assert(myDataView(0)==0);
322     assert(myDataView(1)==1);
323     assert(myDataView(2)==2);
324    
325 jgs 497 //cout << "\tTest adding a single tag value." << endl;
326     //for (int i=0;i<myView.getShape()[0];++i) {
327     // myView(i)=i;
328     //}
329     //values.push_back(myView);
330     //keys.push_back(1);
331     //myData.addTaggedValues(keys,values);
332     //assert(myData.getDataPointByTag(1)==myView);
333     //cout << "\tTest addition of further tags." << endl;
334     //keys.resize(0);
335     //keys.push_back(3);
336     //for (int i=0;i<myView.getShape()[0];++i) {
337     // myView(i)=i+1.5;
338     //}
339     //myData.addTaggedValues(keys,values);
340     //assert(myData.getDataPointByTag(3)==myView);
341     //assert(myData.getDataPointByTag(1)!=myView);
342     //cout << "\tTrigger the size mismatch exception." << endl;
343     //try {
344     // values.push_back(myView);
345     // myData.addTaggedValues(keys,values);
346     // assert(false);
347     //}
348     //catch (EsysException& e) {
349     // assert(true);
350     //}
351 jgs 499
352 jgs 82 }
353 jgs 497
354 jgs 82 {
355 jgs 497
356 jgs 499 cout << "\tTest DataTagged with one tag." << endl;
357    
358     // the one data-point has tag value "1"
359    
360     DataTagged::TagListType keys;
361     keys.push_back(1);
362    
363     DataTagged::ValueListType values;
364    
365     DataArrayView::ShapeType viewShape;
366     viewShape.push_back(3);
367    
368     // default value
369     DataArrayView::ValueType viewData(3);
370     for (int i=0;i<viewShape[0];i++) {
371     viewData[i]=i;
372     }
373     DataArrayView myView(viewData,viewShape);
374    
375     // value for tag "1"
376     DataArray eOne(myView);
377     for (int i=0;i<eOne.getView().getShape()[0];i++) {
378     eOne.getView()(i)=i+1.0;
379     }
380     values.push_back(eOne.getView());
381    
382     DataTagged myData(keys,values,myView,FunctionSpace());
383    
384     //cout << myData.toString() << endl;
385    
386     assert(myData.getNumSamples()==1);
387     assert(myData.getNumDPPSample()==1);
388    
389     assert(myData.validSamplePointNo(0));
390     assert(myData.validSampleNo(0));
391     assert(!myData.validSamplePointNo(1));
392     assert(!myData.validSampleNo(1));
393    
394     assert(myData.getTagNumber(0)==1);
395    
396     assert(!myData.isCurrentTag(0));
397     assert(myData.isCurrentTag(1));
398    
399     assert(myData.getTagLookup().size()==1);
400    
401     assert(myData.getLength()==6);
402    
403     assert(myData.getPointOffset(0,0)==3);
404    
405     DataArrayView myDataView = myData.getDataPoint(0,0);
406     assert(myDataView==eOne.getView());
407     assert(!myDataView.isEmpty());
408     assert(myDataView.getOffset()==3);
409     assert(myDataView.getRank()==1);
410     assert(myDataView.noValues()==3);
411     assert(myDataView.getShape().size()==1);
412     assert(myDataView(0)==1);
413     assert(myDataView(1)==2);
414     assert(myDataView(2)==3);
415    
416     myDataView = myData.getDataPointByTag(1);
417     assert(myDataView==eOne.getView());
418     assert(!myDataView.isEmpty());
419     assert(myDataView.getOffset()==3);
420     assert(myDataView.getRank()==1);
421     assert(myDataView.noValues()==3);
422     assert(myDataView.getShape().size()==1);
423     assert(myDataView(0)==1);
424     assert(myDataView(1)==2);
425     assert(myDataView(2)==3);
426    
427     // Test non-existent tag returns the default value.
428     myDataView = myData.getDataPointByTag(0);
429     assert(myDataView==myView);
430     assert(!myDataView.isEmpty());
431     assert(myDataView.getOffset()==0);
432     assert(myDataView.getRank()==1);
433     assert(myDataView.noValues()==3);
434     assert(myDataView.getShape().size()==1);
435     assert(myDataView(0)==0);
436     assert(myDataView(1)==1);
437     assert(myDataView(2)==2);
438    
439     myDataView = myData.getDefaultValue();
440     assert(myDataView==myView);
441     assert(!myDataView.isEmpty());
442     assert(myDataView.getOffset()==0);
443     assert(myDataView.getRank()==1);
444     assert(myDataView.noValues()==3);
445     assert(myDataView.getShape().size()==1);
446     assert(myDataView(0)==0);
447     assert(myDataView(1)==1);
448     assert(myDataView(2)==2);
449    
450     }
451    
452     {
453    
454     cout << "\tTest DataTagged with multiple tags." << endl;
455    
456     // the one data-point has tag value "1"
457    
458     DataTagged::TagListType keys;
459     keys.push_back(1);
460     keys.push_back(2);
461     keys.push_back(3);
462    
463     DataTagged::ValueListType values;
464    
465     DataArrayView::ShapeType viewShape;
466     viewShape.push_back(3);
467    
468     // default value
469     DataArrayView::ValueType viewData(3);
470     for (int i=0;i<viewShape[0];i++) {
471     viewData[i]=i;
472     }
473     DataArrayView myView(viewData,viewShape);
474    
475     // value for tag "1"
476     DataArray eOne(myView);
477     for (int i=0;i<eOne.getView().getShape()[0];i++) {
478     eOne.getView()(i)=i+1.0;
479     }
480     values.push_back(eOne.getView());
481    
482     // value for tag "2"
483     DataArray eTwo(myView);
484     for (int i=0;i<eTwo.getView().getShape()[0];i++) {
485     eTwo.getView()(i)=i+2.0;
486     }
487     values.push_back(eTwo.getView());
488    
489     // value for tag "3"
490     DataArray eThree(myView);
491     for (int i=0;i<eThree.getView().getShape()[0];i++) {
492     eThree.getView()(i)=i+3.0;
493     }
494     values.push_back(eThree.getView());
495    
496     DataTagged myData(keys,values,myView,FunctionSpace());
497    
498     //cout << myData.toString() << endl;
499    
500     assert(myData.getNumSamples()==1);
501     assert(myData.getNumDPPSample()==1);
502    
503     assert(myData.validSamplePointNo(0));
504     assert(myData.validSampleNo(0));
505     assert(!myData.validSamplePointNo(1));
506     assert(!myData.validSampleNo(1));
507    
508     assert(myData.getTagNumber(0)==1);
509    
510     assert(!myData.isCurrentTag(0));
511     assert(myData.isCurrentTag(1));
512     assert(myData.isCurrentTag(2));
513     assert(myData.isCurrentTag(3));
514    
515     assert(myData.getTagLookup().size()==3);
516    
517     assert(myData.getLength()==12);
518    
519     assert(myData.getPointOffset(0,0)==3);
520    
521     DataArrayView myDataView = myData.getDataPoint(0,0);
522     assert(myDataView==eOne.getView());
523     assert(!myDataView.isEmpty());
524     assert(myDataView.getOffset()==3);
525     assert(myDataView.getRank()==1);
526     assert(myDataView.noValues()==3);
527     assert(myDataView.getShape().size()==1);
528     assert(myDataView(0)==1);
529     assert(myDataView(1)==2);
530     assert(myDataView(2)==3);
531    
532     myDataView = myData.getDataPointByTag(1);
533     assert(myDataView==eOne.getView());
534     assert(!myDataView.isEmpty());
535     assert(myDataView.getOffset()==3);
536     assert(myDataView.getRank()==1);
537     assert(myDataView.noValues()==3);
538     assert(myDataView.getShape().size()==1);
539     assert(myDataView(0)==1);
540     assert(myDataView(1)==2);
541     assert(myDataView(2)==3);
542    
543     // Test non-existent tag returns the default value.
544     myDataView = myData.getDataPointByTag(0);
545     assert(myDataView==myView);
546     assert(!myDataView.isEmpty());
547     assert(myDataView.getOffset()==0);
548     assert(myDataView.getRank()==1);
549     assert(myDataView.noValues()==3);
550     assert(myDataView.getShape().size()==1);
551     assert(myDataView(0)==0);
552     assert(myDataView(1)==1);
553     assert(myDataView(2)==2);
554    
555     myDataView = myData.getDefaultValue();
556     assert(myDataView==myView);
557     assert(!myDataView.isEmpty());
558     assert(myDataView.getOffset()==0);
559     assert(myDataView.getRank()==1);
560     assert(myDataView.noValues()==3);
561     assert(myDataView.getShape().size()==1);
562     assert(myDataView(0)==0);
563     assert(myDataView(1)==1);
564     assert(myDataView(2)==2);
565    
566     // Test data-points held for remaining tags
567     myDataView = myData.getDataPointByTag(2);
568     assert(myDataView==eTwo.getView());
569     assert(!myDataView.isEmpty());
570     assert(myDataView.getOffset()==6);
571     assert(myDataView.getRank()==1);
572     assert(myDataView.noValues()==3);
573     assert(myDataView.getShape().size()==1);
574     assert(myDataView(0)==2);
575     assert(myDataView(1)==3);
576     assert(myDataView(2)==4);
577    
578     myDataView = myData.getDataPointByTag(3);
579     assert(myDataView==eThree.getView());
580     assert(!myDataView.isEmpty());
581     assert(myDataView.getOffset()==9);
582     assert(myDataView.getRank()==1);
583     assert(myDataView.noValues()==3);
584     assert(myDataView.getShape().size()==1);
585     assert(myDataView(0)==3);
586     assert(myDataView(1)==4);
587     assert(myDataView(2)==5);
588    
589 jgs 497 //cout << "\tTrigger bad shape in input values exception." << endl;
590     //viewShape.clear();
591     //viewShape.push_back(1);
592     //keys.clear();
593     //values.clear();
594     //viewData.resize(1,0.0);
595     //DataArrayView myView2(viewData,viewShape);
596     //try {
597     // myData.addTaggedValue(5,myView2);
598     // assert(false);
599     //}
600     //catch (EsysException& e) {
601     // assert(true);
602     //}
603     //cout << "\tTest addTaggedValues." << endl;
604     //DataTagged myData2;
605     //myData2.reshapeDataPoint(myView.getShape());
606     //keys.clear();
607     //values.clear();
608     //keys.push_back(1);
609     //keys.push_back(2);
610     //keys.push_back(3);
611     //values.push_back(eOne.getView());
612     //values.push_back(eTwo.getView());
613     //values.push_back(eThree.getView());
614     //myData2.addTaggedValues(keys,values);
615     //assert(myData2.getDataPointByTag(1)==eOne.getView());
616     //assert(myData2.getDataPointByTag(2)==eTwo.getView());
617     //assert(myData2.getDataPointByTag(3)==eThree.getView());
618     //cout << "\tTest setTaggedValue." << endl;
619     //DataTagged myData3;
620     //myData3.reshapeDataPoint(myView.getShape());
621     //myData3.addTaggedValue(1,eThree.getView());
622     //myData3.addTaggedValue(2,eOne.getView());
623     //myData3.addTaggedValue(3,eTwo.getView());
624     //myData3.setTaggedValue(1,eOne.getView());
625     //myData3.setTaggedValue(2,eTwo.getView());
626     //myData3.setTaggedValue(3,eThree.getView());
627     //assert(myData3.getDataPointByTag(1)==eOne.getView());
628     //assert(myData3.getDataPointByTag(2)==eTwo.getView());
629     //assert(myData3.getDataPointByTag(3)==eThree.getView());
630 jgs 499
631 jgs 82 }
632    
633     }
634    
635     TestSuite* DataTaggedTestCase::suite ()
636     {
637     //
638     // create the suite of tests to perform.
639     TestSuite *testSuite = new TestSuite ("DataTaggedTestCase");
640     testSuite->addTest (new TestCaller< DataTaggedTestCase>("testAll",&DataTaggedTestCase::testAll));
641 jgs 497 // testSuite->addTest (new TestCaller< DataTaggedTestCase>("testOperations",&DataTaggedTestCase::testOperations));
642     // testSuite->addTest (new TestCaller< DataTaggedTestCase>("testReshape",&DataTaggedTestCase::testReshape));
643 jgs 82 return testSuite;
644     }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26