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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2881 - (show annotations)
Thu Jan 28 02:03:15 2010 UTC (9 years, 9 months ago) by jfenwick
File size: 9335 byte(s)
Don't panic.
Updating copyright stamps

1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2010 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 #include "escript/DataConstant.h"
16 #include "escript/DataLazy.h"
17 #include "escript/FunctionSpace.h"
18 #include "esysUtils/EsysException.h"
19
20 #include "DataLazyTestCase.h"
21
22 #include <iostream>
23 #include <boost/shared_ptr.hpp> // for the cast operator
24
25 using namespace CppUnitTest;
26 using namespace escript;
27 using namespace std;
28 using namespace esysUtils;
29 using namespace escript::DataTypes;
30 using namespace boost;
31
32 void DataLazyTestCase::setUp() {
33 //
34 // This is called before each test is run
35 }
36
37 void DataLazyTestCase::tearDown() {
38 //
39 // This is called after each test has been run
40 }
41
42
43 // This test file checks the basic properties of lazy data.
44 // It does not check the correctness of particular operations
45
46
47 namespace
48 {
49
50 DataReady_ptr
51 resolveAndDelete(DataAbstract* p)
52 {
53 DataReady_ptr p2=p->resolve();
54 if (p!=p2.get())
55 {
56 delete p;
57 }
58 return p2;
59 }
60
61
62 DataAbstract_ptr
63 getLazy(DataTypes::ShapeType& shape,bool minus=false)
64 {
65 int pts=DataTypes::noValues(shape);
66 DataTypes::ValueType data(pts,0);
67 for (int i=0;i<pts;++i)
68 {
69 data[i]=minus?-(i+1):i+1;
70 }
71 DataConstant* p=new DataConstant(FunctionSpace(),shape,data);
72 DataAbstract_ptr pp(p);
73 DataLazy* l=new DataLazy(pp);
74 return DataAbstract_ptr(l);
75 }
76
77 DataAbstract_ptr
78 getLazyU(DataTypes::ShapeType& shape, ES_optype typ)
79 {
80 int pts=DataTypes::noValues(shape);
81 DataTypes::ValueType data(pts,0);
82 for (int i=0;i<pts;++i)
83 {
84 data[i]=(i+1);
85 }
86 DataConstant* p=new DataConstant(FunctionSpace(),shape,data);
87 DataAbstract_ptr pp(p);
88 DataLazy* l=new DataLazy(pp,typ);
89 return DataAbstract_ptr(l);
90 }
91
92 DataAbstract_ptr
93 getLazyUP(DataTypes::ShapeType& shape, ES_optype typ, int par)
94 {
95 int pts=DataTypes::noValues(shape);
96 DataTypes::ValueType data(pts,0);
97 for (int i=0;i<pts;++i)
98 {
99 data[i]=(i+1);
100 }
101 DataConstant* p=new DataConstant(FunctionSpace(),shape,data);
102 DataAbstract_ptr pp(p);
103 DataLazy* l=new DataLazy(pp,typ,par);
104 return DataAbstract_ptr(l);
105 }
106
107
108 DataAbstract_ptr
109 getLazyB(DataTypes::ShapeType& shape, ES_optype typ)
110 {
111 int pts=DataTypes::noValues(shape);
112 DataTypes::ValueType data(pts,0);
113 DataTypes::ValueType data2(pts,0);
114 for (int i=0;i<pts;++i)
115 {
116 data[i]=(i+1);
117 data2[i]=-(i+1);
118 }
119 DataConstant* p=new DataConstant(FunctionSpace(),shape,data);
120 DataConstant* p2=new DataConstant(FunctionSpace(),shape,data2);
121 DataAbstract_ptr pp(p);
122 DataAbstract_ptr pp2(p2);
123 DataLazy* l=new DataLazy(pp,pp2,typ);
124 return DataAbstract_ptr(l);
125 }
126
127 DataAbstract_ptr
128 getLazyGTP(DataTypes::ShapeType& shape, ES_optype typ, int ax, int tr)
129 {
130 int pts=DataTypes::noValues(shape);
131 DataTypes::ValueType data(pts,0);
132 DataTypes::ValueType data2(pts,0);
133 for (int i=0;i<pts;++i)
134 {
135 data[i]=(i+1);
136 data2[i]=-(i+1);
137 }
138 DataConstant* p=new DataConstant(FunctionSpace(),shape,data);
139 DataConstant* p2=new DataConstant(FunctionSpace(),shape,data2);
140 DataAbstract_ptr pp(p);
141 DataAbstract_ptr pp2(p2);
142 DataLazy* l=new DataLazy(pp,pp2,typ,ax,tr);
143 return DataAbstract_ptr(l);
144 }
145
146
147
148 #define TESTOP(X,V) { DataAbstract_ptr d1=getLazy(shape); assert(d1->X()==V); assert(d1->isLazy());}
149
150 }
151
152 // This method tests the identity constructor
153 void DataLazyTestCase::testLazy1()
154 {
155 cout << endl;
156 cout << "\tTesting IDENTITY constructor\n";
157
158 DataTypes::ShapeType shape;
159 DataAbstract_ptr d1=getLazy(shape);
160 assert(d1->isLazy());
161
162 for (int i=0;i<5;++i)
163 {
164 TESTOP(getRank,i);
165 TESTOP(getNoValues,DataTypes::noValues(shape));
166 TESTOP(getShape,shape);
167 TESTOP(getNumDPPSample,1);
168 TESTOP(getNumSamples,1);
169 shape.push_back(3);
170 }
171 }
172
173 #define TESTOPU(X,V,O) { DataAbstract_ptr d1=getLazyU(shape,O); assert(dynamic_pointer_cast<DataLazy>(d1)->X()==V); assert(d1->isLazy());}
174 // This method tests the unary op constructor
175 // We aren't checking the correctness of the results here, just that they have the right properties
176 void DataLazyTestCase::testLazy2()
177 {
178 cout << endl;
179 cout << "\tTesting UNARY constructor (basic checks only)\n";
180
181 DataTypes::ShapeType shape;
182 DataAbstract_ptr d1=getLazyU(shape,LOG);
183 assert(d1->isLazy());
184
185 for (int j=SIN;j<=LEZ;++j)
186 {
187 shape=DataTypes::scalarShape;
188 ES_optype op=(ES_optype)(j); // not even reinterpret_cast works here
189 // if other compilers object I'll write a switch
190 cout << "\t" << opToString(op) << endl;
191 for (int i=0;i<5;++i)
192 {
193 TESTOPU(getRank,i,op);
194 TESTOPU(getNoValues,DataTypes::noValues(shape),op);
195 TESTOPU(getShape,shape,op);
196 TESTOPU(getNumDPPSample,1,op);
197 TESTOPU(getNumSamples,1,op);
198 shape.push_back(3);
199 }
200 }
201 }
202
203 #define TESTOPUP(X,V,O) { DataAbstract_ptr d1=getLazyUP(shape,O,0); assert(dynamic_pointer_cast<DataLazy>(d1)->X()==V); assert(d1->isLazy());}
204 // This method tests the unary op constructor
205 // We aren't checking the correctness of the results here, just that they have the right properties
206 void DataLazyTestCase::testLazy2p()
207 {
208 cout << endl;
209 cout << "\tTesting UNARY (with arg) constructor (basic checks only)\n";
210
211 DataTypes::ShapeType shape;
212 DataTypes::ShapeType traceshape;
213 DataAbstract_ptr d1=getLazyUP(shape,TRANS,0);
214 assert(d1->isLazy());
215
216 for (int j=TRANS;j<=TRACE;++j)
217 {
218 shape=DataTypes::scalarShape; // traceshape is only used once so not initialised here
219
220 ES_optype op=(ES_optype)(j); // not even reinterpret_cast works here
221 // if other compilers object I'll write a switch
222 cout << "\t" << opToString(op) << endl;
223 for (int i=0;i<5;++i)
224 {
225 if (op==TRACE)
226 {
227 if (i>1) // trace only works 2 and up
228 {
229 TESTOPUP(getRank,i-2,op);
230 TESTOPUP(getNoValues, DataTypes::noValues(traceshape),op);
231 TESTOPUP(getShape,traceshape,op);
232
233 TESTOPUP(getNumDPPSample,1,op);
234 TESTOPUP(getNumSamples,1,op);
235 traceshape.push_back(3);
236 }
237 }
238 else
239 {
240 TESTOPUP(getRank,i,op);
241 TESTOPUP(getNoValues,DataTypes::noValues(shape),op);
242 TESTOPUP(getShape,shape,op);
243 TESTOPUP(getNumDPPSample,1,op);
244 TESTOPUP(getNumSamples,1,op);
245 }
246 shape.push_back(3);
247 }
248 }
249 }
250
251 #define TESTOPB(X,V,O) { DataAbstract_ptr d1=getLazyB(shape,O); assert(dynamic_pointer_cast<DataLazy>(d1)->X()==V); assert(dynamic_pointer_cast<DataLazy>(d1)->isLazy());}
252 // This method tests the binary op constructor
253 // We aren't checking the correctness of the results here, just that they have the right properties
254 void DataLazyTestCase::testLazy3()
255 {
256 cout << endl;
257 cout << "\tTesting BINARY constructor (basic checks only)\n";
258
259 DataTypes::ShapeType shape;
260 DataAbstract_ptr d1=getLazyB(shape,ADD);
261 assert(d1->isLazy());
262
263 for (int j=ADD;j<=POW;++j)
264 {
265 shape=DataTypes::scalarShape;
266 ES_optype op=(ES_optype)(j); // not even reinterpret_cast works here
267 // if other compilers object I'll write a switch
268 cout << "\t" << opToString(op) << endl;
269 for (int i=0;i<5;++i)
270 {
271 TESTOPB(getRank,i,op);
272 TESTOPB(getNoValues,DataTypes::noValues(shape),op);
273 TESTOPB(getShape,shape,op);
274 TESTOPB(getNumDPPSample,1,op);
275 TESTOPB(getNumSamples,1,op);
276 shape.push_back(3);
277 }
278 }
279 }
280
281
282
283
284 #define TESTOPGTP(X,V,O) { DataAbstract_ptr d1=getLazyGTP(shape,O,0,0); assert(dynamic_pointer_cast<DataLazy>(d1)->X()==V); assert(dynamic_pointer_cast<DataLazy>(d1)->isLazy());}
285
286 // This method tests the GeneralTensorproduct constructor
287 // We aren't checking the correctness of the results here, just that they have the right properties
288 void DataLazyTestCase::testLazy4()
289 {
290 cout << endl;
291 cout << "\tTesting GTP constructor (basic checks only)\n";
292
293 DataTypes::ShapeType shape;
294 DataTypes::ShapeType prodshape;
295
296 DataAbstract_ptr d1=getLazyGTP(shape,PROD,0,0);
297 assert(d1->isLazy());
298
299 for (int j=PROD;j<=PROD;++j)
300 {
301 shape=DataTypes::scalarShape;
302 ES_optype op=(ES_optype)(j); // not even reinterpret_cast works here
303 // if other compilers object I'll write a switch
304 cout << "\t" << opToString(op) << endl;
305 for (int i=0;i<3;++i)
306 {
307 ShapeType ns;
308 for (int k=0;k<i;++k)
309 {
310 ns.push_back(3);
311 ns.push_back(3);
312 }
313 TESTOPGTP(getRank,i*2,op);
314 TESTOPGTP(getNoValues,DataTypes::noValues(ns),op);
315 TESTOPGTP(getShape,ns,op);
316 TESTOPGTP(getNumDPPSample,1,op);
317 TESTOPGTP(getNumSamples,1,op);
318 shape.push_back(3);
319 }
320 }
321 }
322
323
324
325 TestSuite* DataLazyTestCase::suite ()
326 {
327 //
328 // create the suite of tests to perform.
329 TestSuite *testSuite = new TestSuite ("DataLazyTestCase");
330
331 testSuite->addTest (new TestCaller< DataLazyTestCase>("Identity",&DataLazyTestCase::testLazy1));
332 testSuite->addTest (new TestCaller< DataLazyTestCase>("Unary",&DataLazyTestCase::testLazy2));
333 testSuite->addTest (new TestCaller< DataLazyTestCase>("Unary (params)",&DataLazyTestCase::testLazy2p));
334 testSuite->addTest (new TestCaller< DataLazyTestCase>("Binary",&DataLazyTestCase::testLazy3));
335 testSuite->addTest (new TestCaller< DataLazyTestCase>("GTP",&DataLazyTestCase::testLazy4));
336 return testSuite;
337 }

  ViewVC Help
Powered by ViewVC 1.1.26