File: DataLazyTestCase.cpp

package info (click to toggle)
python-escript 5.6-10
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 144,304 kB
  • sloc: python: 592,074; cpp: 136,909; ansic: 18,675; javascript: 9,411; xml: 3,384; sh: 738; makefile: 207
file content (337 lines) | stat: -rw-r--r-- 9,714 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337

/*****************************************************************************
*
* Copyright (c) 2003-2020 by The University of Queensland
* http://www.uq.edu.au
*
* Primary Business: Queensland, Australia
* Licensed under the Apache License, version 2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Development until 2012 by Earth Systems Science Computational Center (ESSCC)
* Development 2012-2013 by School of Earth Sciences
* Development from 2014-2017 by Centre for Geoscience Computing (GeoComp)
* Development from 2019 by School of Earth and Environmental Sciences
**
*****************************************************************************/

#include <escript/DataConstant.h>
#include "DataLazyTestCase.h"

#include <escript/DataLazy.h>
#include <escript/FunctionSpace.h>

#include <iostream>
#include <cppunit/TestCaller.h>
#include <boost/shared_ptr.hpp>	// for the cast operator

using namespace CppUnit;
using namespace escript;
using namespace std;
using namespace escript::DataTypes;
using namespace boost;

// This test file checks the basic properties of lazy data.
// It does not check the correctness of particular operations 


namespace
{

//DataReady_ptr
//resolveAndDelete(DataAbstract* p)
//{
//   DataReady_ptr p2=p->resolve();
//   if (p!=p2.get())
//   {
//	delete p;
//   }
//   return p2;
//}


DataAbstract_ptr
getLazy(DataTypes::ShapeType& shape,bool minus=false)
{
  int pts=DataTypes::noValues(shape);
  DataTypes::RealVectorType data(pts,0);
  for (int i=0;i<pts;++i)
  {
	data[i]=minus?-(i+1):i+1;
  }
  DataConstant* p=new DataConstant(FunctionSpace(),shape,data);
  DataAbstract_ptr pp(p);
  DataLazy* l=new DataLazy(pp);
  return DataAbstract_ptr(l);
}

DataAbstract_ptr
getLazyU(DataTypes::ShapeType& shape, ES_optype typ)
{
  int pts=DataTypes::noValues(shape);
  DataTypes::RealVectorType data(pts,0);
  for (int i=0;i<pts;++i)
  {
	data[i]=(i+1);
  }
  DataConstant* p=new DataConstant(FunctionSpace(),shape,data);
  DataAbstract_ptr pp(p);
  DataLazy* l=new DataLazy(pp,typ);
  return DataAbstract_ptr(l);
}

DataAbstract_ptr
getLazyUP(DataTypes::ShapeType& shape, ES_optype typ, int par)
{
  int pts=DataTypes::noValues(shape);
  DataTypes::RealVectorType data(pts,0);
  for (int i=0;i<pts;++i)
  {
	data[i]=(i+1);
  }
  DataConstant* p=new DataConstant(FunctionSpace(),shape,data);
  DataAbstract_ptr pp(p);
  DataLazy* l=new DataLazy(pp,typ,par);
  return DataAbstract_ptr(l);
}


DataAbstract_ptr
getLazyB(DataTypes::ShapeType& shape, ES_optype typ)
{
  int pts=DataTypes::noValues(shape);
  DataTypes::RealVectorType data(pts,0);
  DataTypes::RealVectorType data2(pts,0);
  for (int i=0;i<pts;++i)
  {
	data[i]=(i+1);
	data2[i]=-(i+1);
  }
  DataConstant* p=new DataConstant(FunctionSpace(),shape,data);
  DataConstant* p2=new DataConstant(FunctionSpace(),shape,data2);
  DataAbstract_ptr pp(p);
  DataAbstract_ptr pp2(p2);
  DataLazy* l=new DataLazy(pp,pp2,typ);
  return DataAbstract_ptr(l);
}

DataAbstract_ptr
getLazyGTP(DataTypes::ShapeType& shape, ES_optype typ, int ax, int tr)
{
  int pts=DataTypes::noValues(shape);
  DataTypes::RealVectorType data(pts,0);
  DataTypes::RealVectorType data2(pts,0);
  for (int i=0;i<pts;++i)
  {
	data[i]=(i+1);
	data2[i]=-(i+1);
  }
  DataConstant* p=new DataConstant(FunctionSpace(),shape,data);
  DataConstant* p2=new DataConstant(FunctionSpace(),shape,data2);
  DataAbstract_ptr pp(p);
  DataAbstract_ptr pp2(p2);
  DataLazy* l=new DataLazy(pp,pp2,typ,ax,tr);
  return DataAbstract_ptr(l);
}



#define TESTOP(X,V) { DataAbstract_ptr d1=getLazy(shape); CPPUNIT_ASSERT(d1->X()==V); CPPUNIT_ASSERT(d1->isLazy());}

}

