/[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 1809 by ksteube, Thu Sep 25 06:43:44 2008 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__="http://www.uq.edu.au/esscc/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 75  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 141  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(self.p).getElementsByTagName("ParameterSet"))          assert (self._dom(self.p).getElementsByTagName("ParameterSet"))
162    
163      def testParameterSetFromXML(self):      def testParameterSetFromXML(self):
164            s = StringIO()
165            self.p.writeXML(s)
166            s.reset()
167            xmlout = s.read()
168            esysxml=ESySXMLParser(xmlout)
169          doc = self._class(self.p)          doc = self._class(self.p)
170          pset = ParameterSet.fromDom(self._dom(self.p).getElementsByTagName("ParameterSet")[0])          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 181  class ParamaterSetTestCase(unittest.Test Line 201  class ParamaterSetTestCase(unittest.Test
201          input.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):      def testFromDomInt(self):
# Line 191  class ParamaterSetTestCase(unittest.Test Line 212  class ParamaterSetTestCase(unittest.Test
212          assert type(doc.inttest)==int          assert type(doc.inttest)==int
213    
214      def testFromDomNumarrayVector(self):      def testFromDomNumarrayVector(self):
         import numarray  
215          p3 = ParameterSet()          p3 = ParameterSet()
216          mynumarray = numarray.array([3., 4., 5.], type=numarray.Float64)          mynumarray = numarray.array([3., 4., 5.], type=numarray.Float64)
217          p3.declareParameter(numtest=mynumarray)          p3.declareParameter(numtest=mynumarray)
# Line 200  class ParamaterSetTestCase(unittest.Test Line 220  class ParamaterSetTestCase(unittest.Test
220          assert type(doc.numtest) == numarray.NumArray          assert type(doc.numtest) == numarray.NumArray
221    
222      def testFromDomNumarrayMulti(self):      def testFromDomNumarrayMulti(self):
         import numarray  
223          p3 = ParameterSet()          p3 = ParameterSet()
224          mynumarray = numarray.array([[1., 2., 3.], [3., 4., 5.]], type=numarray.Float64)          mynumarray = numarray.array([[1., 2., 3.], [3., 4., 5.]], type=numarray.Float64)
225          p3.declareParameter(numtest=mynumarray)          p3.declareParameter(numtest=mynumarray)
# Line 208  class ParamaterSetTestCase(unittest.Test Line 227  class ParamaterSetTestCase(unittest.Test
227          assert doc.numtest.type() == numarray.Float64          assert doc.numtest.type() == numarray.Float64
228          assert type(doc.numtest) == numarray.NumArray          assert type(doc.numtest) == numarray.NumArray
229    
230      def testLists(self):      def testBoolLists(self):
231          p4 = ParameterSet()          p4 = ParameterSet()
232          mylist = [True, False, False, True]          mylist = [True, False, False, True]
233          p4.declareParameter(listest=mylist)          p4.declareParameter(listest=mylist)
234          doc = self._class(p4)          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          assert type(doc.listest) == list
266          self.assertEquals(mylist, doc.listest)          assert len(doc.listest) == len(mylist)
267          assert type(doc.listest[0]) == bool          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 226  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 282  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 292  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 303  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 316  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('run_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 401  class ODETEST(Model): Line 463  class ODETEST(Model):
463    
464            
465  if __name__ == "__main__":  if __name__ == "__main__":
466      unittest.main()      suite = unittest.TestSuite()
467        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.874  
changed lines
  Added in v.1809

  ViewVC Help
Powered by ViewVC 1.1.26