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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 97 - (hide annotations)
Tue Dec 14 05:39:33 2004 UTC (14 years, 11 months ago) by jgs
Original Path: trunk/esys2/escript/test/DataTagged/DataTaggedTestCase.cpp
File size: 13299 byte(s)
*** empty log message ***

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     #include "escript/Data/DataTagged.h"
16     #include "escript/Data/BinaryOp.h"
17     #include "escript/Data/UnaryOp.h"
18     #include "esysUtils/EsysException.h"
19    
20 jgs 97 /*
21 jgs 82 #include "finley/CPPAdapter/MeshAdapter.h"
22     #include "finley/CPPAdapter/MeshAdapterFactory.h"
23     #include "escript/Data/AbstractContinuousDomain.h"
24 jgs 97 */
25 jgs 82
26     #include "escript/Data/FunctionSpaceFactory.h"
27     #include "escript/Data/DataFactory.h"
28    
29     #include "DataTaggedTestCase.h"
30    
31     #include <iostream>
32     #include <functional>
33     #include <algorithm>
34    
35     using namespace CppUnitTest;
36     using namespace escript;
37     using namespace esysUtils;
38     using namespace std;
39    
40 jgs 97 //using namespace finley;
41 jgs 82
42     void DataTaggedTestCase::setUp() {
43     //
44     // This is called before each test is run
45    
46     }
47    
48     void DataTaggedTestCase::tearDown() {
49     //
50     // This is called after each test has been run
51    
52     }
53    
54     void DataTaggedTestCase::testReshape() {
55    
56     cout << endl;
57    
58     {
59     cout << "\tTest reshape of default constructed DataTagged to rank 1." << endl;
60     DataTagged value;
61     value.getPointDataView()()=1.0;
62     DataArrayView::ShapeType shape;
63     shape.push_back(2);
64     value.reshapeDataPoint(shape);
65     for (int i=0;i<shape[0];++i) {
66     assert(value.getDefaultValue()(i)==1);
67     }
68     }
69     {
70     cout << "\tTest reshape of default constructed DataTagged to rank 2." << endl;
71     DataTagged value;
72     value.getPointDataView()()=0.0;
73     DataArray vOne(1.0);
74     DataArray vTwo(2.0);
75     value.addTaggedValue(1,vOne.getView());
76     value.addTaggedValue(2,vTwo.getView());
77     DataArrayView::ShapeType shape;
78     shape.push_back(2);
79     shape.push_back(5);
80     value.reshapeDataPoint(shape);
81     for (int j=0;j<shape[1];++j) {
82     for (int i=0;i<shape[0];++i) {
83     assert(value.getDefaultValue()(i,j)==0.0);
84     assert(value.getDataPointByTag(1)(i,j)==vOne.getView()());
85     assert(value.getDataPointByTag(2)(i,j)==vTwo.getView()());
86     }
87     }
88     }
89     }
90    
91     void DataTaggedTestCase::testOperations() {
92    
93     cout << endl;
94    
95     {
96     cout << "\tTest default DataTagged contains only a default value." << endl;
97     DataTagged left;
98     DataTagged right;
99     binaryOp(left,right,plus<double>());
100     assert(left.getPointDataView()()==0);
101     assert(right.getPointDataView()()==0);
102     cout << "\tTest addTaggedValue and binaryOp(plus)." << endl;
103     DataArray vOne(1.0);
104     DataArray vTwo(2.0);
105     right.addTaggedValue(1,vOne.getView());
106     right.addTaggedValue(2,vTwo.getView());
107     binaryOp(left,right,plus<double>());
108     assert(left.getPointDataView()()==0);
109     assert(left.getDataPointByTag(1)==vOne.getView());
110     assert(left.getDataPointByTag(2)==vTwo.getView());
111     cout << "\tTest setTaggedValue and binaryOp(multiplies)." << endl;
112     DataArray vZero(0.0);
113     right.setTaggedValue(1,vZero.getView());
114     right.setTaggedValue(2,vZero.getView());
115     binaryOp(left,right,multiplies<double>());
116     assert(left.getPointDataView()()==0);
117     assert(left.getDataPointByTag(1)==vZero.getView());
118     assert(left.getDataPointByTag(2)==vZero.getView());
119     }
120     {
121     DataArrayView::ValueType viewData;
122     DataArrayView::ShapeType viewShape;
123     viewShape.push_back(3);
124     DataTagged::TagListType keys;
125     DataTagged::ValueListType values;
126     for (int i=0;i<viewShape[0];++i) {
127     viewData.push_back(i);
128     }
129     DataArrayView myView(viewData,viewShape);
130     cout << "\tCreate tagged data with no tag values just a default." << endl;
131     DataTagged left(keys,values,myView,FunctionSpace());
132     DataTagged right(keys,values,myView,FunctionSpace());
133     binaryOp(left,right,minus<double>());
134     for (int i=0;i<viewShape[0];++i) {
135     assert(left.getDefaultValue()(i)==0);
136     }
137     double mVal=10.0;
138     for (int i=0;i<viewShape[0];++i) {
139     viewData[i]=i*mVal;
140     }
141     cout << "\tTest binaryOp(minus) with the right hand side as a view." << endl;
142     binaryOp(left,myView,minus<double>());
143     for (int i=0;i<viewShape[0];++i) {
144     assert(left.getDefaultValue()(i)==-(i*mVal));
145     }
146     }
147     {
148     cout << "\tTest unaryOp(negate) on default DataTagged." << endl;
149     DataTagged data;
150     unaryOp(data,negate<double>());
151     assert(data.getDefaultValue()()==0);
152     DataArray vOne(1);
153     binaryOp(data,vOne.getView(),plus<double>());
154     assert(data.getDefaultValue()()==1);
155     unaryOp(data,negate<double>());
156     assert(data.getDefaultValue()()==-1);
157     }
158     {
159     cout << "\tTest unaryOp(negate) on DataTagged with 3 tags." << endl;
160     DataArrayView::ShapeType vShape;
161     vShape.push_back(3);
162     vShape.push_back(2);
163     vShape.push_back(1);
164     DataArray defData(vShape,0.0);
165     DataArrayView& defView=defData.getView();
166     DataArray tOneData(vShape,1.0);
167     DataArrayView& tOneView=tOneData.getView();
168     DataArray tTwoData(vShape,2.0);
169     DataArrayView& tTwoView=tTwoData.getView();
170     DataArray tThreeData(vShape,3.0);
171     DataArrayView& tThreeView=tThreeData.getView();
172     DataTagged::TagListType keys;
173     DataTagged::ValueListType values;
174     keys.push_back(1);
175     keys.push_back(2);
176     keys.push_back(3);
177     values.push_back(tOneView);
178     values.push_back(tTwoView);
179     values.push_back(tThreeView);
180     DataTagged tData(keys,values,defView,FunctionSpace());
181     unaryOp(tData,negate<double>());
182     unaryOp(tData,negate<double>());
183     assert(tData.getDataPointByTag(1)==tOneView);
184     assert(tData.getDataPointByTag(2)==tTwoView);
185     assert(tData.getDataPointByTag(3)==tThreeView);
186     }
187    
188     }
189    
190     void DataTaggedTestCase::testAll() {
191     cout << endl;
192     {
193     cout << "\tTest default construction." << endl;
194     DataTagged myData;
195     assert(myData.getPointDataView()()==0);
196     assert(myData.getNumDPPSample()==1);
197     assert(myData.getNumSamples()==1);
198     cout << "\tTest adding two keys with empty value list." << endl;
199     DataTagged::TagListType keys;
200     DataTagged::ValueListType values;
201     keys.push_back(1);
202     keys.push_back(2);
203     myData.addTaggedValues(keys,values);
204     for (int i=0;i<keys.size();++i) {
205     assert(myData.getPointDataView()()==0);
206     }
207     }
208     {
209     DataArrayView::ValueType viewData;
210     DataArrayView::ShapeType viewShape;
211     viewShape.push_back(3);
212     DataTagged::TagListType keys;
213     DataTagged::ValueListType values;
214     for (int i=0;i<viewShape[0];++i) {
215     viewData.push_back(0.0);
216     }
217     DataArrayView myView(viewData,viewShape);
218     cout << "\tCreate tagged data with no tag values just a default." << endl;
219     DataTagged myData(keys,values,myView,FunctionSpace());
220     assert(myData.getNumDPPSample()==1);
221     assert(myData.getNumSamples()==1);
222     cout << "\tTest non existent tag returns the default value." << endl;
223     assert(myData.getDataPointByTag(1)==myView);
224     cout << "\tTest adding a single tag value." << endl;
225     for (int i=0;i<myView.getShape()[0];++i) {
226     myView(i)=i;
227     }
228     values.push_back(myView);
229     keys.push_back(1);
230     myData.addTaggedValues(keys,values);
231     assert(myData.getDataPointByTag(1)==myView);
232     cout << "\tTest addition of further tags." << endl;
233     keys.clear();
234     keys.push_back(3);
235     for (int i=0;i<myView.getShape()[0];++i) {
236     myView(i)=i+1.5;
237     }
238     myData.addTaggedValues(keys,values);
239     assert(myData.getDataPointByTag(3)==myView);
240     assert(myData.getDataPointByTag(1)!=myView);
241     cout << "\tTrigger the size mismatch exception." << endl;
242     try {
243     values.push_back(myView);
244     myData.addTaggedValues(keys,values);
245     assert(false);
246     }
247     catch (EsysException& e) {
248     //cout << e.what() << endl;
249     assert(true);
250     }
251     }
252     {
253     cout << "\tTest creation of tagged data with multiple tags." << endl;
254     DataArrayView::ValueType viewData;
255     DataArrayView::ShapeType viewShape;
256     viewShape.push_back(3);
257     DataTagged::TagListType keys;
258     DataTagged::ValueListType values;
259     for (int i=0;i<viewShape[0];++i) {
260     viewData.push_back(0.0);
261     }
262     DataArrayView myView(viewData,viewShape);
263     DataArray eOne(myView);
264     DataArray eTwo(myView);
265     DataArray eThree(myView);
266     for (int i=0;i<eOne.getView().getShape()[0];++i) {
267     eOne.getView()(i)=i+1.0;
268     }
269     for (int i=0;i<eTwo.getView().getShape()[0];++i) {
270     eTwo.getView()(i)=i+2.0;
271     }
272     for (int i=0;i<eThree.getView().getShape()[0];++i) {
273     eThree.getView()(i)=i+3.0;
274     }
275     values.push_back(eOne.getView());
276     values.push_back(eTwo.getView());
277     values.push_back(eThree.getView());
278     keys.push_back(1);
279     keys.push_back(2);
280     keys.push_back(3);
281     DataTagged myData(keys,values,myView,FunctionSpace());
282     assert(myData.getDataPointByTag(1)==eOne.getView());
283     assert(myData.getDataPointByTag(2)==eTwo.getView());
284     assert(myData.getDataPointByTag(3)==eThree.getView());
285     cout << "\tTest isCurrentTag function." << endl;
286     for (int i=0;i<keys.size();++i) {
287     assert(myData.isCurrentTag(keys[i]));
288     }
289     cout << "\tCheck correct operation for key that doesn't exist." << endl;
290     assert(!myData.isCurrentTag(123));
291     cout << "\tTrigger bad shape in input values exception." << endl;
292     viewShape.clear();
293     viewShape.push_back(1);
294     keys.clear();
295     values.clear();
296     viewData.clear();
297     for (int i=0;i<viewShape[0];++i) {
298     viewData.push_back(0.0);
299     }
300     DataArrayView myView2(viewData,viewShape);
301     try {
302     myData.addTaggedValue(5,myView2);
303     assert(false);
304     }
305     catch (EsysException& e) {
306     //cout << e.what() << endl;
307     assert(true);
308     }
309     cout << "\tTest setTaggedValues." << endl;
310     DataTagged myData2;
311     myData2.reshapeDataPoint(myView.getShape());
312     keys.clear();
313     values.clear();
314     keys.push_back(1);
315     keys.push_back(2);
316     keys.push_back(3);
317     values.push_back(eOne.getView());
318     values.push_back(eTwo.getView());
319     values.push_back(eThree.getView());
320     myData2.setTaggedValues(keys,values);
321     assert(myData2.getDataPointByTag(1)==eOne.getView());
322     assert(myData2.getDataPointByTag(2)==eTwo.getView());
323     assert(myData2.getDataPointByTag(3)==eThree.getView());
324     cout << "\tTest setTaggedValue." << endl;
325     DataTagged myData3;
326     myData3.reshapeDataPoint(myView.getShape());
327     myData3.setTaggedValue(1,eOne.getView());
328     myData3.setTaggedValue(2,eTwo.getView());
329     myData3.setTaggedValue(3,eThree.getView());
330     assert(myData3.getDataPointByTag(1)==eOne.getView());
331     assert(myData3.getDataPointByTag(2)==eTwo.getView());
332     assert(myData3.getDataPointByTag(3)==eThree.getView());
333     }
334    
335     }
336    
337     void DataTaggedTestCase::testSubtraction() {
338    
339     // An error with FinleyMesh::getTagList used to cause binary operations
340     // between DataExpanded and DataTagged objects to seg-fault. This test
341     // case will provoke this error if it arises again.
342    
343 jgs 95 // This test requires the version of setTaggedData which takes a Data
344     // object as an argument. This version is not currently available, so
345     // this test is disabled for now
346    
347     /*
348    
349 jgs 82 cout << endl;
350    
351     cout << "\tCreate domain and function-space." << endl;
352     AbstractContinuousDomain* myDomain = rectangle(10,10);
353     FunctionSpace f = functionOnBoundary(*myDomain);
354    
355     cout << "\tCreate two vectors, one being DataExpanded." << endl;
356     Data A = Vector(0,f);
357     Data B = Vector(0,f,true);
358    
359     cout << "\tCreate some tags and values to add to the other." << endl;
360     DataArrayView::ValueType viewData;
361     DataArrayView::ShapeType viewShape;
362     viewShape.push_back(2);
363     for (int i=0;i<viewShape[0];++i) {
364     viewData.push_back(0.0);
365     }
366     DataArrayView myView(viewData,viewShape);
367     DataArray eOne(myView);
368     for (int i=0;i<eOne.getView().getShape()[0];++i) {
369     eOne.getView()(i)=i+1.0;
370     }
371    
372     A.setTaggedValue(2,eOne.getView());
373    
374     //cout << A.toString() << endl;
375    
376     cout << "\tCalculate difference." << endl;
377     Data difference = B - A;
378    
379     cout << "\tCalculate other binaryOps just to be sure." << endl;
380     Data sum = B + A;
381     Data product = B * A;
382     Data dividend = B / A;
383    
384     // If we get here, subtraction operation did not seg-fault.
385     assert(true);
386    
387 jgs 95 */
388    
389 jgs 82 }
390    
391     TestSuite* DataTaggedTestCase::suite ()
392     {
393     //
394     // create the suite of tests to perform.
395     TestSuite *testSuite = new TestSuite ("DataTaggedTestCase");
396     testSuite->addTest (new TestCaller< DataTaggedTestCase>("testAll",&DataTaggedTestCase::testAll));
397     testSuite->addTest (new TestCaller< DataTaggedTestCase>("testOperations",&DataTaggedTestCase::testOperations));
398     testSuite->addTest (new TestCaller< DataTaggedTestCase>("testReshape",&DataTaggedTestCase::testReshape));
399 jgs 95 //testSuite->addTest (new TestCaller< DataTaggedTestCase>("testSubtraction",&DataTaggedTestCase::testSubtraction));
400 jgs 82 return testSuite;
401     }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26