/[escript]/trunk/escript/test/python/run_xml.py
ViewVC logotype

Diff of /trunk/escript/test/python/run_xml.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/escript/test/python/test_xml.py revision 155 by jgs, Wed Nov 9 02:02:19 2005 UTC trunk/escript/test/python/run_xml.py revision 2344 by jfenwick, Mon Mar 30 02:13:58 2009 UTC
# Line 1  Line 1 
 #!/usr/bin/python  
1    
2  # $Id$  ########################################################
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    __copyright__="""Copyright (c) 2003-2008 by University of Queensland
15    Earth Systems Science Computational Center (ESSCC)
16    http://www.uq.edu.au/esscc
17    Primary Business: Queensland, Australia"""
18    __license__="""Licensed under the Open Software License version 3.0
19    http://www.opensource.org/licenses/osl-3.0.php"""
20    __url__="https://launchpad.net/escript-finley"
21    
22  import unittest  import unittest
23  from esys.escript.modelframe import Model,Link,Simulation,ParameterSet,parse  from esys.escript.modelframe import Model,Link,Simulation,ParameterSet,ESySXMLParser,DataSource
24  import math  import math
25  from cStringIO import StringIO  from cStringIO import StringIO
26  from xml.dom import minidom  from xml.dom import minidom
27    import numarray
28    
29  class XMLDocumentTestCase(unittest.TestCase):  class XMLDocumentTestCase(unittest.TestCase):
30    
# Line 70  class SimulationTestCase(unittest.TestCa Line 88  class SimulationTestCase(unittest.TestCa
88                    
89      def testParseAndInstanceOfSimulation(self):      def testParseAndInstanceOfSimulation(self):
90                    
91          newSim = parse(self.xml)          xml = ESySXMLParser(self.xml)
92            newSim = xml.parse()[0]
93          assert (isinstance (newSim, Simulation))          assert (isinstance (newSim, Simulation))
94          newout = StringIO()          newout = StringIO()
95          newSim.writeXML(newout)          newSim.writeXML(newout)
# Line 136  class ParamaterSetTestCase(unittest.Test Line 155  class ParamaterSetTestCase(unittest.Test
155          assert ("gamma1" in xmlout)          assert ("gamma1" in xmlout)
156          assert ("gamma2" in xmlout)          assert ("gamma2" in xmlout)
157          assert ("gamma3" in xmlout)          assert ("gamma3" in xmlout)
158          parsable = parse(xmlout)          esysxml=ESySXMLParser(xmlout)
159            parsable = esysxml.parse()[0]
160          assert (isinstance (parsable, ParameterSet))          assert (isinstance (parsable, ParameterSet))
161          assert (self._dom().getElementsByTagName("ParameterSet"))          assert (self._dom(self.p).getElementsByTagName("ParameterSet"))
162    
163      def testParameterSetFromXML(self):      def testParameterSetFromXML(self):
164          doc = self._class()          s = StringIO()
165          pset = ParameterSet.fromDom(self._dom().getElementsByTagName("ParameterSet")[0])          self.p.writeXML(s)
166            s.reset()
167            xmlout = s.read()
168            esysxml=ESySXMLParser(xmlout)
169            doc = self._class(self.p)
170            pset = ParameterSet.fromDom(esysxml, self._dom(self.p).getElementsByTagName("ParameterSet")[0])
171          assert (isinstance(pset, ParameterSet))          assert (isinstance(pset, ParameterSet))
172          assert (isinstance(doc, ParameterSet))          assert (isinstance(doc, ParameterSet))
173          self.assertEqual(self.p.gamma1,doc.gamma1)          self.assertEqual(self.p.gamma1,doc.gamma1)
# Line 152  class ParamaterSetTestCase(unittest.Test Line 177  class ParamaterSetTestCase(unittest.Test
177          p2 = ParameterSet()          p2 = ParameterSet()
178          p2.declareParameter(s="abc", f=3.)          p2.declareParameter(s="abc", f=3.)
179          self.p.declareParameter(child=p2)          self.p.declareParameter(child=p2)
180          doc = self._class()          doc = self._class(self.p)
181          #pset = ParameterSet.fromDom(doc.getElementsByTagName("ParameterSet")[0])          #pset = ParameterSet.fromDom(doc.getElementsByTagName("ParameterSet")[0])
182          self.assertEqual(self.p.child.f, doc.child.f)          self.assertEqual(self.p.child.f, doc.child.f)
183    
# Line 163  class ParamaterSetTestCase(unittest.Test Line 188  class ParamaterSetTestCase(unittest.Test
188          self.assertEqual(self.p.child.s, "abc")          self.assertEqual(self.p.child.s, "abc")
189          self.assertEqual(self.p.child.f, 3.)          self.assertEqual(self.p.child.f, 3.)
190    
191      def _dom(self):      def _dom(self, input):
192          s = StringIO()          s = StringIO()
193          self.p.writeXML(s)          input.writeXML(s)
194          s.reset()          s.reset()
195          xmlout = s.read()          xmlout = s.read()
196          doc = minidom.parseString(xmlout)          doc = minidom.parseString(xmlout)
197          return doc          return doc
198    
199      def _class(self):      def _class(self, input):
200          s = StringIO()          s = StringIO()
201          self.p.writeXML(s)          input.writeXML(s)
202          s.reset()          s.reset()
203          xmlout = s.read()          xmlout = s.read()
204          doc = parse(xmlout)          esysxml=ESySXMLParser(xmlout)
205            doc =esysxml.parse()[0]
206          return doc          return doc
207    
208        def testFromDomInt(self):
209            p3 = ParameterSet()
210            p3.declareParameter(inttest=1)
211            doc = self._class(p3)
212            assert type(doc.inttest)==int
213    
214        def testFromDomNumarrayVector(self):
215            p3 = ParameterSet()
216            mynumarray = numarray.array([3., 4., 5.], type=numarray.Float64)
217            p3.declareParameter(numtest=mynumarray)
218            doc = self._class(p3)
219            assert doc.numtest.type() == numarray.Float64
220            assert type(doc.numtest) == numarray.NumArray
221    
222        def testFromDomNumarrayMulti(self):
223            p3 = ParameterSet()
224            mynumarray = numarray.array([[1., 2., 3.], [3., 4., 5.]], type=numarray.Float64)
225            p3.declareParameter(numtest=mynumarray)
226            doc = self._class(p3)
227            assert doc.numtest.type() == numarray.Float64
228            assert type(doc.numtest) == numarray.NumArray
229    
230        def testBoolLists(self):
231            p4 = ParameterSet()
232            mylist = [True, False, False, True]
233            p4.declareParameter(listest=mylist)
234            doc = self._class(p4)
235            assert type(doc.listest) == numarray.NumArray
236            assert doc.listest.type() == numarray.Bool
237            assert len(doc.listest) == len(mylist)
238            assert min([ mylist[i] == doc.listest[i] for i in range(len( doc.listest)) ])
239    
240        def testIntLists(self):
241            p4 = ParameterSet()
242            mylist = [1,2,4]
243            p4.declareParameter(listest=mylist)
244            doc = self._class(p4)
245            assert type(doc.listest) == numarray.NumArray
246            assert doc.listest.type() == numarray.Int
247            assert len(doc.listest) == len(mylist)
248            assert min([ mylist[i] == doc.listest[i] for i in range(len( doc.listest)) ])
249    
250        def testFloatLists(self):
251            p4 = ParameterSet()
252            mylist = [1.,2.,4.]
253            p4.declareParameter(listest=mylist)
254            doc = self._class(p4)
255            assert type(doc.listest) == numarray.NumArray
256            assert doc.listest.type() == numarray.Float
257            assert len(doc.listest) == len(mylist)
258            assert min([ mylist[i] == doc.listest[i] for i in range(len( doc.listest)) ])
259    
260        def testStringLists(self):
261            p4 = ParameterSet()
262            mylist = ["a", "b", "c"]
263            p4.declareParameter(listest=mylist)
264            doc = self._class(p4)
265            assert type(doc.listest) == list
266            assert len(doc.listest) == len(mylist)
267            assert min([ mylist[i] == doc.listest[i] for i in range(len( doc.listest)) ])
268            
269        def testDatasource(self):
270            p5 = ParameterSet()
271            myURI = DataSource("somelocalfile.txt", "text")
272            p5.declareParameter(uritest=myURI)
273            doc = self._class(p5)
274            self.assertEquals(myURI.uri, doc.uritest.uri)
275            self.assertEquals(myURI.fileformat, doc.uritest.fileformat)
276            assert type(doc.uritest) == DataSource
277            
278    
279          
280  class ModeltoDomTestCase(unittest.TestCase):  class ModeltoDomTestCase(unittest.TestCase):
281            
282      def _class(self):      def _class(self):
# Line 187  class ModeltoDomTestCase(unittest.TestCa Line 285  class ModeltoDomTestCase(unittest.TestCa
285          self.o1.writeXML(s)          self.o1.writeXML(s)
286          s.reset()          s.reset()
287          self.xmlout = s.read()          self.xmlout = s.read()
288          doc = parse(self.xmlout)          esysxml=ESySXMLParser(xmlout)
289            doc =esysxml.parse()[0]
290          return doc          return doc
291    
292      def _dom(self):      def _dom(self):
# Line 243  type="float"> <Name> tend </Name> <Value Line 342  type="float"> <Name> tend </Name> <Value
342    <Component rank="2"> <Model id="129" type="Messenger"> <Parameter    <Component rank="2"> <Model id="129" type="Messenger"> <Parameter
343    type="Link"> <Name> message </Name> <Value> <Link> <Target> 127 </Target>    type="Link"> <Name> message </Name> <Value> <Link> <Target> 127 </Target>
344    <Attribute> message </Attribute> </Link> </Value> </Parameter> </Model>    <Attribute> message </Attribute> </Link> </Value> </Parameter> </Model>
345    </Component> </Simulation> <Model id="128" type="ODETEST"> <Parameter    </Component> </Simulation> <Model id="150" type="ODETEST"> <Parameter
346    type="float"> <Name> a </Name> <Value>    type="float"> <Name> a </Name> <Value>
347  0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>  0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>
348    <Link> <Target> 127 </Target> <Attribute> u </Attribute> </Link> </Value>    <Link> <Target> 127 </Target> <Attribute> u </Attribute> </Link> </Value>
# Line 253  type="float"> <Name> tend </Name> <Value Line 352  type="float"> <Name> tend </Name> <Value
352    <Value> 1e-08 </Value> </Parameter> <Parameter type="float"> <Name> dt    <Value> 1e-08 </Value> </Parameter> <Parameter type="float"> <Name> dt
353    </Name> <Value>    </Name> <Value>
354  0.1 </Value> </Parameter> <Parameter type="str"> <Name> message </Name> <Value>  0.1 </Value> </Parameter> <Parameter type="str"> <Name> message </Name> <Value>
355    current error = 1.904837e+01 </Value> </Parameter> </Model> <Model id="127"    current error = 1.904837e+01 </Value> </Parameter> </Model> <Model id="130"
356    type="ODETEST"> <Parameter type="float"> <Name> a </Name> <Value>    type="ODETEST"> <Parameter type="float"> <Name> a </Name> <Value>
357  0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>  0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>
358    <Link> <Target> 128 </Target> <Attribute> u </Attribute> </Link> </Value>    <Link> <Target> 128 </Target> <Attribute> u </Attribute> </Link> </Value>
# Line 264  type="float"> <Name> tend </Name> <Value Line 363  type="float"> <Name> tend </Name> <Value
363    <Value>    <Value>
364  0.01 </Value> </Parameter> <Parameter type="str"> <Name> message </Name>  0.01 </Value> </Parameter> <Parameter type="str"> <Name> message </Name>
365    <Value> current error = 9.516258e-01 </Value> </Parameter> </Model> <Model    <Value> current error = 9.516258e-01 </Value> </Parameter> </Model> <Model
366    id="127" type="ODETEST"> <Parameter type="float"> <Name> a </Name> <Value>    id="170" type="ODETEST"> <Parameter type="float"> <Name> a </Name> <Value>
367  0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>  0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>
368    <Link> <Target> 128 </Target> <Attribute> u </Attribute> </Link> </Value>    <Link> <Target> 128 </Target> <Attribute> u </Attribute> </Link> </Value>
369    </Parameter> <Parameter type="float"> <Name> tend </Name> <Value>    </Parameter> <Parameter type="float"> <Name> tend </Name> <Value>
# Line 277  type="float"> <Name> tend </Name> <Value Line 376  type="float"> <Name> tend </Name> <Value
376  ''' % (modulename, modelname)  ''' % (modulename, modelname)
377    
378      def testModuleAttribute(self):      def testModuleAttribute(self):
379          modeldoc = parse(self._xml('test_xml', 'ODETEST'))          esysxml=ESySXMLParser(self._xml('run_xml', 'ODETEST'))
380            modeldoc=esysxml.parse()[0]
381    
382      def testModuleAttributeFails(self):      def testModuleAttributeFails(self):
383          try:          try:
384              modeldoc = parse(self._xml('a', 'b'))              esysxml=ESySXMLParser(self._xml('a', 'b'))
385                modeldoc=esysxml.parse()[0]
386          except ImportError:          except ImportError:
387              return # correct              return # correct
388    
# Line 362  class ODETEST(Model): Line 463  class ODETEST(Model):
463    
464            
465  if __name__ == "__main__":  if __name__ == "__main__":
466      from test_xml import Messenger, ODETEST      suite = unittest.TestSuite()
467      unittest.main()      suite.addTest(unittest.makeSuite(XMLDocumentTestCase))
468        suite.addTest(unittest.makeSuite(SimulationTestCase))
469        suite.addTest(unittest.makeSuite(LinkTestCase))
470        suite.addTest(unittest.makeSuite(ParamaterSetTestCase))
471        suite.addTest(unittest.makeSuite(ModeltoDomTestCase))
472        suite.addTest(unittest.makeSuite(ModeltoDomTestCase))
473        suite.addTest(unittest.makeSuite(Messenger))
474        suite.addTest(unittest.makeSuite(ODETEST))
475        s=unittest.TextTestRunner(verbosity=2).run(suite)
476        if not s.wasSuccessful(): sys.exit(1)

Legend:
Removed from v.155  
changed lines
  Added in v.2344

  ViewVC Help
Powered by ViewVC 1.1.26