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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2066 - (show annotations)
Thu Nov 20 05:31:33 2008 UTC (10 years, 8 months ago) by jfenwick
File size: 6515 byte(s)
Fixed Data::toString to look at the amount of data actually stored rather than the number of points in the domain.

Added support for GeneralTensorProduct to LazyData
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
100 DataAbstract_ptr
101 getLazyB(DataTypes::ShapeType& shape, ES_optype typ)
102 {
103 int pts=DataTypes::noValues(shape);
104 DataTypes::ValueType data(pts,0);
105 DataTypes::ValueType data2(pts,0);
106 for (int i=0;i<pts;++i)
107 {
108 data[i]=(i+1);
109 data2[i]=-(i+1);
110 }
111 DataConstant* p=new DataConstant(FunctionSpace(),shape,data);
112 DataConstant* p2=new DataConstant(FunctionSpace(),shape,data2);
113 DataAbstract_ptr pp(p);
114 DataAbstract_ptr pp2(p2);
115 DataLazy* l=new DataLazy(pp,pp2,typ);
116 return DataAbstract_ptr(l);
117 }
118
119 #define TESTOP(X,V) { DataAbstract_ptr d1=getLazy(shape); assert(d1->X()==V); assert(d1->isLazy());}
120
121 }
122
123 // This method tests the identity constructor
124 void DataLazyTestCase::testLazy1()
125 {
126 cout << endl;
127 cout << "\tTesting IDENTITY constructor\n";
128
129 DataTypes::ShapeType shape;
130 DataAbstract_ptr d1=getLazy(shape);
131 assert(d1->isLazy());
132 assert(dynamic_pointer_cast<DataLazy>(d1)->getBuffsRequired()==1);
133
134 for (int i=0;i<5;++i)
135 {
136 TESTOP(getRank,i);
137 TESTOP(getNoValues,DataTypes::noValues(shape));
138 TESTOP(getShape,shape);
139 TESTOP(getNumDPPSample,1);
140 TESTOP(getNumSamples,1);
141 shape.push_back(3);
142 }
143 }
144
145 #define TESTOPU(X,V,O) { DataAbstract_ptr d1=getLazyU(shape,O); assert(dynamic_pointer_cast<DataLazy>(d1)->X()==V); assert(d1->isLazy());}
146 // This method tests the unary op constructor
147 // We aren't checking the correctness of the results here, just that they have the right properties
148 void DataLazyTestCase::testLazy2()
149 {
150 cout << endl;
151 cout << "\tTesting UNARY constructor (basic checks only)\n";
152
153 DataTypes::ShapeType shape;
154 DataAbstract_ptr d1=getLazyU(shape,LOG);
155 assert(d1->isLazy());
156
157 for (int j=SIN;j<=LEZ;++j)
158 {
159 shape=DataTypes::scalarShape;
160 ES_optype op=(ES_optype)(j); // not even reinterpret_cast works here
161 // if other compilers object I'll write a switch
162 cout << "\t" << opToString(op) << endl;
163 for (int i=0;i<5;++i)
164 {
165 TESTOPU(getRank,i,op);
166 TESTOPU(getNoValues,DataTypes::noValues(shape),op);
167 TESTOPU(getShape,shape,op);
168 TESTOPU(getNumDPPSample,1,op);
169 TESTOPU(getNumSamples,1,op);
170 TESTOPU(getBuffsRequired,1,op);
171 shape.push_back(3);
172 }
173 }
174 }
175
176 #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());}
177 // This method tests the unary op constructor
178 // We aren't checking the correctness of the results here, just that they have the right properties
179 void DataLazyTestCase::testLazy3()
180 {
181 cout << endl;
182 cout << "\tTesting BINARY constructor (basic checks only)\n";
183
184 DataTypes::ShapeType shape;
185 DataAbstract_ptr d1=getLazyB(shape,ADD);
186 assert(d1->isLazy());
187
188 for (int j=ADD;j<=POW;++j)
189 {
190 shape=DataTypes::scalarShape;
191 ES_optype op=(ES_optype)(j); // not even reinterpret_cast works here
192 // if other compilers object I'll write a switch
193 cout << "\t" << opToString(op) << endl;
194 for (int i=0;i<5;++i)
195 {
196 TESTOPB(getRank,i,op);
197 TESTOPB(getNoValues,DataTypes::noValues(shape),op);
198 TESTOPB(getShape,shape,op);
199 TESTOPB(getNumDPPSample,1,op);
200 TESTOPB(getNumSamples,1,op);
201 TESTOPB(getBuffsRequired,2,op);
202 shape.push_back(3);
203 }
204 }
205 }
206
207 void DataLazyTestCase::testBuffers()
208 {
209 cout << endl;
210 cout << "\tTesting Buffs required\n";
211 DataTypes::ShapeType shape;
212 DataAbstract_ptr p=(new DataLazy(getLazy(shape),getLazy(shape),ADD))->getPtr();
213 DataAbstract_ptr p2=(new DataLazy(p,SIN))->getPtr();
214 DataAbstract_ptr p3=(new DataLazy(p2,COS))->getPtr();
215 DataAbstract_ptr p4=(new DataLazy(p3,getLazy(shape),ADD))->getPtr();
216 assert(dynamic_pointer_cast<DataLazy>(p4)->getBuffsRequired()==2);
217 DataAbstract_ptr p5=(new DataLazy(p2,p4,ADD))->getPtr();
218 assert(dynamic_pointer_cast<DataLazy>(p5)->getBuffsRequired()==3);
219 }
220
221
222 TestSuite* DataLazyTestCase::suite ()
223 {
224 //
225 // create the suite of tests to perform.
226 TestSuite *testSuite = new TestSuite ("DataLazyTestCase");
227
228 testSuite->addTest (new TestCaller< DataLazyTestCase>("Identity",&DataLazyTestCase::testLazy1));
229 testSuite->addTest (new TestCaller< DataLazyTestCase>("Unary",&DataLazyTestCase::testLazy2));
230 testSuite->addTest (new TestCaller< DataLazyTestCase>("Binary",&DataLazyTestCase::testLazy3));
231 testSuite->addTest (new TestCaller< DataLazyTestCase>("Buffers",&DataLazyTestCase::testBuffers));
232 return testSuite;
233 }

  ViewVC Help
Powered by ViewVC 1.1.26