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