File: SharedDataTestCase.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 (165 lines) | stat: -rw-r--r-- 5,730 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
/*****************************************************************************
*
* 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
**
*****************************************************************************/

// The purpose of these tests is to check for unwanted sharing of between Data objects

#include <escript/Data.h>
#include <escript/TestDomain.h>

#include "SharedDataTestCase.h"
#include <escript/EscriptParams.h>

#include <cppunit/TestCaller.h>
#include <iostream>

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

FunctionSpace getSharedFs()
{
    static FunctionSpace fs=getTestDomainFunctionSpace(1,50,1);
    return fs;
  
}


// Create a data, involve it in a lazy expression. Then modify the original
// and see if the value of the lazy is affected.
#define TESTEQOP(OP) { Data d((double)42,DataTypes::scalarShape, getSharedFs(),false); Data L=d.delay(); L-=Data((double)42,DataTypes::scalarShape, getSharedFs(),false); d OP Data(2,DataTypes::scalarShape, getSharedFs(),false); CPPUNIT_ASSERT(L.Lsup()<0.001);} 

// Test if the copy constructor shares a DataAbstract with its originator
void SharedDataTestCase::testEQ()
{
  cout << endl << "Testing +=" << flush;
  TESTEQOP(+=)
  cout << "\tOK" << endl << "Testing -=";
  TESTEQOP(-=)
  cout << "\tOK" << endl << "Testing *=";
  TESTEQOP(*=)
  cout << "\tOK" << endl << "Testing /=";
  TESTEQOP(/=)
  cout << "\tOK" << endl;
}

// Test for shared data caused by using a copy constructor
void SharedDataTestCase::testCC()
{
  cout << endl;
  Data d(42, DataTypes::scalarShape, getSharedFs(),false);
  Data shared(d);
  d+=Data(20,DataTypes::scalarShape, getSharedFs(),false);
  shared-=Data(42,DataTypes::scalarShape, getSharedFs(),false);
  CPPUNIT_ASSERT(shared.Lsup()<0.001);
}

// Test for shared data caused by using = operator
void SharedDataTestCase::testAssign()
{
  cout << endl;
  Data d(42, DataTypes::scalarShape, getSharedFs(),false);
  Data shared=d;
  d+=Data(20,DataTypes::scalarShape, getSharedFs(),false);
  shared-=Data(42,DataTypes::scalarShape, getSharedFs(),false);
  CPPUNIT_ASSERT(shared.Lsup()<0.001);
}

void SharedDataTestCase::testSetToZero()
{
  Data d((double)42,DataTypes::scalarShape, getSharedFs(),false); 
  Data L=d.delay(); 
  L-=Data((double)42,DataTypes::scalarShape, getSharedFs(),false);
  d.setToZero();
  CPPUNIT_ASSERT(L.Lsup()<0.001);
}

void SharedDataTestCase::testSetTaggedValueFromCPP()
{
  Data d((double)42,DataTypes::scalarShape, getSharedFs(),false);
  d.tag(); 
  Data L=d.delay();
  RealVectorType v(1,17);
  d.setTaggedValueFromCPP(1,DataTypes::scalarShape,v);
  L.resolve();
  // at this point, d should have a tag and L should not
  // unfortunately its a little tricky to find out what tags a Data object has so I'll use strings
  string s=L.toString();
  CPPUNIT_ASSERT(s.find("Tag(1)")==string::npos);		// if the tag shows up we have shared data
}

void SharedDataTestCase::testGetDataAtOffset()
{
  Data d((double)42,DataTypes::scalarShape, getSharedFs(),false);
  Data L=d.delay();
  // now change the data directly
  d.requireWrite();
  d.getDataAtOffsetRW(0, static_cast<DataTypes::real_t>(0))=17;
  CPPUNIT_ASSERT(L.getDataAtOffsetRO(0, static_cast<DataTypes::real_t>(0))==42);
}

void SharedDataTestCase::testGetDataPoint()
{
  Data d((double)42,DataTypes::scalarShape, getSharedFs(),false);
  Data L=d.delay();
  // now change the data directly
  d.requireWrite();
  d.getDataPointRW(0,0)=17;
  CPPUNIT_ASSERT(L.getDataPointRO(0,0)==42);
}

void SharedDataTestCase::testGetSampleRW()
{
  Data d((double)42,DataTypes::scalarShape, getSharedFs(),false);
  Data L=d.delay();
  
  std::cerr << "Please ignore the shared object message. We are testing the error checking.\n";
#ifdef SLOWSHARECHECK
  // now change the data directly
  CPPUNIT_ASSERT_THROW(*d.getSampleDataRW(0)=17, DataException);
#endif  
  std::cerr << "End ignore message\n";
  // Now try again properly 
  d.requireWrite();
  *d.getSampleDataRW(0)=17;
  L.resolve();
  CPPUNIT_ASSERT(*L.getSampleDataRO(0)==42);
}

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

  testSuite->addTest(new TestCaller<SharedDataTestCase>(
              "Arithmetic Assignment operators",&SharedDataTestCase::testEQ));
  testSuite->addTest(new TestCaller<SharedDataTestCase>(
              "Copy Constructor",&SharedDataTestCase::testCC));
  testSuite->addTest(new TestCaller<SharedDataTestCase>(
              "Assignment operator",&SharedDataTestCase::testAssign));
  testSuite->addTest(new TestCaller<SharedDataTestCase>(
              "setToZero",&SharedDataTestCase::testSetToZero));
  testSuite->addTest(new TestCaller<SharedDataTestCase>(
              "setTaggedValueFromCPP",&SharedDataTestCase::testSetTaggedValueFromCPP));
  testSuite->addTest(new TestCaller<SharedDataTestCase>(
              "getDataAtOffset",&SharedDataTestCase::testGetDataAtOffset));
  testSuite->addTest(new TestCaller<SharedDataTestCase>(
              "getDataPoint",&SharedDataTestCase::testGetDataPoint));
  testSuite->addTest(new TestCaller<SharedDataTestCase>(
              "getSampleRW",&SharedDataTestCase::testGetSampleRW));
  return testSuite;
}