/[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 2471 by jfenwick, Thu Jun 11 23:33:06 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):      def testBoolLists(self):
232          p4 = ParameterSet()          p4 = ParameterSet()
233          mylist = [True, False, False, True]          mylist = [True, False, False, True]
234          p4.declareParameter(listest=mylist)          p4.declareParameter(listest=mylist)
235          doc = self._class(p4)          doc = self._class(p4)
236            assert type(doc.listest) == numpy.ndarray
237            assert doc.listest.dtype == numpy.bool_
238            assert len(doc.listest) == len(mylist)
239            assert min([ mylist[i] == doc.listest[i] for i in range(len( doc.listest)) ])
240    
241        def testIntLists(self):
242            p4 = ParameterSet()
243            mylist = [1,2,4]
244            p4.declareParameter(listest=mylist)
245            doc = self._class(p4)
246            assert type(doc.listest) == numpy.ndarray
247            assert doc.listest.dtype == numpy.int_
248            assert len(doc.listest) == len(mylist)
249            assert min([ mylist[i] == doc.listest[i] for i in range(len( doc.listest)) ])
250    
251        def testFloatLists(self):
252            p4 = ParameterSet()
253            mylist = [1.,2.,4.]
254            p4.declareParameter(listest=mylist)
255            doc = self._class(p4)
256            assert type(doc.listest) == numpy.ndarray
257            assert doc.listest.dtype == numpy.float_
258            assert len(doc.listest) == len(mylist)
259            assert min([ mylist[i] == doc.listest[i] for i in range(len( doc.listest)) ])
260    
261        def testStringLists(self):
262            p4 = ParameterSet()
263            mylist = ["a", "b", "c"]
264            p4.declareParameter(listest=mylist)
265            doc = self._class(p4)
266          assert type(doc.listest) == list          assert type(doc.listest) == list
267          self.assertEquals(mylist, doc.listest)          assert len(doc.listest) == len(mylist)
268          assert type(doc.listest[0]) == bool          assert min([ mylist[i] == doc.listest[i] for i in range(len( doc.listest)) ])
269                    
270        def testDatasource(self):
271            p5 = ParameterSet()
272            myURI = DataSource("somelocalfile.txt", "text")
273            p5.declareParameter(uritest=myURI)
274            doc = self._class(p5)
275            self.assertEquals(myURI.uri, doc.uritest.uri)
276            self.assertEquals(myURI.fileformat, doc.uritest.fileformat)
277            assert type(doc.uritest) == DataSource
278                    
279    
280          
281  class ModeltoDomTestCase(unittest.TestCase):  class ModeltoDomTestCase(unittest.TestCase):
282            
283      def _class(self):      def _class(self):
# Line 226  class ModeltoDomTestCase(unittest.TestCa Line 286  class ModeltoDomTestCase(unittest.TestCa
286          self.o1.writeXML(s)          self.o1.writeXML(s)
287          s.reset()          s.reset()
288          self.xmlout = s.read()          self.xmlout = s.read()
289          doc = parse(self.xmlout)          esysxml=ESySXMLParser(xmlout)
290            doc =esysxml.parse()[0]
291          return doc          return doc
292    
293      def _dom(self):      def _dom(self):
# Line 244  class ModeltoDomTestCase(unittest.TestCa Line 305  class ModeltoDomTestCase(unittest.TestCa
305    
306      def testModelExists(self):      def testModelExists(self):
307          modeldoc = self._class()          modeldoc = self._class()
308          assert (isinstance, (modeldoc, Model))          assert isinstance(modeldoc, Model)
309          assert self._dom().getElementsByTagName("Model")          assert self._dom().getElementsByTagName("Model")
310            
311      def testModelhasID(self):      def testModelhasID(self):
# Line 282  type="float"> <Name> tend </Name> <Value Line 343  type="float"> <Name> tend </Name> <Value
343    <Component rank="2"> <Model id="129" type="Messenger"> <Parameter    <Component rank="2"> <Model id="129" type="Messenger"> <Parameter
344    type="Link"> <Name> message </Name> <Value> <Link> <Target> 127 </Target>    type="Link"> <Name> message </Name> <Value> <Link> <Target> 127 </Target>
345    <Attribute> message </Attribute> </Link> </Value> </Parameter> </Model>    <Attribute> message </Attribute> </Link> </Value> </Parameter> </Model>
346    </Component> </Simulation> <Model id="128" type="ODETEST"> <Parameter    </Component> </Simulation> <Model id="150" type="ODETEST"> <Parameter
347    type="float"> <Name> a </Name> <Value>    type="float"> <Name> a </Name> <Value>
348  0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>  0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>
349    <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 353  type="float"> <Name> tend </Name> <Value
353    <Value> 1e-08 </Value> </Parameter> <Parameter type="float"> <Name> dt    <Value> 1e-08 </Value> </Parameter> <Parameter type="float"> <Name> dt
354    </Name> <Value>    </Name> <Value>
355  0.1 </Value> </Parameter> <Parameter type="str"> <Name> message </Name> <Value>  0.1 </Value> </Parameter> <Parameter type="str"> <Name> message </Name> <Value>
356    current error = 1.904837e+01 </Value> </Parameter> </Model> <Model id="127"    current error = 1.904837e+01 </Value> </Parameter> </Model> <Model id="130"
357    type="ODETEST"> <Parameter type="float"> <Name> a </Name> <Value>    type="ODETEST"> <Parameter type="float"> <Name> a </Name> <Value>
358  0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>  0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>
359    <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 364  type="float"> <Name> tend </Name> <Value
364    <Value>    <Value>
365  0.01 </Value> </Parameter> <Parameter type="str"> <Name> message </Name>  0.01 </Value> </Parameter> <Parameter type="str"> <Name> message </Name>
366    <Value> current error = 9.516258e-01 </Value> </Parameter> </Model> <Model    <Value> current error = 9.516258e-01 </Value> </Parameter> </Model> <Model
367    id="127" type="ODETEST"> <Parameter type="float"> <Name> a </Name> <Value>    id="170" type="ODETEST"> <Parameter type="float"> <Name> a </Name> <Value>
368  0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>  0.9 </Value> </Parameter> <Parameter type="Link"> <Name> f </Name> <Value>
369    <Link> <Target> 128 </Target> <Attribute> u </Attribute> </Link> </Value>    <Link> <Target> 128 </Target> <Attribute> u </Attribute> </Link> </Value>
370    </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 377  type="float"> <Name> tend </Name> <Value
377  ''' % (modulename, modelname)  ''' % (modulename, modelname)
378    
379      def testModuleAttribute(self):      def testModuleAttribute(self):
380          modeldoc = parse(self._xml('run_xml', 'ODETEST'))          esysxml=ESySXMLParser(self._xml('run_xml', 'ODETEST'))
381            modeldoc=esysxml.parse()[0]
382    
383      def testModuleAttributeFails(self):      def testModuleAttributeFails(self):
384          try:          try:
385              modeldoc = parse(self._xml('a', 'b'))              esysxml=ESySXMLParser(self._xml('a', 'b'))
386                modeldoc=esysxml.parse()[0]
387          except ImportError:          except ImportError:
388              return # correct              return # correct
389    
# Line 401  class ODETEST(Model): Line 464  class ODETEST(Model):
464    
465            
466  if __name__ == "__main__":  if __name__ == "__main__":
467      unittest.main()      suite = unittest.TestSuite()
468        suite.addTest(unittest.makeSuite(XMLDocumentTestCase))
469        suite.addTest(unittest.makeSuite(SimulationTestCase))
470        suite.addTest(unittest.makeSuite(LinkTestCase))
471        suite.addTest(unittest.makeSuite(ParamaterSetTestCase))
472        suite.addTest(unittest.makeSuite(ModeltoDomTestCase))
473        suite.addTest(unittest.makeSuite(ModeltoDomTestCase))
474        suite.addTest(unittest.makeSuite(Messenger))
475        suite.addTest(unittest.makeSuite(ODETEST))
476        s=unittest.TextTestRunner(verbosity=2).run(suite)
477        if not s.wasSuccessful(): sys.exit(1)

Legend:
Removed from v.874  
changed lines
  Added in v.2471

  ViewVC Help
Powered by ViewVC 1.1.26