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