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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26