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

Contents of /trunk/escript/test/DataTagged/DataTaggedTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 497 - (show annotations)
Tue Feb 7 01:28:01 2006 UTC (14 years ago) by jgs
File size: 12971 byte(s)
extend testing of default DataTagged object

1 // $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
16 #include "EsysException.h"
17
18 #include "DataTagged.h"
19
20 #include "DataTaggedTestCase.h"
21
22 #include "BinaryOp.h"
23 #include "UnaryOp.h"
24 #include "FunctionSpaceFactory.h"
25 #include "DataFactory.h"
26
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
93 cout << "\tTest default DataTagged contains only a default value." << endl;
94 binaryOp(left,right,plus<double>());
95 assert(left.getPointDataView()()==0);
96 assert(right.getPointDataView()()==0);
97
98 cout << "\tTest binaryOp(plus)." << endl;
99 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
108 cout << "\tTest binaryOp(multiplies)." << endl;
109 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 DataArrayView::ValueType viewData(3);
121 DataTagged::TagListType keys;
122 DataTagged::ValueListType values;
123 for (int i=0;i<viewShape[0];++i) {
124 viewData[i]=i;
125 }
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
188 cout << endl;
189
190 {
191
192 cout << "\tTest default DataTagged." << endl;
193 DataTagged myData;
194
195 assert(myData.getNumSamples()==1);
196 assert(myData.getNumDPPSample()==1);
197
198 assert(myData.validSamplePointNo(0));
199 assert(myData.validSampleNo(0));
200 assert(!myData.validSamplePointNo(1));
201 assert(!myData.validSampleNo(1));
202
203 assert(myData.getTagNumber(0)==1);
204
205 assert(!myData.isCurrentTag(1));
206
207 assert(myData.getTagLookup().size()==0);
208
209 assert(myData.getLength()==1);
210
211 assert(myData.getPointOffset(0,0)==0);
212
213 // cout << myData.toString() << endl;
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 myDataView = myData.getDataPointByTag(1);
224 assert(!myDataView.isEmpty());
225 assert(myDataView.getOffset()==0);
226 assert(myDataView.getRank()==0);
227 assert(myDataView.noValues()==1);
228 assert(myDataView.getShape().size()==0);
229 assert(myDataView()==0.0);
230
231 myDataView = myData.getDefaultValue();
232 assert(!myDataView.isEmpty());
233 assert(myDataView.getOffset()==0);
234 assert(myDataView.getRank()==0);
235 assert(myDataView.noValues()==1);
236 assert(myDataView.getShape().size()==0);
237 assert(myDataView()==0.0);
238
239 //assert(myData.getSampleDataByTag(0)[0]==0.0);
240 //assert(myData.getSampleDataByTag(3)[0]==0.0);
241 //assert(myData.getSampleDataByTag(472)[0]==0.0);
242
243 //cout << "\tTest adding two keys with empty value list." << endl;
244 //DataTagged::TagListType keys;
245 //DataTagged::ValueListType values;
246 //keys.push_back(1);
247 //keys.push_back(2);
248 //myData.addTaggedValues(keys,values);
249 //for (int i=0;i<keys.size();++i) {
250 // assert(myData.getPointDataView()()==0);
251 //}
252 }
253
254 {
255 // cout << "\tCreate tagged data with no tag values just a default." << endl;
256 // DataArrayView::ShapeType viewShape;
257 // viewShape.push_back(3);
258 // DataArrayView::ValueType viewData(3);
259 // DataTagged::TagListType keys;
260 // DataTagged::ValueListType values;
261 // for (int i=0;i<viewShape[0];i++) {
262 // viewData[i]=0.0;
263 // }
264 // DataArrayView myView(viewData,viewShape);
265 // DataTagged myData(keys,values,myView,FunctionSpace());
266 // assert(myData.getNumDPPSample()==1);
267 // assert(myData.getNumSamples()==1);
268 // // Test non existent tag returns the default value.
269 // assert(myData.getDataPointByTag(1)==myView);
270
271 //cout << "\tTest adding a single tag value." << endl;
272 //for (int i=0;i<myView.getShape()[0];++i) {
273 // myView(i)=i;
274 //}
275 //values.push_back(myView);
276 //keys.push_back(1);
277 //myData.addTaggedValues(keys,values);
278 //assert(myData.getDataPointByTag(1)==myView);
279 //cout << "\tTest addition of further tags." << endl;
280 //keys.resize(0);
281 //keys.push_back(3);
282 //for (int i=0;i<myView.getShape()[0];++i) {
283 // myView(i)=i+1.5;
284 //}
285 //myData.addTaggedValues(keys,values);
286 //assert(myData.getDataPointByTag(3)==myView);
287 //assert(myData.getDataPointByTag(1)!=myView);
288 //cout << "\tTrigger the size mismatch exception." << endl;
289 //try {
290 // values.push_back(myView);
291 // myData.addTaggedValues(keys,values);
292 // assert(false);
293 //}
294 //catch (EsysException& e) {
295 // assert(true);
296 //}
297 }
298
299 {
300 // cout << "\tTest creation of tagged data with multiple tags." << endl;
301 // DataArrayView::ShapeType viewShape;
302 // viewShape.push_back(3);
303 // DataArrayView::ValueType viewData(3);
304 // DataTagged::TagListType keys;
305 // DataTagged::ValueListType values;
306 // for (int i=0;i<viewShape[0];++i) {
307 // viewData[i]=0.0;
308 // }
309 // DataArrayView myView(viewData,viewShape);
310 // DataArray eOne(myView);
311 // DataArray eTwo(myView);
312 // DataArray eThree(myView);
313 // for (int i=0;i<eOne.getView().getShape()[0];++i) {
314 // eOne.getView()(i)=i+1.0;
315 // }
316 // for (int i=0;i<eTwo.getView().getShape()[0];++i) {
317 // eTwo.getView()(i)=i+2.0;
318 // }
319 // for (int i=0;i<eThree.getView().getShape()[0];++i) {
320 // eThree.getView()(i)=i+3.0;
321 // }
322 // values.push_back(eOne.getView());
323 // values.push_back(eTwo.getView());
324 // values.push_back(eThree.getView());
325 // keys.push_back(1);
326 // keys.push_back(2);
327 // keys.push_back(3);
328 // DataTagged myData(keys,values,myView,FunctionSpace());
329 // assert(myData.getDataPointByTag(1)==eOne.getView());
330 // assert(myData.getDataPointByTag(2)==eTwo.getView());
331 // assert(myData.getDataPointByTag(3)==eThree.getView());
332
333 //cout << "\tTest isCurrentTag function." << endl;
334 //for (int i=0;i<keys.size();++i) {
335 // assert(myData.isCurrentTag(keys[i]));
336 //}
337 //cout << "\tCheck correct operation for key that doesn't exist." << endl;
338 //assert(!myData.isCurrentTag(123));
339 //cout << "\tTrigger bad shape in input values exception." << endl;
340 //viewShape.clear();
341 //viewShape.push_back(1);
342 //keys.clear();
343 //values.clear();
344 //viewData.resize(1,0.0);
345 //DataArrayView myView2(viewData,viewShape);
346 //try {
347 // myData.addTaggedValue(5,myView2);
348 // assert(false);
349 //}
350 //catch (EsysException& e) {
351 // assert(true);
352 //}
353 //cout << "\tTest addTaggedValues." << endl;
354 //DataTagged myData2;
355 //myData2.reshapeDataPoint(myView.getShape());
356 //keys.clear();
357 //values.clear();
358 //keys.push_back(1);
359 //keys.push_back(2);
360 //keys.push_back(3);
361 //values.push_back(eOne.getView());
362 //values.push_back(eTwo.getView());
363 //values.push_back(eThree.getView());
364 //myData2.addTaggedValues(keys,values);
365 //assert(myData2.getDataPointByTag(1)==eOne.getView());
366 //assert(myData2.getDataPointByTag(2)==eTwo.getView());
367 //assert(myData2.getDataPointByTag(3)==eThree.getView());
368 //cout << "\tTest setTaggedValue." << endl;
369 //DataTagged myData3;
370 //myData3.reshapeDataPoint(myView.getShape());
371 //myData3.addTaggedValue(1,eThree.getView());
372 //myData3.addTaggedValue(2,eOne.getView());
373 //myData3.addTaggedValue(3,eTwo.getView());
374 //myData3.setTaggedValue(1,eOne.getView());
375 //myData3.setTaggedValue(2,eTwo.getView());
376 //myData3.setTaggedValue(3,eThree.getView());
377 //assert(myData3.getDataPointByTag(1)==eOne.getView());
378 //assert(myData3.getDataPointByTag(2)==eTwo.getView());
379 //assert(myData3.getDataPointByTag(3)==eThree.getView());
380 }
381
382 }
383
384 TestSuite* DataTaggedTestCase::suite ()
385 {
386 //
387 // create the suite of tests to perform.
388 TestSuite *testSuite = new TestSuite ("DataTaggedTestCase");
389 testSuite->addTest (new TestCaller< DataTaggedTestCase>("testAll",&DataTaggedTestCase::testAll));
390 // testSuite->addTest (new TestCaller< DataTaggedTestCase>("testOperations",&DataTaggedTestCase::testOperations));
391 // testSuite->addTest (new TestCaller< DataTaggedTestCase>("testReshape",&DataTaggedTestCase::testReshape));
392 return testSuite;
393 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26