/[escript]/branches/DataC_2092/escript/test/DataLazyTestCase.cpp
ViewVC logotype

Contents of /branches/DataC_2092/escript/test/DataLazyTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2093 - (show annotations)
Tue Nov 25 04:20:56 2008 UTC (10 years, 7 months ago) by jfenwick
File size: 10453 byte(s)
Branching to experiment with changes to the DataC interface.
The goal is to separate read and write access to pointers.

1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2008 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 ValueType::reference
51 getRef(DataReady& data,int i, int j, int k)
52 {
53 return data.getVector()[getRelIndex(data.getShape(),i,j,k)];
54 }
55
56
57 DataReady_ptr
58 resolveAndDelete(DataAbstract* p)
59 {
60 DataReady_ptr p2=p->resolve();
61 if (p!=p2.get())
62 {
63 delete p;
64 }
65 return p2;
66 }
67
68
69 DataAbstract_ptr
70 getLazy(DataTypes::ShapeType& shape,bool minus=false)
71 {
72 int pts=DataTypes::noValues(shape);
73 DataTypes::ValueType data(pts,0);
74 for (int i=0;i<pts;++i)
75 {
76 data[i]=minus?-(i+1):i+1;
77 }
78 DataConstant* p=new DataConstant(FunctionSpace(),shape,data);
79 DataAbstract_ptr pp(p);
80 DataLazy* l=new DataLazy(pp);
81 return DataAbstract_ptr(l);
82 }
83
84 DataAbstract_ptr
85 getLazyU(DataTypes::ShapeType& shape, ES_optype typ)
86 {
87 int pts=DataTypes::noValues(shape);
88 DataTypes::ValueType data(pts,0);
89 for (int i=0;i<pts;++i)
90 {
91 data[i]=(i+1);
92 }
93 DataConstant* p=new DataConstant(FunctionSpace(),shape,data);
94 DataAbstract_ptr pp(p);
95 DataLazy* l=new DataLazy(pp,typ);
96 return DataAbstract_ptr(l);
97 }
98
99 DataAbstract_ptr
100 getLazyUP(DataTypes::ShapeType& shape, ES_optype typ, int par)
101 {
102 int pts=DataTypes::noValues(shape);
103 DataTypes::ValueType data(pts,0);
104 for (int i=0;i<pts;++i)
105 {
106 data[i]=(i+1);
107 }
108 DataConstant* p=new DataConstant(FunctionSpace(),shape,data);
109 DataAbstract_ptr pp(p);
110 DataLazy* l=new DataLazy(pp,typ,par);
111 return DataAbstract_ptr(l);
112 }
113
114
115 DataAbstract_ptr
116 getLazyB(DataTypes::ShapeType& shape, ES_optype typ)
117 {
118 int pts=DataTypes::noValues(shape);
119 DataTypes::ValueType data(pts,0);
120 DataTypes::ValueType data2(pts,0);
121 for (int i=0;i<pts;++i)
122 {
123 data[i]=(i+1);
124 data2[i]=-(i+1);
125 }
126 DataConstant* p=new DataConstant(FunctionSpace(),shape,data);
127 DataConstant* p2=new DataConstant(FunctionSpace(),shape,data2);
128 DataAbstract_ptr pp(p);
129 DataAbstract_ptr pp2(p2);
130 DataLazy* l=new DataLazy(pp,pp2,typ);
131 return DataAbstract_ptr(l);
132 }
133
134 DataAbstract_ptr
135 getLazyGTP(DataTypes::ShapeType& shape, ES_optype typ, int ax, int tr)
136 {
137 int pts=DataTypes::noValues(shape);
138 DataTypes::ValueType data(pts,0);
139 DataTypes::ValueType data2(pts,0);
140 for (int i=0;i<pts;++i)
141 {
142 data[i]=(i+1);
143 data2[i]=-(i+1);
144 }
145 DataConstant* p=new DataConstant(FunctionSpace(),shape,data);
146 DataConstant* p2=new DataConstant(FunctionSpace(),shape,data2);
147 DataAbstract_ptr pp(p);
148 DataAbstract_ptr pp2(p2);
149 DataLazy* l=new DataLazy(pp,pp2,typ,ax,tr);
150 return DataAbstract_ptr(l);
151 }
152
153
154
155 #define TESTOP(X,V) { DataAbstract_ptr d1=getLazy(shape); assert(d1->X()==V); assert(d1->isLazy());}
156
157 }
158
159 // This method tests the identity constructor
160 void DataLazyTestCase::testLazy1()
161 {
162 cout << endl;
163 cout << "\tTesting IDENTITY constructor\n";
164
165 DataTypes::ShapeType shape;
166 DataAbstract_ptr d1=getLazy(shape);
167 assert(d1->isLazy());
168 assert(dynamic_pointer_cast<DataLazy>(d1)->getBuffsRequired()==1);
169
170 for (int i=0;i<5;++i)
171 {
172 TESTOP(getRank,i);
173 TESTOP(getNoValues,DataTypes::noValues(shape));
174 TESTOP(getShape,shape);
175 TESTOP(getNumDPPSample,1);
176 TESTOP(getNumSamples,1);
177 shape.push_back(3);
178 }
179 }
180
181 #define TESTOPU(X,V,O) { DataAbstract_ptr d1=getLazyU(shape,O); assert(dynamic_pointer_cast<DataLazy>(d1)->X()==V); assert(d1->isLazy());}
182 // This method tests the unary op constructor
183 // We aren't checking the correctness of the results here, just that they have the right properties
184 void DataLazyTestCase::testLazy2()
185 {
186 cout << endl;
187 cout << "\tTesting UNARY constructor (basic checks only)\n";
188
189 DataTypes::ShapeType shape;
190 DataAbstract_ptr d1=getLazyU(shape,LOG);
191 assert(d1->isLazy());
192
193 for (int j=SIN;j<=LEZ;++j)
194 {
195 shape=DataTypes::scalarShape;
196 ES_optype op=(ES_optype)(j); // not even reinterpret_cast works here
197 // if other compilers object I'll write a switch
198 cout << "\t" << opToString(op) << endl;
199 for (int i=0;i<5;++i)
200 {
201 TESTOPU(getRank,i,op);
202 TESTOPU(getNoValues,DataTypes::noValues(shape),op);
203 TESTOPU(getShape,shape,op);
204 TESTOPU(getNumDPPSample,1,op);
205 TESTOPU(getNumSamples,1,op);
206 TESTOPU(getBuffsRequired,1,op);
207 shape.push_back(3);
208 }
209 }
210 }
211
212 #define TESTOPUP(X,V,O) { DataAbstract_ptr d1=getLazyUP(shape,O,0); assert(dynamic_pointer_cast<DataLazy>(d1)->X()==V); assert(d1->isLazy());}
213 // This method tests the unary op constructor
214 // We aren't checking the correctness of the results here, just that they have the right properties
215 void DataLazyTestCase::testLazy2p()
216 {
217 cout << endl;
218 cout << "\tTesting UNARY (with arg) constructor (basic checks only)\n";
219
220 DataTypes::ShapeType shape;
221 DataAbstract_ptr d1=getLazyUP(shape,TRANS,0);
222 assert(d1->isLazy());
223
224 for (int j=TRANS;j<=TRACE;++j)
225 {
226 shape=DataTypes::scalarShape;
227 ES_optype op=(ES_optype)(j); // not even reinterpret_cast works here
228 // if other compilers object I'll write a switch
229 cout << "\t" << opToString(op) << endl;
230 for (int i=0;i<5;++i)
231 {
232 if (op==TRACE)
233 {
234 TESTOPUP(getRank,0,op);
235 TESTOPUP(getNoValues,1,op);
236 TESTOPUP(getShape,DataTypes::scalarShape,op);
237 }
238 else
239 {
240 TESTOPUP(getRank,i,op);
241 TESTOPUP(getNoValues,DataTypes::noValues(shape),op);
242 TESTOPUP(getShape,shape,op);
243
244 }
245 TESTOPUP(getNumDPPSample,1,op);
246 TESTOPUP(getNumSamples,1,op);
247 TESTOPUP(getBuffsRequired,2,op);
248 shape.push_back(3);
249 }
250 }
251 }
252
253 #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());}
254 // This method tests the binary op constructor
255 // We aren't checking the correctness of the results here, just that they have the right properties
256 void DataLazyTestCase::testLazy3()
257 {
258 cout << endl;
259 cout << "\tTesting BINARY constructor (basic checks only)\n";
260
261 DataTypes::ShapeType shape;
262 DataAbstract_ptr d1=getLazyB(shape,ADD);
263 assert(d1->isLazy());
264
265 for (int j=ADD;j<=POW;++j)
266 {
267 shape=DataTypes::scalarShape;
268 ES_optype op=(ES_optype)(j); // not even reinterpret_cast works here
269 // if other compilers object I'll write a switch
270 cout << "\t" << opToString(op) << endl;
271 for (int i=0;i<5;++i)
272 {
273 TESTOPB(getRank,i,op);
274 TESTOPB(getNoValues,DataTypes::noValues(shape),op);
275 TESTOPB(getShape,shape,op);
276 TESTOPB(getNumDPPSample,1,op);
277 TESTOPB(getNumSamples,1,op);
278 TESTOPB(getBuffsRequired,2,op);
279 shape.push_back(3);
280 }
281 }
282 }
283
284
285
286
287 #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());}
288
289 // This method tests the GeneralTensorproduct constructor
290 // We aren't checking the correctness of the results here, just that they have the right properties
291 void DataLazyTestCase::testLazy4()
292 {
293 cout << endl;
294 cout << "\tTesting GTP constructor (basic checks only)\n";
295
296 DataTypes::ShapeType shape;
297 DataTypes::ShapeType prodshape;
298
299 DataAbstract_ptr d1=getLazyGTP(shape,PROD,0,0);
300 assert(d1->isLazy());
301
302 for (int j=PROD;j<=PROD;++j)
303 {
304 shape=DataTypes::scalarShape;
305 ES_optype op=(ES_optype)(j); // not even reinterpret_cast works here
306 // if other compilers object I'll write a switch
307 cout << "\t" << opToString(op) << endl;
308 for (int i=0;i<3;++i)
309 {
310 ShapeType ns;
311 for (int k=0;k<i;++k)
312 {
313 ns.push_back(3);
314 ns.push_back(3);
315 }
316 TESTOPGTP(getRank,i*2,op);
317 TESTOPGTP(getNoValues,DataTypes::noValues(ns),op);
318 TESTOPGTP(getShape,ns,op);
319 TESTOPGTP(getNumDPPSample,1,op);
320 TESTOPGTP(getNumSamples,1,op);
321 TESTOPGTP(getBuffsRequired,3,op);
322 shape.push_back(3);
323 }
324 }
325 }
326
327
328
329 void DataLazyTestCase::testBuffers()
330 {
331 cout << endl;
332 cout << "\tTesting Buffs required\n";
333 DataTypes::ShapeType shape;
334 DataAbstract_ptr p=(new DataLazy(getLazy(shape),getLazy(shape),ADD))->getPtr();
335 DataAbstract_ptr p2=(new DataLazy(p,SIN))->getPtr();
336 DataAbstract_ptr p3=(new DataLazy(p2,COS))->getPtr();
337 DataAbstract_ptr p4=(new DataLazy(p3,getLazy(shape),ADD))->getPtr();
338 assert(dynamic_pointer_cast<DataLazy>(p4)->getBuffsRequired()==2);
339 DataAbstract_ptr p5=(new DataLazy(p2,p4,ADD))->getPtr();
340 assert(dynamic_pointer_cast<DataLazy>(p5)->getBuffsRequired()==3);
341 DataAbstract_ptr p6=(new DataLazy(p5,TRANS,0))->getPtr();
342 assert(dynamic_pointer_cast<DataLazy>(p6)->getBuffsRequired()==4);
343 DataAbstract_ptr p7=(new DataLazy(p6,p6,PROD,0,0))->getPtr();
344 assert(dynamic_pointer_cast<DataLazy>(p7)->getBuffsRequired()==6);
345 DataAbstract_ptr p8=(new DataLazy(p7,TRACE,0))->getPtr();
346 assert(dynamic_pointer_cast<DataLazy>(p8)->getBuffsRequired()==7);
347 }
348
349
350 TestSuite* DataLazyTestCase::suite ()
351 {
352 //
353 // create the suite of tests to perform.
354 TestSuite *testSuite = new TestSuite ("DataLazyTestCase");
355
356 testSuite->addTest (new TestCaller< DataLazyTestCase>("Identity",&DataLazyTestCase::testLazy1));
357 testSuite->addTest (new TestCaller< DataLazyTestCase>("Unary",&DataLazyTestCase::testLazy2));
358 testSuite->addTest (new TestCaller< DataLazyTestCase>("Unary (params)",&DataLazyTestCase::testLazy2p));
359 testSuite->addTest (new TestCaller< DataLazyTestCase>("Binary",&DataLazyTestCase::testLazy3));
360 testSuite->addTest (new TestCaller< DataLazyTestCase>("GTP",&DataLazyTestCase::testLazy4));
361 testSuite->addTest (new TestCaller< DataLazyTestCase>("Buffers",&DataLazyTestCase::testBuffers));
362 return testSuite;
363 }

  ViewVC Help
Powered by ViewVC 1.1.26