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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3597 - (show annotations)
Thu Sep 15 06:03:17 2011 UTC (8 years, 2 months ago) by caltinay
File size: 9363 byte(s)
Switching to external cppunit:
-removed tools/CppUnitTest
-updated scons script and files
-updated tests to changed API
-updated readme

Bumped scons options file version to 201. Additions:
-cppunit*
-boomeramg*
-vsl_random


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

  ViewVC Help
Powered by ViewVC 1.1.26