/[escript]/branches/RW_WIN32/escript/test/DataTestCase.cpp
ViewVC logotype

Contents of /branches/RW_WIN32/escript/test/DataTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 218 - (show annotations)
Thu Nov 24 07:42:12 2005 UTC (13 years, 9 months ago) by robwdcock
File size: 17709 byte(s)
All C++/C Unit tests now build (using scons) and pass on win32
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 DataExpanded
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::ShapeType viewShape;
194 viewShape.push_back(3);
195 DataArrayView::ValueType viewData(3);
196 for (int i=0;i<viewShape[0];++i) {
197 viewData[i]=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
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 DataArrayView::ValueType viewData(3);
240 for (int i=0;i<viewShape[0];++i) {
241 viewData[i]=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::ShapeType viewShape;
268 viewShape.push_back(2);
269 viewShape.push_back(3);
270 viewShape.push_back(4);
271 DataArrayView::ValueType viewData(2*3*4);
272 for (int i=0;i<DataArrayView::noValues(viewShape);++i) {
273 viewData[i]=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
306 Data myData;
307 DataArrayView myView;
308
309 try {
310 myData.getSampleDataByTag(0);;
311 assert(false);
312 }
313 catch (EsysException& e) {
314 //cout << e.what() << endl;
315 assert(true);
316 }
317
318 /*
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 DataArrayView::ValueType viewData(3);
342 for (int i=0;i<viewShape[0];++i) {
343 viewData[i]=i;
344 }
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
380 cout << endl;
381
382 // define the shape for the DataArrayView test data
383 DataArrayView::ShapeType shape;
384 shape.push_back(2);
385 shape.push_back(3);
386
387 // allocate the data for the DataArrayView
388 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
389
390 // construct DataArrayView
391 DataArrayView dataView(data,shape);
392
393 // 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 Data result(base.powD(power));
407 for (int i=0;i<shape[0];i++) {
408 for (int j=0;j<shape[1];j++) {
409 assert(result.getPointDataView()(i,j) == pow((double)dataView.index(i,j),3.0));
410 }
411 }
412
413 cout << "\tTest Data::sin." << endl;
414 result.copy(base.sin());
415 assert(true);
416
417 cout << "\tTest Data::cos." << endl;
418 result.copy(base.cos());
419 assert(true);
420
421 cout << "\tTest Data::tan." << endl;
422 result.copy(base.tan());
423 assert(true);
424
425 cout << "\tTest Data::asin." << endl;
426 result.copy(base.asin());
427 assert(true);
428
429 cout << "\tTest Data::acos." << endl;
430 result.copy(base.acos());
431 assert(true);
432
433 cout << "\tTest Data::atan." << endl;
434 result.copy(base.atan());
435 assert(true);
436
437 cout << "\tTest Data::sinh." << endl;
438 result.copy(base.sinh());
439 assert(true);
440
441 cout << "\tTest Data::cosh." << endl;
442 result.copy(base.cosh());
443 assert(true);
444
445 cout << "\tTest Data::tanh." << endl;
446 result.copy(base.tanh());
447 assert(true);
448
449 cout << "\tTest Data::asinh." << endl;
450 result.copy(base.asinh());
451 assert(true);
452
453 cout << "\tTest Data::acosh." << endl;
454 result.copy(base.acosh());
455 assert(true);
456
457 cout << "\tTest Data::atanh." << endl;
458 result.copy(base.atanh());
459 assert(true);
460
461 cout << "\tTest Data::log." << endl;
462 result.copy(base.log());
463 assert(true);
464
465 cout << "\tTest Data::ln." << endl;
466 result.copy(base.ln());
467 assert(true);
468
469 cout << "\tTest Data::abs." << endl;
470 result.copy(base.abs());
471 assert(true);
472
473 cout << "\tTest Data::sign." << endl;
474 result.copy(base.sign());
475 assert(true);
476
477 cout << "\tTest Data::exp." << endl;
478 result.copy(base.exp());
479 assert(true);
480
481 cout << "\tTest Data::sqrt." << endl;
482 result.copy(base.sqrt());
483 assert(true);
484
485 cout << "\tTest Data::neg." << endl;
486 result.copy(base.neg());
487 assert(true);
488
489 cout << "\tTest Data::pos." << endl;
490 result.copy(base.pos());
491 for (int i=0;i<shape[0];i++) {
492 for (int j=0;j<shape[1];j++) {
493 assert(result.getPointDataView()(i,j) == dataView.index(i,j));
494 }
495 }
496
497 // test reduction operations
498
499 cout << "\tTest Data::Lsup." << endl;
500 assert(base.Lsup() == 5);
501
502 cout << "\tTest Data::sup." << endl;
503 assert(base.sup() == 5);
504
505 cout << "\tTest Data::inf." << endl;
506 assert(base.inf() == 0);
507
508 // test data-point reduction operations
509
510 cout << "\tTest Data::minval." << endl;
511 result.copy(base.minval());
512 assert(result.getPointDataView()() == 0);
513
514 cout << "\tTest Data::maxval." << endl;
515 result.copy(base.maxval());
516 assert(result.getPointDataView()() == 5);
517
518 cout << "\tTest Data::length." << endl;
519 result.copy(base.length());
520 assert(pow(result.getPointDataView()(),2.0) == 55);
521
522 cout << "\tTest Data::trace." << endl;
523 result.copy(base.trace());
524 assert(result.getPointDataView()() == 15);
525
526 //result.copy(base.transpose(0));
527 //assert(true);
528
529 }
530
531 void DataTestCase::testRefValue() {
532
533 //
534 // Note - this test can't be run as boost::python::numeric::array
535 // objects can only be created and used from within a python thread!
536 //
537
538 cout << endl;
539
540 cout << "\tTest Data object RefValue methods." << endl;
541
542 // Create three Data object - DataExpanded, DataConstant and DataEmpty
543 DataArrayView::ShapeType viewShape;
544 viewShape.push_back(3);
545 DataArrayView::ValueType viewData(3);
546 for (int i=0;i<viewShape[0];++i) {
547 viewData[i]=i;
548 }
549 DataArrayView myView(viewData,viewShape);
550
551 bool expanded=true;
552
553 Data expandedData(myView,FunctionSpace(),expanded);
554 Data constantData(myView);
555 Data emptyData;
556
557 assert(expandedData.isExpanded());
558 assert(constantData.isConstant());
559 assert(emptyData.isEmpty());
560
561 // Check assertions are thrown for RefValue methods on DataEmpty
562
563 int ref = 0;
564 boost::python::numeric::array num_array(1.0);
565
566 try {
567 emptyData.getRefValue(ref,num_array);
568 assert(false);
569 }
570 catch (EsysException& e) {
571 assert(true);
572 }
573 try {
574 emptyData.setRefValue(ref,num_array);
575 assert(false);
576 }
577 catch (EsysException& e) {
578 assert(true);
579 }
580
581 // Check assertions are thrown for RefValue methods on DataConstant
582 try {
583 constantData.getRefValue(ref,num_array);
584 assert(false);
585 }
586 catch (EsysException& e) {
587 assert(true);
588 }
589 try {
590 constantData.setRefValue(ref,num_array);
591 assert(false);
592 }
593 catch (EsysException& e) {
594 assert(true);
595 }
596
597 // Check calls to RefValue methods on DataExpanded
598 expandedData.getRefValue(ref,num_array);
599 expandedData.setRefValue(ref,num_array);
600
601 }
602
603 void DataTestCase::testMemAlloc() {
604
605 //
606 // Simple little sanity check for the memory allocator
607
608 cout << endl;
609
610 Data *testData;
611 for (int i=0; i<1000; i++) {
612 testData = new Data(0.0, DataArrayView::ShapeType(), FunctionSpace(), true);
613 delete testData;
614 }
615
616 DataArrayView::ShapeType viewShape;
617 viewShape.push_back(10);
618 viewShape.push_back(10);
619 viewShape.push_back(10);
620
621 Data *testData2;
622 Data *testData3 = new Data(0.0, viewShape, FunctionSpace(), true);
623 for (int i=0; i<1000; i++) {
624 testData2 = new Data(0.0, viewShape, FunctionSpace(), true);
625 delete testData2;
626 }
627 delete testData3;
628
629 }
630
631 TestSuite* DataTestCase::suite ()
632 {
633 //
634 // create the suite of tests to perform.
635 TestSuite *testSuite = new TestSuite ("DataTestCase");
636
637 testSuite->addTest (new TestCaller< DataTestCase>("testAll",&DataTestCase::testAll));
638 testSuite->addTest (new TestCaller< DataTestCase>("testMore",&DataTestCase::testMore));
639 testSuite->addTest (new TestCaller< DataTestCase>("testDataConstant",&DataTestCase::testDataConstant));
640 testSuite->addTest (new TestCaller< DataTestCase>("testDataTagged",&DataTestCase::testDataTagged));
641 testSuite->addTest (new TestCaller< DataTestCase>("testDataTaggedExceptions",&DataTestCase::testDataTaggedExceptions));
642 testSuite->addTest (new TestCaller< DataTestCase>("testConstructors",&DataTestCase::testConstructors));
643 testSuite->addTest (new TestCaller< DataTestCase>("testSlicing",&DataTestCase::testSlicing));
644 testSuite->addTest (new TestCaller< DataTestCase>("testOperations",&DataTestCase::testOperations));
645 //testSuite->addTest (new TestCaller< DataTestCase>("testRefValue",&DataTestCase::testRefValue));
646 testSuite->addTest (new TestCaller< DataTestCase>("testMemAlloc",&DataTestCase::testMemAlloc));
647
648 return testSuite;
649 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26