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

Contents of /trunk/escript/test/DataAlgorithmAdapterTestCase.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: 8484 byte(s)
Merged noarrayview branch onto trunk.


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26