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 "esysUtils/EsysException.h" |
16 |
#include "escript/Data/Data.h" |
17 |
#include "escript/Data/FunctionSpace.h" |
18 |
|
19 |
#include "DataTestCase.h" |
20 |
|
21 |
#include <iostream> |
22 |
|
23 |
using namespace std; |
24 |
using namespace CppUnitTest; |
25 |
using namespace escript; |
26 |
using namespace esysUtils; |
27 |
|
28 |
void DataTestCase::setUp() { |
29 |
// |
30 |
// This is called before each test is run |
31 |
} |
32 |
|
33 |
void DataTestCase::tearDown() { |
34 |
// |
35 |
// This is called after each test has been run |
36 |
} |
37 |
|
38 |
void DataTestCase::testSlicing() { |
39 |
|
40 |
cout << endl; |
41 |
|
42 |
{ |
43 |
DataArrayView::ShapeType viewShape; |
44 |
// |
45 |
// weak tests for slicing DataConstant |
46 |
cout << "\tTest slicing DataConstant" << endl; |
47 |
viewShape.push_back(2); |
48 |
viewShape.push_back(3); |
49 |
Data temp(1.3,viewShape,FunctionSpace(),false); |
50 |
DataArrayView::RegionType region; |
51 |
region.push_back(DataArrayView::RegionType::value_type(0,0)); |
52 |
region.push_back(DataArrayView::RegionType::value_type(0,0)); |
53 |
Data slice(temp.getSlice(region)); |
54 |
assert(slice.getDataPointRank()==0); |
55 |
assert(slice.getDataPoint(0,0)()==1.3); |
56 |
// |
57 |
// try the same but this time to produce a matrix containing one value |
58 |
region.clear(); |
59 |
region.push_back(DataArrayView::RegionType::value_type(0,1)); |
60 |
region.push_back(DataArrayView::RegionType::value_type(0,1)); |
61 |
slice=temp.getSlice(region); |
62 |
assert(slice.getDataPointRank()==2); |
63 |
assert(slice.getDataPoint(0,0)(0,0)==1.3); |
64 |
} |
65 |
|
66 |
{ |
67 |
DataArrayView::ShapeType viewShape; |
68 |
// |
69 |
// weak tests for slicing DataExpanded |
70 |
cout << "\tTest slicing DataExpanded" << endl; |
71 |
viewShape.push_back(2); |
72 |
viewShape.push_back(3); |
73 |
Data temp(1.3,viewShape,FunctionSpace(),true); |
74 |
temp.getDataPoint(0,0)(0,0)=0.0; |
75 |
temp.getDataPoint(0,0)(1,1)=1.0; |
76 |
DataArrayView::RegionType region; |
77 |
region.push_back(DataArrayView::RegionType::value_type(0,0)); |
78 |
region.push_back(DataArrayView::RegionType::value_type(0,0)); |
79 |
Data slice(temp.getSlice(region)); |
80 |
assert(slice.getDataPointRank()==0); |
81 |
assert(slice.getDataPoint(0,0)()==0.0); |
82 |
// |
83 |
// try the same but this time to produce a matrix containing one value |
84 |
region.clear(); |
85 |
region.push_back(DataArrayView::RegionType::value_type(0,1)); |
86 |
region.push_back(DataArrayView::RegionType::value_type(0,1)); |
87 |
slice=temp.getSlice(region); |
88 |
assert(slice.getDataPointRank()==2); |
89 |
assert(slice.getDataPoint(0,0)(0,0)==0.0); |
90 |
region.clear(); |
91 |
region.push_back(DataArrayView::RegionType::value_type(0,2)); |
92 |
region.push_back(DataArrayView::RegionType::value_type(0,2)); |
93 |
slice=temp.getSlice(region); |
94 |
assert(slice.getDataPoint(0,0)(0,0)==0.0); |
95 |
assert(slice.getDataPoint(0,0)(1,1)==1.0); |
96 |
} |
97 |
|
98 |
{ |
99 |
DataArrayView::ShapeType viewShape; |
100 |
// |
101 |
// weak tests for slicing DataTagged |
102 |
cout << "\tTest slicing DataTagged" << endl; |
103 |
viewShape.push_back(2); |
104 |
viewShape.push_back(3); |
105 |
Data temp(1.3,viewShape,FunctionSpace(),false); |
106 |
// |
107 |
// convert the data to tagged |
108 |
temp.tag(); |
109 |
temp.getDataPoint(0,0)(0,0)=0.0; |
110 |
temp.getDataPoint(0,0)(1,1)=1.0; |
111 |
DataArrayView::RegionType region; |
112 |
region.push_back(DataArrayView::RegionType::value_type(0,0)); |
113 |
region.push_back(DataArrayView::RegionType::value_type(0,0)); |
114 |
Data slice(temp.getSlice(region)); |
115 |
assert(slice.getDataPointRank()==0); |
116 |
assert(slice.getDataPoint(0,0)()==0.0); |
117 |
// |
118 |
// try the same but this time to produce a matrix containing one value |
119 |
region.clear(); |
120 |
region.push_back(DataArrayView::RegionType::value_type(0,1)); |
121 |
region.push_back(DataArrayView::RegionType::value_type(0,1)); |
122 |
slice=temp.getSlice(region); |
123 |
assert(slice.getDataPointRank()==2); |
124 |
assert(slice.getDataPoint(0,0)(0,0)==0.0); |
125 |
region.clear(); |
126 |
region.push_back(DataArrayView::RegionType::value_type(0,2)); |
127 |
region.push_back(DataArrayView::RegionType::value_type(0,2)); |
128 |
slice=temp.getSlice(region); |
129 |
assert(slice.getDataPoint(0,0)(0,0)==0.0); |
130 |
assert(slice.getDataPoint(0,0)(1,1)==1.0); |
131 |
} |
132 |
|
133 |
{ |
134 |
DataArrayView::ShapeType viewShape; |
135 |
Data source(10.0,viewShape,FunctionSpace(),false); |
136 |
// |
137 |
// weak tests for setting a slice of DataConstant |
138 |
cout << "\tTest slicing DataConstant" << endl; |
139 |
viewShape.push_back(2); |
140 |
viewShape.push_back(3); |
141 |
Data target(1.3,viewShape,FunctionSpace(),false); |
142 |
DataArrayView::RegionType region; |
143 |
region.push_back(DataArrayView::RegionType::value_type(0,0)); |
144 |
region.push_back(DataArrayView::RegionType::value_type(0,0)); |
145 |
target.setSlice(source,region); |
146 |
assert(target.getDataPoint(0,0)(0,0)==source.getDataPoint(0,0)()); |
147 |
} |
148 |
|
149 |
{ |
150 |
DataArrayView::ShapeType viewShape; |
151 |
Data source(10.0,viewShape,FunctionSpace(),true); |
152 |
// |
153 |
// weak tests for setting a slice of DataConstant |
154 |
viewShape.push_back(2); |
155 |
viewShape.push_back(3); |
156 |
Data target(1.3,viewShape,FunctionSpace(),true); |
157 |
DataArrayView::RegionType region; |
158 |
region.push_back(DataArrayView::RegionType::value_type(0,0)); |
159 |
region.push_back(DataArrayView::RegionType::value_type(0,0)); |
160 |
target.setSlice(source,region); |
161 |
assert(target.getDataPoint(0,0)(0,0)==source.getDataPoint(0,0)()); |
162 |
} |
163 |
|
164 |
{ |
165 |
DataArrayView::ShapeType viewShape; |
166 |
Data source(10.0,viewShape,FunctionSpace(),false); |
167 |
source.tag(); |
168 |
// |
169 |
// weak tests for slicing DataTagged |
170 |
cout << "\tTest slicing DataTagged" << endl; |
171 |
viewShape.push_back(2); |
172 |
viewShape.push_back(3); |
173 |
Data target(1.3,viewShape,FunctionSpace(),false); |
174 |
// |
175 |
// convert the data to tagged |
176 |
target.tag(); |
177 |
DataArrayView::RegionType region; |
178 |
region.push_back(DataArrayView::RegionType::value_type(0,0)); |
179 |
region.push_back(DataArrayView::RegionType::value_type(0,0)); |
180 |
target.setSlice(source,region); |
181 |
assert(target.getDataPoint(0,0)(0,0)==source.getDataPoint(0,0)()); |
182 |
} |
183 |
|
184 |
} |
185 |
|
186 |
void DataTestCase::testMore() { |
187 |
|
188 |
cout << endl; |
189 |
|
190 |
cout << "\tCreate a Data object from a DataArrayView" << endl; |
191 |
|
192 |
DataArrayView::ValueType viewData; |
193 |
DataArrayView::ShapeType viewShape; |
194 |
viewShape.push_back(3); |
195 |
for (int i=0;i<viewShape[0];++i) { |
196 |
viewData.push_back(i); |
197 |
} |
198 |
DataArrayView myView(viewData,viewShape); |
199 |
|
200 |
bool expanded=true; |
201 |
Data exData(myView,FunctionSpace(),expanded); |
202 |
Data cData(myView); |
203 |
Data result; |
204 |
|
205 |
assert(exData.isExpanded()); |
206 |
assert(cData.isConstant()); |
207 |
assert(result.isEmpty()); |
208 |
|
209 |
cout << "\tTest some basic operations" << endl; |
210 |
result=exData*cData; |
211 |
assert(result.isExpanded()); |
212 |
|
213 |
assert(result.Lsup()==4); |
214 |
assert(result.sup()==4); |
215 |
assert(result.inf()==0); |
216 |
|
217 |
result=exData+cData; |
218 |
result=exData-cData; |
219 |
result=exData/cData; |
220 |
|
221 |
cout << "\tExercise wherePositive method" << endl; |
222 |
assert(!exData.wherePositive().isEmpty()); |
223 |
//assert(exData.wherePositive()==exData.wherePositive()); |
224 |
|
225 |
cout << "\tExercise copyWithMask method" << endl; |
226 |
exData.copyWithMask(result, exData.wherePositive()); |
227 |
assert(!exData.wherePositive().isEmpty()); |
228 |
|
229 |
} |
230 |
|
231 |
void DataTestCase::testAll() { |
232 |
|
233 |
cout << endl; |
234 |
|
235 |
cout << "\tCreate a Data object from a DataArrayView" << endl; |
236 |
|
237 |
DataArrayView::ValueType viewData; |
238 |
DataArrayView::ShapeType viewShape; |
239 |
viewShape.push_back(3); |
240 |
for (int i=0;i<viewShape[0];++i) { |
241 |
viewData.push_back(i); |
242 |
} |
243 |
DataArrayView myView(viewData,viewShape); |
244 |
|
245 |
bool expanded=true; |
246 |
|
247 |
Data exData(myView,FunctionSpace(),expanded); |
248 |
Data cData(myView); |
249 |
Data result; |
250 |
|
251 |
assert(exData.isExpanded()); |
252 |
assert(cData.isConstant()); |
253 |
assert(result.isEmpty()); |
254 |
|
255 |
cout << "\tTest some basic operations" << endl; |
256 |
result=exData*cData; |
257 |
assert(result.isExpanded()); |
258 |
|
259 |
} |
260 |
|
261 |
void DataTestCase::testDataConstant() { |
262 |
|
263 |
cout << endl; |
264 |
|
265 |
cout << "\tCreate a DataConstant object from a DataArrayView" << endl; |
266 |
|
267 |
DataArrayView::ValueType viewData; |
268 |
DataArrayView::ShapeType viewShape; |
269 |
viewShape.push_back(2); |
270 |
viewShape.push_back(3); |
271 |
viewShape.push_back(4); |
272 |
for (int i=0;i<DataArrayView::noValues(viewShape);++i) { |
273 |
viewData.push_back(i); |
274 |
} |
275 |
DataArrayView myView(viewData,viewShape); |
276 |
|
277 |
Data left(myView); |
278 |
Data right(myView); |
279 |
Data result; |
280 |
|
281 |
cout << "\tTest some basic operations" << endl; |
282 |
|
283 |
result=left-right; |
284 |
|
285 |
assert(left.isConstant()); |
286 |
assert(right.isConstant()); |
287 |
assert(result.isConstant()); |
288 |
|
289 |
result=left+right; |
290 |
|
291 |
assert(left.isConstant()); |
292 |
assert(right.isConstant()); |
293 |
assert(result.isConstant()); |
294 |
|
295 |
assert(!result.isExpanded()); |
296 |
assert(!result.isTagged()); |
297 |
|
298 |
} |
299 |
|
300 |
void DataTestCase::testDataTaggedExceptions() { |
301 |
|
302 |
cout << endl; |
303 |
|
304 |
cout << "\tTest DataTagged operations exceptions." << endl; |
305 |
Data myData; |
306 |
DataArrayView myView; |
307 |
try { |
308 |
myData.getSampleDataByTag(0);; |
309 |
assert(false); |
310 |
} |
311 |
catch (EsysException& e) { |
312 |
//cout << e.what() << endl; |
313 |
assert(true); |
314 |
} |
315 |
/* |
316 |
try { |
317 |
myData.setTaggedValue(0,myView);; |
318 |
assert(false); |
319 |
} |
320 |
catch (EsysException& e) { |
321 |
//cout << e.what() << endl; |
322 |
assert(true); |
323 |
} |
324 |
*/ |
325 |
|
326 |
} |
327 |
|
328 |
void DataTestCase::testDataTagged() { |
329 |
|
330 |
cout << endl; |
331 |
|
332 |
cout << "\tCreate a DataTagged object from a DataArrayView" << endl; |
333 |
|
334 |
DataTagged::TagListType keys; |
335 |
DataTagged::ValueListType values; |
336 |
DataArrayView::ValueType viewData; |
337 |
DataArrayView::ShapeType viewShape; |
338 |
viewShape.push_back(3); |
339 |
for (int i=0;i<viewShape[0];++i) { |
340 |
viewData.push_back(i); |
341 |
} |
342 |
DataArrayView myView(viewData,viewShape); |
343 |
|
344 |
// create tagged data with no tag values just a default |
345 |
bool expanded=false; |
346 |
|
347 |
Data myData(keys,values,myView,FunctionSpace(),expanded); |
348 |
assert(myData.isTagged()); |
349 |
|
350 |
cout << "\tTest some basic operations" << endl; |
351 |
|
352 |
Data myDataCopy(myData); |
353 |
myDataCopy.expand(); |
354 |
assert(myDataCopy.isExpanded()); |
355 |
|
356 |
} |
357 |
|
358 |
void DataTestCase::testConstructors() { |
359 |
|
360 |
cout << endl; |
361 |
|
362 |
DataArrayView::ShapeType viewShape; |
363 |
{ |
364 |
cout << "\tCreate an Empty Data object" << endl; |
365 |
Data temp(1.3,viewShape,FunctionSpace(),false); |
366 |
} |
367 |
{ |
368 |
cout << "\tCreate a rank 2 Data object" << endl; |
369 |
viewShape.push_back(2); |
370 |
viewShape.push_back(3); |
371 |
Data temp(1.3,viewShape,FunctionSpace(),false); |
372 |
cout << "\tDump it toString:" << endl; |
373 |
cout << temp.toString() << endl; |
374 |
} |
375 |
} |
376 |
|
377 |
void DataTestCase::testOperations() { |
378 |
cout << endl; |
379 |
|
380 |
cout << "\tCreate a rank 2 Data object" << endl; |
381 |
DataArrayView::ShapeType viewShape; |
382 |
viewShape.push_back(2); |
383 |
viewShape.push_back(3); |
384 |
|
385 |
Data base(2.0,viewShape,FunctionSpace(),false); |
386 |
Data power(3.0,viewShape,FunctionSpace(),false); |
387 |
|
388 |
cout << "\tPerform basic exercises of unary operations" << endl; |
389 |
|
390 |
Data result(base.powD(power)); |
391 |
assert(result.getDataPoint(0,0)(0,0) == 8); |
392 |
|
393 |
result.copy(base.sin()); |
394 |
assert(true); |
395 |
|
396 |
result.copy(base.cos()); |
397 |
assert(true); |
398 |
|
399 |
result.copy(base.tan()); |
400 |
assert(true); |
401 |
|
402 |
result.copy(base.log()); |
403 |
assert(true); |
404 |
|
405 |
result.copy(base.ln()); |
406 |
assert(true); |
407 |
|
408 |
result.copy(base.abs()); |
409 |
assert(true); |
410 |
|
411 |
result.copy(base.maxval()); |
412 |
assert(true); |
413 |
|
414 |
result.copy(base.minval()); |
415 |
assert(true); |
416 |
|
417 |
result.copy(base.length()); |
418 |
assert(true); |
419 |
|
420 |
result.copy(base.sign()); |
421 |
assert(true); |
422 |
|
423 |
result.copy(base.transpose(0)); |
424 |
assert(true); |
425 |
|
426 |
result.copy(base.trace()); |
427 |
assert(true); |
428 |
|
429 |
result.copy(base.exp()); |
430 |
assert(true); |
431 |
|
432 |
result.copy(base.sqrt()); |
433 |
assert(true); |
434 |
|
435 |
result.copy(base.neg()); |
436 |
assert(true); |
437 |
|
438 |
result.copy(base.pos()); |
439 |
assert(true); |
440 |
} |
441 |
|
442 |
TestSuite* DataTestCase::suite () |
443 |
{ |
444 |
// |
445 |
// create the suite of tests to perform. |
446 |
TestSuite *testSuite = new TestSuite ("DataTestCase"); |
447 |
|
448 |
testSuite->addTest (new TestCaller< DataTestCase>("testAll",&DataTestCase::testAll)); |
449 |
testSuite->addTest (new TestCaller< DataTestCase>("testMore",&DataTestCase::testMore)); |
450 |
testSuite->addTest (new TestCaller< DataTestCase>("testDataConstant",&DataTestCase::testDataConstant)); |
451 |
testSuite->addTest (new TestCaller< DataTestCase>("testDataTagged",&DataTestCase::testDataTagged)); |
452 |
testSuite->addTest (new TestCaller< DataTestCase>("testDataTaggedExceptions",&DataTestCase::testDataTaggedExceptions)); |
453 |
testSuite->addTest (new TestCaller< DataTestCase>("testConstructors",&DataTestCase::testConstructors)); |
454 |
testSuite->addTest (new TestCaller< DataTestCase>("testSlicing",&DataTestCase::testSlicing)); |
455 |
testSuite->addTest (new TestCaller< DataTestCase>("testOperations",&DataTestCase::testOperations)); |
456 |
|
457 |
return testSuite; |
458 |
} |