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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 113 - (show annotations)
Mon Feb 28 07:06:33 2005 UTC (14 years, 1 month ago) by jgs
File size: 16362 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 #include <math.h>
23
24 using namespace std;
25 using namespace CppUnitTest;
26 using namespace escript;
27 using namespace esysUtils;
28
29 void DataTestCase::setUp() {
30 //
31 // This is called before each test is run
32 }
33
34 void DataTestCase::tearDown() {
35 //
36 // This is called after each test has been run
37 }
38
39 void DataTestCase::testSlicing() {
40
41 cout << endl;
42
43 {
44 DataArrayView::ShapeType viewShape;
45 //
46 // weak tests for slicing DataConstant
47 cout << "\tTest slicing DataConstant" << endl;
48 viewShape.push_back(2);
49 viewShape.push_back(3);
50 Data temp(1.3,viewShape,FunctionSpace(),false);
51 DataArrayView::RegionType region;
52 region.push_back(DataArrayView::RegionType::value_type(0,0));
53 region.push_back(DataArrayView::RegionType::value_type(0,0));
54 Data slice(temp.getSlice(region));
55 assert(slice.getDataPointRank()==0);
56 assert(slice.getDataPoint(0,0)()==1.3);
57 //
58 // try the same but this time to produce a matrix containing one value
59 region.clear();
60 region.push_back(DataArrayView::RegionType::value_type(0,1));
61 region.push_back(DataArrayView::RegionType::value_type(0,1));
62 slice=temp.getSlice(region);
63 assert(slice.getDataPointRank()==2);
64 assert(slice.getDataPoint(0,0)(0,0)==1.3);
65 }
66
67 {
68 DataArrayView::ShapeType viewShape;
69 //
70 // weak tests for slicing DataExpanded
71 cout << "\tTest slicing DataExpanded" << endl;
72 viewShape.push_back(2);
73 viewShape.push_back(3);
74 Data temp(1.3,viewShape,FunctionSpace(),true);
75 temp.getDataPoint(0,0)(0,0)=0.0;
76 temp.getDataPoint(0,0)(1,1)=1.0;
77 DataArrayView::RegionType region;
78 region.push_back(DataArrayView::RegionType::value_type(0,0));
79 region.push_back(DataArrayView::RegionType::value_type(0,0));
80 Data slice(temp.getSlice(region));
81 assert(slice.getDataPointRank()==0);
82 assert(slice.getDataPoint(0,0)()==0.0);
83 //
84 // try the same but this time to produce a matrix containing one value
85 region.clear();
86 region.push_back(DataArrayView::RegionType::value_type(0,1));
87 region.push_back(DataArrayView::RegionType::value_type(0,1));
88 slice=temp.getSlice(region);
89 assert(slice.getDataPointRank()==2);
90 assert(slice.getDataPoint(0,0)(0,0)==0.0);
91 region.clear();
92 region.push_back(DataArrayView::RegionType::value_type(0,2));
93 region.push_back(DataArrayView::RegionType::value_type(0,2));
94 slice=temp.getSlice(region);
95 assert(slice.getDataPoint(0,0)(0,0)==0.0);
96 assert(slice.getDataPoint(0,0)(1,1)==1.0);
97 }
98
99 {
100 DataArrayView::ShapeType viewShape;
101 //
102 // weak tests for slicing DataTagged
103 cout << "\tTest slicing DataTagged" << endl;
104 viewShape.push_back(2);
105 viewShape.push_back(3);
106 Data temp(1.3,viewShape,FunctionSpace(),false);
107 //
108 // convert the data to tagged
109 temp.tag();
110 temp.getDataPoint(0,0)(0,0)=0.0;
111 temp.getDataPoint(0,0)(1,1)=1.0;
112 DataArrayView::RegionType region;
113 region.push_back(DataArrayView::RegionType::value_type(0,0));
114 region.push_back(DataArrayView::RegionType::value_type(0,0));
115 Data slice(temp.getSlice(region));
116 assert(slice.getDataPointRank()==0);
117 assert(slice.getDataPoint(0,0)()==0.0);
118 //
119 // try the same but this time to produce a matrix containing one value
120 region.clear();
121 region.push_back(DataArrayView::RegionType::value_type(0,1));
122 region.push_back(DataArrayView::RegionType::value_type(0,1));
123 slice=temp.getSlice(region);
124 assert(slice.getDataPointRank()==2);
125 assert(slice.getDataPoint(0,0)(0,0)==0.0);
126 region.clear();
127 region.push_back(DataArrayView::RegionType::value_type(0,2));
128 region.push_back(DataArrayView::RegionType::value_type(0,2));
129 slice=temp.getSlice(region);
130 assert(slice.getDataPoint(0,0)(0,0)==0.0);
131 assert(slice.getDataPoint(0,0)(1,1)==1.0);
132 }
133
134 {
135 DataArrayView::ShapeType viewShape;
136 Data source(10.0,viewShape,FunctionSpace(),false);
137 //
138 // weak tests for setting a slice of DataConstant
139 cout << "\tTest slicing DataConstant" << endl;
140 viewShape.push_back(2);
141 viewShape.push_back(3);
142 Data target(1.3,viewShape,FunctionSpace(),false);
143 DataArrayView::RegionType region;
144 region.push_back(DataArrayView::RegionType::value_type(0,0));
145 region.push_back(DataArrayView::RegionType::value_type(0,0));
146 target.setSlice(source,region);
147 assert(target.getDataPoint(0,0)(0,0)==source.getDataPoint(0,0)());
148 }
149
150 {
151 DataArrayView::ShapeType viewShape;
152 Data source(10.0,viewShape,FunctionSpace(),true);
153 //
154 // weak tests for setting a slice of DataConstant
155 viewShape.push_back(2);
156 viewShape.push_back(3);
157 Data target(1.3,viewShape,FunctionSpace(),true);
158 DataArrayView::RegionType region;
159 region.push_back(DataArrayView::RegionType::value_type(0,0));
160 region.push_back(DataArrayView::RegionType::value_type(0,0));
161 target.setSlice(source,region);
162 assert(target.getDataPoint(0,0)(0,0)==source.getDataPoint(0,0)());
163 }
164
165 {
166 DataArrayView::ShapeType viewShape;
167 Data source(10.0,viewShape,FunctionSpace(),false);
168 source.tag();
169 //
170 // weak tests for slicing DataTagged
171 cout << "\tTest slicing DataTagged" << endl;
172 viewShape.push_back(2);
173 viewShape.push_back(3);
174 Data target(1.3,viewShape,FunctionSpace(),false);
175 //
176 // convert the data to tagged
177 target.tag();
178 DataArrayView::RegionType region;
179 region.push_back(DataArrayView::RegionType::value_type(0,0));
180 region.push_back(DataArrayView::RegionType::value_type(0,0));
181 target.setSlice(source,region);
182 assert(target.getDataPoint(0,0)(0,0)==source.getDataPoint(0,0)());
183 }
184
185 }
186
187 void DataTestCase::testMore() {
188
189 cout << endl;
190
191 cout << "\tCreate a Data object from a DataArrayView" << endl;
192
193 DataArrayView::ValueType viewData;
194 DataArrayView::ShapeType viewShape;
195 viewShape.push_back(3);
196 for (int i=0;i<viewShape[0];++i) {
197 viewData.push_back(i);
198 }
199 DataArrayView myView(viewData,viewShape);
200
201 bool expanded=true;
202 Data exData(myView,FunctionSpace(),expanded);
203 Data cData(myView);
204 Data result;
205
206 assert(exData.isExpanded());
207 assert(cData.isConstant());
208 assert(result.isEmpty());
209
210 cout << "\tTest some basic operations" << endl;
211 result=exData*cData;
212 assert(result.isExpanded());
213
214 assert(result.Lsup()==4);
215 assert(result.sup()==4);
216 assert(result.inf()==0);
217
218 result=exData+cData;
219 result=exData-cData;
220 result=exData/cData;
221
222 cout << "\tExercise wherePositive method" << endl;
223 assert(!exData.wherePositive().isEmpty());
224 //assert(exData.wherePositive()==exData.wherePositive());
225
226 cout << "\tExercise copyWithMask method" << endl;
227 exData.copyWithMask(result, exData.wherePositive());
228 assert(!exData.wherePositive().isEmpty());
229
230 }
231
232 void DataTestCase::testAll() {
233
234 cout << endl;
235
236 cout << "\tCreate a Data object from a DataArrayView" << endl;
237
238 DataArrayView::ValueType viewData;
239 DataArrayView::ShapeType viewShape;
240 viewShape.push_back(3);
241 for (int i=0;i<viewShape[0];++i) {
242 viewData.push_back(i);
243 }
244 DataArrayView myView(viewData,viewShape);
245
246 bool expanded=true;
247
248 Data exData(myView,FunctionSpace(),expanded);
249 Data cData(myView);
250 Data result;
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
379 cout << endl;
380
381 // define the shape for the DataArrayView test data
382 DataArrayView::ShapeType shape;
383 shape.push_back(2);
384 shape.push_back(3);
385
386 // allocate the data for the DataArrayView
387 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
388
389 // construct DataArrayView
390 DataArrayView dataView(data,shape);
391
392 // assign values to the data
393 for (int i=0;i<shape[0];i++) {
394 for (int j=0;j<shape[1];j++) {
395 dataView(i,j)=dataView.index(i,j);
396 }
397 }
398
399 Data base(dataView);
400
401 // test unary operations
402
403 cout << "\tTest Data::pow." << endl;
404 Data power(3.0,shape,FunctionSpace(),true);
405 Data result(base.powD(power));
406 for (int i=0;i<shape[0];i++) {
407 for (int j=0;j<shape[1];j++) {
408 assert(result.getPointDataView()(i,j) == pow(dataView.index(i,j),3.0));
409 }
410 }
411
412 cout << "\tTest Data::sin." << endl;
413 result.copy(base.sin());
414 assert(true);
415
416 cout << "\tTest Data::cos." << endl;
417 result.copy(base.cos());
418 assert(true);
419
420 cout << "\tTest Data::tan." << endl;
421 result.copy(base.tan());
422 assert(true);
423
424 cout << "\tTest Data::log." << endl;
425 result.copy(base.log());
426 assert(true);
427
428 cout << "\tTest Data::ln." << endl;
429 result.copy(base.ln());
430 assert(true);
431
432 cout << "\tTest Data::abs." << endl;
433 result.copy(base.abs());
434 assert(true);
435
436 cout << "\tTest Data::sign." << endl;
437 result.copy(base.sign());
438 assert(true);
439
440 cout << "\tTest Data::exp." << endl;
441 result.copy(base.exp());
442 assert(true);
443
444 cout << "\tTest Data::sqrt." << endl;
445 result.copy(base.sqrt());
446 assert(true);
447
448 cout << "\tTest Data::neg." << endl;
449 result.copy(base.neg());
450 assert(true);
451
452 cout << "\tTest Data::pos." << endl;
453 result.copy(base.pos());
454 for (int i=0;i<shape[0];i++) {
455 for (int j=0;j<shape[1];j++) {
456 assert(result.getPointDataView()(i,j) == dataView.index(i,j));
457 }
458 }
459
460 // test reduction operations
461
462 cout << "\tTest Data::Lsup." << endl;
463 assert(base.Lsup() == 5);
464
465 cout << "\tTest Data::sup." << endl;
466 assert(base.sup() == 5);
467
468 cout << "\tTest Data::inf." << endl;
469 assert(base.inf() == 0);
470
471 // test data-point reduction operations
472
473 cout << "\tTest Data::minval." << endl;
474 result.copy(base.minval());
475 assert(result.getPointDataView()() == 0);
476
477 cout << "\tTest Data::maxval." << endl;
478 result.copy(base.maxval());
479 assert(result.getPointDataView()() == 5);
480
481 cout << "\tTest Data::length." << endl;
482 result.copy(base.length());
483 assert(pow(result.getPointDataView()(),2.0) == 55);
484
485 cout << "\tTest Data::trace." << endl;
486 result.copy(base.trace());
487 assert(result.getPointDataView()() == 15);
488
489 //result.copy(base.transpose(0));
490 //assert(true);
491
492 }
493
494 void DataTestCase::testRefValue() {
495
496 //
497 // Note - this test can't be run as boost::python::numeric::array
498 // objects can only be created and used from within a python thread!
499 //
500
501 cout << endl;
502
503 cout << "\tTest Data object RefValue methods." << endl;
504
505 // Create three Data object - DataExpanded, DataConstant and DataEmpty
506 DataArrayView::ValueType viewData;
507 DataArrayView::ShapeType viewShape;
508 viewShape.push_back(3);
509 for (int i=0;i<viewShape[0];++i) {
510 viewData.push_back(i);
511 }
512 DataArrayView myView(viewData,viewShape);
513
514 bool expanded=true;
515
516 Data expandedData(myView,FunctionSpace(),expanded);
517 Data constantData(myView);
518 Data emptyData;
519
520 assert(expandedData.isExpanded());
521 assert(constantData.isConstant());
522 assert(emptyData.isEmpty());
523
524 // Check assertions are thrown for RefValue methods on DataEmpty
525
526 int ref = 0;
527 boost::python::numeric::array num_array(1.0);
528
529 try {
530 emptyData.getRefValue(ref,num_array);
531 assert(false);
532 }
533 catch (EsysException& e) {
534 assert(true);
535 }
536 try {
537 emptyData.setRefValue(ref,num_array);
538 assert(false);
539 }
540 catch (EsysException& e) {
541 assert(true);
542 }
543
544 // Check assertions are thrown for RefValue methods on DataConstant
545 try {
546 constantData.getRefValue(ref,num_array);
547 assert(false);
548 }
549 catch (EsysException& e) {
550 assert(true);
551 }
552 try {
553 constantData.setRefValue(ref,num_array);
554 assert(false);
555 }
556 catch (EsysException& e) {
557 assert(true);
558 }
559
560 // Check calls to RefValue methods on DataExpanded
561 expandedData.getRefValue(ref,num_array);
562 expandedData.setRefValue(ref,num_array);
563
564 }
565
566 TestSuite* DataTestCase::suite ()
567 {
568 //
569 // create the suite of tests to perform.
570 TestSuite *testSuite = new TestSuite ("DataTestCase");
571
572 testSuite->addTest (new TestCaller< DataTestCase>("testAll",&DataTestCase::testAll));
573 testSuite->addTest (new TestCaller< DataTestCase>("testMore",&DataTestCase::testMore));
574 testSuite->addTest (new TestCaller< DataTestCase>("testDataConstant",&DataTestCase::testDataConstant));
575 testSuite->addTest (new TestCaller< DataTestCase>("testDataTagged",&DataTestCase::testDataTagged));
576 testSuite->addTest (new TestCaller< DataTestCase>("testDataTaggedExceptions",&DataTestCase::testDataTaggedExceptions));
577 testSuite->addTest (new TestCaller< DataTestCase>("testConstructors",&DataTestCase::testConstructors));
578 testSuite->addTest (new TestCaller< DataTestCase>("testSlicing",&DataTestCase::testSlicing));
579 testSuite->addTest (new TestCaller< DataTestCase>("testOperations",&DataTestCase::testOperations));
580 //testSuite->addTest (new TestCaller< DataTestCase>("testRefValue",&DataTestCase::testRefValue));
581
582 return testSuite;
583 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26