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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1995 - (show annotations)
Fri Nov 7 06:02:06 2008 UTC (11 years, 2 months ago) by phornby
File size: 6707 byte(s)
replaced a few getLazy calls with getLazyU and getLazyB. It looked like the right thing to do given the arguments.
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(getLength,DataTypes::noValues(shape)); // since we only have one point
140 TESTOP(getNumDPPSample,1);
141 TESTOP(getNumSamples,1);
142 shape.push_back(3);
143 }
144 }
145
146 #define TESTOPU(X,V,O) { DataAbstract_ptr d1=getLazyU(shape,O); assert(dynamic_pointer_cast<DataLazy>(d1)->X()==V); assert(d1->isLazy());}
147 // This method tests the unary op constructor
148 // We aren't checking the correctness of the results here, just that they have the right properties
149 void DataLazyTestCase::testLazy2()
150 {
151 cout << endl;
152 cout << "\tTesting UNARY constructor (basic checks only)\n";
153
154 DataTypes::ShapeType shape;
155 DataAbstract_ptr d1=getLazyU(shape,LOG);
156 assert(d1->isLazy());
157
158 for (int j=SIN;j<=LEZ;++j)
159 {
160 shape=DataTypes::scalarShape;
161 ES_optype op=(ES_optype)(j); // not even reinterpret_cast works here
162 // if other compilers object I'll write a switch
163 cout << "\t" << opToString(op) << endl;
164 for (int i=0;i<5;++i)
165 {
166 TESTOPU(getRank,i,op);
167 TESTOPU(getNoValues,DataTypes::noValues(shape),op);
168 TESTOPU(getShape,shape,op);
169 TESTOPU(getLength,DataTypes::noValues(shape),op);
170 TESTOPU(getNumDPPSample,1,op);
171 TESTOPU(getNumSamples,1,op);
172 TESTOPU(getBuffsRequired,1,op);
173 shape.push_back(3);
174 }
175 }
176 }
177
178 #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());}
179 // This method tests the unary op constructor
180 // We aren't checking the correctness of the results here, just that they have the right properties
181 void DataLazyTestCase::testLazy3()
182 {
183 cout << endl;
184 cout << "\tTesting BINARY constructor (basic checks only)\n";
185
186 DataTypes::ShapeType shape;
187 DataAbstract_ptr d1=getLazyB(shape,ADD);
188 assert(d1->isLazy());
189
190 for (int j=ADD;j<=POW;++j)
191 {
192 shape=DataTypes::scalarShape;
193 ES_optype op=(ES_optype)(j); // not even reinterpret_cast works here
194 // if other compilers object I'll write a switch
195 cout << "\t" << opToString(op) << endl;
196 for (int i=0;i<5;++i)
197 {
198 TESTOPB(getRank,i,op);
199 TESTOPB(getNoValues,DataTypes::noValues(shape),op);
200 TESTOPB(getShape,shape,op);
201 TESTOPB(getLength,DataTypes::noValues(shape),op);
202 TESTOPB(getNumDPPSample,1,op);
203 TESTOPB(getNumSamples,1,op);
204 TESTOPB(getBuffsRequired,2,op);
205 shape.push_back(3);
206 }
207 }
208 }
209
210 void DataLazyTestCase::testBuffers()
211 {
212 cout << endl;
213 cout << "\tTesting Buffs required\n";
214 DataTypes::ShapeType shape;
215 DataAbstract_ptr p=(new DataLazy(getLazy(shape),getLazy(shape),ADD))->getPtr();
216 DataAbstract_ptr p2=(new DataLazy(p,SIN))->getPtr();
217 DataAbstract_ptr p3=(new DataLazy(p2,COS))->getPtr();
218 DataAbstract_ptr p4=(new DataLazy(p3,getLazy(shape),ADD))->getPtr();
219 assert(dynamic_pointer_cast<DataLazy>(p4)->getBuffsRequired()==2);
220 DataAbstract_ptr p5=(new DataLazy(p2,p4,ADD))->getPtr();
221 assert(dynamic_pointer_cast<DataLazy>(p5)->getBuffsRequired()==3);
222 }
223
224
225 TestSuite* DataLazyTestCase::suite ()
226 {
227 //
228 // create the suite of tests to perform.
229 TestSuite *testSuite = new TestSuite ("DataLazyTestCase");
230
231 testSuite->addTest (new TestCaller< DataLazyTestCase>("Identity",&DataLazyTestCase::testLazy1));
232 testSuite->addTest (new TestCaller< DataLazyTestCase>("Unary",&DataLazyTestCase::testLazy2));
233 testSuite->addTest (new TestCaller< DataLazyTestCase>("Binary",&DataLazyTestCase::testLazy3));
234 testSuite->addTest (new TestCaller< DataLazyTestCase>("Buffers",&DataLazyTestCase::testBuffers));
235 return testSuite;
236 }

  ViewVC Help
Powered by ViewVC 1.1.26