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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1796 - (show annotations)
Wed Sep 17 01:45:46 2008 UTC (11 years, 1 month ago) by jfenwick
File size: 42067 byte(s)
Merged noarrayview branch onto trunk.


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

  ViewVC Help
Powered by ViewVC 1.1.26