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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 82 - (show annotations)
Tue Oct 26 06:53:54 2004 UTC (15 years, 1 month ago) by jgs
Original Path: trunk/esys2/escript/test/DataTagged/DataTaggedTestCase.cpp
File size: 13097 byte(s)
Initial revision

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26