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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2548 - (show annotations)
Mon Jul 20 06:20:06 2009 UTC (10 years, 4 months ago) by jfenwick
File size: 10738 byte(s)
Updating copyright notices
1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2009 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 assert(dynamic_pointer_cast<DataLazy>(d1)->getBuffsRequired()==1);
162
163 for (int i=0;i<5;++i)
164 {
165 TESTOP(getRank,i);
166 TESTOP(getNoValues,DataTypes::noValues(shape));
167 TESTOP(getShape,shape);
168 TESTOP(getNumDPPSample,1);
169 TESTOP(getNumSamples,1);
170 shape.push_back(3);
171 }
172 }
173
174 #define TESTOPU(X,V,O) { DataAbstract_ptr d1=getLazyU(shape,O); assert(dynamic_pointer_cast<DataLazy>(d1)->X()==V); assert(d1->isLazy());}
175 // This method tests the unary op constructor
176 // We aren't checking the correctness of the results here, just that they have the right properties
177 void DataLazyTestCase::testLazy2()
178 {
179 cout << endl;
180 cout << "\tTesting UNARY constructor (basic checks only)\n";
181
182 DataTypes::ShapeType shape;
183 DataAbstract_ptr d1=getLazyU(shape,LOG);
184 assert(d1->isLazy());
185
186 for (int j=SIN;j<=LEZ;++j)
187 {
188 shape=DataTypes::scalarShape;
189 ES_optype op=(ES_optype)(j); // not even reinterpret_cast works here
190 // if other compilers object I'll write a switch
191 cout << "\t" << opToString(op) << endl;
192 for (int i=0;i<5;++i)
193 {
194 TESTOPU(getRank,i,op);
195 TESTOPU(getNoValues,DataTypes::noValues(shape),op);
196 TESTOPU(getShape,shape,op);
197 TESTOPU(getNumDPPSample,1,op);
198 TESTOPU(getNumSamples,1,op);
199 TESTOPU(getBuffsRequired,1,op);
200 shape.push_back(3);
201 }
202 }
203 }
204
205 #define TESTOPUP(X,V,O) { DataAbstract_ptr d1=getLazyUP(shape,O,0); assert(dynamic_pointer_cast<DataLazy>(d1)->X()==V); assert(d1->isLazy());}
206 // This method tests the unary op constructor
207 // We aren't checking the correctness of the results here, just that they have the right properties
208 void DataLazyTestCase::testLazy2p()
209 {
210 cout << endl;
211 cout << "\tTesting UNARY (with arg) constructor (basic checks only)\n";
212
213 DataTypes::ShapeType shape;
214 DataTypes::ShapeType traceshape;
215 DataAbstract_ptr d1=getLazyUP(shape,TRANS,0);
216 assert(d1->isLazy());
217
218 for (int j=TRANS;j<=TRACE;++j)
219 {
220 shape=DataTypes::scalarShape; // traceshape is only used once so not initialised here
221
222 ES_optype op=(ES_optype)(j); // not even reinterpret_cast works here
223 // if other compilers object I'll write a switch
224 cout << "\t" << opToString(op) << endl;
225 for (int i=0;i<5;++i)
226 {
227 if (op==TRACE)
228 {
229 if (i>1) // trace only works 2 and up
230 {
231 TESTOPUP(getRank,i-2,op);
232 TESTOPUP(getNoValues, DataTypes::noValues(traceshape),op);
233 TESTOPUP(getShape,traceshape,op);
234
235 TESTOPUP(getNumDPPSample,1,op);
236 TESTOPUP(getNumSamples,1,op);
237 TESTOPUP(getBuffsRequired,2,op);
238 traceshape.push_back(3);
239 }
240 }
241 else
242 {
243 TESTOPUP(getRank,i,op);
244 TESTOPUP(getNoValues,DataTypes::noValues(shape),op);
245 TESTOPUP(getShape,shape,op);
246 TESTOPUP(getNumDPPSample,1,op);
247 TESTOPUP(getNumSamples,1,op);
248 TESTOPUP(getBuffsRequired,2,op);
249 }
250 shape.push_back(3);
251 }
252 }
253 }
254
255 #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());}
256 // This method tests the binary op constructor
257 // We aren't checking the correctness of the results here, just that they have the right properties
258 void DataLazyTestCase::testLazy3()
259 {
260 cout << endl;
261 cout << "\tTesting BINARY constructor (basic checks only)\n";
262
263 DataTypes::ShapeType shape;
264 DataAbstract_ptr d1=getLazyB(shape,ADD);
265 assert(d1->isLazy());
266
267 for (int j=ADD;j<=POW;++j)
268 {
269 shape=DataTypes::scalarShape;
270 ES_optype op=(ES_optype)(j); // not even reinterpret_cast works here
271 // if other compilers object I'll write a switch
272 cout << "\t" << opToString(op) << endl;
273 for (int i=0;i<5;++i)
274 {
275 TESTOPB(getRank,i,op);
276 TESTOPB(getNoValues,DataTypes::noValues(shape),op);
277 TESTOPB(getShape,shape,op);
278 TESTOPB(getNumDPPSample,1,op);
279 TESTOPB(getNumSamples,1,op);
280 TESTOPB(getBuffsRequired,3,op);
281 shape.push_back(3);
282 }
283 }
284 }
285
286
287
288
289 #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());}
290
291 // This method tests the GeneralTensorproduct constructor
292 // We aren't checking the correctness of the results here, just that they have the right properties
293 void DataLazyTestCase::testLazy4()
294 {
295 cout << endl;
296 cout << "\tTesting GTP constructor (basic checks only)\n";
297
298 DataTypes::ShapeType shape;
299 DataTypes::ShapeType prodshape;
300
301 DataAbstract_ptr d1=getLazyGTP(shape,PROD,0,0);
302 assert(d1->isLazy());
303
304 for (int j=PROD;j<=PROD;++j)
305 {
306 shape=DataTypes::scalarShape;
307 ES_optype op=(ES_optype)(j); // not even reinterpret_cast works here
308 // if other compilers object I'll write a switch
309 cout << "\t" << opToString(op) << endl;
310 for (int i=0;i<3;++i)
311 {
312 ShapeType ns;
313 for (int k=0;k<i;++k)
314 {
315 ns.push_back(3);
316 ns.push_back(3);
317 }
318 TESTOPGTP(getRank,i*2,op);
319 TESTOPGTP(getNoValues,DataTypes::noValues(ns),op);
320 TESTOPGTP(getShape,ns,op);
321 TESTOPGTP(getNumDPPSample,1,op);
322 TESTOPGTP(getNumSamples,1,op);
323 TESTOPGTP(getBuffsRequired,3,op);
324 shape.push_back(3);
325 }
326 }
327 }
328
329
330
331 void DataLazyTestCase::testBuffers()
332 {
333 cout << endl;
334 cout << "\tTesting Buffs required\n";
335 DataTypes::ShapeType shape;
336 DataAbstract_ptr p=(new DataLazy(getLazy(shape),getLazy(shape),ADD))->getPtr();
337 DataAbstract_ptr p2=(new DataLazy(p,SIN))->getPtr();
338 DataAbstract_ptr p3=(new DataLazy(p2,COS))->getPtr();
339 DataAbstract_ptr p4=(new DataLazy(p3,getLazy(shape),ADD))->getPtr();
340 assert(dynamic_pointer_cast<DataLazy>(p4)->getBuffsRequired()==4);
341 DataAbstract_ptr p5=(new DataLazy(p2,p4,ADD))->getPtr();
342 assert(dynamic_pointer_cast<DataLazy>(p5)->getBuffsRequired()==6);
343 DataAbstract_ptr p6=(new DataLazy(p5,TRANS,0))->getPtr();
344 assert(dynamic_pointer_cast<DataLazy>(p6)->getBuffsRequired()==7);
345 DataAbstract_ptr p7=(new DataLazy(p6,p6,PROD,0,0))->getPtr();
346 assert(dynamic_pointer_cast<DataLazy>(p7)->getBuffsRequired()==9);
347
348 DataTypes::ShapeType r2;
349 r2.push_back(4);
350 r2.push_back(4);
351 DataAbstract_ptr p8=(new DataLazy(getLazy(r2),TRACE,0))->getPtr();
352 assert(dynamic_pointer_cast<DataLazy>(p8)->getBuffsRequired()==2);
353 }
354
355
356 TestSuite* DataLazyTestCase::suite ()
357 {
358 //
359 // create the suite of tests to perform.
360 TestSuite *testSuite = new TestSuite ("DataLazyTestCase");
361
362 testSuite->addTest (new TestCaller< DataLazyTestCase>("Identity",&DataLazyTestCase::testLazy1));
363 testSuite->addTest (new TestCaller< DataLazyTestCase>("Unary",&DataLazyTestCase::testLazy2));
364 testSuite->addTest (new TestCaller< DataLazyTestCase>("Unary (params)",&DataLazyTestCase::testLazy2p));
365 testSuite->addTest (new TestCaller< DataLazyTestCase>("Binary",&DataLazyTestCase::testLazy3));
366 testSuite->addTest (new TestCaller< DataLazyTestCase>("GTP",&DataLazyTestCase::testLazy4));
367 testSuite->addTest (new TestCaller< DataLazyTestCase>("Buffers",&DataLazyTestCase::testBuffers));
368 return testSuite;
369 }

  ViewVC Help
Powered by ViewVC 1.1.26