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

Annotation of /trunk/escript/test/Data/DataTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 123 - (hide annotations)
Fri Jul 8 04:08:13 2005 UTC (14 years, 4 months ago) by jgs
Original Path: trunk/esys2/escript/test/Data/DataTestCase.cpp
File size: 16205 byte(s)
Merge of development branch back to main trunk on 2005-07-08

1 jgs 82 // $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 jgs 113 #include <math.h>
23 jgs 82
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 jgs 123 // weak tests for setting a slice of DataExpanded
155 jgs 82 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::ShapeType viewShape;
194     viewShape.push_back(3);
195 jgs 121 DataArrayView::ValueType viewData(3);
196 jgs 82 for (int i=0;i<viewShape[0];++i) {
197 jgs 121 viewData[i]=i;
198 jgs 82 }
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    
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::ShapeType viewShape;
238     viewShape.push_back(3);
239 jgs 121 DataArrayView::ValueType viewData(3);
240 jgs 82 for (int i=0;i<viewShape[0];++i) {
241 jgs 121 viewData[i]=i;
242 jgs 82 }
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 jgs 123
251 jgs 82 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::ShapeType viewShape;
268     viewShape.push_back(2);
269     viewShape.push_back(3);
270     viewShape.push_back(4);
271 jgs 121 DataArrayView::ValueType viewData(2*3*4);
272 jgs 82 for (int i=0;i<DataArrayView::noValues(viewShape);++i) {
273 jgs 121 viewData[i]=i;
274 jgs 82 }
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 jgs 123
306 jgs 82 Data myData;
307     DataArrayView myView;
308 jgs 123
309 jgs 82 try {
310     myData.getSampleDataByTag(0);;
311     assert(false);
312     }
313     catch (EsysException& e) {
314     //cout << e.what() << endl;
315     assert(true);
316     }
317 jgs 123
318 jgs 82 /*
319     try {
320     myData.setTaggedValue(0,myView);;
321     assert(false);
322     }
323     catch (EsysException& e) {
324     //cout << e.what() << endl;
325     assert(true);
326     }
327     */
328    
329     }
330    
331     void DataTestCase::testDataTagged() {
332    
333     cout << endl;
334    
335     cout << "\tCreate a DataTagged object from a DataArrayView" << endl;
336    
337     DataTagged::TagListType keys;
338     DataTagged::ValueListType values;
339     DataArrayView::ShapeType viewShape;
340     viewShape.push_back(3);
341 jgs 121 DataArrayView::ValueType viewData(3);
342 jgs 82 for (int i=0;i<viewShape[0];++i) {
343 jgs 121 viewData[i]=i;
344 jgs 82 }
345     DataArrayView myView(viewData,viewShape);
346    
347     // create tagged data with no tag values just a default
348     bool expanded=false;
349    
350     Data myData(keys,values,myView,FunctionSpace(),expanded);
351     assert(myData.isTagged());
352    
353     cout << "\tTest some basic operations" << endl;
354    
355     Data myDataCopy(myData);
356     myDataCopy.expand();
357     assert(myDataCopy.isExpanded());
358    
359     }
360    
361     void DataTestCase::testConstructors() {
362    
363     cout << endl;
364    
365     DataArrayView::ShapeType viewShape;
366     {
367     cout << "\tCreate an Empty Data object" << endl;
368     Data temp(1.3,viewShape,FunctionSpace(),false);
369     }
370     {
371     cout << "\tCreate a rank 2 Data object" << endl;
372     viewShape.push_back(2);
373     viewShape.push_back(3);
374     Data temp(1.3,viewShape,FunctionSpace(),false);
375     }
376     }
377    
378     void DataTestCase::testOperations() {
379 jgs 113
380 jgs 82 cout << endl;
381    
382 jgs 113 // define the shape for the DataArrayView test data
383     DataArrayView::ShapeType shape;
384     shape.push_back(2);
385     shape.push_back(3);
386 jgs 82
387 jgs 113 // allocate the data for the DataArrayView
388     DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
389 jgs 82
390 jgs 113 // construct DataArrayView
391     DataArrayView dataView(data,shape);
392 jgs 82
393 jgs 113 // assign values to the data
394     for (int i=0;i<shape[0];i++) {
395     for (int j=0;j<shape[1];j++) {
396     dataView(i,j)=dataView.index(i,j);
397     }
398     }
399    
400     Data base(dataView);
401    
402     // test unary operations
403    
404     cout << "\tTest Data::pow." << endl;
405     Data power(3.0,shape,FunctionSpace(),true);
406 jgs 82 Data result(base.powD(power));
407 jgs 113 for (int i=0;i<shape[0];i++) {
408     for (int j=0;j<shape[1];j++) {
409     assert(result.getPointDataView()(i,j) == pow(dataView.index(i,j),3.0));
410     }
411     }
412 jgs 82
413 jgs 113 cout << "\tTest Data::sin." << endl;
414 jgs 82 result.copy(base.sin());
415 jgs 102 assert(true);
416    
417 jgs 113 cout << "\tTest Data::cos." << endl;
418 jgs 82 result.copy(base.cos());
419 jgs 102 assert(true);
420    
421 jgs 113 cout << "\tTest Data::tan." << endl;
422 jgs 82 result.copy(base.tan());
423 jgs 102 assert(true);
424    
425 jgs 113 cout << "\tTest Data::log." << endl;
426 jgs 82 result.copy(base.log());
427 jgs 102 assert(true);
428    
429 jgs 113 cout << "\tTest Data::ln." << endl;
430 jgs 82 result.copy(base.ln());
431 jgs 102 assert(true);
432    
433 jgs 113 cout << "\tTest Data::abs." << endl;
434 jgs 102 result.copy(base.abs());
435     assert(true);
436    
437 jgs 113 cout << "\tTest Data::sign." << endl;
438 jgs 106 result.copy(base.sign());
439 jgs 102 assert(true);
440    
441 jgs 113 cout << "\tTest Data::exp." << endl;
442 jgs 106 result.copy(base.exp());
443 jgs 102 assert(true);
444    
445 jgs 113 cout << "\tTest Data::sqrt." << endl;
446 jgs 106 result.copy(base.sqrt());
447 jgs 102 assert(true);
448    
449 jgs 113 cout << "\tTest Data::neg." << endl;
450 jgs 106 result.copy(base.neg());
451 jgs 102 assert(true);
452    
453 jgs 113 cout << "\tTest Data::pos." << endl;
454 jgs 106 result.copy(base.pos());
455 jgs 113 for (int i=0;i<shape[0];i++) {
456     for (int j=0;j<shape[1];j++) {
457     assert(result.getPointDataView()(i,j) == dataView.index(i,j));
458     }
459     }
460 jgs 102
461 jgs 113 // test reduction operations
462    
463     cout << "\tTest Data::Lsup." << endl;
464     assert(base.Lsup() == 5);
465    
466     cout << "\tTest Data::sup." << endl;
467     assert(base.sup() == 5);
468    
469     cout << "\tTest Data::inf." << endl;
470     assert(base.inf() == 0);
471    
472     // test data-point reduction operations
473    
474     cout << "\tTest Data::minval." << endl;
475 jgs 106 result.copy(base.minval());
476 jgs 113 assert(result.getPointDataView()() == 0);
477 jgs 102
478 jgs 113 cout << "\tTest Data::maxval." << endl;
479 jgs 106 result.copy(base.maxval());
480 jgs 113 assert(result.getPointDataView()() == 5);
481 jgs 102
482 jgs 113 cout << "\tTest Data::length." << endl;
483 jgs 106 result.copy(base.length());
484 jgs 113 assert(pow(result.getPointDataView()(),2.0) == 55);
485 jgs 102
486 jgs 113 cout << "\tTest Data::trace." << endl;
487 jgs 106 result.copy(base.trace());
488 jgs 113 assert(result.getPointDataView()() == 15);
489 jgs 102
490 jgs 106 //result.copy(base.transpose(0));
491     //assert(true);
492    
493 jgs 82 }
494    
495 jgs 110 void DataTestCase::testRefValue() {
496    
497     //
498     // Note - this test can't be run as boost::python::numeric::array
499 jgs 113 // objects can only be created and used from within a python thread!
500 jgs 110 //
501    
502     cout << endl;
503    
504     cout << "\tTest Data object RefValue methods." << endl;
505    
506     // Create three Data object - DataExpanded, DataConstant and DataEmpty
507     DataArrayView::ShapeType viewShape;
508     viewShape.push_back(3);
509 jgs 121 DataArrayView::ValueType viewData(3);
510 jgs 110 for (int i=0;i<viewShape[0];++i) {
511 jgs 121 viewData[i]=i;
512 jgs 110 }
513     DataArrayView myView(viewData,viewShape);
514    
515     bool expanded=true;
516    
517     Data expandedData(myView,FunctionSpace(),expanded);
518     Data constantData(myView);
519     Data emptyData;
520    
521     assert(expandedData.isExpanded());
522     assert(constantData.isConstant());
523     assert(emptyData.isEmpty());
524    
525     // Check assertions are thrown for RefValue methods on DataEmpty
526    
527     int ref = 0;
528     boost::python::numeric::array num_array(1.0);
529    
530     try {
531     emptyData.getRefValue(ref,num_array);
532     assert(false);
533     }
534     catch (EsysException& e) {
535     assert(true);
536     }
537     try {
538     emptyData.setRefValue(ref,num_array);
539     assert(false);
540     }
541     catch (EsysException& e) {
542     assert(true);
543     }
544    
545     // Check assertions are thrown for RefValue methods on DataConstant
546     try {
547     constantData.getRefValue(ref,num_array);
548     assert(false);
549     }
550     catch (EsysException& e) {
551     assert(true);
552     }
553     try {
554     constantData.setRefValue(ref,num_array);
555     assert(false);
556     }
557     catch (EsysException& e) {
558     assert(true);
559     }
560    
561     // Check calls to RefValue methods on DataExpanded
562     expandedData.getRefValue(ref,num_array);
563     expandedData.setRefValue(ref,num_array);
564    
565     }
566    
567 jgs 82 TestSuite* DataTestCase::suite ()
568     {
569     //
570     // create the suite of tests to perform.
571     TestSuite *testSuite = new TestSuite ("DataTestCase");
572    
573     testSuite->addTest (new TestCaller< DataTestCase>("testAll",&DataTestCase::testAll));
574     testSuite->addTest (new TestCaller< DataTestCase>("testMore",&DataTestCase::testMore));
575     testSuite->addTest (new TestCaller< DataTestCase>("testDataConstant",&DataTestCase::testDataConstant));
576     testSuite->addTest (new TestCaller< DataTestCase>("testDataTagged",&DataTestCase::testDataTagged));
577     testSuite->addTest (new TestCaller< DataTestCase>("testDataTaggedExceptions",&DataTestCase::testDataTaggedExceptions));
578     testSuite->addTest (new TestCaller< DataTestCase>("testConstructors",&DataTestCase::testConstructors));
579     testSuite->addTest (new TestCaller< DataTestCase>("testSlicing",&DataTestCase::testSlicing));
580     testSuite->addTest (new TestCaller< DataTestCase>("testOperations",&DataTestCase::testOperations));
581 jgs 110 //testSuite->addTest (new TestCaller< DataTestCase>("testRefValue",&DataTestCase::testRefValue));
582 jgs 82
583     return testSuite;
584     }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26