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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 110 - (show annotations)
Mon Feb 14 04:14:42 2005 UTC (14 years, 5 months ago) by jgs
Original Path: trunk/esys2/escript/test/Data/DataTestCase.cpp
File size: 14812 byte(s)
*** empty log message ***

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 assert(exData.isExpanded());
251 assert(cData.isConstant());
252 assert(result.isEmpty());
253
254 cout << "\tTest some basic operations" << endl;
255 result=exData*cData;
256 assert(result.isExpanded());
257
258 }
259
260 void DataTestCase::testDataConstant() {
261
262 cout << endl;
263
264 cout << "\tCreate a DataConstant object from a DataArrayView" << endl;
265
266 DataArrayView::ValueType viewData;
267 DataArrayView::ShapeType viewShape;
268 viewShape.push_back(2);
269 viewShape.push_back(3);
270 viewShape.push_back(4);
271 for (int i=0;i<DataArrayView::noValues(viewShape);++i) {
272 viewData.push_back(i);
273 }
274 DataArrayView myView(viewData,viewShape);
275
276 Data left(myView);
277 Data right(myView);
278 Data result;
279
280 cout << "\tTest some basic operations" << endl;
281
282 result=left-right;
283
284 assert(left.isConstant());
285 assert(right.isConstant());
286 assert(result.isConstant());
287
288 result=left+right;
289
290 assert(left.isConstant());
291 assert(right.isConstant());
292 assert(result.isConstant());
293
294 assert(!result.isExpanded());
295 assert(!result.isTagged());
296
297 }
298
299 void DataTestCase::testDataTaggedExceptions() {
300
301 cout << endl;
302
303 cout << "\tTest DataTagged operations exceptions." << endl;
304 Data myData;
305 DataArrayView myView;
306 try {
307 myData.getSampleDataByTag(0);;
308 assert(false);
309 }
310 catch (EsysException& e) {
311 //cout << e.what() << endl;
312 assert(true);
313 }
314 /*
315 try {
316 myData.setTaggedValue(0,myView);;
317 assert(false);
318 }
319 catch (EsysException& e) {
320 //cout << e.what() << endl;
321 assert(true);
322 }
323 */
324
325 }
326
327 void DataTestCase::testDataTagged() {
328
329 cout << endl;
330
331 cout << "\tCreate a DataTagged object from a DataArrayView" << endl;
332
333 DataTagged::TagListType keys;
334 DataTagged::ValueListType values;
335 DataArrayView::ValueType viewData;
336 DataArrayView::ShapeType viewShape;
337 viewShape.push_back(3);
338 for (int i=0;i<viewShape[0];++i) {
339 viewData.push_back(i);
340 }
341 DataArrayView myView(viewData,viewShape);
342
343 // create tagged data with no tag values just a default
344 bool expanded=false;
345
346 Data myData(keys,values,myView,FunctionSpace(),expanded);
347 assert(myData.isTagged());
348
349 cout << "\tTest some basic operations" << endl;
350
351 Data myDataCopy(myData);
352 myDataCopy.expand();
353 assert(myDataCopy.isExpanded());
354
355 }
356
357 void DataTestCase::testConstructors() {
358
359 cout << endl;
360
361 DataArrayView::ShapeType viewShape;
362 {
363 cout << "\tCreate an Empty Data object" << endl;
364 Data temp(1.3,viewShape,FunctionSpace(),false);
365 }
366 {
367 cout << "\tCreate a rank 2 Data object" << endl;
368 viewShape.push_back(2);
369 viewShape.push_back(3);
370 Data temp(1.3,viewShape,FunctionSpace(),false);
371 cout << "\tDump it toString:" << endl;
372 cout << temp.toString() << endl;
373 }
374 }
375
376 void DataTestCase::testOperations() {
377 cout << endl;
378
379 cout << "\tCreate a rank 2 Data object" << endl;
380 DataArrayView::ShapeType viewShape;
381 viewShape.push_back(2);
382 viewShape.push_back(3);
383
384 Data base(2.0,viewShape,FunctionSpace(),true);
385 Data power(3.0,viewShape,FunctionSpace(),true);
386
387 cout << "\tPerform basic exercises of unary operations" << endl;
388
389 Data result(base.powD(power));
390 assert(result.getDataPoint(0,0)(0,0) == 8);
391
392 result.copy(base.sin());
393 assert(true);
394
395 result.copy(base.cos());
396 assert(true);
397
398 result.copy(base.tan());
399 assert(true);
400
401 result.copy(base.log());
402 assert(true);
403
404 result.copy(base.ln());
405 assert(true);
406
407 result.copy(base.abs());
408 assert(true);
409
410 result.copy(base.sign());
411 assert(true);
412
413 result.copy(base.exp());
414 assert(true);
415
416 result.copy(base.sqrt());
417 assert(true);
418
419 result.copy(base.neg());
420 assert(true);
421
422 result.copy(base.pos());
423 assert(true);
424
425 result.copy(base.minval());
426 assert(true);
427
428 result.copy(base.maxval());
429 assert(true);
430
431 result.copy(base.length());
432 assert(true);
433
434 result.copy(base.trace());
435 assert(true);
436
437 //result.copy(base.transpose(0));
438 //assert(true);
439
440 }
441
442 void DataTestCase::testRefValue() {
443
444 //
445 // Note - this test can't be run as boost::python::numeric::array
446 // objects can only be created and used form within a pythin thread!
447 //
448
449 cout << endl;
450
451 cout << "\tTest Data object RefValue methods." << endl;
452
453 // Create three Data object - DataExpanded, DataConstant and DataEmpty
454 DataArrayView::ValueType viewData;
455 DataArrayView::ShapeType viewShape;
456 viewShape.push_back(3);
457 for (int i=0;i<viewShape[0];++i) {
458 viewData.push_back(i);
459 }
460 DataArrayView myView(viewData,viewShape);
461
462 bool expanded=true;
463
464 Data expandedData(myView,FunctionSpace(),expanded);
465 Data constantData(myView);
466 Data emptyData;
467
468 assert(expandedData.isExpanded());
469 assert(constantData.isConstant());
470 assert(emptyData.isEmpty());
471
472 // Check assertions are thrown for RefValue methods on DataEmpty
473
474 int ref = 0;
475 boost::python::numeric::array num_array(1.0);
476
477 try {
478 emptyData.getRefValue(ref,num_array);
479 assert(false);
480 }
481 catch (EsysException& e) {
482 assert(true);
483 }
484 try {
485 emptyData.setRefValue(ref,num_array);
486 assert(false);
487 }
488 catch (EsysException& e) {
489 assert(true);
490 }
491
492 // Check assertions are thrown for RefValue methods on DataConstant
493 try {
494 constantData.getRefValue(ref,num_array);
495 assert(false);
496 }
497 catch (EsysException& e) {
498 assert(true);
499 }
500 try {
501 constantData.setRefValue(ref,num_array);
502 assert(false);
503 }
504 catch (EsysException& e) {
505 assert(true);
506 }
507
508 // Check calls to RefValue methods on DataExpanded
509 expandedData.getRefValue(ref,num_array);
510 expandedData.setRefValue(ref,num_array);
511
512 }
513
514 TestSuite* DataTestCase::suite ()
515 {
516 //
517 // create the suite of tests to perform.
518 TestSuite *testSuite = new TestSuite ("DataTestCase");
519
520 testSuite->addTest (new TestCaller< DataTestCase>("testAll",&DataTestCase::testAll));
521 testSuite->addTest (new TestCaller< DataTestCase>("testMore",&DataTestCase::testMore));
522 testSuite->addTest (new TestCaller< DataTestCase>("testDataConstant",&DataTestCase::testDataConstant));
523 testSuite->addTest (new TestCaller< DataTestCase>("testDataTagged",&DataTestCase::testDataTagged));
524 testSuite->addTest (new TestCaller< DataTestCase>("testDataTaggedExceptions",&DataTestCase::testDataTaggedExceptions));
525 testSuite->addTest (new TestCaller< DataTestCase>("testConstructors",&DataTestCase::testConstructors));
526 testSuite->addTest (new TestCaller< DataTestCase>("testSlicing",&DataTestCase::testSlicing));
527 testSuite->addTest (new TestCaller< DataTestCase>("testOperations",&DataTestCase::testOperations));
528 //testSuite->addTest (new TestCaller< DataTestCase>("testRefValue",&DataTestCase::testRefValue));
529
530 return testSuite;
531 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26