// This method tests the identity constructor
void DataLazyTestCase::testLazy1()
{
  cout << endl;
  cout << "\tTesting IDENTITY constructor\n";

  DataTypes::ShapeType shape;
  DataAbstract_ptr d1=getLazy(shape);
  CPPUNIT_ASSERT(d1->isLazy());

  for (int i=0;i<5;++i)
  {
    TESTOP(getRank,i);
    TESTOP(getNoValues,DataTypes::noValues(shape));
    TESTOP(getShape,shape);
    TESTOP(getNumDPPSample,1);
    TESTOP(getNumSamples,1);
    shape.push_back(3);
  }
}

#define TESTOPU(X,V,O) { DataAbstract_ptr d1=getLazyU(shape,O); CPPUNIT_ASSERT(dynamic_pointer_cast<DataLazy>(d1)->X()==V); CPPUNIT_ASSERT(d1->isLazy());}
// This method tests the unary op  constructor
// We aren't checking the correctness of the results here, just that they have the right properties
void DataLazyTestCase::testLazy2()
{
  cout << endl;
  cout << "\tTesting UNARY constructor (basic checks only)\n";

  DataTypes::ShapeType shape;
  DataAbstract_ptr d1=getLazyU(shape,LOG);
  CPPUNIT_ASSERT(d1->isLazy());

  for (int j=SIN;j<=LEZ;++j)
  {
    shape=DataTypes::scalarShape;
    ES_optype op=(ES_optype)(j);			// not even reinterpret_cast works here
					// if other compilers object I'll write a switch 
    if (op==POS)
    {
        continue;       // not testing this, python handles it differently
    }
    cout << "\t" << opToString(op) << endl;
    for (int i=0;i<5;++i)
    {
	TESTOPU(getRank,i,op);
    	TESTOPU(getNoValues,DataTypes::noValues(shape),op);
    	TESTOPU(getShape,shape,op);
    	TESTOPU(getNumDPPSample,1,op);
    	TESTOPU(getNumSamples,1,op);
    	shape.push_back(3);
    }
  }
}

#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());}
// This method tests the unary op  constructor
// We aren't checking the correctness of the results here, just that they have the right properties
void DataLazyTestCase::testLazy2p()
{
  cout << endl;
  cout << "\tTesting UNARY (with arg) constructor (basic checks only)\n";

  DataTypes::ShapeType shape;
  DataTypes::ShapeType traceshape;
  DataAbstract_ptr d1=getLazyUP(shape,TRANS,0);
  CPPUNIT_ASSERT(d1->isLazy());

  for (int j=TRANS;j<=TRACE;++j)
  {
    shape=DataTypes::scalarShape;	// traceshape is only used once so not initialised here
    
    ES_optype op=(ES_optype)(j);			// not even reinterpret_cast works here
					// if other compilers object I'll write a switch 
    cout << "\t" << opToString(op) << endl;
    for (int i=0;i<5;++i)
    {
	if (op==TRACE)
	{
	   if (i>1)	// trace only works 2 and up
	   {
	      TESTOPUP(getRank,i-2,op);
    	      TESTOPUP(getNoValues, DataTypes::noValues(traceshape),op);
    	      TESTOPUP(getShape,traceshape,op);

    	      TESTOPUP(getNumDPPSample,1,op);
    	      TESTOPUP(getNumSamples,1,op);
	      traceshape.push_back(3);
	   }
	}
	else
	{
	   TESTOPUP(getRank,i,op);
    	   TESTOPUP(getNoValues,DataTypes::noValues(shape),op);
    	   TESTOPUP(getShape,shape,op);
    	   TESTOPUP(getNumDPPSample,1,op);
    	   TESTOPUP(getNumSamples,1,op);
	}
    	shape.push_back(3);
    }
  }
}

