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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2271 - (show annotations)
Mon Feb 16 05:08:29 2009 UTC (10 years, 9 months ago) by jfenwick
File size: 4782 byte(s)
Merging version 2269 to trunk

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.requireWrite();
106 d.getDataAtOffsetRW(0)=17;
107 assert(L.getDataAtOffsetRO(0)==42);
108 }
109
110 void SharedDataTestCase::testGetDataPoint()
111 {
112 Data d((double)42,DataTypes::scalarShape);
113 Data L=d.delay();
114 // now change the data directly
115 d.requireWrite();
116 d.getDataPointRW(0,0)=17;
117 assert(L.getDataPointRO(0,0)==42);
118 }
119
120 void SharedDataTestCase::testGetSampleRW()
121 {
122 Data d((double)42,DataTypes::scalarShape);
123 Data L=d.delay();
124 // now change the data directly
125 try
126 {
127 *d.getSampleDataRW(0)=17;
128 assert(false); // should have thrown
129 } catch (DataException e)
130 {
131 }
132 // Now try again properly
133 d.requireWrite();
134 *d.getSampleDataRW(0)=17;
135 L.resolve();
136 assert(*L.getSampleDataRO(0)==42);
137 }
138
139 TestSuite* SharedDataTestCase::suite ()
140 {
141 //
142 // create the suite of tests to perform.
143 TestSuite *testSuite = new TestSuite ("SharedDataTestCase");
144
145 testSuite->addTest (new TestCaller< SharedDataTestCase>("Arithmetic Assignment operators",&SharedDataTestCase::testEQ));
146 testSuite->addTest (new TestCaller< SharedDataTestCase>("Copy Constructor",&SharedDataTestCase::testCC));
147 testSuite->addTest (new TestCaller< SharedDataTestCase>("Assignment operator",&SharedDataTestCase::testAssign));
148 testSuite->addTest (new TestCaller< SharedDataTestCase>("setToZero",&SharedDataTestCase::testSetToZero));
149 testSuite->addTest (new TestCaller< SharedDataTestCase>("setTaggedValueFromCPP",&SharedDataTestCase::testSetTaggedValueFromCPP));
150 testSuite->addTest (new TestCaller< SharedDataTestCase>("getDataAtOffset",&SharedDataTestCase::testGetDataAtOffset));
151 testSuite->addTest (new TestCaller< SharedDataTestCase>("getDataPoint",&SharedDataTestCase::testGetDataPoint));
152 testSuite->addTest (new TestCaller< SharedDataTestCase>("getSampleRW",&SharedDataTestCase::testGetSampleRW));
153 return testSuite;
154 }

  ViewVC Help
Powered by ViewVC 1.1.26