/[escript]/trunk/esys2/escript/test/DataExpanded/DataExpandedTestCase.cpp
ViewVC logotype

Contents of /trunk/esys2/escript/test/DataExpanded/DataExpandedTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26