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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1312 - (show annotations)
Mon Sep 24 06:18:44 2007 UTC (11 years, 10 months ago) by ksteube
File size: 16447 byte(s)
The MPI branch is hereby closed. All future work should be in trunk.

Previously in revision 1295 I merged the latest changes to trunk into trunk-mpi-branch.
In this revision I copied all files from trunk-mpi-branch over the corresponding
trunk files. I did not use 'svn merge', it was a copy.

1
2 /* $Id$ */
3
4 /*******************************************************
5 *
6 * Copyright 2003-2007 by ACceSS MNRF
7 * Copyright 2007 by University of Queensland
8 *
9 * http://esscc.uq.edu.au
10 * Primary Business: Queensland, Australia
11 * Licensed under the Open Software License version 3.0
12 * http://www.opensource.org/licenses/osl-3.0.php
13 *
14 *******************************************************/
15
16 #include "escript/FunctionSpace.h"
17 #include "escript/DataExpanded.h"
18 #include "esysUtils/EsysException.h"
19 #include "DataExpandedTestCase.h"
20
21 #include <iostream>
22
23 using namespace CppUnitTest;
24 using namespace escript;
25 using namespace std;
26 using namespace esysUtils;
27
28 void DataExpandedTestCase::setUp() {
29 //
30 // This is called before each test is run
31
32 }
33
34 void DataExpandedTestCase::tearDown() {
35 //
36 // This is called after each test has been run
37
38 }
39
40 void DataExpandedTestCase::testAll() {
41
42 cout << endl;
43
44 //
45 // Create a rank 1 pointData
46 DataArrayView::ShapeType shape;
47 shape.push_back(3);
48 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
49 DataArrayView pointData(data,shape);
50
51 //
52 // Assign an arbitrary value
53 pointData(0)=0.0;
54 pointData(1)=1.0;
55 pointData(2)=2.0;
56
57 //
58 // Test constructor
59 cout << "\tTest DataExpanded constructor." << endl;
60 DataExpanded testData(pointData,FunctionSpace());
61
62 cout << "\tTest getLength." << endl;
63 assert(testData.getLength()==pointData.noValues());
64
65 cout << "\tTest getPointDataView." << endl;
66 for (int i=0;i<testData.getPointDataView().getShape()[0];i++) {
67 assert(testData.getPointDataView()(i)==pointData(i));
68 }
69
70 cout << "\tVerify data point attributes." << endl;
71 DataArrayView dataView=testData.getPointDataView();
72 assert(dataView.getRank()==shape.size());
73 assert(dataView.noValues()==shape[0]*1);
74 assert(dataView.getShape()[0]==shape[0]);
75 assert(testData.getNumDPPSample()==1);
76 assert(testData.getNumSamples()==1);
77 assert(testData.validSamplePointNo(testData.getNumDPPSample()-1));
78 assert(testData.validSampleNo(testData.getNumSamples()-1));
79
80 //
81 // Test alternative constructor
82 cout << "\tTest DataExpanded alternative constructor." << endl;
83 data[0]=0.0;
84 data[1]=1.0;
85 data[2]=2.0;
86 FunctionSpace tmp_fns;
87 DataExpanded testData1(tmp_fns,shape,data);
88
89 cout << "\tTest getLength." << endl;
90 assert(testData1.getLength()==pointData.noValues());
91
92 cout << "\tTest getPointDataView." << endl;
93 for (int i=0;i<testData1.getPointDataView().getShape()[0];i++) {
94 assert(testData1.getPointDataView()(i)==pointData(i));
95 }
96
97 cout << "\tVerify data point attributes." << endl;
98 dataView=testData1.getPointDataView();
99 assert(dataView.getRank()==shape.size());
100 assert(dataView.noValues()==shape[0]*1);
101 assert(dataView.getShape()[0]==shape[0]);
102 assert(testData.getNumDPPSample()==1);
103 assert(testData.getNumSamples()==1);
104 assert(testData.validSamplePointNo(testData.getNumDPPSample()-1));
105 assert(testData.validSampleNo(testData.getNumSamples()-1));
106
107 //
108 // Test copy constructor
109 cout << "\tTest DataExpanded copy constructor." << endl;
110 DataExpanded testData2(testData);
111
112 cout << "\tTest getLength." << endl;
113 assert(testData2.getLength()==pointData.noValues());
114
115 cout << "\tTest getPointDataView." << endl;
116 for (int i=0;i<testData2.getPointDataView().getShape()[0];i++) {
117 assert(testData2.getPointDataView()(i)==pointData(i));
118 }
119
120 cout << "\tVerify data point attributes." << endl;
121 dataView=testData2.getPointDataView();
122 assert(dataView.getRank()==shape.size());
123 assert(dataView.noValues()==shape[0]*1);
124 assert(dataView.getShape()[0]==shape[0]);
125 assert(testData2.getNumDPPSample()==1);
126 assert(testData2.getNumSamples()==1);
127 assert(testData2.validSamplePointNo(testData2.getNumDPPSample()-1));
128 assert(testData2.validSampleNo(testData2.getNumSamples()-1));
129
130 }
131
132
133 void DataExpandedTestCase::testSlicing() {
134
135 cout << endl;
136
137 //
138 // Create a rank 1 pointData
139 DataArrayView::ShapeType shape;
140 shape.push_back(3);
141 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
142 DataArrayView pointData(data,shape);
143
144 //
145 // Assign an arbitrary value
146 pointData(0)=0.0;
147 pointData(1)=1.0;
148 pointData(2)=2.0;
149
150 //
151 // Create object to test
152 cout << "\tCreate rank 1 DataExpanded object." << endl;
153 DataExpanded testData(pointData,FunctionSpace());
154
155 cout << "\tTest slicing (whole object)." << endl;
156 DataArrayView::RegionType region;
157 region.push_back(DataArrayView::RegionType::value_type(0,shape[0]));
158
159 DataAbstract* testData2=testData.getSlice(region);
160
161 //
162 // Verify data values
163 cout << "\tVerify data point values." << endl;
164 for (int i=0;i<testData2->getPointDataView().getShape()[0];i++) {
165 assert(testData2->getPointDataView()(i)==pointData(region[0].first+i));
166 }
167
168 cout << "\tVerify data point attributes." << endl;
169 DataArrayView dataView=testData2->getPointDataView();
170 assert(dataView.getRank()==shape.size());
171 assert(dataView.noValues()==shape[0]*1);
172 assert(dataView.getShape()[0]==shape[0]);
173
174 delete testData2;
175 }
176
177 void DataExpandedTestCase::testSlicing2() {
178
179 cout << endl;
180
181 //
182 // Create a rank 2 pointData
183 DataArrayView::ShapeType shape;
184 shape.push_back(3);
185 shape.push_back(3);
186 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
187 DataArrayView pointData(data,shape);
188
189 //
190 // Assign an arbitrary value
191 pointData(0,0)=0.0;
192 pointData(1,0)=1.0;
193 pointData(2,0)=2.0;
194 pointData(0,1)=3.0;
195 pointData(1,1)=4.0;
196 pointData(2,1)=5.0;
197 pointData(0,2)=6.0;
198 pointData(1,2)=7.0;
199 pointData(2,2)=8.0;
200
201 //
202 // Create object to test
203 cout << "\tCreate rank 2 DataExpanded object." << endl;
204 DataExpanded testData(pointData,FunctionSpace());
205
206 cout << "\tTest slicing (part object)." << endl;
207 DataArrayView::RegionType region;
208 region.push_back(DataArrayView::RegionType::value_type(0,2));
209 region.push_back(DataArrayView::RegionType::value_type(0,2));
210 DataAbstract* testData2=testData.getSlice(region);
211
212 //
213 // Verify data values
214 cout << "\tVerify data point values." << endl;
215 for (int j=0;j<testData2->getPointDataView().getShape()[1];j++) {
216 for (int i=0;i<testData2->getPointDataView().getShape()[0];i++) {
217 assert(testData2->getPointDataView()(i,j)==pointData(region[0].first+i,region[1].first+j));
218 }
219 }
220
221 cout << "\tVerify data point attributes." << endl;
222 DataArrayView dataView=testData2->getPointDataView();
223 assert(dataView.getRank()==region.size());
224 assert(dataView.noValues()==(region[0].second-region[0].first)*(region[1].second-region[1].first));
225 assert(dataView.getShape()[0]==(region[0].second-region[0].first));
226 assert(dataView.getShape()[1]==(region[1].second-region[1].first));
227
228 cout << "\tTest slicing (part object)." << endl;
229 DataArrayView::RegionType region2;
230 region2.push_back(DataArrayView::RegionType::value_type(1,3));
231 region2.push_back(DataArrayView::RegionType::value_type(1,3));
232 DataAbstract* testData3=testData.getSlice(region2);
233
234 //
235 // Verify data values
236 cout << "\tVerify data point values." << endl;
237 for (int j=0;j<testData3->getPointDataView().getShape()[1];j++) {
238 for (int i=0;i<testData3->getPointDataView().getShape()[0];i++) {
239 assert(testData3->getPointDataView()(i,j)==pointData(region2[0].first+i,region2[1].first+j));
240 }
241 }
242
243 cout << "\tVerify data point attributes." << endl;
244 dataView=testData3->getPointDataView();
245 assert(dataView.getRank()==region2.size());
246 assert(dataView.noValues()==(region2[0].second-region2[0].first)*(region2[1].second-region2[1].first));
247 assert(dataView.getShape()[0]==(region2[0].second-region2[0].first));
248 assert(dataView.getShape()[1]==(region2[1].second-region2[1].first));
249
250 delete testData2;
251 delete testData3;
252
253 }
254
255 void DataExpandedTestCase::testSlicing3() {
256
257 cout << endl;
258
259 //
260 // Create a rank 3 pointData
261 DataArrayView::ShapeType shape;
262 shape.push_back(3);
263 shape.push_back(3);
264 shape.push_back(3);
265 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
266 DataArrayView pointData(data,shape);
267
268 //
269 // Assign an arbitrary value
270 pointData(0,0,0)=0.0;
271 pointData(1,0,0)=1.0;
272 pointData(2,0,0)=2.0;
273 pointData(0,1,0)=3.0;
274 pointData(1,1,0)=4.0;
275 pointData(2,1,0)=5.0;
276 pointData(0,2,0)=6.0;
277 pointData(1,2,0)=7.0;
278 pointData(2,2,0)=8.0;
279
280 pointData(0,0,1)=9.0;
281 pointData(1,0,1)=10.0;
282 pointData(2,0,1)=11.0;
283 pointData(0,1,1)=12.0;
284 pointData(1,1,1)=13.0;
285 pointData(2,1,1)=14.0;
286 pointData(0,2,1)=15.0;
287 pointData(1,2,1)=16.0;
288 pointData(2,2,1)=17.0;
289
290 pointData(0,0,2)=18.0;
291 pointData(1,0,2)=19.0;
292 pointData(2,0,2)=20.0;
293 pointData(0,1,2)=21.0;
294 pointData(1,1,2)=22.0;
295 pointData(2,1,2)=23.0;
296 pointData(0,2,2)=24.0;
297 pointData(1,2,2)=25.0;
298 pointData(2,2,2)=26.0;
299
300 //
301 // Create object to test
302 cout << "\tCreate rank 3 DataExpanded object." << endl;
303 DataExpanded testData(pointData,FunctionSpace());
304
305 cout << "\tTest slicing (part object)." << endl;
306 DataArrayView::RegionType region;
307 region.push_back(DataArrayView::RegionType::value_type(0,2));
308 region.push_back(DataArrayView::RegionType::value_type(0,2));
309 region.push_back(DataArrayView::RegionType::value_type(0,2));
310 DataAbstract* testData2=testData.getSlice(region);
311
312 //
313 // Verify data values
314 cout << "\tVerify data point values." << endl;
315 for (int k=0;k<testData2->getPointDataView().getShape()[2];k++) {
316 for (int j=0;j<testData2->getPointDataView().getShape()[1];j++) {
317 for (int i=0;i<testData2->getPointDataView().getShape()[0];i++) {
318 assert(testData2->getPointDataView()(i,j,k)==pointData(region[0].first+i,
319 region[1].first+j,
320 region[2].first+k));
321 }
322 }
323 }
324
325 cout << "\tVerify data point attributes." << endl;
326 DataArrayView dataView=testData2->getPointDataView();
327 assert(dataView.getRank()==region.size());
328 assert(dataView.noValues()==(region[0].second-region[0].first)
329 *(region[1].second-region[1].first)
330 *(region[2].second-region[2].first));
331 assert(dataView.getShape()[0]==(region[0].second-region[0].first));
332 assert(dataView.getShape()[1]==(region[1].second-region[1].first));
333 assert(dataView.getShape()[2]==(region[2].second-region[2].first));
334
335 cout << "\tTest slicing (part object)." << endl;
336 DataArrayView::RegionType region2;
337 region2.push_back(DataArrayView::RegionType::value_type(1,3));
338 region2.push_back(DataArrayView::RegionType::value_type(1,3));
339 region2.push_back(DataArrayView::RegionType::value_type(1,3));
340 DataAbstract* testData3=testData.getSlice(region2);
341
342 //
343 // Verify data values
344 cout << "\tVerify data point values." << endl;
345 for (int k=0;k<testData3->getPointDataView().getShape()[2];k++) {
346 for (int j=0;j<testData3->getPointDataView().getShape()[1];j++) {
347 for (int i=0;i<testData3->getPointDataView().getShape()[0];i++) {
348 assert(testData3->getPointDataView()(i,j,k)==pointData(region2[0].first+i,
349 region2[1].first+j,
350 region2[2].first+k));
351 }
352 }
353 }
354
355 cout << "\tVerify data point attributes." << endl;
356 dataView=testData2->getPointDataView();
357 assert(dataView.getRank()==region.size());
358 assert(dataView.noValues()==(region[0].second-region[0].first)
359 *(region[1].second-region[1].first)
360 *(region[2].second-region[2].first));
361 assert(dataView.getShape()[0]==(region[0].second-region[0].first));
362 assert(dataView.getShape()[1]==(region[1].second-region[1].first));
363 assert(dataView.getShape()[2]==(region[2].second-region[2].first));
364
365 delete testData2;
366 delete testData3;
367
368 }
369
370
371 void DataExpandedTestCase::testSliceSetting() {
372
373 cout << endl;
374
375 //
376 // Create a rank 2 pointData
377 DataArrayView::ShapeType shape;
378 shape.push_back(2);
379 shape.push_back(2);
380 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
381 DataArrayView pointData(data,shape);
382
383 //
384 // Assign an arbitrary value
385 pointData(0,0)=0.0;
386 pointData(0,1)=1.0;
387 pointData(1,0)=2.0;
388 pointData(1,1)=3.0;
389
390 //
391 // Create object to test
392 cout << "\tCreate rank 2 DataExpanded object." << endl;
393 DataExpanded testData(pointData,FunctionSpace());
394
395 //
396 // Create another rank 2 pointData
397 DataArrayView::ShapeType shape2;
398 shape2.push_back(3);
399 shape2.push_back(3);
400 DataArrayView::ValueType data2(DataArrayView::noValues(shape2),0);
401 DataArrayView pointData2(data2,shape2);
402
403 //
404 // Assign an arbitrary value
405 pointData2(0,0)=0.1;
406 pointData2(0,1)=1.1;
407 pointData2(0,2)=2.1;
408 pointData2(1,0)=3.1;
409 pointData2(1,1)=4.1;
410 pointData2(1,2)=5.1;
411 pointData2(2,0)=6.1;
412 pointData2(2,1)=7.1;
413 pointData2(2,2)=8.1;
414
415 //
416 // Create object to test
417 cout << "\tCreate second rank 2 DataExpanded object." << endl;
418 DataExpanded testData2(pointData2,FunctionSpace());
419
420 cout << "\tTest slice setting (1)." << endl;
421
422 DataArrayView::RegionType region;
423 region.push_back(DataArrayView::RegionType::value_type(0,2));
424 region.push_back(DataArrayView::RegionType::value_type(0,2));
425
426 DataArrayView::RegionType region2;
427 region2.push_back(DataArrayView::RegionType::value_type(1,3));
428 region2.push_back(DataArrayView::RegionType::value_type(1,3));
429
430 DataAbstract* testData3=testData.getSlice(region);
431
432 testData2.setSlice(testData3,region2);
433
434 //
435 // Verify data values
436 cout << "\tVerify data point values." << endl;
437 for (int j=region2[1].first;j<region2[1].second;j++) {
438 for (int i=region2[0].first;i<region2[0].second;i++) {
439 assert(testData2.getPointDataView()(i,j)==pointData(i-(region[0].second-1),j-(region[1].second-1)));
440 }
441 }
442
443 delete testData3;
444
445 }
446
447 void DataExpandedTestCase::testSliceSetting2() {
448
449 cout << endl;
450
451 //
452 // Create a rank 0 pointData
453 DataArrayView::ShapeType shape;
454 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
455 DataArrayView pointData(data,shape);
456
457 //
458 // Assign an arbitrary value
459 pointData()=0.0;
460
461 //
462 // Create object to test
463 cout << "\tCreate rank 0 DataExpanded object." << endl;
464 DataExpanded testData(pointData,FunctionSpace());
465
466 //
467 // Create a rank 2 pointData
468 DataArrayView::ShapeType shape2;
469 shape2.push_back(3);
470 shape2.push_back(3);
471 DataArrayView::ValueType data2(DataArrayView::noValues(shape2),0);
472 DataArrayView pointData2(data2,shape2);
473
474 //
475 // Assign an arbitrary value
476 pointData2(0,0)=0.1;
477 pointData2(0,1)=1.1;
478 pointData2(0,2)=2.1;
479 pointData2(1,0)=3.1;
480 pointData2(1,1)=4.1;
481 pointData2(1,2)=5.1;
482 pointData2(2,0)=6.1;
483 pointData2(2,1)=7.1;
484 pointData2(2,2)=8.1;
485
486 //
487 // Create object to test
488 cout << "\tCreate rank 2 DataExpanded object." << endl;
489 DataExpanded testData2(pointData2,FunctionSpace());
490
491 cout << "\tTest slice setting (1)." << endl;
492
493 DataArrayView::RegionType region;
494
495 DataArrayView::RegionType region2;
496 region2.push_back(DataArrayView::RegionType::value_type(1,1));
497 region2.push_back(DataArrayView::RegionType::value_type(1,1));
498
499 DataAbstract* testData3=testData.getSlice(region);
500
501 testData2.setSlice(testData3,region2);
502
503 //
504 // Verify data values
505 cout << "\tVerify data point values." << endl;
506 for (int j=region2[1].first;j<region2[1].second;j++) {
507 for (int i=region2[0].first;i<region2[0].second;i++) {
508 assert(testData2.getPointDataView()(i,j)==pointData());
509 }
510 }
511
512 delete testData3;
513
514 }
515
516
517 TestSuite* DataExpandedTestCase::suite ()
518 {
519 //
520 // Create the suite of tests to perform.
521 TestSuite *testSuite = new TestSuite ("DataExpandedTestCase");
522 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testAll",&DataExpandedTestCase::testAll));
523 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSlicing",&DataExpandedTestCase::testSlicing));
524 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSlicing2",&DataExpandedTestCase::testSlicing2));
525 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSlicing3",&DataExpandedTestCase::testSlicing3));
526 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSliceSetting",&DataExpandedTestCase::testSliceSetting));
527 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSliceSetting2",&DataExpandedTestCase::testSliceSetting2));
528 return testSuite;
529 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26