/[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

revision 874 by elspeth, Tue Oct 17 12:06:11 2006 UTC revision 2468 by gross, Thu Jun 11 02:55:51 2009 UTC
# Line 1  Line 1 
 #!/usr/bin/python  
1    
2  # $Id$  ########################################################
3  __copyright__="""  Copyright (c) 2006 by ACcESS MNRF  #
4                      http://www.access.edu.au  # Copyright (c) 2003-2008 by University of Queensland
5                  Primary Business: Queensland, Australia"""  # 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  __license__="""Licensed under the Open Software License version 3.0
19               http://www.opensource.org/licenses/osl-3.0.php"""  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 numpy
28    import sys
29    
30  class XMLDocumentTestCase(unittest.TestCase):  class XMLDocumentTestCase(unittest.TestCase):
31    
# Line 75  class SimulationTestCase(unittest.TestCa Line 89  class SimulationTestCase(unittest.TestCa
89                    
90      def testParseAndInstanceOfSimulation(self):      def testParseAndInstanceOfSimulation(self):
91                    
92          newSim = parse(self.xml)          xml = ESySXMLParser(self.xml)
93          assert (isinstance (newSim, Simulation))          newSim = xml.parse()[0]
94            assert isinstance(newSim, Simulation)
95          newout = StringIO()          newout = StringIO()
96          newSim.writeXML(newout)          newSim.writeXML(newout)
97          newout.reset()          newout.reset()
# Line 141  class ParamaterSetTestCase(unittest.Test Line 156  class ParamaterSetTestCase(unittest.Test
156          assert ("gamma1" in xmlout)          assert ("gamma1" in xmlout)
157          assert ("gamma2" in xmlout)          assert ("gamma2" in xmlout)
158          assert ("gamma3" in xmlout)          assert ("gamma3" in xmlout)
159          parsable = parse(xmlout)          esysxml=ESySXMLParser(xmlout)
160            parsable = esysxml.parse()[0]
161          assert (isinstance (parsable, ParameterSet))          assert (isinstance (parsable, ParameterSet))
162          assert (self._dom(self.p).getElementsByTagName("ParameterSet"))          assert (self._dom(self.p).getElementsByTagName("ParameterSet"))
163    
164      def testParameterSetFromXML(self):      def testParameterSetFromXML(self):
165            s = StringIO()
166            self.p.writeXML(s)
167            s.reset()
168            xmlout = s.read()
169            esysxml=ESySXMLParser(xmlout)
170          doc = self._class(self.p)          doc = self._class(self.p)
171          pset = ParameterSet.fromDom(self._dom(self.p).getElementsByTagName("ParameterSet")[0])          pset = ParameterSet.fromDom(esysxml, self._dom(self.p).getElementsByTagName("ParameterSet")[0])
172          assert (isinstance(pset, ParameterSet))          assert (isinstance(pset, ParameterSet))
173          assert (isinstance(doc, ParameterSet))          assert (isinstance(doc, ParameterSet))
174          self.assertEqual(self.p.gamma1,doc.gamma1)          self.assertEqual(self.p.gamma1,doc.gamma1)
# Line 181  class ParamaterSetTestCase(unittest.Test Line 202  class ParamaterSetTestCase(unittest.Test
202          input.writeXML(s)          input.writeXML(s)
203          s.reset()          s.reset()
204          xmlout = s.read()          xmlout = s.read()
205          doc = parse(xmlout)          esysxml=ESySXMLParser(xmlout)
206            doc =esysxml.parse()[0]
207          return doc          return doc
208    
209      def testFromDomInt(self):      def testFromDomInt(self):
# Line 191  class ParamaterSetTestCase(unittest.Test Line 213  class ParamaterSetTestCase(unittest.Test
213          assert type(doc.inttest)==int          assert type(doc.inttest)==int
214    
215      def testFromDomNumarrayVector(self):      def testFromDomNumarrayVector(self):
         import numarray  
216          p3 = ParameterSet()          p3 = ParameterSet()
217          mynumarray = numarray.array([3., 4., 5.], type=numarray.Float64)          mynumpy = numpy.array([3., 4., 5.], dtype=numpy.float64)
218          p3.declareParameter(numtest=mynumarray)          p3.declareParameter(numtest=mynumpy)
219          doc = self._class(p3)          doc = self._class(p3)
220          assert doc.numtest.type() == numarray.Float64          assert doc.numtest.dtype == numpy.float64
221          assert type(doc.numtest) == numarray.NumArray          assert type(doc.numtest) == numpy.ndarray
222    
223      def testFromDomNumarrayMulti(self):      def testFromDomNumarrayMulti(self):
         import numarray  
224          p3 = ParameterSet()          p3 = ParameterSet()
225          mynumarray = numarray.array([[1., 2., 3.], [3., 4., 5.]], type=numarray.Float64)          mynumpy = numpy.array([[1., 2., 3.], [3., 4., 5.]], dtype=numpy.float64)
226          p3.declareParameter(numtest=mynumarray)          p3.declareParameter(numtest=mynumpy)
227          doc = self._class(p3)          doc = self._class(p3)
228          assert doc.numtest.type() == numarray.Float64          assert doc.numtest.dtype == numpy.float64
229          assert type(doc.numtest) == numarray.NumArray          assert type(doc.numtest) == numpy.ndarray
230    
231      def testLists(self):  # This method is disabled until the correct behaviour for lists of bools is decided
232    #numarray converts them into ints whereas numpy leaves them as bools
233        def testBoolLists(self):
234          p4 = ParameterSet()          p4 = ParameterSet()
235          mylist = [True, False, False, True]          mylist = [True, False, False, True]
236          p4.declareParameter(listest=mylist)          p4.declareParameter(listest=mylist)
237          doc = self._class(p4)          doc = self._class(p4)
238            assert type(doc.listest) == numpy.ndarray
239            assert doc.listest.dtype == numpy.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) == numpy.ndarray
249            assert doc.listest.dtype == numpy.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) == numpy.ndarray
259            assert doc.listest.dtype == numpy.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          assert type(doc.listest) == list
269          self.assertEquals(mylist, doc.listest)          assert len(doc.listest) == len(mylist)
270          assert type(doc.listest[0]) == bool          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 226  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 244  class ModeltoDomTestCase(unittest.TestCa Line 307  class ModeltoDomTestCase(unittest.TestCa
307    
308      def testModelExists(self):      def testModelExists(self):
309          modeldoc = self._class()          modeldoc = self._class()
310          assert (isinstance, (modeldoc, Model))          assert isinstance(modeldoc, Model)
311          assert self._dom().getElementsByTagName("Model")          assert self._dom().getElementsByTagName("Model")
312            
313      def testModelhasID(self):      def testModelhasID(self):
# Line 282  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 292  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 303  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 316  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('run_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 401  class ODETEST(Model): Line 466  class ODETEST(Model):
466    
467            
468  if __name__ == "__main__":  if __name__ == "__main__":
469      unittest.main()      suite = unittest.TestSuite()
470        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.874  
changed lines
  Added in v.2468

  ViewVC Help
Powered by ViewVC 1.1.26