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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 854 - (show annotations)
Thu Sep 21 05:29:42 2006 UTC (13 years ago) by gross
File size: 7981 byte(s)
Some modifications to the binary operations +,-,*/, pow. 
The code is a bit simpler now and more efficient has there is
no reseising required now. the resizing method has been removed as
it is very, very inefficient. Even serial code should be faster now.
It is now forbidden to do an inplace update of scalar data object with an object 
of rank >0 as this is very slow (and does not make much sense). 


1 // $Id$
2 /*
3 *****************************************************************************
4 * *
5 * COPYRIGHT ACcESS - All Rights Reserved *
6 * *
7 * This software is the property of ACcESS. No part of this code *
8 * may be copied in any form or by any means without the expressed written *
9 * consent of ACcESS. Copying, use or modification of this software *
10 * by any unauthorised person is illegal unless that person has a software *
11 * license agreement with ACcESS. *
12 * *
13 *****************************************************************************
14 */
15 #include "escript/DataConstant.h"
16 #include "escript/FunctionSpace.h"
17 #include "esysUtils/EsysException.h"
18
19 #include "DataConstantTestCase.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 DataConstantTestCase::setUp() {
29 //
30 // This is called before each test is run
31 }
32
33 void DataConstantTestCase::tearDown() {
34 //
35 // This is called after each test has been run
36 }
37
38 void DataConstantTestCase::testAll() {
39
40 cout << endl;
41
42 //
43 // Create a scalar pointData
44 DataArrayView::ShapeType shape;
45 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
46 DataArrayView pointData(data,shape);
47
48 //
49 // assign an arbitrary value
50 pointData()=1.0;
51
52 //
53 // Test construction
54 cout << "\tTesting default constructor." << endl;
55 DataConstant testData(pointData, FunctionSpace());
56
57 cout << "\tTest getLength." << endl;
58 assert(testData.getLength()==1);
59
60 shape.push_back(2);
61 shape.push_back(3);
62 shape.push_back(21);
63
64 /*
65 cout << "\tTest reshape." << endl;
66 testData.reshapeDataPoint(shape);
67 assert((unsigned int)testData.getPointDataView().getRank()==shape.size());
68
69 cout << "\tTest getPointDataView." << endl;
70 for (int k=0;k<shape[2];k++) {
71 for (int j=0;j<shape[1];j++) {
72 for (int i=0;i<shape[0];i++) {
73 assert(testData.getPointDataView()(i,j,k)==pointData());
74 }
75 }
76 }
77
78 try {
79 cout << "\tTest illegal reshape." << endl;
80 testData.reshapeDataPoint(shape);
81 assert(false);
82 }
83 catch (EsysException& e) {
84 //cout << e.toString() << endl;
85 assert(true);
86 }
87
88 cout << "\tVerify data point attributes." << endl;
89 DataArrayView dataView=testData.getPointDataView();
90 assert(dataView.getRank()==3);
91 assert(dataView.noValues()==126);
92 assert(dataView.getShape()[0]==2);
93 assert(dataView.getShape()[1]==3);
94 assert(dataView.getShape()[2]==21);
95 */
96
97 cout << "\tTesting alternative constructor." << endl;
98 DataArrayView::ValueType data1(DataArrayView::noValues(shape),1.0);
99 // do not call the FunctionSpace constructor directly
100 // in the argument of DataConstant
101 // GCC chokes on it.
102 FunctionSpace tmp_fns;
103 DataConstant testData1(tmp_fns, shape, data1);
104
105 for (int k=0;k<shape[2];k++) {
106 for (int j=0;j<shape[1];j++) {
107 for (int i=0;i<shape[0];i++) {
108 assert(testData1.getPointDataView()(i,j,k)==1.0);
109 }
110 }
111 }
112
113 cout << "\tTest getLength." << endl;
114 assert(testData1.getLength()==126);
115
116 cout << "\tVerify data point attributes." << endl;
117 DataArrayView dataView=testData1.getPointDataView();
118 assert(dataView.getRank()==3);
119 assert(dataView.noValues()==126);
120 assert(dataView.getShape()[0]==2);
121 assert(dataView.getShape()[1]==3);
122 assert(dataView.getShape()[2]==21);
123
124 cout << "\tTesting copy constructor." << endl;
125 DataConstant testData2(testData1);
126
127 for (int k=0;k<shape[2];k++) {
128 for (int j=0;j<shape[1];j++) {
129 for (int i=0;i<shape[0];i++) {
130 assert(testData2.getPointDataView()(i,j,k)==pointData());
131 }
132 }
133 }
134
135 cout << "\tTest getLength." << endl;
136 assert(testData2.getLength()==126);
137
138 cout << "\tVerify data point attributes." << endl;
139 dataView=testData2.getPointDataView();
140 assert(dataView.getRank()==3);
141 assert(dataView.noValues()==126);
142 assert(dataView.getShape()[0]==2);
143 assert(dataView.getShape()[1]==3);
144 assert(dataView.getShape()[2]==21);
145
146 cout << "\tTest slicing (whole object)." << endl;
147
148 DataArrayView::RegionType region;
149 region.push_back(DataArrayView::RegionType::value_type(0,shape[0]));
150 region.push_back(DataArrayView::RegionType::value_type(0,shape[1]));
151 region.push_back(DataArrayView::RegionType::value_type(0,shape[2]));
152
153 DataAbstract* testData3=testData2.getSlice(region);
154
155 for (int k=0;k<shape[2];k++) {
156 for (int j=0;j<shape[1];j++) {
157 for (int i=0;i<shape[0];i++) {
158 assert(testData3->getPointDataView()(i,j,k)==pointData());
159 }
160 }
161 }
162
163 assert(testData3->getLength()==126);
164
165 cout << "\tVerify data point attributes." << endl;
166 dataView=testData3->getPointDataView();
167 assert(dataView.getRank()==3);
168 assert(dataView.noValues()==126);
169 assert(dataView.getShape()[0]==2);
170 assert(dataView.getShape()[1]==3);
171 assert(dataView.getShape()[2]==21);
172
173 cout << "\tTest slicing (part object)." << endl;
174
175 DataArrayView::RegionType region2;
176 region2.push_back(DataArrayView::RegionType::value_type(0,2));
177 region2.push_back(DataArrayView::RegionType::value_type(0,2));
178 region2.push_back(DataArrayView::RegionType::value_type(0,2));
179
180 DataAbstract* testData4=testData3->getSlice(region2);
181
182 for (int k=0;k<2;k++) {
183 for (int j=0;j<2;j++) {
184 for (int i=0;i<2;i++) {
185 assert(testData4->getPointDataView()(i,j,k)==pointData());
186 }
187 }
188 }
189
190 assert(testData4->getLength()==8);
191
192 cout << "\tVerify data point attributes." << endl;
193 dataView=testData4->getPointDataView();
194 assert(dataView.getRank()==3);
195 assert(dataView.noValues()==8);
196 assert(dataView.getShape()[0]==2);
197 assert(dataView.getShape()[1]==2);
198 assert(dataView.getShape()[2]==2);
199
200 cout << "\tTest slicing (part object)." << endl;
201
202 DataArrayView::RegionType region3;
203 region3.push_back(DataArrayView::RegionType::value_type(1,2));
204 region3.push_back(DataArrayView::RegionType::value_type(1,3));
205 region3.push_back(DataArrayView::RegionType::value_type(5,9));
206
207 DataAbstract* testData5=testData3->getSlice(region3);
208
209 for (int k=0;k<4;k++) {
210 for (int j=0;j<2;j++) {
211 for (int i=0;i<1;i++) {
212 assert(testData5->getPointDataView()(i,j,k)==pointData());
213 }
214 }
215 }
216
217 assert(testData5->getLength()==8);
218
219 cout << "\tVerify data point attributes." << endl;
220 dataView=testData5->getPointDataView();
221 assert(dataView.getRank()==3);
222 assert(dataView.noValues()==8);
223 assert(dataView.getShape()[0]==1);
224 assert(dataView.getShape()[1]==2);
225 assert(dataView.getShape()[2]==4);
226
227 cout << "\tTest slice setting (1)." << endl;
228
229 DataArrayView::RegionType region4;
230 region4.push_back(DataArrayView::RegionType::value_type(0,1));
231 region4.push_back(DataArrayView::RegionType::value_type(0,2));
232 region4.push_back(DataArrayView::RegionType::value_type(0,4));
233
234 DataAbstract* testData6=testData3->getSlice(region3);
235
236 testData5->setSlice(testData6,region4);
237
238 for (int k=0;k<4;k++) {
239 for (int j=0;j<2;j++) {
240 for (int i=0;i<1;i++) {
241 assert(testData5->getPointDataView()(i,j,k)==pointData());
242 }
243 }
244 }
245
246 assert(testData5->getLength()==8);
247
248 cout << "\tVerify data point attributes." << endl;
249 dataView=testData5->getPointDataView();
250 assert(dataView.getRank()==3);
251 assert(dataView.noValues()==8);
252 assert(dataView.getShape()[0]==1);
253 assert(dataView.getShape()[1]==2);
254 assert(dataView.getShape()[2]==4);
255
256 delete testData3;
257 delete testData4;
258 delete testData5;
259 delete testData6;
260 }
261
262 TestSuite* DataConstantTestCase::suite ()
263 {
264 //
265 // create the suite of tests to perform.
266 TestSuite *testSuite = new TestSuite ("DataConstantTestCase");
267
268 testSuite->addTest (new TestCaller< DataConstantTestCase>("testAll",&DataConstantTestCase::testAll));
269 return testSuite;
270 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26