/[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/run_xml.py revision 700 by gross, Thu Apr 6 00:13:40 2006 UTC temp_trunk_copy/escript/test/python/run_xml.py revision 1384 by phornby, Fri Jan 11 02:29:38 2008 UTC
# Line 1  Line 1 
1  #!/usr/bin/python  #!/usr/bin/python
2    
3    #
4  # $Id$  # $Id$
5    #
6    #######################################################
7    #
8    #           Copyright 2003-2007 by ACceSS MNRF
9    #       Copyright 2007 by University of Queensland
10    #
11    #                http://esscc.uq.edu.au
12    #        Primary Business: Queensland, Australia
13    #  Licensed under the Open Software License version 3.0
14    #     http://www.opensource.org/licenses/osl-3.0.php
15    #
16    #######################################################
17    #
18    
19  __copyright__="""  Copyright (c) 2006 by ACcESS MNRF  __copyright__="""  Copyright (c) 2006 by ACcESS MNRF
20                      http://www.access.edu.au                      http://www.access.edu.au
21                  Primary Business: Queensland, Australia"""                  Primary Business: Queensland, Australia"""
# Line 8  __license__="""Licensed under the Open S Line 23  __license__="""Licensed under the Open S
23               http://www.opensource.org/licenses/osl-3.0.php"""               http://www.opensource.org/licenses/osl-3.0.php"""
24    
25  import unittest  import unittest
26  from esys.escript.modelframe import Model,Link,Simulation,ParameterSet,parse  from esys.escript.modelframe import Model,Link,Simulation,ParameterSet,ESySXMLParser,DataSource
27  import math  import math
28  from cStringIO import StringIO  from cStringIO import StringIO
29  from xml.dom import minidom  from xml.dom import minidom
30    import numarray
31    
32  class XMLDocumentTestCase(unittest.TestCase):  class XMLDocumentTestCase(unittest.TestCase):
33    
# Line 75  class SimulationTestCase(unittest.TestCa Line 91  class SimulationTestCase(unittest.TestCa
91                    
92      def testParseAndInstanceOfSimulation(self):      def testParseAndInstanceOfSimulation(self):
93                    
94          newSim = parse(self.xml)          xml = ESySXMLParser(self.xml)
95            newSim = xml.parse()[0]
96          assert (isinstance (newSim, Simulation))          assert (isinstance (newSim, Simulation))
97          newout = StringIO()          newout = StringIO()
98          newSim.writeXML(newout)          newSim.writeXML(newout)
# Line 141  class ParamaterSetTestCase(unittest.Test Line 158  class ParamaterSetTestCase(unittest.Test
158          assert ("gamma1" in xmlout)          assert ("gamma1" in xmlout)
159          assert ("gamma2" in xmlout)          assert ("gamma2" in xmlout)
160          assert ("gamma3" in xmlout)          assert ("gamma3" in xmlout)
161          parsable = parse(xmlout)          esysxml=ESySXMLParser(xmlout)
162            parsable = esysxml.parse()[0]
163          assert (isinstance (parsable, ParameterSet))          assert (isinstance (parsable, ParameterSet))
164          assert (self._dom().getElementsByTagName("ParameterSet"))          assert (self._dom(self.p).getElementsByTagName("ParameterSet"))
165    
166      def testParameterSetFromXML(self):      def testParameterSetFromXML(self):
167          doc = self._class()          s = StringIO()
168          pset = ParameterSet.fromDom(self._dom().getElementsByTagName("ParameterSet")[0])          self.p.writeXML(s)
169            s.reset()
170            xmlout = s.read()
171            esysxml=ESySXMLParser(xmlout)
172            doc = self._class(self.p)
173            pset = ParameterSet.fromDom(esysxml, self._dom(self.p).getElementsByTagName("ParameterSet")[0])
174          assert (isinstance(pset, ParameterSet))          assert (isinstance(pset, ParameterSet))
175          assert (isinstance(doc, ParameterSet))          assert (isinstance(doc, ParameterSet))
176          self.assertEqual(self.p.gamma1,doc.gamma1)          self.assertEqual(self.p.gamma1,doc.gamma1)
# Line 157  class ParamaterSetTestCase(unittest.Test Line 180  class ParamaterSetTestCase(unittest.Test
180          p2 = ParameterSet()          p2 = ParameterSet()
181          p2.declareParameter(s="abc", f=3.)          p2.declareParameter(s="abc", f=3.)
182          self.p.declareParameter(child=p2)          self.p.declareParameter(child=p2)
183          doc = self._class()          doc = self._class(self.p)
184          #pset = ParameterSet.fromDom(doc.getElementsByTagName("ParameterSet")[0])          #pset = ParameterSet.fromDom(doc.getElementsByTagName("ParameterSet")[0])
185          self.assertEqual(self.p.child.f, doc.child.f)          self.assertEqual(self.p.child.f, doc.child.f)
186    
# Line 168  class ParamaterSetTestCase(unittest.Test Line 191  class ParamaterSetTestCase(unittest.Test
191          self.assertEqual(self.p.child.s, "abc")          self.assertEqual(self.p.child.s, "abc")
192          self.assertEqual(self.p.child.f, 3.)          self.assertEqual(self.p.child.f, 3.)
193    
194      def _dom(self):      def _dom(self, input):
195          s = StringIO()          s = StringIO()
196          self.p.writeXML(s)          input.writeXML(s)
197          s.reset()          s.reset()
198          xmlout = s.read()          xmlout = s.read()
199          doc = minidom.parseString(xmlout)          doc = minidom.parseString(xmlout)
200          return doc          return doc
201    
202      def _class(self):      def _class(self, input):
203          s = StringIO()          s = StringIO()
204          self.p.writeXML(s)          input.writeXML(s)
205          s.reset()          s.reset()
206          xmlout = s.read()          xmlout = s.read()
207          doc = parse(xmlout)          esysxml=ESySXMLParser(xmlout)
208            doc =esysxml.parse()[0]
209          return doc          return doc
210    
211        def testFromDomInt(self):
212            p3 = ParameterSet()
213            p3.declareParameter(inttest=1)
214            doc = self._class(p3)
215            assert type(doc.inttest)==int
216    
217        def testFromDomNumarrayVector(self):
218            p3 = ParameterSet()
219            mynumarray = numarray.array([3., 4., 5.], type=numarray.Float64)
220            p3.declareParameter(numtest=mynumarray)
221            doc = self._class(p3)
222            assert doc.numtest.type() == numarray.Float64
223            assert type(doc.numtest) == numarray.NumArray
224    
225        def testFromDomNumarrayMulti(self):
226            p3 = ParameterSet()
227            mynumarray = numarray.array([[1., 2., 3.], [3., 4., 5.]], type=numarray.Float64)
228            p3.declareParameter(numtest=mynumarray)
229            doc = self._class(p3)
230            assert doc.numtest.type() == numarray.Float64
231            assert type(doc.numtest) == numarray.NumArray
232    
233        def testBoolLists(self):
234            p4 = ParameterSet()
235            mylist = [True, False, False, True]
236            p4.declareParameter(listest=mylist)
237            doc = self._class(p4)
238            assert type(doc.listest) == numarray.NumArray
239            assert doc.listest.type() == numarray.Bool
240            assert len(doc.listest) == len(mylist)
241            assert min([ mylist[i] == doc.listest[i] for i in range(len( doc.listest)) ])
242    
243        def testIntLists(self):
244            p4 = ParameterSet()
245            mylist = [1,2,4]
246            p4.declareParameter(listest=mylist)
247            doc = self._class(p4)
248            assert type(doc.listest) == numarray.NumArray
249            assert doc.listest.type() == numarray.Int
250            assert len(doc.listest) == len(mylist)
251            assert min([ mylist[i] == doc.listest[i] for i in range(len( doc.listest)) ])
252    
253        def testFloatLists(self):
254            p4 = ParameterSet()
255            mylist = [1.,2.,4.]
256            p4.declareParameter(listest=mylist)
257            doc = self._class(p4)
258            assert type(doc.listest) == numarray.NumArray
259            assert doc.listest.type() == numarray.Float
260            assert len(doc.listest) == len(mylist)
261            assert min([ mylist[i] == doc.listest[i] for i in range(len( doc.listest)) ])
262    
263        def testStringLists(self):
264            p4 = ParameterSet()
265            mylist = ["a", "b", "c"]
266            p4.declareParameter(listest=mylist)
267            doc = self._class(p4)
268            assert type(doc.listest) == list
269            assert len(doc.listest) == len(mylist)
270            assert min([ mylist[i] == doc.listest[i] for i in range(len( doc.listest)) ])
271            
272        def testDatasource(self):
273            p5 = ParameterSet()
274            myURI = DataSource("somelocalfile.txt", "text")
275            p5.declareParameter(uritest=myURI)
276            doc = self._class(p5)
277            self.assertEquals(myURI.uri, doc.uritest.uri)
278            self.assertEquals(myURI.fileformat, doc.uritest.fileformat)
279            assert type(doc.uritest) == DataSource
280            
281    
282          
283  class ModeltoDomTestCase(unittest.TestCase):  class ModeltoDomTestCase(unittest.TestCase):
284            
285      def _class(self):      def _class(self):
# Line 192  class ModeltoDomTestCase(unittest.TestCa Line 288  class ModeltoDomTestCase(unittest.TestCa
288          self.o1.writeXML(s)          self.o1.writeXML(s)
289          s.reset()          s.reset()
290          self.xmlout = s.read()          self.xmlout = s.read()
291          doc = parse(self.xmlout)          esysxml=ESySXMLParser(xmlout)
292            doc =esysxml.parse()[0]
293          return doc          return doc
294    
295      def _dom(self):      def _dom(self):
# Line 248  type="float"> <Name> tend </Name> <Value Line 345  type="float"> <Name> tend </Name> <Value
345    <Component rank="2"> <Model id="129" type="Messenger"> <Parameter    <Component rank="2"> <Model id="129" type="Messenger"> <Parameter
346    type="Link"> <Name> message </Name> <Value> <Link> <Target> 127 </Target>    type="Link"> <Name> message </Name> <Value> <Link> <Target> 127 </Target>
347    <Attribute> message </Attribute> </Link> </Value> </Parameter> </Model>    <Attribute> message </Attribute> </Link> </Value> </Parameter> </Model>
348    </Component> </Simulation> <Model id="128" type="ODETEST"> <Parameter    </Component> </Simulation> <Model id="150" type="ODETEST"> <Parameter
349    type="float"> <Name> a </Name> <Value>    type="float"> <Name> a </Name> <Value>
350  0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>  0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>
351    <Link> <Target> 127 </Target> <Attribute> u </Attribute> </Link> </Value>    <Link> <Target> 127 </Target> <Attribute> u </Attribute> </Link> </Value>
# Line 258  type="float"> <Name> tend </Name> <Value Line 355  type="float"> <Name> tend </Name> <Value
355    <Value> 1e-08 </Value> </Parameter> <Parameter type="float"> <Name> dt    <Value> 1e-08 </Value> </Parameter> <Parameter type="float"> <Name> dt
356    </Name> <Value>    </Name> <Value>
357  0.1 </Value> </Parameter> <Parameter type="str"> <Name> message </Name> <Value>  0.1 </Value> </Parameter> <Parameter type="str"> <Name> message </Name> <Value>
358    current error = 1.904837e+01 </Value> </Parameter> </Model> <Model id="127"    current error = 1.904837e+01 </Value> </Parameter> </Model> <Model id="130"
359    type="ODETEST"> <Parameter type="float"> <Name> a </Name> <Value>    type="ODETEST"> <Parameter type="float"> <Name> a </Name> <Value>
360  0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>  0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>
361    <Link> <Target> 128 </Target> <Attribute> u </Attribute> </Link> </Value>    <Link> <Target> 128 </Target> <Attribute> u </Attribute> </Link> </Value>
# Line 269  type="float"> <Name> tend </Name> <Value Line 366  type="float"> <Name> tend </Name> <Value
366    <Value>    <Value>
367  0.01 </Value> </Parameter> <Parameter type="str"> <Name> message </Name>  0.01 </Value> </Parameter> <Parameter type="str"> <Name> message </Name>
368    <Value> current error = 9.516258e-01 </Value> </Parameter> </Model> <Model    <Value> current error = 9.516258e-01 </Value> </Parameter> </Model> <Model
369    id="127" type="ODETEST"> <Parameter type="float"> <Name> a </Name> <Value>    id="170" type="ODETEST"> <Parameter type="float"> <Name> a </Name> <Value>
370  0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>  0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>
371    <Link> <Target> 128 </Target> <Attribute> u </Attribute> </Link> </Value>    <Link> <Target> 128 </Target> <Attribute> u </Attribute> </Link> </Value>
372    </Parameter> <Parameter type="float"> <Name> tend </Name> <Value>    </Parameter> <Parameter type="float"> <Name> tend </Name> <Value>
# Line 282  type="float"> <Name> tend </Name> <Value Line 379  type="float"> <Name> tend </Name> <Value
379  ''' % (modulename, modelname)  ''' % (modulename, modelname)
380    
381      def testModuleAttribute(self):      def testModuleAttribute(self):
382          modeldoc = parse(self._xml('test_xml', 'ODETEST'))          esysxml=ESySXMLParser(self._xml('run_xml', 'ODETEST'))
383            modeldoc=esysxml.parse()[0]
384    
385      def testModuleAttributeFails(self):      def testModuleAttributeFails(self):
386          try:          try:
387              modeldoc = parse(self._xml('a', 'b'))              esysxml=ESySXMLParser(self._xml('a', 'b'))
388                modeldoc=esysxml.parse()[0]
389          except ImportError:          except ImportError:
390              return # correct              return # correct
391    
# Line 367  class ODETEST(Model): Line 466  class ODETEST(Model):
466    
467            
468  if __name__ == "__main__":  if __name__ == "__main__":
469      from test_xml import Messenger, ODETEST      suite = unittest.TestSuite()
470      unittest.main()      suite.addTest(unittest.makeSuite(XMLDocumentTestCase))
471        suite.addTest(unittest.makeSuite(SimulationTestCase))
472        suite.addTest(unittest.makeSuite(LinkTestCase))
473        suite.addTest(unittest.makeSuite(ParamaterSetTestCase))
474        suite.addTest(unittest.makeSuite(ModeltoDomTestCase))
475        suite.addTest(unittest.makeSuite(ModeltoDomTestCase))
476        suite.addTest(unittest.makeSuite(Messenger))
477        suite.addTest(unittest.makeSuite(ODETEST))
478        s=unittest.TextTestRunner(verbosity=2).run(suite)
479        if not s.wasSuccessful(): sys.exit(1)

Legend:
Removed from v.700  
changed lines
  Added in v.1384

  ViewVC Help
Powered by ViewVC 1.1.26