/[escript]/branches/arrexp_2137_win/escript/test/SharedDataTestCase.cpp
ViewVC logotype

Contents of /branches/arrexp_2137_win/escript/test/SharedDataTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2212 - (show annotations)
Wed Jan 14 00:15:00 2009 UTC (11 years, 5 months ago) by jfenwick
File size: 4568 byte(s)
Executive summary:

This commit adds copy on write checks to operations involving shared data. 

Changes:

new #defines:
~~~~~~~~~~~~~
Data.cpp has ASSIGNMENT_MEANS_DEEPCOPY (defaults to undefined).
Defining this will put the data = operator back to making deep copies instead
of sharing data (now the default.)

Data:
~~~~~
. Added exclusiveWrite method to copy the underlying data if it is shared.
. Some operators which took python objects now call the c++ versions intead of duplicating code.

DataAbstract and offspring:
~~~~~~~~~~~~~~~~~~~~~~~~~~~
. Added method to determine whether the data is currently shared.
. Added getVectorRO to children of DataReady.
. Added getTagRO.

. Operations which modify values in place (or return modifiable pointers) now use
a macro to check for sharing. In the case where a modification attempt is detected, it throws an exception. In the future, I will enable this only for debugging.

. This shold not really have been required but the compiler was not choosing the use the const version as I would have liked. Besides, this makes things explict.

. Moved (and de-inlined) getVector in DataConstant (It was virtual in a parent class).

Unit tests:
~~~~~~~~~~~
Added both python and c++ unit tests to check known cases of sharing and "inplace"
modification operations.

General:
~~~~~~~~
Removed some commented out code.

1 /*******************************************************
2 *
3 * Copyright (c) 2003-2008 by University of Queensland
4 * Earth Systems Science Computational Center (ESSCC)
5 * http://www.uq.edu.au/esscc
6 *
7 * Primary Business: Queensland, Australia
8 * Licensed under the Open Software License version 3.0
9 * http://www.opensource.org/licenses/osl-3.0.php
10 *
11 *******************************************************/
12
13 // The purpose of these tests is to check for unwanted sharing of between Data objects
14
15
16 #include "SharedDataTestCase.h"
17 #include "escript/Data.h"
18 #include "escript/EscriptParams.h"
19
20 #include <iostream>
21
22 using namespace escript;
23 using namespace std;
24 using namespace CppUnitTest;
25 using namespace escript::DataTypes;
26
27 void SharedDataTestCase::setUp()
28 {
29 //
30 // This is called before each test is run
31 }
32
33 void SharedDataTestCase::tearDown()
34 {
35 //
36 // This is called after each test has been run
37 }
38
39 // Create a data, involve it in a lazy expression. Then modify the original and see if the value of the lazy is affected.
40 #define TESTEQOP(OP) { Data d((double)42,DataTypes::scalarShape); Data L=d.delay(); L-=Data((double)42,DataTypes::scalarShape); d OP Data(2,DataTypes::scalarShape); assert(L.Lsup()<0.001);}
41
42 // Test if the copy constructor shares a DataAbstract with its originator
43 void SharedDataTestCase::testEQ()
44 {
45 cout << endl << "Testing +=" << flush;
46 TESTEQOP(+=)
47 cout << "\tOK" << endl << "Testing -=";
48 TESTEQOP(-=)
49 cout << "\tOK" << endl << "Testing *=";
50 TESTEQOP(*=)
51 cout << "\tOK" << endl << "Testing /=";
52 TESTEQOP(/=)
53 }
54
55 // Test for shared data caused by using a copy constructor
56 void SharedDataTestCase::testCC()
57 {
58 cout << endl;
59 Data d(42, DataTypes::scalarShape);
60 Data shared(d);
61 d+=Data(20,DataTypes::scalarShape);
62 shared-=Data(42,DataTypes::scalarShape);
63 assert(shared.Lsup()<0.001);
64 }
65
66 // Test for shared data caused by using = operator
67 void SharedDataTestCase::testAssign()
68 {
69 cout << endl;
70 Data d(42, DataTypes::scalarShape);
71 Data shared=d;
72 d+=Data(20,DataTypes::scalarShape);
73 shared-=Data(42,DataTypes::scalarShape);
74 assert(shared.Lsup()<0.001);
75 }
76
77 void SharedDataTestCase::testSetToZero()
78 {
79 Data d((double)42,DataTypes::scalarShape);
80 Data L=d.delay();
81 L-=Data((double)42,DataTypes::scalarShape);
82 d.setToZero();
83 assert(L.Lsup()<0.001);
84 }
85
86 void SharedDataTestCase::testSetTaggedValueFromCPP()
87 {
88 Data d((double)42,DataTypes::scalarShape);
89 d.tag();
90 Data L=d.delay();
91 ValueType v(1,17);
92 d.setTaggedValueFromCPP(1,DataTypes::scalarShape,v);
93 L.resolve();
94 // at this point, d should have a tag and L should not
95 // unfortunately its a little tricky to find out what tags a Data object has so I'll use strings
96 string s=L.toString();
97 assert(s.find("Tag(1)")==string::npos); // if the tag shows up we have shared data
98 }
99
100 void SharedDataTestCase::testGetDataAtOffset()
101 {
102 Data d((double)42,DataTypes::scalarShape);
103 Data L=d.delay();
104 // now change the data directly
105 d.getDataAtOffset(0)=17;
106 assert(L.getDataAtOffset(0)==42);
107 }
108
109 void SharedDataTestCase::testGetDataPoint()
110 {
111 Data d((double)42,DataTypes::scalarShape);
112 Data L=d.delay();
113 // now change the data directly
114 d.getDataPoint(0,0)=17;
115 assert(L.getDataPoint(0,0)==42);
116 }
117
118 void SharedDataTestCase::testGetSampleRW()
119 {
120 Data d((double)42,DataTypes::scalarShape);
121 Data L=d.delay();
122 // now change the data directly
123 *d.getSampleDataRW(0)=17;
124 L.resolve();
125 assert(*L.getSampleDataRO(0)==42);
126 }
127
128 TestSuite* SharedDataTestCase::suite ()
129 {
130 //
131 // create the suite of tests to perform.
132 TestSuite *testSuite = new TestSuite ("SharedDataTestCase");
133
134 testSuite->addTest (new TestCaller< SharedDataTestCase>("Arithmetic Assignment operators",&SharedDataTestCase::testEQ));
135 testSuite->addTest (new TestCaller< SharedDataTestCase>("Copy Constructor",&SharedDataTestCase::testCC));
136 testSuite->addTest (new TestCaller< SharedDataTestCase>("Assignment operator",&SharedDataTestCase::testAssign));
137 testSuite->addTest (new TestCaller< SharedDataTestCase>("setToZero",&SharedDataTestCase::testSetToZero));
138 testSuite->addTest (new TestCaller< SharedDataTestCase>("setTaggedValueFromCPP",&SharedDataTestCase::testSetTaggedValueFromCPP));
139 testSuite->addTest (new TestCaller< SharedDataTestCase>("getDataAtOffset",&SharedDataTestCase::testGetDataAtOffset));
140 testSuite->addTest (new TestCaller< SharedDataTestCase>("getDataPoint",&SharedDataTestCase::testGetDataPoint));
141 testSuite->addTest (new TestCaller< SharedDataTestCase>("getSampleRW",&SharedDataTestCase::testGetSampleRW));
142 return testSuite;
143 }

  ViewVC Help
Powered by ViewVC 1.1.26