/[escript]/branches/RW_WIN32/escript/test/DataExpanded/DataExpandedTestCase.cpp
ViewVC logotype

Contents of /branches/RW_WIN32/escript/test/DataExpanded/DataExpandedTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 185 - (show annotations)
Sun Nov 20 05:43:12 2005 UTC (13 years, 10 months ago) by robwdcock
File size: 20210 byte(s)
Robert Woodcock private branch for win32 port. Will eventually be merged with trunk when build systems are unified.
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 "escript/Data/FunctionSpace.h"
16 #include "escript/Data/DataExpanded.h"
17 #include "esysUtils/EsysException.h"
18 #include "DataExpandedTestCase.h"
19
20 #include <iostream>
21
22 using namespace CppUnitTest;
23 using namespace escript;
24 using namespace std;
25 using namespace esysUtils;
26
27 void DataExpandedTestCase::setUp() {
28 //
29 // This is called before each test is run
30
31 }
32
33 void DataExpandedTestCase::tearDown() {
34 //
35 // This is called after each test has been run
36
37 }
38
39 void DataExpandedTestCase::testAll() {
40
41 cout << endl;
42
43 //
44 // Create a rank 1 pointData
45 DataArrayView::ShapeType shape;
46 shape.push_back(3);
47 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
48 DataArrayView pointData(data,shape);
49
50 //
51 // Assign an arbitrary value
52 pointData(0)=0.0;
53 pointData(1)=1.0;
54 pointData(2)=2.0;
55
56 //
57 // Test constructor
58 cout << "\tTest DataExpanded constructor." << endl;
59 DataExpanded testData(pointData,FunctionSpace());
60
61 cout << "\tTest getLength." << endl;
62 assert(testData.getLength()==pointData.noValues());
63
64 cout << "\tTest getPointDataView." << endl;
65 for (int i=0;i<testData.getPointDataView().getShape()[0];i++) {
66 assert(testData.getPointDataView()(i)==pointData(i));
67 }
68
69 cout << "\tVerify data point attributes." << endl;
70 DataArrayView dataView=testData.getPointDataView();
71 assert(dataView.getRank()==shape.size());
72 assert(dataView.noValues()==shape[0]*1);
73 assert(dataView.getShape()[0]==shape[0]);
74 assert(testData.getNumDPPSample()==1);
75 assert(testData.getNumSamples()==1);
76 assert(testData.validSamplePointNo(testData.getNumDPPSample()-1));
77 assert(testData.validSampleNo(testData.getNumSamples()-1));
78
79 //
80 // Test alternative constructor
81 cout << "\tTest DataExpanded alternative constructor." << endl;
82 data[0]=0.0;
83 data[1]=1.0;
84 data[2]=2.0;
85 FunctionSpace tmp_fns;
86 DataExpanded testData1(tmp_fns,shape,data);
87
88 cout << "\tTest getLength." << endl;
89 assert(testData1.getLength()==pointData.noValues());
90
91 cout << "\tTest getPointDataView." << endl;
92 for (int i=0;i<testData1.getPointDataView().getShape()[0];i++) {
93 assert(testData1.getPointDataView()(i)==pointData(i));
94 }
95
96 cout << "\tVerify data point attributes." << endl;
97 dataView=testData1.getPointDataView();
98 assert(dataView.getRank()==shape.size());
99 assert(dataView.noValues()==shape[0]*1);
100 assert(dataView.getShape()[0]==shape[0]);
101 assert(testData.getNumDPPSample()==1);
102 assert(testData.getNumSamples()==1);
103 assert(testData.validSamplePointNo(testData.getNumDPPSample()-1));
104 assert(testData.validSampleNo(testData.getNumSamples()-1));
105
106 //
107 // Test copy constructor
108 cout << "\tTest DataExpanded copy constructor." << endl;
109 DataExpanded testData2(testData);
110
111 cout << "\tTest getLength." << endl;
112 assert(testData2.getLength()==pointData.noValues());
113
114 cout << "\tTest getPointDataView." << endl;
115 for (int i=0;i<testData2.getPointDataView().getShape()[0];i++) {
116 assert(testData2.getPointDataView()(i)==pointData(i));
117 }
118
119 cout << "\tVerify data point attributes." << endl;
120 dataView=testData2.getPointDataView();
121 assert(dataView.getRank()==shape.size());
122 assert(dataView.noValues()==shape[0]*1);
123 assert(dataView.getShape()[0]==shape[0]);
124 assert(testData2.getNumDPPSample()==1);
125 assert(testData2.getNumSamples()==1);
126 assert(testData2.validSamplePointNo(testData2.getNumDPPSample()-1));
127 assert(testData2.validSampleNo(testData2.getNumSamples()-1));
128
129 }
130
131 void DataExpandedTestCase::testReshape() {
132
133 cout << endl;
134
135 //
136 // Create a scalar pointData
137 DataArrayView::ShapeType shape;
138 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
139 DataArrayView pointData(data,shape);
140
141 //
142 // Assign an arbitrary value
143 pointData()=1.0;
144
145 //
146 // Create object to test
147 cout << "\tCreate scalar DataExpanded object." << endl;
148 DataExpanded testData(pointData,FunctionSpace());
149
150 //
151 // Test reshape - legal
152 cout << "\tTest reshape." << endl;
153 shape.push_back(2);
154 shape.push_back(3);
155 shape.push_back(21);
156 testData.reshapeDataPoint(shape);
157 assert(testData.getPointDataView().getRank()==shape.size());
158
159 //
160 // Verify data values
161 cout << "\tVerify data point values." << endl;
162 for (int k=0;k<testData.getPointDataView().getShape()[2];k++) {
163 for (int j=0;j<testData.getPointDataView().getShape()[1];j++) {
164 for (int i=0;i<testData.getPointDataView().getShape()[0];i++) {
165 assert(testData.getPointDataView()(i,j,k)==pointData());
166 }
167 }
168 }
169
170 //
171 // Verify data attributes
172 cout << "\tVerify data point attributes." << endl;
173 DataArrayView dataView=testData.getPointDataView();
174 assert(dataView.getRank()==shape.size());
175 assert(dataView.noValues()==shape[0]*shape[1]*shape[2]);
176 assert(dataView.getShape()[0]==shape[0]);
177 assert(dataView.getShape()[1]==shape[1]);
178 assert(dataView.getShape()[2]==shape[2]);
179 assert(testData.getNumDPPSample()==1);
180 assert(testData.getNumSamples()==1);
181 assert(testData.validSamplePointNo(testData.getNumDPPSample()-1));
182 assert(testData.validSampleNo(testData.getNumSamples()-1));
183
184 //
185 // Test reshape - illegal
186 cout << "\tTest illegal reshape." << endl;
187 try {
188 testData.reshapeDataPoint(shape);
189 assert(false);
190 }
191 catch (EsysException& e) {
192 //cout << e.toString() << endl;
193 assert(true);
194 }
195
196 }
197
198 void DataExpandedTestCase::testSlicing() {
199
200 cout << endl;
201
202 //
203 // Create a rank 1 pointData
204 DataArrayView::ShapeType shape;
205 shape.push_back(3);
206 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
207 DataArrayView pointData(data,shape);
208
209 //
210 // Assign an arbitrary value
211 pointData(0)=0.0;
212 pointData(1)=1.0;
213 pointData(2)=2.0;
214
215 //
216 // Create object to test
217 cout << "\tCreate rank 1 DataExpanded object." << endl;
218 DataExpanded testData(pointData,FunctionSpace());
219
220 cout << "\tTest slicing (whole object)." << endl;
221 DataArrayView::RegionType region;
222 region.push_back(DataArrayView::RegionType::value_type(0,shape[0]));
223
224 DataAbstract* testData2=testData.getSlice(region);
225
226 //
227 // Verify data values
228 cout << "\tVerify data point values." << endl;
229 for (int i=0;i<testData2->getPointDataView().getShape()[0];i++) {
230 assert(testData2->getPointDataView()(i)==pointData(region[0].first+i));
231 }
232
233 cout << "\tVerify data point attributes." << endl;
234 DataArrayView dataView=testData2->getPointDataView();
235 assert(dataView.getRank()==shape.size());
236 assert(dataView.noValues()==shape[0]*1);
237 assert(dataView.getShape()[0]==shape[0]);
238
239 delete testData2;
240 }
241
242 void DataExpandedTestCase::testSlicing2() {
243
244 cout << endl;
245
246 //
247 // Create a rank 2 pointData
248 DataArrayView::ShapeType shape;
249 shape.push_back(3);
250 shape.push_back(3);
251 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
252 DataArrayView pointData(data,shape);
253
254 //
255 // Assign an arbitrary value
256 pointData(0,0)=0.0;
257 pointData(1,0)=1.0;
258 pointData(2,0)=2.0;
259 pointData(0,1)=3.0;
260 pointData(1,1)=4.0;
261 pointData(2,1)=5.0;
262 pointData(0,2)=6.0;
263 pointData(1,2)=7.0;
264 pointData(2,2)=8.0;
265
266 //
267 // Create object to test
268 cout << "\tCreate rank 2 DataExpanded object." << endl;
269 DataExpanded testData(pointData,FunctionSpace());
270
271 cout << "\tTest slicing (part object)." << endl;
272 DataArrayView::RegionType region;
273 region.push_back(DataArrayView::RegionType::value_type(0,2));
274 region.push_back(DataArrayView::RegionType::value_type(0,2));
275 DataAbstract* testData2=testData.getSlice(region);
276
277 //
278 // Verify data values
279 cout << "\tVerify data point values." << endl;
280 for (int j=0;j<testData2->getPointDataView().getShape()[1];j++) {
281 for (int i=0;i<testData2->getPointDataView().getShape()[0];i++) {
282 assert(testData2->getPointDataView()(i,j)==pointData(region[0].first+i,region[1].first+j));
283 }
284 }
285
286 cout << "\tVerify data point attributes." << endl;
287 DataArrayView dataView=testData2->getPointDataView();
288 assert(dataView.getRank()==region.size());
289 assert(dataView.noValues()==(region[0].second-region[0].first)*(region[1].second-region[1].first));
290 assert(dataView.getShape()[0]==(region[0].second-region[0].first));
291 assert(dataView.getShape()[1]==(region[1].second-region[1].first));
292
293 cout << "\tTest slicing (part object)." << endl;
294 DataArrayView::RegionType region2;
295 region2.push_back(DataArrayView::RegionType::value_type(1,3));
296 region2.push_back(DataArrayView::RegionType::value_type(1,3));
297 DataAbstract* testData3=testData.getSlice(region2);
298
299 //
300 // Verify data values
301 cout << "\tVerify data point values." << endl;
302 for (int j=0;j<testData3->getPointDataView().getShape()[1];j++) {
303 for (int i=0;i<testData3->getPointDataView().getShape()[0];i++) {
304 assert(testData3->getPointDataView()(i,j)==pointData(region2[0].first+i,region2[1].first+j));
305 }
306 }
307
308 cout << "\tVerify data point attributes." << endl;
309 dataView=testData3->getPointDataView();
310 assert(dataView.getRank()==region2.size());
311 assert(dataView.noValues()==(region2[0].second-region2[0].first)*(region2[1].second-region2[1].first));
312 assert(dataView.getShape()[0]==(region2[0].second-region2[0].first));
313 assert(dataView.getShape()[1]==(region2[1].second-region2[1].first));
314
315 delete testData2;
316 delete testData3;
317
318 }
319
320 void DataExpandedTestCase::testSlicing3() {
321
322 cout << endl;
323
324 //
325 // Create a rank 3 pointData
326 DataArrayView::ShapeType shape;
327 shape.push_back(3);
328 shape.push_back(3);
329 shape.push_back(3);
330 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
331 DataArrayView pointData(data,shape);
332
333 //
334 // Assign an arbitrary value
335 pointData(0,0,0)=0.0;
336 pointData(1,0,0)=1.0;
337 pointData(2,0,0)=2.0;
338 pointData(0,1,0)=3.0;
339 pointData(1,1,0)=4.0;
340 pointData(2,1,0)=5.0;
341 pointData(0,2,0)=6.0;
342 pointData(1,2,0)=7.0;
343 pointData(2,2,0)=8.0;
344
345 pointData(0,0,1)=9.0;
346 pointData(1,0,1)=10.0;
347 pointData(2,0,1)=11.0;
348 pointData(0,1,1)=12.0;
349 pointData(1,1,1)=13.0;
350 pointData(2,1,1)=14.0;
351 pointData(0,2,1)=15.0;
352 pointData(1,2,1)=16.0;
353 pointData(2,2,1)=17.0;
354
355 pointData(0,0,2)=18.0;
356 pointData(1,0,2)=19.0;
357 pointData(2,0,2)=20.0;
358 pointData(0,1,2)=21.0;
359 pointData(1,1,2)=22.0;
360 pointData(2,1,2)=23.0;
361 pointData(0,2,2)=24.0;
362 pointData(1,2,2)=25.0;
363 pointData(2,2,2)=26.0;
364
365 //
366 // Create object to test
367 cout << "\tCreate rank 3 DataExpanded object." << endl;
368 DataExpanded testData(pointData,FunctionSpace());
369
370 cout << "\tTest slicing (part object)." << endl;
371 DataArrayView::RegionType region;
372 region.push_back(DataArrayView::RegionType::value_type(0,2));
373 region.push_back(DataArrayView::RegionType::value_type(0,2));
374 region.push_back(DataArrayView::RegionType::value_type(0,2));
375 DataAbstract* testData2=testData.getSlice(region);
376
377 //
378 // Verify data values
379 cout << "\tVerify data point values." << endl;
380 for (int k=0;k<testData2->getPointDataView().getShape()[2];k++) {
381 for (int j=0;j<testData2->getPointDataView().getShape()[1];j++) {
382 for (int i=0;i<testData2->getPointDataView().getShape()[0];i++) {
383 assert(testData2->getPointDataView()(i,j,k)==pointData(region[0].first+i,
384 region[1].first+j,
385 region[2].first+k));
386 }
387 }
388 }
389
390 cout << "\tVerify data point attributes." << endl;
391 DataArrayView dataView=testData2->getPointDataView();
392 assert(dataView.getRank()==region.size());
393 assert(dataView.noValues()==(region[0].second-region[0].first)
394 *(region[1].second-region[1].first)
395 *(region[2].second-region[2].first));
396 assert(dataView.getShape()[0]==(region[0].second-region[0].first));
397 assert(dataView.getShape()[1]==(region[1].second-region[1].first));
398 assert(dataView.getShape()[2]==(region[2].second-region[2].first));
399
400 cout << "\tTest slicing (part object)." << endl;
401 DataArrayView::RegionType region2;
402 region2.push_back(DataArrayView::RegionType::value_type(1,3));
403 region2.push_back(DataArrayView::RegionType::value_type(1,3));
404 region2.push_back(DataArrayView::RegionType::value_type(1,3));
405 DataAbstract* testData3=testData.getSlice(region2);
406
407 //
408 // Verify data values
409 cout << "\tVerify data point values." << endl;
410 for (int k=0;k<testData3->getPointDataView().getShape()[2];k++) {
411 for (int j=0;j<testData3->getPointDataView().getShape()[1];j++) {
412 for (int i=0;i<testData3->getPointDataView().getShape()[0];i++) {
413 assert(testData3->getPointDataView()(i,j,k)==pointData(region2[0].first+i,
414 region2[1].first+j,
415 region2[2].first+k));
416 }
417 }
418 }
419
420 cout << "\tVerify data point attributes." << endl;
421 dataView=testData2->getPointDataView();
422 assert(dataView.getRank()==region.size());
423 assert(dataView.noValues()==(region[0].second-region[0].first)
424 *(region[1].second-region[1].first)
425 *(region[2].second-region[2].first));
426 assert(dataView.getShape()[0]==(region[0].second-region[0].first));
427 assert(dataView.getShape()[1]==(region[1].second-region[1].first));
428 assert(dataView.getShape()[2]==(region[2].second-region[2].first));
429
430 delete testData2;
431 delete testData3;
432
433 }
434
435
436 void DataExpandedTestCase::testSliceSetting() {
437
438 cout << endl;
439
440 //
441 // Create a rank 2 pointData
442 DataArrayView::ShapeType shape;
443 shape.push_back(2);
444 shape.push_back(2);
445 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
446 DataArrayView pointData(data,shape);
447
448 //
449 // Assign an arbitrary value
450 pointData(0,0)=0.0;
451 pointData(0,1)=1.0;
452 pointData(1,0)=2.0;
453 pointData(1,1)=3.0;
454
455 //
456 // Create object to test
457 cout << "\tCreate rank 2 DataExpanded object." << endl;
458 DataExpanded testData(pointData,FunctionSpace());
459
460 //
461 // Create another rank 2 pointData
462 DataArrayView::ShapeType shape2;
463 shape2.push_back(3);
464 shape2.push_back(3);
465 DataArrayView::ValueType data2(DataArrayView::noValues(shape2),0);
466 DataArrayView pointData2(data2,shape2);
467
468 //
469 // Assign an arbitrary value
470 pointData2(0,0)=0.1;
471 pointData2(0,1)=1.1;
472 pointData2(0,2)=2.1;
473 pointData2(1,0)=3.1;
474 pointData2(1,1)=4.1;
475 pointData2(1,2)=5.1;
476 pointData2(2,0)=6.1;
477 pointData2(2,1)=7.1;
478 pointData2(2,2)=8.1;
479
480 //
481 // Create object to test
482 cout << "\tCreate second rank 2 DataExpanded object." << endl;
483 DataExpanded testData2(pointData2,FunctionSpace());
484
485 cout << "\tTest slice setting (1)." << endl;
486
487 DataArrayView::RegionType region;
488 region.push_back(DataArrayView::RegionType::value_type(0,2));
489 region.push_back(DataArrayView::RegionType::value_type(0,2));
490
491 DataArrayView::RegionType region2;
492 region2.push_back(DataArrayView::RegionType::value_type(1,3));
493 region2.push_back(DataArrayView::RegionType::value_type(1,3));
494
495 DataAbstract* testData3=testData.getSlice(region);
496
497 testData2.setSlice(testData3,region2);
498
499 //
500 // Verify data values
501 cout << "\tVerify data point values." << endl;
502 for (int j=region2[1].first;j<region2[1].second;j++) {
503 for (int i=region2[0].first;i<region2[0].second;i++) {
504 assert(testData2.getPointDataView()(i,j)==pointData(i-(region[0].second-1),j-(region[1].second-1)));
505 }
506 }
507
508 delete testData3;
509
510 }
511
512 void DataExpandedTestCase::testSliceSetting2() {
513
514 cout << endl;
515
516 //
517 // Create a rank 0 pointData
518 DataArrayView::ShapeType shape;
519 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
520 DataArrayView pointData(data,shape);
521
522 //
523 // Assign an arbitrary value
524 pointData()=0.0;
525
526 //
527 // Create object to test
528 cout << "\tCreate rank 0 DataExpanded object." << endl;
529 DataExpanded testData(pointData,FunctionSpace());
530
531 //
532 // Create a rank 2 pointData
533 DataArrayView::ShapeType shape2;
534 shape2.push_back(3);
535 shape2.push_back(3);
536 DataArrayView::ValueType data2(DataArrayView::noValues(shape2),0);
537 DataArrayView pointData2(data2,shape2);
538
539 //
540 // Assign an arbitrary value
541 pointData2(0,0)=0.1;
542 pointData2(0,1)=1.1;
543 pointData2(0,2)=2.1;
544 pointData2(1,0)=3.1;
545 pointData2(1,1)=4.1;
546 pointData2(1,2)=5.1;
547 pointData2(2,0)=6.1;
548 pointData2(2,1)=7.1;
549 pointData2(2,2)=8.1;
550
551 //
552 // Create object to test
553 cout << "\tCreate rank 2 DataExpanded object." << endl;
554 DataExpanded testData2(pointData2,FunctionSpace());
555
556 cout << "\tTest slice setting (1)." << endl;
557
558 DataArrayView::RegionType region;
559
560 DataArrayView::RegionType region2;
561 region2.push_back(DataArrayView::RegionType::value_type(1,1));
562 region2.push_back(DataArrayView::RegionType::value_type(1,1));
563
564 DataAbstract* testData3=testData.getSlice(region);
565
566 testData2.setSlice(testData3,region2);
567
568 //
569 // Verify data values
570 cout << "\tVerify data point values." << endl;
571 for (int j=region2[1].first;j<region2[1].second;j++) {
572 for (int i=region2[0].first;i<region2[0].second;i++) {
573 assert(testData2.getPointDataView()(i,j)==pointData());
574 }
575 }
576
577 delete testData3;
578
579 }
580
581 void DataExpandedTestCase::testRefValue() {
582
583 cout << endl;
584
585 cout << "\tTest DataExpanded refValue methods." << endl;
586
587 //
588 // Create a rank 1 data array to create a DataExpanded object
589 DataArrayView::ShapeType shape;
590 shape.push_back(3);
591 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
592 DataArrayView dataView(data,shape);
593
594 //
595 // Create another DataArray object to pass to refValue methods
596 DataArray data2(dataView);
597
598 //
599 // Create a DataExpanded object
600 dataView(0)=0.0;
601 dataView(1)=1.0;
602 dataView(2)=2.0;
603 DataExpanded dataExp(dataView,FunctionSpace());
604
605 //
606 // Try out ref value methods
607
608 dataExp.getRefValue(1,data2);
609
610 // check the values loaded into data2 by getRefValue
611 // match the values in dataExp
612 assert(data2.getView()(0)==0.0);
613 assert(data2.getView()(1)==1.0);
614 assert(data2.getView()(2)==2.0);
615
616 data2.getView()(0)=3.0;
617 data2.getView()(1)=4.0;
618 data2.getView()(2)=5.0;
619
620 dataExp.setRefValue(1,data2);
621
622 // check the values now in dataExp match the values
623 // that were supplied in data2
624 assert(dataExp.getPointDataView()(0)==3.0);
625 assert(dataExp.getPointDataView()(1)==4.0);
626 assert(dataExp.getPointDataView()(2)==5.0);
627
628 cout << endl;
629
630 }
631
632 TestSuite* DataExpandedTestCase::suite ()
633 {
634 //
635 // Create the suite of tests to perform.
636 TestSuite *testSuite = new TestSuite ("DataExpandedTestCase");
637 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testAll",&DataExpandedTestCase::testAll));
638 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testReshape",&DataExpandedTestCase::testReshape));
639 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSlicing",&DataExpandedTestCase::testSlicing));
640 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSlicing2",&DataExpandedTestCase::testSlicing2));
641 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSlicing3",&DataExpandedTestCase::testSlicing3));
642 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSliceSetting",&DataExpandedTestCase::testSliceSetting));
643 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSliceSetting2",&DataExpandedTestCase::testSliceSetting2));
644 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testRefValue",&DataExpandedTestCase::testRefValue));
645 return testSuite;
646 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26