1 |
jgs |
82 |
// $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/Data/DataConstant.h" |
16 |
|
|
#include "escript/Data/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 |
jgs |
102 |
cout << "\tTest getLength." << endl; |
58 |
|
|
assert(testData.getLength()==1); |
59 |
|
|
|
60 |
jgs |
82 |
cout << "\tTest reshape." << endl; |
61 |
|
|
shape.push_back(2); |
62 |
|
|
shape.push_back(3); |
63 |
|
|
shape.push_back(21); |
64 |
|
|
testData.reshapeDataPoint(shape); |
65 |
|
|
assert(testData.getPointDataView().getRank()==shape.size()); |
66 |
|
|
|
67 |
|
|
cout << "\tTest getPointDataView." << endl; |
68 |
jgs |
102 |
for (int k=0;k<shape[2];k++) { |
69 |
|
|
for (int j=0;j<shape[1];j++) { |
70 |
|
|
for (int i=0;i<shape[0];i++) { |
71 |
jgs |
82 |
assert(testData.getPointDataView()(i,j,k)==pointData()); |
72 |
|
|
} |
73 |
|
|
} |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
try { |
77 |
|
|
cout << "\tTest illegal reshape." << endl; |
78 |
|
|
testData.reshapeDataPoint(shape); |
79 |
|
|
assert(false); |
80 |
|
|
} |
81 |
|
|
catch (EsysException& e) { |
82 |
|
|
//cout << e.toString() << endl; |
83 |
|
|
assert(true); |
84 |
|
|
} |
85 |
|
|
|
86 |
jgs |
102 |
cout << "\tVerify data point attributes." << endl; |
87 |
|
|
DataArrayView dataView=testData.getPointDataView(); |
88 |
|
|
assert(dataView.getRank()==3); |
89 |
|
|
assert(dataView.noValues()==126); |
90 |
|
|
assert(dataView.getShape()[0]==2); |
91 |
|
|
assert(dataView.getShape()[1]==3); |
92 |
|
|
assert(dataView.getShape()[2]==21); |
93 |
|
|
|
94 |
jgs |
82 |
cout << "\tTesting copy constructor." << endl; |
95 |
|
|
DataConstant testData2(testData); |
96 |
|
|
|
97 |
jgs |
102 |
for (int k=0;k<shape[2];k++) { |
98 |
|
|
for (int j=0;j<shape[1];j++) { |
99 |
|
|
for (int i=0;i<shape[0];i++) { |
100 |
|
|
assert(testData2.getPointDataView()(i,j,k)==pointData()); |
101 |
|
|
} |
102 |
|
|
} |
103 |
|
|
} |
104 |
|
|
|
105 |
jgs |
82 |
cout << "\tTest getLength." << endl; |
106 |
jgs |
102 |
assert(testData2.getLength()==126); |
107 |
jgs |
82 |
|
108 |
jgs |
102 |
cout << "\tVerify data point attributes." << endl; |
109 |
|
|
dataView=testData2.getPointDataView(); |
110 |
|
|
assert(dataView.getRank()==3); |
111 |
|
|
assert(dataView.noValues()==126); |
112 |
|
|
assert(dataView.getShape()[0]==2); |
113 |
|
|
assert(dataView.getShape()[1]==3); |
114 |
|
|
assert(dataView.getShape()[2]==21); |
115 |
|
|
|
116 |
|
|
cout << "\tTest slicing (whole object)." << endl; |
117 |
|
|
|
118 |
|
|
DataArrayView::RegionType region; |
119 |
|
|
region.push_back(DataArrayView::RegionType::value_type(0,shape[0])); |
120 |
|
|
region.push_back(DataArrayView::RegionType::value_type(0,shape[1])); |
121 |
|
|
region.push_back(DataArrayView::RegionType::value_type(0,shape[2])); |
122 |
|
|
|
123 |
|
|
DataAbstract* testData3=testData2.getSlice(region); |
124 |
|
|
|
125 |
|
|
for (int k=0;k<shape[2];k++) { |
126 |
|
|
for (int j=0;j<shape[1];j++) { |
127 |
|
|
for (int i=0;i<shape[0];i++) { |
128 |
|
|
assert(testData3->getPointDataView()(i,j,k)==pointData()); |
129 |
|
|
} |
130 |
|
|
} |
131 |
|
|
} |
132 |
|
|
|
133 |
|
|
assert(testData3->getLength()==126); |
134 |
|
|
|
135 |
|
|
cout << "\tVerify data point attributes." << endl; |
136 |
|
|
dataView=testData3->getPointDataView(); |
137 |
|
|
assert(dataView.getRank()==3); |
138 |
|
|
assert(dataView.noValues()==126); |
139 |
|
|
assert(dataView.getShape()[0]==2); |
140 |
|
|
assert(dataView.getShape()[1]==3); |
141 |
|
|
assert(dataView.getShape()[2]==21); |
142 |
|
|
|
143 |
|
|
cout << "\tTest slicing (part object)." << endl; |
144 |
|
|
|
145 |
|
|
DataArrayView::RegionType region2; |
146 |
|
|
region2.push_back(DataArrayView::RegionType::value_type(0,2)); |
147 |
|
|
region2.push_back(DataArrayView::RegionType::value_type(0,2)); |
148 |
|
|
region2.push_back(DataArrayView::RegionType::value_type(0,2)); |
149 |
|
|
|
150 |
|
|
DataAbstract* testData4=testData3->getSlice(region2); |
151 |
|
|
|
152 |
|
|
for (int k=0;k<2;k++) { |
153 |
|
|
for (int j=0;j<2;j++) { |
154 |
|
|
for (int i=0;i<2;i++) { |
155 |
|
|
assert(testData4->getPointDataView()(i,j,k)==pointData()); |
156 |
|
|
} |
157 |
|
|
} |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
assert(testData4->getLength()==8); |
161 |
|
|
|
162 |
|
|
cout << "\tVerify data point attributes." << endl; |
163 |
|
|
dataView=testData4->getPointDataView(); |
164 |
|
|
assert(dataView.getRank()==3); |
165 |
|
|
assert(dataView.noValues()==8); |
166 |
|
|
assert(dataView.getShape()[0]==2); |
167 |
|
|
assert(dataView.getShape()[1]==2); |
168 |
|
|
assert(dataView.getShape()[2]==2); |
169 |
|
|
|
170 |
|
|
cout << "\tTest slicing (part object)." << endl; |
171 |
|
|
|
172 |
|
|
DataArrayView::RegionType region3; |
173 |
|
|
region3.push_back(DataArrayView::RegionType::value_type(1,2)); |
174 |
|
|
region3.push_back(DataArrayView::RegionType::value_type(1,3)); |
175 |
|
|
region3.push_back(DataArrayView::RegionType::value_type(5,9)); |
176 |
|
|
|
177 |
|
|
DataAbstract* testData5=testData3->getSlice(region3); |
178 |
|
|
|
179 |
|
|
for (int k=0;k<4;k++) { |
180 |
|
|
for (int j=0;j<2;j++) { |
181 |
|
|
for (int i=0;i<1;i++) { |
182 |
|
|
assert(testData5->getPointDataView()(i,j,k)==pointData()); |
183 |
|
|
} |
184 |
|
|
} |
185 |
|
|
} |
186 |
|
|
|
187 |
|
|
assert(testData5->getLength()==8); |
188 |
|
|
|
189 |
|
|
cout << "\tVerify data point attributes." << endl; |
190 |
|
|
dataView=testData5->getPointDataView(); |
191 |
|
|
assert(dataView.getRank()==3); |
192 |
|
|
assert(dataView.noValues()==8); |
193 |
|
|
assert(dataView.getShape()[0]==1); |
194 |
|
|
assert(dataView.getShape()[1]==2); |
195 |
|
|
assert(dataView.getShape()[2]==4); |
196 |
|
|
|
197 |
|
|
cout << "\tTest slice setting (1)." << endl; |
198 |
|
|
|
199 |
|
|
DataArrayView::RegionType region4; |
200 |
|
|
region4.push_back(DataArrayView::RegionType::value_type(0,1)); |
201 |
|
|
region4.push_back(DataArrayView::RegionType::value_type(0,2)); |
202 |
|
|
region4.push_back(DataArrayView::RegionType::value_type(0,4)); |
203 |
|
|
|
204 |
|
|
DataAbstract* testData6=testData3->getSlice(region3); |
205 |
|
|
|
206 |
|
|
testData5->setSlice(testData6,region4); |
207 |
|
|
|
208 |
|
|
for (int k=0;k<4;k++) { |
209 |
|
|
for (int j=0;j<2;j++) { |
210 |
|
|
for (int i=0;i<1;i++) { |
211 |
|
|
assert(testData5->getPointDataView()(i,j,k)==pointData()); |
212 |
|
|
} |
213 |
|
|
} |
214 |
|
|
} |
215 |
|
|
|
216 |
|
|
assert(testData5->getLength()==8); |
217 |
|
|
|
218 |
|
|
cout << "\tVerify data point attributes." << endl; |
219 |
|
|
dataView=testData5->getPointDataView(); |
220 |
|
|
assert(dataView.getRank()==3); |
221 |
|
|
assert(dataView.noValues()==8); |
222 |
|
|
assert(dataView.getShape()[0]==1); |
223 |
|
|
assert(dataView.getShape()[1]==2); |
224 |
|
|
assert(dataView.getShape()[2]==4); |
225 |
|
|
|
226 |
|
|
//cout << "\tTest slice setting (2)." << endl; |
227 |
|
|
|
228 |
|
|
//DataArrayView::RegionType region5; |
229 |
|
|
//region5.push_back(DataArrayView::RegionType::value_type(1,2)); |
230 |
|
|
//region5.push_back(DataArrayView::RegionType::value_type(1,3)); |
231 |
|
|
//region5.push_back(DataArrayView::RegionType::value_type(14,18)); |
232 |
|
|
|
233 |
|
|
//DataAbstract* testData7=testData3->getSlice(region); |
234 |
|
|
|
235 |
|
|
//testData5->setSlice(testData7,region5); |
236 |
|
|
|
237 |
|
|
//for (int k=0;k<4;k++) { |
238 |
|
|
// for (int j=0;j<2;j++) { |
239 |
|
|
// for (int i=0;i<1;i++) { |
240 |
|
|
// assert(testData5->getPointDataView()(i,j,k)==pointData()); |
241 |
|
|
// } |
242 |
|
|
// } |
243 |
|
|
// } |
244 |
|
|
|
245 |
|
|
//assert(testData5->getLength()==8); |
246 |
|
|
|
247 |
|
|
//cout << "\tVerify data point attributes." << endl; |
248 |
|
|
//dataView=testData5->getPointDataView(); |
249 |
|
|
//assert(dataView.getRank()==3); |
250 |
|
|
//assert(dataView.noValues()==8); |
251 |
|
|
//assert(dataView.getShape()[0]==1); |
252 |
|
|
//assert(dataView.getShape()[1]==2); |
253 |
|
|
//assert(dataView.getShape()[2]==4); |
254 |
|
|
|
255 |
jgs |
82 |
} |
256 |
|
|
|
257 |
|
|
TestSuite* DataConstantTestCase::suite () |
258 |
|
|
{ |
259 |
|
|
// |
260 |
|
|
// create the suite of tests to perform. |
261 |
|
|
TestSuite *testSuite = new TestSuite ("DataConstantTestCase"); |
262 |
|
|
|
263 |
|
|
testSuite->addTest (new TestCaller< DataConstantTestCase>("testAll",&DataConstantTestCase::testAll)); |
264 |
|
|
return testSuite; |
265 |
|
|
} |