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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2182 - (show annotations)
Thu Dec 18 03:30:17 2008 UTC (10 years, 10 months ago) by caltinay
File size: 41972 byte(s)
Fixed some more typos

1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2008 by University of Queensland
5 * Earth Systems Science Computational Center (ESSCC)
6 * http://www.uq.edu.au/esscc
7 *
8 * Primary Business: Queensland, Australia
9 * Licensed under the Open Software License version 3.0
10 * http://www.opensource.org/licenses/osl-3.0.php
11 *
12 *******************************************************/
13
14
15 #include "escript/DataAlgorithm.h"
16 #include "escript/DataVector.h"
17 #include "esysUtils/EsysException.h"
18
19 #include "DataTypesTestCase.h"
20 #include "escript/DataTypes.h"
21
22 #include <iostream>
23
24 using namespace CppUnitTest;
25 using namespace esysUtils;
26 using namespace escript;
27 using namespace escript::DataTypes;
28 using namespace std;
29
30 void DataTypesTestCase::setUp() {
31 //
32 // This is called before each test is run
33
34 }
35
36 void DataTypesTestCase::tearDown() {
37 //
38 // This is called after each test has been run
39
40 }
41
42
43 void DataTypesTestCase::testShapeFns() {
44 cout << "\n\tTest Shape functions." << endl;
45 ShapeType shape;
46 shape.push_back(2);
47 ShapeType s1=shape;
48 assert(checkShape(s1,shape));
49 shape.push_back(3);
50 ShapeType s2=shape;
51 assert(checkShape(s2,shape));
52 shape.push_back(4);
53 ShapeType s3=shape;
54 assert(checkShape(s3,shape));
55 ShapeType s4=shape;
56 s4.push_back(5);
57
58 cout << "\t\tNumber of Values." << endl;
59 assert(noValues(shape)==24);
60 assert(noValues(scalarShape)==1);
61 cout << "\t\tGet Rank." << endl;
62 assert(getRank(scalarShape)==0);
63 assert(getRank(shape)==3);
64
65
66 cout << "\t\tNumber of Values for RegionLoopRangeType." << endl;
67 RegionLoopRangeType rlr;
68 assert(noValues(rlr)==1);
69 rlr.push_back(std::pair<int,int>(0,2));
70 assert(noValues(rlr)==2);
71 rlr.push_back(std::pair<int,int>(1,4));
72 assert(noValues(rlr)==6);
73 rlr.push_back(std::pair<int,int>(2,7));
74 assert(noValues(rlr)==30);
75 cout << "\t\tRelative index methods.\n";
76 assert(getRelIndex(s1,1)==1);
77 assert(getRelIndex(s2,1,2)==5);
78 assert(getRelIndex(shape,1,1,1)==9);
79 assert(getRelIndex(s4,2,1,1,1)==34);
80 cout << "\t\tCreateShapeErrorMessage.\n";
81 assert(createShapeErrorMessage("prefix",s1,s2)==string("prefix This shape: (2,3) Other shape: (2)"));
82
83 cout << "\t\tgetSliceRegionLoopRange." << endl;
84 RegionType r;
85 r.push_back(std::pair<int,int>(1,1));
86 r.push_back(std::pair<int,int>(0,3));
87 r.push_back(std::pair<int,int>(0,3));
88 RegionLoopRangeType rl;
89 rl.push_back(std::pair<int,int>(1,2));
90 rl.push_back(std::pair<int,int>(0,3));
91 rl.push_back(std::pair<int,int>(0,3));
92 RegionLoopRangeType rt=getSliceRegionLoopRange(r);
93 assert(rt==rl);
94
95
96 #ifdef DOASSERT
97 // The errors we are testing for are triggered by ESysAssert which is only defined when DOASSERT is.
98
99 cout << "\t\tInvalid index.(too many)" << endl;
100 // test too many indices
101 try
102 {
103 getRelIndex(s1,1,1);
104 assert(false);
105 } catch (EsysException&) {}
106 try
107 {
108 getRelIndex(s2,1,1,1);
109 assert(false);
110 } catch (EsysException&) {}
111 try
112 {
113 getRelIndex(s3,1,1,1,1);
114 assert(false);
115 } catch (EsysException&) {}
116 // too few inidicies
117 cout << "\t\tInvalid index.(too few)" << endl;
118 try
119 {
120 getRelIndex(s2,1);
121 assert(false);
122 } catch (EsysException&) {}
123 try
124 {
125 getRelIndex(s3,1,1);
126 assert(false);
127 } catch (EsysException&) {}
128 // inidicies too large
129 cout << "\t\tInvalid index.(too large for shape)" << endl;
130 try
131 {
132 getRelIndex(s1,10);
133 assert(false);
134 } catch (EsysException&) {}
135 try
136 {
137 getRelIndex(s3,2,4,4);
138 assert(false);
139 } catch (EsysException&) {}
140
141 #endif
142 }
143
144 void DataTypesTestCase::testResultSliceShape() {
145
146 cout << endl;
147 cout << "\tTest getResultSliceShape method." << endl;
148
149 DataTypes::RegionType region;
150 DataTypes::ShapeType resultShape;
151
152 region.push_back(DataTypes::RegionType::value_type(1,5));
153 resultShape.push_back(4);
154 assert(DataTypes::getResultSliceShape(region)==resultShape);
155
156 region.push_back(DataTypes::RegionType::value_type(2,5));
157 resultShape.push_back(3);
158 assert(DataTypes::getResultSliceShape(region)==resultShape);
159
160 region.push_back(DataTypes::RegionType::value_type(3,9));
161 resultShape.push_back(6);
162 assert(DataTypes::getResultSliceShape(region)==resultShape);
163
164 region.push_back(DataTypes::RegionType::value_type(1,7));
165 resultShape.push_back(6);
166 assert(DataTypes::getResultSliceShape(region)==resultShape);
167
168 }
169
170 void DataTypesTestCase::testSlicing() {
171
172 using namespace DataTypes;
173 {
174
175 cout << endl;
176 cout << "\tSlice a scalar to a scalar.";
177
178 // Define slice region.
179 DataTypes::RegionType region;
180
181 // Define shape of views.
182 DataTypes::ShapeType sourceShape;
183
184 // Create source and target views.
185 int len = DataTypes::noValues(sourceShape);
186 DataVector sourceData(len, 2.0, len);
187 // DataArrayView sourceView(sourceData, sourceShape);
188 DataVector targetData(1, 2.0, 1);
189 // DataArrayView targetView(targetData, DataTypes::ShapeType());
190
191 // Copy source view to target view.
192 // targetView.copySlice(sourceView,region);
193
194
195 DataTypes::copySlice(targetData, DataTypes::scalarShape, 0, sourceData, sourceShape, 0,region);
196
197 // Check results of copy.
198 // assert(sourceView==targetView);
199 assert(targetData==sourceData);
200 }
201
202 {
203 cout << endl;
204 cout << "\tSlice a scalar from a scalar.";
205
206 // Define slice region.
207 DataTypes::RegionType region;
208
209 // Define shape of views.
210 DataTypes::ShapeType sourceShape;
211
212 // Create source and target views.
213 int len = DataTypes::noValues(sourceShape);
214 DataVector sourceData(len, 2.0, len);
215 // DataArrayView sourceView(sourceData, sourceShape);
216 DataVector targetData(1, 2.0, 1);
217 // DataArrayView targetView(targetData, DataTypes::ShapeType());
218
219 // Copy source view to target view.
220 DataTypes::copySliceFrom(targetData, DataTypes::scalarShape, 0, sourceData, sourceShape, 0,region);
221
222 // Check results of copy.
223 // assert(sourceView==targetView);
224 assert(sourceData==targetData);
225 }
226
227 {
228 cout << endl;
229 cout << "\tSlice a 1 dimensional slice to a 1 dimensional array.";
230
231 // Define slice region.
232 DataTypes::RegionType region;
233 region.push_back(DataTypes::RegionType::value_type(2,4));
234
235 // Define shapes of views.
236 DataTypes::ShapeType sourceShape;
237 sourceShape.push_back(6);
238 DataTypes::ShapeType targetShape = DataTypes::getResultSliceShape(region);
239
240 // Create source and target views.
241 int len = DataTypes::noValues(sourceShape);
242 DataVector sourceData(len, 2.0, len);
243 //DataArrayView sourceView(sourceData, sourceShape);
244 for (int i=0;i<sourceShape[0];i++) {
245 sourceData[i]=i;
246 }
247
248 len = DataTypes::noValues(targetShape);
249 DataVector targetData(len, 2.0, len);
250 //DataArrayView targetView(targetData, targetShape);
251
252 // Copy source view to target view.
253 // targetView.copySlice(sourceView,region);
254 DataTypes::copySlice(targetData, targetShape,0,sourceData, sourceShape,0,region);
255
256 // Check results of copy.
257 for (int i=region[0].first;i<region[0].second;i++) {
258 // assert(sourceView(i)==
259 // targetView(i-region[0].first));
260 assert(sourceData[i]==targetData[i-region[0].first]);
261 }
262 }
263
264 {
265 cout << endl;
266 cout << "\tSlice a 1 dimensional slice to a scalar.";
267
268 // Define slice region.
269 DataTypes::RegionType region;
270 region.push_back(DataTypes::RegionType::value_type(2,3));
271
272 // Define shapes of views.
273 DataTypes::ShapeType sourceShape;
274 sourceShape.push_back(6);
275 DataTypes::ShapeType targetShape;
276
277 // Create source and target views.
278 int len = DataTypes::noValues(sourceShape);
279 DataVector sourceData(len, 2.0, len);
280 // DataArrayView sourceView(sourceData, sourceShape);
281
282 for (int i=0;i<sourceShape[0];i++) {
283 sourceData[i]=i;
284 }
285
286 len = DataTypes::noValues(targetShape);
287 DataVector targetData(len, 2.0, len);
288 // DataArrayView targetView(targetData, targetShape);
289
290 // Copy source view to target view.
291 // targetView.copySlice(sourceView,region);
292 DataTypes::copySlice(targetData, targetShape, 0, sourceData, sourceShape,0,region);
293
294 // Check results of copy.
295 for (int i=region[0].first;i<region[0].second;i++) {
296 /* assert(sourceView(i)==
297 targetView());*/
298 assert(sourceData[i]==
299 targetData[0]);
300
301 }
302 }
303
304 {
305 cout << endl;
306 cout << "\tSlice a 1 dimensional slice from a 1 dimensional array.";
307
308 // Define slice region.
309 DataTypes::RegionType region;
310 region.push_back(DataTypes::RegionType::value_type(2,4));
311
312 // Define shapes of views.
313 DataTypes::ShapeType sourceShape = DataTypes::getResultSliceShape(region);
314 DataTypes::ShapeType targetShape;
315 targetShape.push_back(6);
316
317 // Create source and target views.
318 int len = DataTypes::noValues(sourceShape);
319 DataVector sourceData(len, 2.0, len);
320 // DataArrayView sourceView(sourceData, sourceShape);
321 for (int i=0;i<sourceShape[0];i++) {
322 sourceData[i]=i;
323 }
324
325 len = DataTypes::noValues(targetShape);
326 DataVector targetData(len, 2.0, len);
327 // DataArrayView targetView(targetData, targetShape);
328
329 // Copy source view to target view.
330 // targetView.copySliceFrom(sourceView,region);
331 DataTypes::copySliceFrom(targetData,targetShape,0,sourceData, sourceShape, 0,region);
332
333 // Check results of copy.
334 for (int i=region[0].first;i<region[0].second;i++) {
335 /* assert(sourceView(i-region[0].first)==
336 targetView(i));*/
337 assert(sourceData[i-region[0].first]==
338 targetData[i]);
339 }
340 }
341
342 {
343 cout << endl;
344 cout << "\tSlice a 1 dimensional slice from a scalar.";
345
346 // Define slice region.
347 DataTypes::RegionType region;
348 region.push_back(DataTypes::RegionType::value_type(2,4));
349
350 // Define shapes of views.
351 DataTypes::ShapeType sourceShape;
352 DataTypes::ShapeType targetShape;
353 targetShape.push_back(6);
354
355 // Create source and target views.
356 int len = DataTypes::noValues(sourceShape);
357 DataVector sourceData(len, 2.0, len);
358 // DataArrayView sourceView(sourceData, sourceShape);
359 sourceData[0]=5;
360
361 len = DataTypes::noValues(targetShape);
362 DataVector targetData(len, 2.0, len);
363 // DataArrayView targetView(targetData, targetShape);
364
365 // Copy source view to target view.
366 // targetView.copySliceFrom(sourceView,region);
367 DataTypes::copySliceFrom(targetData,targetShape,0,sourceData, sourceShape,0,region);
368
369 // Check results of copy.
370 for (int i=region[0].first;i<region[0].second;i++) {
371 assert(sourceData[0]==
372 targetData[i]);
373 }
374 }
375
376 {
377 cout << endl;
378 cout << "\tSlice a 2 dimensional slice to a 2 dimensional array.";
379
380 // Define slice region.
381 DataTypes::RegionType region;
382 region.push_back(DataTypes::RegionType::value_type(2,4));
383 region.push_back(DataTypes::RegionType::value_type(0,2));
384
385 // Define shapes of views.
386 DataTypes::ShapeType sourceShape;
387 sourceShape.push_back(6);
388 sourceShape.push_back(3);
389 DataTypes::ShapeType targetShape = DataTypes::getResultSliceShape(region);
390
391 // Create source and target views.
392 int len = DataTypes::noValues(sourceShape);
393 DataVector sourceData(len, 2.0, len);
394 // DataArrayView sourceView(sourceData, sourceShape);
395 int val=0;
396 for (int i=0;i<sourceShape[0];i++) {
397 for (int j=0;j<sourceShape[1];j++) {
398 sourceData[getRelIndex(sourceShape,i,j)]=val++;
399 }
400 }
401
402 len = DataTypes::noValues(targetShape);
403 DataVector targetData(len, 2.0, len);
404 // DataArrayView targetView(targetData, targetShape);
405
406 // Copy source view to target view.
407 copySlice(targetData, targetShape,0,sourceData, sourceShape,0,region);
408
409 // Check results of copy.
410 for (int i=region[0].first;i<region[0].second;i++) {
411 for (int j=region[1].first;j<region[1].second;j++) {
412 assert(sourceData[getRelIndex(sourceShape,i,j)]==
413 targetData[getRelIndex(targetShape,i-region[0].first,j-region[1].first)]);
414 }
415 }
416 }
417
418 {
419 cout << endl;
420 cout << "\tSlice a 2 dimensional slice to a 1 dimensional array.";
421
422 // Define slice region.
423 DataTypes::RegionType region;
424 region.push_back(DataTypes::RegionType::value_type(0,3));
425 region.push_back(DataTypes::RegionType::value_type(1,2));
426
427 // Define shapes of views.
428 DataTypes::ShapeType sourceShape;
429 sourceShape.push_back(3);
430 sourceShape.push_back(6);
431 DataTypes::ShapeType targetShape;
432 targetShape.push_back(3);
433
434 // Create source and target views.
435 int len = DataTypes::noValues(sourceShape);
436 DataVector sourceData(len, 2.0, len);
437 // DataArrayView sourceView(sourceData, sourceShape);
438 int val=0;
439 for (int i=0;i<sourceShape[0];i++) {
440 for (int j=0;j<sourceShape[1];j++) {
441 sourceData[getRelIndex(sourceShape,i,j)]=val++;
442 }
443 }
444
445 len = DataTypes::noValues(targetShape);
446 DataVector targetData(len, 2.0, len);
447 // DataArrayView targetView(targetData, targetShape);
448
449 // Copy source view to target view.
450 // targetView.copySlice(sourceView,region);
451 copySlice(targetData, targetShape,0,sourceData,sourceShape,0,region);
452
453 // Check results of copy.
454 for (int i=region[0].first;i<region[0].second;i++) {
455 for (int j=region[1].first;j<region[1].second;j++) {
456 assert(sourceData[getRelIndex(sourceShape,i,j)]==
457 targetData[getRelIndex(targetShape,i-region[0].first)]);
458 }
459 }
460 }
461
462 {
463 cout << endl;
464 cout << "\tSlice a 2 dimensional slice to a scalar.";
465
466 // Define slice region.
467 DataTypes::RegionType region;
468 region.push_back(DataTypes::RegionType::value_type(2,3));
469 region.push_back(DataTypes::RegionType::value_type(1,2));
470
471 // Define shapes of views.
472 DataTypes::ShapeType sourceShape;
473 sourceShape.push_back(3);
474 sourceShape.push_back(6);
475 DataTypes::ShapeType targetShape;
476
477 // Create source and target views.
478 int len = DataTypes::noValues(sourceShape);
479 DataVector sourceData(len, 2.0, len);
480 // DataArrayView sourceView(sourceData, sourceShape);
481 int val=0;
482 for (int i=0;i<sourceShape[0];i++) {
483 for (int j=0;j<sourceShape[1];j++) {
484 sourceData[getRelIndex(sourceShape,i,j)]=val++;
485 }
486 }
487
488 len = DataTypes::noValues(targetShape);
489 DataVector targetData(len, 2.0, len);
490 // DataArrayView targetView(targetData, targetShape);
491
492 // Copy source view to target view.
493 // targetView.copySlice(sourceView,region);
494 copySlice(targetData, targetShape,0,sourceData, sourceShape,0,region);
495
496
497 // Check results of copy.
498 for (int i=region[0].first;i<region[0].second;i++) {
499 for (int j=region[1].first;j<region[1].second;j++) {
500 /* assert(sourceView(i,j)==
501 targetView());*/
502 assert(sourceData[getRelIndex(sourceShape,i,j)]==
503 targetData[0]);
504 }
505 }
506 }
507
508 {
509 cout << endl;
510 cout << "\tSlice a 2 dimensional slice from a 2 dimensional array.";
511
512 // Define slice region.
513 DataTypes::RegionType region;
514 region.push_back(DataTypes::RegionType::value_type(2,4));
515 region.push_back(DataTypes::RegionType::value_type(0,2));
516
517 // Define shapes of views.
518 DataTypes::ShapeType sourceShape = DataTypes::getResultSliceShape(region);
519 DataTypes::ShapeType targetShape;
520 targetShape.push_back(6);
521 targetShape.push_back(3);
522
523 // Create source and target views.
524 int len = DataTypes::noValues(sourceShape);
525 DataVector sourceData(len, 2.0, len);
526 // DataArrayView sourceView(sourceData, sourceShape);
527 int val=0;
528 for (int i=0;i<sourceShape[0];i++) {
529 for (int j=0;j<sourceShape[1];j++) {
530 sourceData[getRelIndex(sourceShape,i,j)]=val++;
531 }
532 }
533
534 len = DataTypes::noValues(targetShape);
535 DataVector targetData(len, 2.0, len);
536 // DataArrayView targetView(targetData, targetShape);
537
538 // Copy source view to target view.
539 // targetView.copySliceFrom(sourceView,region);
540 copySliceFrom(targetData,targetShape,0,sourceData, sourceShape,0,region);
541
542 // Check results of copy.
543 for (int i=region[0].first;i<region[0].second;i++) {
544 for (int j=region[1].first;j<region[1].second;j++) {
545 // assert(sourceView(i-region[0].first,j-region[1].first)==
546 // targetView(i,j));
547 assert(sourceData[getRelIndex(sourceShape,i-region[0].first,j-region[1].first)]==
548 targetData[getRelIndex(targetShape,i,j)]);
549 }
550 }
551 }
552
553 {
554 cout << endl;
555 cout << "\tSlice a 2 dimensional slice from a scalar.";
556
557 // Define slice region.
558 DataTypes::RegionType region;
559 region.push_back(DataTypes::RegionType::value_type(2,4));
560 region.push_back(DataTypes::RegionType::value_type(0,2));
561
562 // Define shapes of views.
563 DataTypes::ShapeType sourceShape;
564 DataTypes::ShapeType targetShape;
565 targetShape.push_back(6);
566 targetShape.push_back(3);
567
568 // Create source and target views.
569 int len = DataTypes::noValues(sourceShape);
570 DataVector sourceData(len, 2.0, len);
571 // DataArrayView sourceView(sourceData, sourceShape);
572 sourceData[0]=5;
573
574 len = DataTypes::noValues(targetShape);
575 DataVector targetData(len, 2.0, len);
576 // DataArrayView targetView(targetData, targetShape);
577
578 // Copy source view to target view.
579 // targetView.copySliceFrom(sourceView,region);
580 copySliceFrom(targetData,targetShape,0,sourceData,sourceShape,0,region);
581
582 // Check results of copy.
583 for (int i=region[0].first;i<region[0].second;i++) {
584 for (int j=region[1].first;j<region[1].second;j++) {
585 assert(sourceData[0]==
586 targetData[getRelIndex(targetShape,i,j)]);
587 }
588 }
589 }
590
591 {
592 cout << endl;
593 cout << "\tSlice a 3 dimensional slice to a 3 dimensional array.";
594
595 // Define slice region.
596 DataTypes::RegionType region;
597 region.push_back(DataTypes::RegionType::value_type(2,4));
598 region.push_back(DataTypes::RegionType::value_type(0,2));
599 region.push_back(DataTypes::RegionType::value_type(5,9));
600
601 // Define shapes of views.
602 DataTypes::ShapeType sourceShape;
603 sourceShape.push_back(6);
604 sourceShape.push_back(3);
605 sourceShape.push_back(13);
606 DataTypes::ShapeType targetShape = DataTypes::getResultSliceShape(region);
607
608 // Create source and target views.
609 int len = DataTypes::noValues(sourceShape);
610 DataVector sourceData(len, 2.0, len);
611 // DataArrayView sourceView(sourceData, sourceShape);
612 int val=0;
613 for (int i=0;i<sourceShape[0];i++) {
614 for (int j=0;j<sourceShape[1];j++) {
615 for (int k=0;k<sourceShape[2];k++) {
616 sourceData[getRelIndex(sourceShape,i,j,k)]=val++;
617 }
618 }
619 }
620
621 len = DataTypes::noValues(targetShape);
622 DataVector targetData(len, 2.0, len);
623 // DataArrayView targetView(targetData, targetShape);
624
625 // Copy source view to target view.
626 // targetView.copySlice(sourceView,region);
627 copySlice(targetData,targetShape,0,sourceData,sourceShape,0,region);
628
629 // Check results of copy.
630 for (int i=region[0].first;i<region[0].second;i++) {
631 for (int j=region[1].first;j<region[1].second;j++) {
632 for (int k=region[2].first;k<region[2].second;k++) {
633 // assert(sourceView(i,j,k)==
634 // targetView(i-region[0].first,j-region[1].first,k-region[2].first));
635 assert(sourceData[getRelIndex(sourceShape,i,j,k)]==
636 targetData[getRelIndex(targetShape,i-region[0].first,j-region[1].first,k-region[2].first)]);
637 }
638 }
639 }
640 }
641
642 {
643 cout << endl;
644 cout << "\tSlice a 3 dimensional slice to a 2 dimensional array.";
645
646 // Define slice region.
647 DataTypes::RegionType region;
648 region.push_back(DataTypes::RegionType::value_type(2,4));
649 region.push_back(DataTypes::RegionType::value_type(0,1));
650 region.push_back(DataTypes::RegionType::value_type(5,9));
651
652 // Define shapes of views.
653 DataTypes::ShapeType sourceShape;
654 sourceShape.push_back(6);
655 sourceShape.push_back(3);
656 sourceShape.push_back(13);
657 DataTypes::ShapeType targetShape;
658 targetShape.push_back(2);
659 targetShape.push_back(4);
660
661 // Create source and target views.
662 int len = DataTypes::noValues(sourceShape);
663 DataVector sourceData(len, 2.0, len);
664 // DataArrayView sourceView(sourceData, sourceShape);
665 int val=0;
666 for (int i=0;i<sourceShape[0];i++) {
667 for (int j=0;j<sourceShape[1];j++) {
668 for (int k=0;k<sourceShape[2];k++) {
669 sourceData[getRelIndex(sourceShape,i,j,k)]=val++;
670 }
671 }
672 }
673
674 len = DataTypes::noValues(targetShape);
675 DataVector targetData(len, 2.0, len);
676 // DataArrayView targetView(targetData, targetShape);
677
678 // Copy source view to target view.
679 // targetView.copySlice(sourceView,region);
680 copySlice(targetData, targetShape,0,sourceData,sourceShape,0,region);
681
682 // Check results of copy.
683 for (int i=region[0].first;i<region[0].second;i++) {
684 for (int j=region[1].first;j<region[1].second;j++) {
685 for (int k=region[2].first;k<region[2].second;k++) {
686 /* assert(sourceView(i,j,k)==
687 targetView(i-region[0].first,k-region[2].first));*/
688 assert(sourceData[getRelIndex(sourceShape,i,j,k)]==
689 targetData[getRelIndex(targetShape,i-region[0].first,k-region[2].first)]);
690 }
691 }
692 }
693 }
694
695 {
696 cout << endl;
697 cout << "\tSlice a 3 dimensional slice to a 1 dimensional array.";
698
699 // Define slice region.
700 DataTypes::RegionType region;
701 region.push_back(DataTypes::RegionType::value_type(3,4));
702 region.push_back(DataTypes::RegionType::value_type(0,1));
703 region.push_back(DataTypes::RegionType::value_type(5,9));
704
705 // Define shapes of views.
706 DataTypes::ShapeType sourceShape;
707 sourceShape.push_back(6);
708 sourceShape.push_back(3);
709 sourceShape.push_back(13);
710 DataTypes::ShapeType targetShape;
711 targetShape.push_back(4);
712
713 // Create source and target views.
714 int len = DataTypes::noValues(sourceShape);
715 DataVector sourceData(len, 2.0, len);
716 // DataArrayView sourceView(sourceData, sourceShape);
717 int val=0;
718 for (int i=0;i<sourceShape[0];i++) {
719 for (int j=0;j<sourceShape[1];j++) {
720 for (int k=0;k<sourceShape[2];k++) {
721 sourceData[getRelIndex(sourceShape,i,j,k)]=val++;
722 }
723 }
724 }
725
726 len = DataTypes::noValues(targetShape);
727 DataVector targetData(len, 2.0, len);
728 // DataArrayView targetView(targetData, targetShape);
729
730 // Copy source view to target view.
731 // targetView.copySlice(sourceView,region);
732 copySlice(targetData, targetShape,0,sourceData, sourceShape, 0, region);
733
734 // Check results of copy.
735 for (int i=region[0].first;i<region[0].second;i++) {
736 for (int j=region[1].first;j<region[1].second;j++) {
737 for (int k=region[2].first;k<region[2].second;k++) {
738 assert(sourceData[getRelIndex(sourceShape,i,j,k)]==
739 targetData[getRelIndex(targetShape,k-region[2].first)]);
740 }
741 }
742 }
743 }
744
745 {
746 cout << endl;
747 cout << "\tSlice a 3 dimensional slice to a scalar.";
748
749 // Define slice region.
750 DataTypes::RegionType region;
751 region.push_back(DataTypes::RegionType::value_type(3,4));
752 region.push_back(DataTypes::RegionType::value_type(0,1));
753 region.push_back(DataTypes::RegionType::value_type(5,6));
754
755 // Define shapes of views.
756 DataTypes::ShapeType sourceShape;
757 sourceShape.push_back(6);
758 sourceShape.push_back(3);
759 sourceShape.push_back(13);
760 DataTypes::ShapeType targetShape;
761
762 // Create source and target views.
763 int len = DataTypes::noValues(sourceShape);
764 DataVector sourceData(len, 2.0, len);
765 // DataArrayView sourceView(sourceData, sourceShape);
766 int val=0;
767 for (int i=0;i<sourceShape[0];i++) {
768 for (int j=0;j<sourceShape[1];j++) {
769 for (int k=0;k<sourceShape[2];k++) {
770 sourceData[getRelIndex(sourceShape,i,j,k)]=val++;
771 }
772 }
773 }
774
775 len = DataTypes::noValues(targetShape);
776 DataVector targetData(len, 2.0, len);
777 // DataArrayView targetView(targetData, targetShape);
778
779 // Copy source view to target view.
780 // targetView.copySlice(sourceView,region);
781 copySlice(targetData, targetShape,0,sourceData, sourceShape, 0, region);
782
783 // Check results of copy.
784 for (int i=region[0].first;i<region[0].second;i++) {
785 for (int j=region[1].first;j<region[1].second;j++) {
786 for (int k=region[2].first;k<region[2].second;k++) {
787 assert(sourceData[getRelIndex(sourceShape, i,j,k)]==
788 targetData[0]);
789 }
790 }
791 }
792 }
793
794 {
795 cout << endl;
796 cout << "\tSlice a 3 dimensional slice from a 3 dimensional array.";
797
798 // Define slice region.
799 DataTypes::RegionType region;
800 region.push_back(DataTypes::RegionType::value_type(4,7));
801 region.push_back(DataTypes::RegionType::value_type(2,5));
802 region.push_back(DataTypes::RegionType::value_type(6,7));
803
804 // Define shapes of views.
805 DataTypes::ShapeType sourceShape = DataTypes::getResultSliceShape(region);
806 DataTypes::ShapeType targetShape;
807 targetShape.push_back(11);
808 targetShape.push_back(8);
809 targetShape.push_back(9);
810
811 // Create source and target views.
812 int len = DataTypes::noValues(sourceShape);
813 DataVector sourceData(len, 2.0, len);
814 // DataArrayView sourceView(sourceData, sourceShape);
815 int val=0;
816 for (int i=0;i<sourceShape[0];i++) {
817 for (int j=0;j<sourceShape[1];j++) {
818 for (int k=0;k<sourceShape[2];k++) {
819 sourceData[getRelIndex(sourceShape,i,j,k)]=val++;
820 }
821 }
822 }
823
824 len = DataTypes::noValues(targetShape);
825 DataVector targetData(len, 2.0, len);
826 // DataArrayView targetView(targetData, targetShape);
827
828 // Copy source view to target view.
829 // targetView.copySliceFrom(sourceView,region);
830 copySliceFrom(targetData, targetShape,0,sourceData,sourceShape,0, region);
831
832 // Check results of copy.
833 for (int i=region[0].first;i<region[0].second;i++) {
834 for (int j=region[1].first;j<region[1].second;j++) {
835 for (int k=region[2].first;k<region[2].second;k++) {
836 assert(sourceData[getRelIndex(sourceShape,i-region[0].first,j-region[1].first,k-region[2].first)]==
837 targetData[getRelIndex(targetShape,i,j,k)]);
838 }
839 }
840 }
841 }
842
843 {
844 cout << endl;
845 cout << "\tSlice a 3 dimensional slice from a scalar.";
846
847 // Define slice region.
848 DataTypes::RegionType region;
849 region.push_back(DataTypes::RegionType::value_type(4,7));
850 region.push_back(DataTypes::RegionType::value_type(2,5));
851 region.push_back(DataTypes::RegionType::value_type(6,7));
852
853 // Define shapes of views.
854 DataTypes::ShapeType sourceShape;
855 DataTypes::ShapeType targetShape;
856 targetShape.push_back(11);
857 targetShape.push_back(8);
858 targetShape.push_back(9);
859
860 // Create source and target views.
861 int len = DataTypes::noValues(sourceShape);
862 DataVector sourceData(len, 2.0, len);
863 // DataArrayView sourceView(sourceData, sourceShape);
864 sourceData[0]=5;
865
866 len = DataTypes::noValues(targetShape);
867 DataVector targetData(len, 2.0, len);
868
869 // Copy source view to target view.
870 // targetView.copySliceFrom(sourceView,region);
871 copySliceFrom(targetData, targetShape,0,sourceData, sourceShape, 0, region);
872
873 // Check results of copy.
874 for (int i=region[0].first;i<region[0].second;i++) {
875 for (int j=region[1].first;j<region[1].second;j++) {
876 for (int k=region[2].first;k<region[2].second;k++) {
877 assert(sourceData[0]==
878 targetData[getRelIndex(targetShape,i,j,k)]);
879 }
880 }
881 }
882 }
883
884 {
885 cout << endl;
886 cout << "\tSlice a 4 dimensional slice to a 4 dimensional array.";
887
888 // Define slice region.
889 DataTypes::RegionType region;
890 region.push_back(DataTypes::RegionType::value_type(2,4));
891 region.push_back(DataTypes::RegionType::value_type(0,2));
892 region.push_back(DataTypes::RegionType::value_type(5,9));
893 region.push_back(DataTypes::RegionType::value_type(3,5));
894
895 // Define shapes of views.
896 DataTypes::ShapeType sourceShape;
897 sourceShape.push_back(6);
898 sourceShape.push_back(3);
899 sourceShape.push_back(13);
900 sourceShape.push_back(9);
901 DataTypes::ShapeType targetShape = DataTypes::getResultSliceShape(region);
902
903 // Create source and target views.
904 int len = DataTypes::noValues(sourceShape);
905 DataVector sourceData(len, 2.0, len);
906 // DataArrayView sourceView(sourceData, sourceShape);
907 int val=0;
908 for (int i=0;i<sourceShape[0];i++) {
909 for (int j=0;j<sourceShape[1];j++) {
910 for (int k=0;k<sourceShape[2];k++) {
911 for (int l=0;l<sourceShape[3];l++) {
912 sourceData[getRelIndex(sourceShape,i,j,k,l)]=val++;
913 }
914 }
915 }
916 }
917
918 len = DataTypes::noValues(targetShape);
919 DataVector targetData(len, 2.0, len);
920 // DataArrayView targetView(targetData, targetShape);
921
922 // Copy source view to target view.
923 // targetView.copySlice(sourceView,region);
924 copySlice(targetData, targetShape, 0, sourceData, sourceShape,0, region);
925
926 // Check results of copy.
927 for (int i=region[0].first;i<region[0].second;i++) {
928 for (int j=region[1].first;j<region[1].second;j++) {
929 for (int k=region[2].first;k<region[2].second;k++) {
930 for (int l=region[3].first;l<region[3].second;l++) {
931 // assert(sourceView(i,j,k,l)==
932 // targetView(i-region[0].first,j-region[1].first,k-region[2].first,l-region[3].first));
933 assert(sourceData[getRelIndex(sourceShape,i,j,k,l)]==
934 targetData[getRelIndex(targetShape,i-region[0].first,j-region[1].first,k-region[2].first,l-region[3].first)]);
935 }
936 }
937 }
938 }
939 }
940
941 {
942 cout << endl;
943 cout << "\tSlice a 4 dimensional slice to a 3 dimensional array.";
944
945 // Define slice region.
946 DataTypes::RegionType region;
947 region.push_back(DataTypes::RegionType::value_type(2,4));
948 region.push_back(DataTypes::RegionType::value_type(0,2));
949 region.push_back(DataTypes::RegionType::value_type(5,6));
950 region.push_back(DataTypes::RegionType::value_type(3,5));
951
952 // Define shapes of views.
953 DataTypes::ShapeType sourceShape;
954 sourceShape.push_back(6);
955 sourceShape.push_back(3);
956 sourceShape.push_back(13);
957 sourceShape.push_back(9);
958 DataTypes::ShapeType targetShape;
959 targetShape.push_back(2);
960 targetShape.push_back(2);
961 targetShape.push_back(2);
962
963 // Create source and target views.
964 int len = DataTypes::noValues(sourceShape);
965 DataVector sourceData(len, 2.0, len);
966 // DataArrayView sourceView(sourceData, sourceShape);
967 int val=0;
968 for (int i=0;i<sourceShape[0];i++) {
969 for (int j=0;j<sourceShape[1];j++) {
970 for (int k=0;k<sourceShape[2];k++) {
971 for (int l=0;l<sourceShape[3];l++) {
972 sourceData[getRelIndex(sourceShape,i,j,k,l)]=val++;
973 }
974 }
975 }
976 }
977
978 len = DataTypes::noValues(targetShape);
979 DataVector targetData(len, 2.0, len);
980 // DataArrayView targetView(targetData, targetShape);
981
982 // Copy source view to target view.
983 // targetView.copySlice(sourceView,region);
984 copySlice(targetData, targetShape, 0, sourceData, sourceShape, 0, region);
985
986 // Check results of copy.
987 for (int i=region[0].first;i<region[0].second;i++) {
988 for (int j=region[1].first;j<region[1].second;j++) {
989 for (int k=region[2].first;k<region[2].second;k++) {
990 for (int l=region[3].first;l<region[3].second;l++) {
991 assert(sourceData[getRelIndex(sourceShape,i,j,k,l)]==
992 targetData[getRelIndex(targetShape,i-region[0].first,j-region[1].first,l-region[3].first)]);
993 }
994 }
995 }
996 }
997 }
998
999 {
1000 cout << endl;
1001 cout << "\tSlice a 4 dimensional slice to a 2 dimensional array.";
1002
1003 // Define slice region.
1004 DataTypes::RegionType region;
1005 region.push_back(DataTypes::RegionType::value_type(2,4));
1006 region.push_back(DataTypes::RegionType::value_type(0,2));
1007 region.push_back(DataTypes::RegionType::value_type(5,6));
1008 region.push_back(DataTypes::RegionType::value_type(4,5));
1009
1010 // Define shapes of views.
1011 DataTypes::ShapeType sourceShape;
1012 sourceShape.push_back(6);
1013 sourceShape.push_back(3);
1014 sourceShape.push_back(13);
1015 sourceShape.push_back(9);
1016 DataTypes::ShapeType targetShape;
1017 targetShape.push_back(2);
1018 targetShape.push_back(2);
1019
1020 // Create source and target views.
1021 int len = DataTypes::noValues(sourceShape);
1022 DataVector sourceData(len, 2.0, len);
1023 // DataArrayView sourceView(sourceData, sourceShape);
1024 int val=0;
1025 for (int i=0;i<sourceShape[0];i++) {
1026 for (int j=0;j<sourceShape[1];j++) {
1027 for (int k=0;k<sourceShape[2];k++) {
1028 for (int l=0;l<sourceShape[3];l++) {
1029 sourceData[getRelIndex(sourceShape,i,j,k,l)]=val++;
1030 }
1031 }
1032 }
1033 }
1034
1035 len = DataTypes::noValues(targetShape);
1036 DataVector targetData(len, 2.0, len);
1037 // DataArrayView targetView(targetData, targetShape);
1038
1039 // Copy source view to target view.
1040 // targetView.copySlice(sourceView,region);
1041 copySlice(targetData, targetShape, 0, sourceData, sourceShape, 0, region);
1042
1043 // Check results of copy.
1044 for (int i=region[0].first;i<region[0].second;i++) {
1045 for (int j=region[1].first;j<region[1].second;j++) {
1046 for (int k=region[2].first;k<region[2].second;k++) {
1047 for (int l=region[3].first;l<region[3].second;l++) {
1048 assert(sourceData[getRelIndex(sourceShape,i,j,k,l)]==
1049 targetData[getRelIndex(targetShape,i-region[0].first,j-region[1].first)]);
1050 }
1051 }
1052 }
1053 }
1054 }
1055
1056 {
1057 cout << endl;
1058 cout << "\tSlice a 4 dimensional slice to a 1 dimensional array.";
1059
1060 // Define slice region.
1061 DataTypes::RegionType region;
1062 region.push_back(DataTypes::RegionType::value_type(3,4));
1063 region.push_back(DataTypes::RegionType::value_type(0,2));
1064 region.push_back(DataTypes::RegionType::value_type(5,6));
1065 region.push_back(DataTypes::RegionType::value_type(4,5));
1066
1067 // Define shapes of views.
1068 DataTypes::ShapeType sourceShape;
1069 sourceShape.push_back(6);
1070 sourceShape.push_back(3);
1071 sourceShape.push_back(13);
1072 sourceShape.push_back(9);
1073 DataTypes::ShapeType targetShape;
1074 targetShape.push_back(2);
1075
1076 // Create source and target views.
1077 int len = DataTypes::noValues(sourceShape);
1078 DataVector sourceData(len, 2.0, len);
1079 // DataArrayView sourceView(sourceData, sourceShape);
1080 int val=0;
1081 for (int i=0;i<sourceShape[0];i++) {
1082 for (int j=0;j<sourceShape[1];j++) {
1083 for (int k=0;k<sourceShape[2];k++) {
1084 for (int l=0;l<sourceShape[3];l++) {
1085 sourceData[getRelIndex(sourceShape,i,j,k,l)]=val++;
1086 }
1087 }
1088 }
1089 }
1090
1091 len = DataTypes::noValues(targetShape);
1092 DataVector targetData(len, 2.0, len);
1093 // DataArrayView targetView(targetData, targetShape);
1094
1095 // Copy source view to target view.
1096 // targetView.copySlice(sourceView,region);
1097 copySlice(targetData,targetShape,0, sourceData, sourceShape,0,region);
1098
1099 // Check results of copy.
1100 for (int i=region[0].first;i<region[0].second;i++) {
1101 for (int j=region[1].first;j<region[1].second;j++) {
1102 for (int k=region[2].first;k<region[2].second;k++) {
1103 for (int l=region[3].first;l<region[3].second;l++) {
1104 assert(sourceData[getRelIndex(sourceShape,i,j,k,l)]==
1105 targetData[getRelIndex(targetShape,j-region[1].first)]);
1106 }
1107 }
1108 }
1109 }
1110 }
1111
1112 {
1113 cout << endl;
1114 cout << "\tSlice a 4 dimensional slice to a scalar.";
1115
1116 // Define slice region.
1117 DataTypes::RegionType region;
1118 region.push_back(DataTypes::RegionType::value_type(3,4));
1119 region.push_back(DataTypes::RegionType::value_type(1,2));
1120 region.push_back(DataTypes::RegionType::value_type(5,6));
1121 region.push_back(DataTypes::RegionType::value_type(4,5));
1122
1123 // Define shapes of views.
1124 DataTypes::ShapeType sourceShape;
1125 sourceShape.push_back(6);
1126 sourceShape.push_back(3);
1127 sourceShape.push_back(13);
1128 sourceShape.push_back(9);
1129 DataTypes::ShapeType targetShape;
1130
1131 // Create source and target views.
1132 int len = DataTypes::noValues(sourceShape);
1133 DataVector sourceData(len, 2.0, len);
1134 //DataArrayView sourceView(sourceData, sourceShape);
1135 int val=0;
1136 for (int i=0;i<sourceShape[0];i++) {
1137 for (int j=0;j<sourceShape[1];j++) {
1138 for (int k=0;k<sourceShape[2];k++) {
1139 for (int l=0;l<sourceShape[3];l++) {
1140 sourceData[getRelIndex(sourceShape,i,j,k,l)]=val++;
1141 }
1142 }
1143 }
1144 }
1145
1146 len = DataTypes::noValues(targetShape);
1147 DataVector targetData(len, 2.0, len);
1148 // DataArrayView targetView(targetData, targetShape);
1149
1150 // Copy source view to target view.
1151 // targetView.copySlice(sourceView,region);
1152 copySlice(targetData, targetShape, 0, sourceData, sourceShape,0,region);
1153
1154 // Check results of copy.
1155 for (int i=region[0].first;i<region[0].second;i++) {
1156 for (int j=region[1].first;j<region[1].second;j++) {
1157 for (int k=region[2].first;k<region[2].second;k++) {
1158 for (int l=region[3].first;l<region[3].second;l++) {
1159 assert(sourceData[getRelIndex(sourceShape,i,j,k,l)]==
1160 targetData[0]);
1161 }
1162 }
1163 }
1164 }
1165 }
1166
1167 {
1168 cout << endl;
1169 cout << "\tSlice a 4 dimensional slice from a 4 dimensional array.";
1170
1171 // Define slice region.
1172 DataTypes::RegionType region;
1173 region.push_back(DataTypes::RegionType::value_type(14,37));
1174 region.push_back(DataTypes::RegionType::value_type(22,57));
1175 region.push_back(DataTypes::RegionType::value_type(63,71));
1176 region.push_back(DataTypes::RegionType::value_type(23,51));
1177
1178 // Define shapes of views.
1179 DataTypes::ShapeType sourceShape = DataTypes::getResultSliceShape(region);
1180 DataTypes::ShapeType targetShape;
1181 targetShape.push_back(50);
1182 targetShape.push_back(65);
1183 targetShape.push_back(80);
1184 targetShape.push_back(90);
1185
1186 // Create source and target views.
1187 int len = DataTypes::noValues(sourceShape);
1188 DataVector sourceData(len, 2.0, len);
1189 // DataArrayView sourceView(sourceData, sourceShape);
1190 int val=0;
1191 for (int i=0;i<sourceShape[0];i++) {
1192 for (int j=0;j<sourceShape[1];j++) {
1193 for (int k=0;k<sourceShape[2];k++) {
1194 for (int l=0;l<sourceShape[3];l++) {
1195 sourceData[getRelIndex(sourceShape,i,j,k,l)]=val++;
1196 }
1197 }
1198 }
1199 }
1200
1201 len = DataTypes::noValues(targetShape);
1202 DataVector targetData(len, 2.0, len);
1203 // DataArrayView targetView(targetData, targetShape);
1204
1205 // Copy source view to target view.
1206 // targetView.copySliceFrom(sourceView,region);
1207 copySliceFrom(targetData, targetShape, 0, sourceData, sourceShape,0,region);
1208
1209 // Check results of copy.
1210 for (int i=region[0].first;i<region[0].second;i++) {
1211 for (int j=region[1].first;j<region[1].second;j++) {
1212 for (int k=region[2].first;k<region[2].second;k++) {
1213 for (int l=region[3].first;l<region[3].second;l++) {
1214 assert(sourceData[getRelIndex(sourceShape,i-region[0].first,j-region[1].first,k-region[2].first,l-region[3].first)]==
1215 targetData[getRelIndex(targetShape,i,j,k,l)]);
1216 }
1217 }
1218 }
1219 }
1220 }
1221
1222 {
1223 cout << endl;
1224 cout << "\tSlice a 4 dimensional slice from a scalar.";
1225
1226 // Define slice region.
1227 DataTypes::RegionType region;
1228 region.push_back(DataTypes::RegionType::value_type(14,37));
1229 region.push_back(DataTypes::RegionType::value_type(22,57));
1230 region.push_back(DataTypes::RegionType::value_type(63,71));
1231 region.push_back(DataTypes::RegionType::value_type(23,51));
1232
1233 // Define shapes of views.
1234 DataTypes::ShapeType sourceShape;
1235 DataTypes::ShapeType targetShape;
1236 targetShape.push_back(50);
1237 targetShape.push_back(65);
1238 targetShape.push_back(80);
1239 targetShape.push_back(90);
1240
1241 // Create source and target views.
1242 int len = DataTypes::noValues(sourceShape);
1243 DataVector sourceData(len, 2.0, len);
1244 // DataArrayView sourceView(sourceData, sourceShape);
1245 sourceData[0]=5;
1246
1247 len = DataTypes::noValues(targetShape);
1248 DataVector targetData(len, 2.0, len);
1249 // DataArrayView targetView(targetData, targetShape);
1250
1251 // Copy source view to target view.
1252 // targetView.copySliceFrom(sourceView,region);
1253 copySliceFrom(targetData, targetShape, 0, sourceData, sourceShape, 0, region);
1254
1255 // Check results of copy.
1256 for (int i=region[0].first;i<region[0].second;i++) {
1257 for (int j=region[1].first;j<region[1].second;j++) {
1258 for (int k=region[2].first;k<region[2].second;k++) {
1259 for (int l=region[3].first;l<region[3].second;l++) {
1260 assert(sourceData[0]==
1261 targetData[getRelIndex(targetShape,i,j,k,l)]);
1262 }
1263 }
1264 }
1265 }
1266 }
1267
1268 cout << endl;
1269
1270 }
1271
1272 void DataTypesTestCase::testShapeToString() {
1273
1274 cout << endl;
1275 cout << "\tTest shapeToString for a variety of shapes." << endl;
1276
1277 DataTypes::ShapeType shape;
1278 assert(DataTypes::shapeToString(shape)=="()");
1279 shape.push_back(5);
1280 assert(DataTypes::shapeToString(shape)=="(5)");
1281 shape.push_back(2);
1282 assert(DataTypes::shapeToString(shape)=="(5,2)");
1283 shape.push_back(9);
1284 assert(DataTypes::shapeToString(shape)=="(5,2,9)");
1285 shape.push_back(4);
1286 assert(DataTypes::shapeToString(shape)=="(5,2,9,4)");
1287
1288 }
1289
1290
1291
1292 TestSuite* DataTypesTestCase::suite ()
1293 {
1294 //
1295 // create the suite of tests to perform.
1296 TestSuite *testSuite = new TestSuite ("DataTypesTestCase");
1297 testSuite->addTest (new TestCaller< DataTypesTestCase>("testShapeToString",&DataTypesTestCase::testShapeToString));
1298 testSuite->addTest (new TestCaller< DataTypesTestCase>("testResultSliceShape",&DataTypesTestCase::testResultSliceShape));
1299 testSuite->addTest (new TestCaller< DataTypesTestCase>("testSlicing",&DataTypesTestCase::testSlicing));
1300 testSuite->addTest (new TestCaller< DataTypesTestCase>("testShapeFunctions",&DataTypesTestCase::testShapeFns));
1301 return testSuite;
1302 }

  ViewVC Help
Powered by ViewVC 1.1.26