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