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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 511 - (show annotations)
Fri Feb 10 05:39:18 2006 UTC (13 years, 5 months ago) by jgs
Original Path: trunk/escript/test/Data/DataTestCase.cpp
File size: 19473 byte(s)
extend basic testing of Data objects containing DataTagged objects
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
16 #include "DataTestCase.h"
17
18 #include "Data.h"
19 #include "FunctionSpace.h"
20 #include "EsysException.h"
21
22 #include <iostream>
23 #include <math.h>
24
25 using namespace std;
26 using namespace CppUnitTest;
27 using namespace escript;
28 using namespace esysUtils;
29
30 void DataTestCase::setUp() {
31 //
32 // This is called before each test is run
33 }
34
35 void DataTestCase::tearDown() {
36 //
37 // This is called after each test has been run
38 }
39
40 void DataTestCase::testSlicing() {
41
42 cout << endl;
43
44 {
45 DataArrayView::ShapeType viewShape;
46 //
47 // weak tests for slicing DataConstant
48 cout << "\tTest slicing DataConstant" << endl;
49 viewShape.push_back(2);
50 viewShape.push_back(3);
51 Data temp(1.3,viewShape,FunctionSpace(),false);
52 DataArrayView::RegionType region;
53 region.push_back(DataArrayView::RegionType::value_type(0,0));
54 region.push_back(DataArrayView::RegionType::value_type(0,0));
55 Data slice(temp.getSlice(region));
56 assert(slice.getDataPointRank()==0);
57 assert(slice.getDataPoint(0,0)()==1.3);
58 //
59 // try the same but this time to produce a matrix containing one value
60 region.clear();
61 region.push_back(DataArrayView::RegionType::value_type(0,1));
62 region.push_back(DataArrayView::RegionType::value_type(0,1));
63 slice=temp.getSlice(region);
64 assert(slice.getDataPointRank()==2);
65 assert(slice.getDataPoint(0,0)(0,0)==1.3);
66 }
67
68 {
69 DataArrayView::ShapeType viewShape;
70 //
71 // weak tests for slicing DataExpanded
72 cout << "\tTest slicing DataExpanded" << endl;
73 viewShape.push_back(2);
74 viewShape.push_back(3);
75 Data temp(1.3,viewShape,FunctionSpace(),true);
76 temp.getDataPoint(0,0)(0,0)=0.0;
77 temp.getDataPoint(0,0)(1,1)=1.0;
78 DataArrayView::RegionType region;
79 region.push_back(DataArrayView::RegionType::value_type(0,0));
80 region.push_back(DataArrayView::RegionType::value_type(0,0));
81 Data slice(temp.getSlice(region));
82 assert(slice.getDataPointRank()==0);
83 assert(slice.getDataPoint(0,0)()==0.0);
84 //
85 // try the same but this time to produce a matrix containing one value
86 region.clear();
87 region.push_back(DataArrayView::RegionType::value_type(0,1));
88 region.push_back(DataArrayView::RegionType::value_type(0,1));
89 slice=temp.getSlice(region);
90 assert(slice.getDataPointRank()==2);
91 assert(slice.getDataPoint(0,0)(0,0)==0.0);
92 region.clear();
93 region.push_back(DataArrayView::RegionType::value_type(0,2));
94 region.push_back(DataArrayView::RegionType::value_type(0,2));
95 slice=temp.getSlice(region);
96 assert(slice.getDataPoint(0,0)(0,0)==0.0);
97 assert(slice.getDataPoint(0,0)(1,1)==1.0);
98 }
99
100 {
101 DataArrayView::ShapeType viewShape;
102 //
103 // weak tests for slicing DataTagged
104 cout << "\tTest slicing DataTagged" << endl;
105 viewShape.push_back(2);
106 viewShape.push_back(3);
107 Data temp(1.3,viewShape,FunctionSpace(),false);
108 //
109 // convert the data to tagged
110 temp.tag();
111 temp.getDataPoint(0,0)(0,0)=0.0;
112 temp.getDataPoint(0,0)(1,1)=1.0;
113 DataArrayView::RegionType region;
114 region.push_back(DataArrayView::RegionType::value_type(0,0));
115 region.push_back(DataArrayView::RegionType::value_type(0,0));
116 Data slice(temp.getSlice(region));
117 assert(slice.getDataPointRank()==0);
118 assert(slice.getDataPoint(0,0)()==0.0);
119 //
120 // try the same but this time to produce a matrix containing one value
121 region.clear();
122 region.push_back(DataArrayView::RegionType::value_type(0,1));
123 region.push_back(DataArrayView::RegionType::value_type(0,1));
124 slice=temp.getSlice(region);
125 assert(slice.getDataPointRank()==2);
126 assert(slice.getDataPoint(0,0)(0,0)==0.0);
127 region.clear();
128 region.push_back(DataArrayView::RegionType::value_type(0,2));
129 region.push_back(DataArrayView::RegionType::value_type(0,2));
130 slice=temp.getSlice(region);
131 assert(slice.getDataPoint(0,0)(0,0)==0.0);
132 assert(slice.getDataPoint(0,0)(1,1)==1.0);
133 }
134
135 {
136 DataArrayView::ShapeType viewShape;
137 Data source(10.0,viewShape,FunctionSpace(),false);
138 //
139 // weak tests for setting a slice of DataConstant
140 cout << "\tTest slicing DataConstant" << endl;
141 viewShape.push_back(2);
142 viewShape.push_back(3);
143 Data target(1.3,viewShape,FunctionSpace(),false);
144 DataArrayView::RegionType region;
145 region.push_back(DataArrayView::RegionType::value_type(0,0));
146 region.push_back(DataArrayView::RegionType::value_type(0,0));
147 target.setSlice(source,region);
148 assert(target.getDataPoint(0,0)(0,0)==source.getDataPoint(0,0)());
149 }
150
151 {
152 DataArrayView::ShapeType viewShape;
153 Data source(10.0,viewShape,FunctionSpace(),true);
154 //
155 // weak tests for setting a slice of DataExpanded
156 viewShape.push_back(2);
157 viewShape.push_back(3);
158 Data target(1.3,viewShape,FunctionSpace(),true);
159 DataArrayView::RegionType region;
160 region.push_back(DataArrayView::RegionType::value_type(0,0));
161 region.push_back(DataArrayView::RegionType::value_type(0,0));
162 target.setSlice(source,region);
163 assert(target.getDataPoint(0,0)(0,0)==source.getDataPoint(0,0)());
164 }
165
166 {
167 DataArrayView::ShapeType viewShape;
168 Data source(10.0,viewShape,FunctionSpace(),false);
169 source.tag();
170 //
171 // weak tests for slicing DataTagged
172 cout << "\tTest slicing DataTagged" << endl;
173 viewShape.push_back(2);
174 viewShape.push_back(3);
175 Data target(1.3,viewShape,FunctionSpace(),false);
176 //
177 // convert the data to tagged
178 target.tag();
179 DataArrayView::RegionType region;
180 region.push_back(DataArrayView::RegionType::value_type(0,0));
181 region.push_back(DataArrayView::RegionType::value_type(0,0));
182 target.setSlice(source,region);
183 assert(target.getDataPoint(0,0)(0,0)==source.getDataPoint(0,0)());
184 }
185
186 }
187
188 void DataTestCase::testMore() {
189
190 cout << endl;
191
192 cout << "\tCreate a Data object from a DataArrayView" << endl;
193
194 DataArrayView::ShapeType viewShape;
195 viewShape.push_back(3);
196 DataArrayView::ValueType viewData(3);
197 for (int i=0;i<viewShape[0];++i) {
198 viewData[i]=i;
199 }
200 DataArrayView myView(viewData,viewShape);
201
202 bool expanded=true;
203 Data exData(myView,FunctionSpace(),expanded);
204 Data cData(myView);
205 Data result;
206
207 assert(exData.isExpanded());
208 assert(cData.isConstant());
209 assert(result.isEmpty());
210
211 cout << "\tTest some basic operations" << endl;
212 result=exData*cData;
213 assert(result.isExpanded());
214
215 assert(result.Lsup()==4);
216 assert(result.sup()==4);
217 assert(result.inf()==0);
218
219 result=exData+cData;
220 result=exData-cData;
221 result=exData/cData;
222
223 cout << "\tExercise wherePositive method" << endl;
224 assert(!exData.wherePositive().isEmpty());
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::ShapeType viewShape;
239 viewShape.push_back(3);
240 DataArrayView::ValueType viewData(3);
241 for (int i=0;i<viewShape[0];++i) {
242 viewData[i]=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
252 assert(exData.isExpanded());
253 assert(cData.isConstant());
254 assert(result.isEmpty());
255
256 cout << "\tTest some basic operations" << endl;
257 result=exData*cData;
258 assert(result.isExpanded());
259
260 }
261
262 void DataTestCase::testDataConstant() {
263
264 cout << endl;
265
266 cout << "\tCreate a DataConstant object from a DataArrayView" << endl;
267
268 DataArrayView::ShapeType viewShape;
269 viewShape.push_back(2);
270 viewShape.push_back(3);
271 viewShape.push_back(4);
272 DataArrayView::ValueType viewData(2*3*4);
273 for (int i=0;i<DataArrayView::noValues(viewShape);++i) {
274 viewData[i]=i;
275 }
276 DataArrayView myView(viewData,viewShape);
277
278 Data left(myView);
279 Data right(myView);
280 Data result;
281
282 cout << "\tTest some basic operations" << endl;
283
284 result=left-right;
285
286 assert(left.isConstant());
287 assert(right.isConstant());
288 assert(result.isConstant());
289
290 result=left+right;
291
292 assert(left.isConstant());
293 assert(right.isConstant());
294 assert(result.isConstant());
295
296 assert(!result.isExpanded());
297 assert(!result.isTagged());
298
299 }
300
301 void DataTestCase::testDataTaggedExceptions() {
302
303 cout << endl;
304
305 cout << "\tTest DataTagged exceptions." << endl;
306
307 Data myData;
308 DataArrayView myView;
309
310 try {
311 myData.getSampleDataByTag(0);;
312 assert(false);
313 }
314 catch (EsysException& e) {
315 //cout << e.what() << endl;
316 assert(true);
317 }
318
319 try {
320 myData.setTaggedValueFromCPP(0,myView);;
321 assert(false);
322 }
323 catch (EsysException& e) {
324 //cout << e.what() << endl;
325 assert(true);
326 }
327
328 }
329
330 void DataTestCase::testDataTagged() {
331
332 cout << endl;
333
334 cout << "\tCreate a DataTagged object with a default value only." << endl;
335
336 // create tagged data with no tag values just a default
337
338 DataTagged::TagListType keys;
339
340 DataTagged::ValueListType values;
341
342 DataArrayView::ShapeType viewShape;
343 viewShape.push_back(3);
344
345 DataArrayView::ValueType viewData(3);
346 for (int i=0;i<viewShape[0];++i) {
347 viewData[i]=i;
348 }
349 DataArrayView myView(viewData,viewShape);
350
351 bool expanded=false;
352
353 Data myData(keys,values,myView,FunctionSpace(),expanded);
354
355 // cout << myData.toString() << endl;
356
357 assert(!myData.isEmpty());
358 assert(myData.isTagged());
359 assert(myData.getTagNumber(0)==1);
360 assert(myData.getDataPointRank()==1);
361 assert(myData.getLength()==3);
362
363 DataArrayView myDataView = myData.getPointDataView();
364 assert(!myDataView.isEmpty());
365 assert(myDataView.getOffset()==0);
366 assert(myDataView.getRank()==1);
367 assert(myDataView.noValues()==3);
368 assert(myDataView.getShape().size()==1);
369 assert(myDataView(0)==0.0);
370 assert(myDataView(1)==1.0);
371 assert(myDataView(2)==2.0);
372
373 myDataView = myData.getDataPoint(0,0);
374 assert(!myDataView.isEmpty());
375 assert(myDataView.getOffset()==0);
376 assert(myDataView.getRank()==1);
377 assert(myDataView.noValues()==3);
378 assert(myDataView.getShape().size()==1);
379 assert(myDataView(0)==0.0);
380 assert(myDataView(1)==1.0);
381 assert(myDataView(2)==2.0);
382
383 double* sampleData=myData.getSampleData(0);
384 for (int i=0; i<myDataView.noValues(); i++) {
385 assert(sampleData[i]==i);
386 }
387 // use a non-existent tag so we get a pointer to
388 // the first element of the data array
389 sampleData=myData.getSampleDataByTag(9);
390 for (int i=0; i<myData.getLength(); i++) {
391 assert(sampleData[i]==i);
392 }
393
394 cout << "\tTest setting of a tag and associated value." << endl;
395
396 // value for tag "1"
397 DataArray eTwo(myView);
398 for (int i=0;i<eTwo.getView().getShape()[0];i++) {
399 eTwo.getView()(i)=i+2.0;
400 }
401
402 myData.setTaggedValueFromCPP(1,eTwo.getView());
403
404 assert(myData.getLength()==6);
405
406 myDataView = myData.getDataPoint(0,0);
407 assert(myDataView==eTwo.getView());
408 assert(!myDataView.isEmpty());
409 assert(myDataView.getOffset()==3);
410 assert(myDataView.getRank()==1);
411 assert(myDataView.noValues()==3);
412 assert(myDataView.getShape().size()==1);
413 assert(myDataView(0)==2);
414 assert(myDataView(1)==3);
415 assert(myDataView(2)==4);
416
417 sampleData=myData.getSampleDataByTag(1);
418 for (int i=0; i<myDataView.noValues(); i++) {
419 assert(sampleData[i]==i+2);
420 }
421
422 }
423
424 void DataTestCase::testConstructors() {
425
426 cout << endl;
427
428 DataArrayView::ShapeType viewShape;
429 {
430 cout << "\tCreate an Empty Data object" << endl;
431 Data temp(1.3,viewShape,FunctionSpace(),false);
432 }
433 {
434 cout << "\tCreate a rank 2 Data object" << endl;
435 viewShape.push_back(2);
436 viewShape.push_back(3);
437 Data temp(1.3,viewShape,FunctionSpace(),false);
438 }
439 }
440
441 void DataTestCase::testOperations() {
442
443 cout << endl;
444
445 // define the shape for the DataArrayView test data
446 DataArrayView::ShapeType shape;
447 shape.push_back(2);
448 shape.push_back(3);
449
450 // allocate the data for the DataArrayView
451 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
452
453 // construct DataArrayView
454 DataArrayView dataView(data,shape);
455
456 // assign values to the data
457 for (int i=0;i<shape[0];i++) {
458 for (int j=0;j<shape[1];j++) {
459 dataView(i,j)=dataView.index(i,j);
460 }
461 }
462
463 Data base(dataView);
464
465 // test unary operations
466
467 cout << "\tTest Data::pow." << endl;
468 Data power(3.0,shape,FunctionSpace(),true);
469 Data result(base.powD(power));
470 for (int i=0;i<shape[0];i++) {
471 for (int j=0;j<shape[1];j++) {
472 assert(result.getPointDataView()(i,j) == pow(dataView.index(i,j),3.0));
473 }
474 }
475
476 cout << "\tTest Data::sin." << endl;
477 result.copy(base.sin());
478 assert(true);
479
480 cout << "\tTest Data::cos." << endl;
481 result.copy(base.cos());
482 assert(true);
483
484 cout << "\tTest Data::tan." << endl;
485 result.copy(base.tan());
486 assert(true);
487
488 cout << "\tTest Data::asin." << endl;
489 result.copy(base.asin());
490 assert(true);
491
492 cout << "\tTest Data::acos." << endl;
493 result.copy(base.acos());
494 assert(true);
495
496 cout << "\tTest Data::atan." << endl;
497 result.copy(base.atan());
498 assert(true);
499
500 cout << "\tTest Data::sinh." << endl;
501 result.copy(base.sinh());
502 assert(true);
503
504 cout << "\tTest Data::cosh." << endl;
505 result.copy(base.cosh());
506 assert(true);
507
508 cout << "\tTest Data::tanh." << endl;
509 result.copy(base.tanh());
510 assert(true);
511
512 cout << "\tTest Data::asinh." << endl;
513 result.copy(base.asinh());
514 assert(true);
515
516 cout << "\tTest Data::acosh." << endl;
517 result.copy(base.acosh());
518 assert(true);
519
520 cout << "\tTest Data::atanh." << endl;
521 result.copy(base.atanh());
522 assert(true);
523
524 cout << "\tTest Data::log." << endl;
525 result.copy(base.log());
526 assert(true);
527
528 //cout << "\tTest Data::ln." << endl;
529 //result.copy(base.ln());
530 //assert(true);
531
532 cout << "\tTest Data::abs." << endl;
533 result.copy(base.abs());
534 assert(true);
535
536 cout << "\tTest Data::sign." << endl;
537 result.copy(base.sign());
538 assert(true);
539
540 cout << "\tTest Data::exp." << endl;
541 result.copy(base.exp());
542 assert(true);
543
544 cout << "\tTest Data::sqrt." << endl;
545 result.copy(base.sqrt());
546 assert(true);
547
548 cout << "\tTest Data::neg." << endl;
549 result.copy(base.neg());
550 assert(true);
551
552 cout << "\tTest Data::pos." << endl;
553 result.copy(base.pos());
554 for (int i=0;i<shape[0];i++) {
555 for (int j=0;j<shape[1];j++) {
556 assert(result.getPointDataView()(i,j) == dataView.index(i,j));
557 }
558 }
559
560 // test reduction operations
561
562 cout << "\tTest Data::Lsup." << endl;
563 assert(base.Lsup() == 5);
564
565 cout << "\tTest Data::sup." << endl;
566 assert(base.sup() == 5);
567
568 cout << "\tTest Data::inf." << endl;
569 assert(base.inf() == 0);
570
571 // test data-point reduction operations
572
573 cout << "\tTest Data::minval." << endl;
574 result.copy(base.minval());
575 assert(result.getPointDataView()() == 0);
576
577 cout << "\tTest Data::maxval." << endl;
578 result.copy(base.maxval());
579 assert(result.getPointDataView()() == 5);
580
581 //cout << "\tTest Data::length." << endl;
582 //result.copy(base.length());
583 //assert(pow(result.getPointDataView()(),2.0) == 55);
584
585 cout << "\tTest Data::trace." << endl;
586 result.copy(base.trace());
587 assert(result.getPointDataView()() == 15);
588
589 //result.copy(base.transpose(0));
590 //assert(true);
591
592 }
593
594 void DataTestCase::testRefValue() {
595
596 //
597 // Note - this test can't be run as boost::python::numeric::array
598 // objects can only be created and used from within a python thread!
599 //
600
601 cout << endl;
602
603 cout << "\tTest Data object RefValue methods." << endl;
604
605 // Create three Data object - DataExpanded, DataConstant and DataEmpty
606 DataArrayView::ShapeType viewShape;
607 viewShape.push_back(3);
608 DataArrayView::ValueType viewData(3);
609 for (int i=0;i<viewShape[0];++i) {
610 viewData[i]=i;
611 }
612 DataArrayView myView(viewData,viewShape);
613
614 bool expanded=true;
615
616 Data expandedData(myView,FunctionSpace(),expanded);
617 Data constantData(myView);
618 Data emptyData;
619
620 assert(expandedData.isExpanded());
621 assert(constantData.isConstant());
622 assert(emptyData.isEmpty());
623
624 // Check assertions are thrown for RefValue methods on DataEmpty
625
626 int ref = 0;
627 boost::python::numeric::array num_array(1.0);
628
629 try {
630 emptyData.getRefValue(ref,num_array);
631 assert(false);
632 }
633 catch (EsysException& e) {
634 assert(true);
635 }
636 try {
637 emptyData.setRefValue(ref,num_array);
638 assert(false);
639 }
640 catch (EsysException& e) {
641 assert(true);
642 }
643
644 // Check assertions are thrown for RefValue methods on DataConstant
645 try {
646 constantData.getRefValue(ref,num_array);
647 assert(false);
648 }
649 catch (EsysException& e) {
650 assert(true);
651 }
652 try {
653 constantData.setRefValue(ref,num_array);
654 assert(false);
655 }
656 catch (EsysException& e) {
657 assert(true);
658 }
659
660 // Check calls to RefValue methods on DataExpanded
661 expandedData.getRefValue(ref,num_array);
662 expandedData.setRefValue(ref,num_array);
663
664 }
665
666 void DataTestCase::testMemAlloc() {
667
668 //
669 // Simple little sanity check for the memory allocator
670
671 cout << endl;
672
673 Data *testData;
674 for (int i=0; i<1000; i++) {
675 testData = new Data(0.0, DataArrayView::ShapeType(), FunctionSpace(), true);
676 delete testData;
677 }
678
679 DataArrayView::ShapeType viewShape;
680 viewShape.push_back(10);
681 viewShape.push_back(10);
682 viewShape.push_back(10);
683
684 Data *testData2;
685 Data *testData3 = new Data(0.0, viewShape, FunctionSpace(), true);
686 for (int i=0; i<1000; i++) {
687 testData2 = new Data(0.0, viewShape, FunctionSpace(), true);
688 delete testData2;
689 }
690 delete testData3;
691
692 }
693
694 TestSuite* DataTestCase::suite ()
695 {
696 //
697 // create the suite of tests to perform.
698 TestSuite *testSuite = new TestSuite ("DataTestCase");
699
700 testSuite->addTest (new TestCaller< DataTestCase>("testAll",&DataTestCase::testAll));
701 testSuite->addTest (new TestCaller< DataTestCase>("testMore",&DataTestCase::testMore));
702 testSuite->addTest (new TestCaller< DataTestCase>("testDataConstant",&DataTestCase::testDataConstant));
703 testSuite->addTest (new TestCaller< DataTestCase>("testDataTagged",&DataTestCase::testDataTagged));
704 testSuite->addTest (new TestCaller< DataTestCase>("testDataTaggedExceptions",&DataTestCase::testDataTaggedExceptions));
705 testSuite->addTest (new TestCaller< DataTestCase>("testConstructors",&DataTestCase::testConstructors));
706 testSuite->addTest (new TestCaller< DataTestCase>("testSlicing",&DataTestCase::testSlicing));
707 testSuite->addTest (new TestCaller< DataTestCase>("testOperations",&DataTestCase::testOperations));
708 //testSuite->addTest (new TestCaller< DataTestCase>("testRefValue",&DataTestCase::testRefValue));
709 testSuite->addTest (new TestCaller< DataTestCase>("testMemAlloc",&DataTestCase::testMemAlloc));
710
711 return testSuite;
712 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26