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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26