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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2271 - (show annotations)
Mon Feb 16 05:08:29 2009 UTC (10 years, 8 months ago) by jfenwick
File size: 8468 byte(s)
Merging version 2269 to trunk

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 #if (defined _WIN32) && (defined __INTEL_COMPILER)
16 #include <mathimf.h>
17 #else
18 #include <math.h>
19 #endif
20
21 #include "escript/DataExpanded.h"
22 // #include "escript/DataArrayView.h"
23 #include "escript/DataAlgorithm.h"
24 #include "DataAlgorithmAdapterTestCase.h"
25 #include "escript/DataTypes.h"
26
27 #include <iostream>
28 #include <algorithm>
29 #include <limits>
30
31 using namespace CppUnitTest;
32 using namespace std;
33 using namespace escript;
34 using namespace escript::DataTypes;
35
36 void DataAlgorithmAdapterTestCase::setUp() {
37 //
38 // This is called before each test is run
39
40 }
41
42 void DataAlgorithmAdapterTestCase::tearDown() {
43 //
44 // This is called after each test has been run
45
46 }
47
48
49 namespace
50 {
51
52 ValueType::const_reference
53 getSRefRO(DataReady& data,int sample, int point)
54 {
55 return data.getVectorRO()[data.getPointOffset(sample,point)];
56 }
57
58
59 }
60
61 void DataAlgorithmAdapterTestCase::testAll() {
62
63 cout << endl;
64
65 cout << "\tTesting FMax." << endl;
66
67 FMax fmax;
68 assert(std::abs(fmax(5,6)-6)<=REL_TOL*6);
69 assert(std::abs(fmax(5,-6)-5)<=REL_TOL*5);
70 assert(std::abs(fmax(0,0)-0)<=REL_TOL*0);
71 assert(std::abs(fmax(15,-96)-15)<=REL_TOL*15);
72
73 DataAlgorithmAdapter<FMax> sup(numeric_limits<double>::max()*-1);
74 sup.resetResult();
75 sup(-1);
76 sup(-2);
77 sup(-14);
78 sup(3);
79 assert(std::abs(sup.getResult()-3)<=REL_TOL*3);
80
81 cout << "\tTesting AbsMax." << endl;
82
83 AbsMax absmax;
84 assert(std::abs(absmax(5,6)-6)<=REL_TOL*6);
85 assert(std::abs(absmax(5,-6)-6)<=REL_TOL*6);
86 assert(std::abs(absmax(0,0)-0)<=REL_TOL*6);
87 assert(std::abs(absmax(15,-96)-96)<=REL_TOL*6);
88
89 DataAlgorithmAdapter<AbsMax> Lsup(0);
90 Lsup.resetResult();
91 Lsup(-2);
92 Lsup(2);
93 Lsup(5);
94 Lsup(-10);
95 assert(std::abs(Lsup.getResult()-10)<=REL_TOL*10);
96
97 cout << "\tTesting FMin." << endl;
98
99 FMin fmin;
100 assert(std::abs(fmin(5,6)-5)<=REL_TOL*5);
101 assert(std::abs(fmin(5,-6)-(-6))<=REL_TOL*6);
102 assert(std::abs(fmin(0,0)-0)<=REL_TOL*0);
103 assert(std::abs(fmin(15,-96)-(-96))<=REL_TOL*96);
104
105 DataAlgorithmAdapter<FMin> inf(numeric_limits<double>::max());
106 inf.resetResult();
107 inf(1);
108 inf(12);
109 inf(2);
110 inf(99);
111 assert(std::abs(inf.getResult()-1)<=REL_TOL*1);
112
113 cout << "\tTesting Length." << endl;
114
115 Length lngth;
116 assert(std::abs(lngth(5,6)-std::sqrt(61.0))<=REL_TOL*std::sqrt(61.0));
117 assert(std::abs(lngth(5,-6)-std::sqrt(61.0))<=REL_TOL*std::sqrt(61.0));
118 assert(std::abs(lngth(0,0)-std::sqrt(0.0))<=REL_TOL*std::sqrt(61.0));
119 assert(std::abs(lngth(15,-96)-std::sqrt(9441.0))<=REL_TOL*std::sqrt(61.0));
120
121 DataAlgorithmAdapter<Length> length(0);
122 length.resetResult();
123 length(2);
124 length(4);
125 length(6);
126 length(8);
127 assert(std::abs(length.getResult()-std::sqrt(120.0))<=REL_TOL*std::sqrt(120.0));
128 length.resetResult();
129 length(1.5);
130 length(2.5);
131 length(3.5);
132 length(4.5);
133 assert(std::abs(length.getResult()-std::sqrt(41.0))<=REL_TOL*std::sqrt(41.0));
134
135 cout << "\tTesting Trace." << endl;
136
137 Trace trce;
138 assert(std::abs(trce(5,6)-11)<=REL_TOL*11);
139 assert(std::abs(trce(5,-6)-(-1))<=REL_TOL*1);
140 assert(std::abs(trce(0,0)-0)<=REL_TOL*0);
141 assert(std::abs(trce(15,-96)-(-81))<=REL_TOL*81);
142
143 DataAlgorithmAdapter<Trace> trace(0);
144 trace.resetResult();
145 trace(1);
146 trace(2);
147 trace(3);
148 trace(4);
149 trace(5);
150 assert(std::abs(trace.getResult()-15)<=REL_TOL*15);
151 trace.resetResult();
152 trace(1.5);
153 trace(2.5);
154 trace(3.5);
155 trace(4.5);
156 trace(5.5);
157 assert(std::abs(trace.getResult()-17.5)<=REL_TOL*17.5);
158
159 }
160
161 void DataAlgorithmAdapterTestCase::testAlgorithm() {
162
163 cout << endl;
164
165 {
166
167 cout << "\tTest algorithm on Data objects with a single rank 2 data-point." << endl;
168
169 // define the shape for the DataArrayView
170 DataTypes::ShapeType shape;
171 shape.push_back(2);
172 shape.push_back(3);
173
174 // allocate the data for the DataArrayView
175 DataTypes::ValueType dataArray(DataTypes::noValues(shape),0);
176
177 // construct DataArrayView
178 // DataArrayView dataView(dataArray,shape);
179
180 // assign values to the data point
181 for (int i=0;i<shape[0];i++) {
182 for (int j=0;j<shape[1];j++) {
183 dataArray[getRelIndex(shape,i,j)]=getRelIndex(shape,i,j);
184 }
185 }
186
187 // create a few Data objects from the created DataArrayView
188 DataExpanded dataExp(FunctionSpace(),shape,dataArray);
189 DataConstant dataCon(FunctionSpace(),shape,dataArray);
190 DataTagged dataTag(dataCon);
191
192 // test algorithm on DataExpanded
193 FMin fmin_func;
194 assert(std::abs(escript::algorithm(dataExp,fmin_func,numeric_limits<double>::max())-0)<=REL_TOL*0);
195 FMax fmax_func;
196 assert(std::abs(escript::algorithm(dataExp,fmax_func,numeric_limits<double>::max()*-1)-5)<=REL_TOL*5);
197
198 // test algorithm on DataTagged
199 assert(std::abs(escript::algorithm(dataTag,fmin_func,numeric_limits<double>::max())-0)<=REL_TOL*0);
200 assert(std::abs(escript::algorithm(dataTag,fmax_func,numeric_limits<double>::max()*-1)-5)<=REL_TOL*5);
201
202 // test algorithm on DataConstant
203 assert(std::abs(escript::algorithm(dataCon,fmin_func,numeric_limits<double>::max())-0)<=REL_TOL*0);
204 assert(std::abs(escript::algorithm(dataCon,fmax_func,numeric_limits<double>::max()*-1)-5)<=REL_TOL*5);
205
206 }
207
208 }
209
210 void DataAlgorithmAdapterTestCase::testDpAlgorithm() {
211
212 cout << endl;
213
214 {
215
216 cout << "\tTest dp_algorithm on Data objects with a single rank 2 data-point." << endl;
217
218 // define the shapes for the DataArrayViews
219 DataTypes::ShapeType shape;
220 shape.push_back(2);
221 shape.push_back(3);
222 DataTypes::ShapeType shape2;
223
224 // allocate the data for the DataArrayViews
225 DataTypes::ValueType dataArray(DataTypes::noValues(shape),0);
226 DataTypes::ValueType dataArray2(DataTypes::noValues(shape2),0);
227
228 // construct DataArrayViews
229 // DataArrayView dataView(dataArray,shape);
230 // DataArrayView dataView2(dataArray2,shape2);
231
232 // assign values to the data point
233 for (int i=0;i<shape[0];i++) {
234 for (int j=0;j<shape[1];j++) {
235 dataArray[getRelIndex(shape,i,j)]=getRelIndex(shape,i,j);
236 }
237 }
238
239 // create a few Data objects from the created DataArrayViews
240 DataExpanded dataExp(FunctionSpace(),shape,dataArray);
241 DataConstant dataCon(FunctionSpace(),shape,dataArray);
242 DataTagged dataTag(dataCon);
243
244 // and create Data objects to receive the results of the dp_algorithm calls
245 DataExpanded dataExp2(FunctionSpace(),shape2,dataArray2);
246 DataConstant dataCon2(FunctionSpace(),shape2,dataArray2);
247 DataTagged dataTag2(dataCon2);
248
249 // test dp_algorithm on DataExpanded
250 FMin fmin_func;
251 escript::dp_algorithm(dataExp,dataExp2,fmin_func,numeric_limits<double>::max());
252 assert(std::abs(getSRefRO(dataExp2,0,0)-0)<=REL_TOL*0);
253 FMax fmax_func;
254 escript::dp_algorithm(dataExp,dataExp2,fmax_func,numeric_limits<double>::max()*-1);
255 assert(std::abs(getSRefRO(dataExp2,0,0)-5)<=REL_TOL*5);
256
257 // test dp_algorithm on DataTagged
258 escript::dp_algorithm(dataTag,dataTag2,fmin_func,numeric_limits<double>::max());
259 assert(std::abs(getSRefRO(dataTag2,0,0)-0)<=REL_TOL*0);
260 escript::dp_algorithm(dataTag,dataTag2,fmax_func,numeric_limits<double>::max()*-1);
261 assert(std::abs(getSRefRO(dataTag2,0,0)-5)<=REL_TOL*5);
262
263 // test dp_algorithm on DataConstant
264 escript::dp_algorithm(dataCon,dataCon2,fmin_func,numeric_limits<double>::max());
265 assert(std::abs(getSRefRO(dataCon2,0,0)-0)<=REL_TOL*0);
266 escript::dp_algorithm(dataCon,dataCon2,fmax_func,numeric_limits<double>::max()*-1);
267 assert(std::abs(getSRefRO(dataCon2,0,0)-5)<=REL_TOL*5);
268
269 }
270
271 }
272
273 TestSuite* DataAlgorithmAdapterTestCase::suite ()
274 {
275 //
276 // create the suite of tests to perform.
277 TestSuite *testSuite = new TestSuite ("DataAlgorithmAdapterTestCase");
278
279 testSuite->addTest (new TestCaller< DataAlgorithmAdapterTestCase>("testAll",&DataAlgorithmAdapterTestCase::testAll));
280 testSuite->addTest (new TestCaller<DataAlgorithmAdapterTestCase>("testAlgorithm",&DataAlgorithmAdapterTestCase::testAlgorithm));
281 testSuite->addTest (new TestCaller<DataAlgorithmAdapterTestCase>("testDpAlgorithm",&DataAlgorithmAdapterTestCase::testDpAlgorithm));
282 return testSuite;
283 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26