#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());}
// This method tests the binary op  constructor
// We aren't checking the correctness of the results here, just that they have the right properties
void DataLazyTestCase::testLazy3()
{
  cout << endl;
  cout << "\tTesting BINARY constructor (basic checks only)\n";

  DataTypes::ShapeType shape;
  DataAbstract_ptr d1=getLazyB(shape,ADD);
  CPPUNIT_ASSERT(d1->isLazy());

  for (int j=ADD;j<=POW;++j)
  {
    shape=DataTypes::scalarShape;
    ES_optype op=(ES_optype)(j);			// not even reinterpret_cast works here
					// if other compilers object I'll write a switch 
    cout << "\t" << opToString(op) << endl;
    for (int i=0;i<5;++i)
    {
	TESTOPB(getRank,i,op);
    	TESTOPB(getNoValues,DataTypes::noValues(shape),op);
    	TESTOPB(getShape,shape,op);
    	TESTOPB(getNumDPPSample,1,op);
    	TESTOPB(getNumSamples,1,op);
    	shape.push_back(3);
    }
  }
}




#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());}

// This method tests the GeneralTensorproduct  constructor
// We aren't checking the correctness of the results here, just that they have the right properties
void DataLazyTestCase::testLazy4()
{
  cout << endl;
  cout << "\tTesting GTP constructor (basic checks only)\n";

  DataTypes::ShapeType shape;
  DataTypes::ShapeType prodshape;

  DataAbstract_ptr d1=getLazyGTP(shape,PROD,0,0);
  CPPUNIT_ASSERT(d1->isLazy());

  for (int j=PROD;j<=PROD;++j)
  {
    shape=DataTypes::scalarShape;
    ES_optype op=(ES_optype)(j);			// not even reinterpret_cast works here
					// if other compilers object I'll write a switch 
    cout << "\t" << opToString(op) << endl;
    for (int i=0;i<3;++i)
    {
	ShapeType ns;
	for (int k=0;k<i;++k)
	{
	  ns.push_back(3);
	  ns.push_back(3);
	}
	TESTOPGTP(getRank,i*2,op);
    	TESTOPGTP(getNoValues,DataTypes::noValues(ns),op);
    	TESTOPGTP(getShape,ns,op);
    	TESTOPGTP(getNumDPPSample,1,op);
    	TESTOPGTP(getNumSamples,1,op);
    	shape.push_back(3);
    }
  }
}



TestSuite* DataLazyTestCase::suite()
{
  // create the suite of tests to perform.
  TestSuite *testSuite = new TestSuite("DataLazyTestCase");

  testSuite->addTest(new TestCaller<DataLazyTestCase>(
              "Identity",&DataLazyTestCase::testLazy1));
  testSuite->addTest(new TestCaller<DataLazyTestCase>(
              "Unary",&DataLazyTestCase::testLazy2));
  testSuite->addTest(new TestCaller<DataLazyTestCase>(
              "Unary (params)",&DataLazyTestCase::testLazy2p));
  testSuite->addTest(new TestCaller<DataLazyTestCase>(
              "Binary",&DataLazyTestCase::testLazy3));
  testSuite->addTest(new TestCaller<DataLazyTestCase>(
              "GTP",&DataLazyTestCase::testLazy4));
  return testSuite;
}