Salome HOME
Merge from V6_main 01/04/2013
[modules/yacs.git] / src / yacsloader_swig / Test / testSave.py
1 #!/usr/bin/env python
2 # Copyright (C) 2006-2013  CEA/DEN, EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 import time
22 import unittest
23 import threading
24
25 import SALOMERuntime
26 import loader
27 import pilot
28
29 class TestSave(unittest.TestCase):
30
31     def setUp(self):
32         SALOMERuntime.RuntimeSALOME_setRuntime(1)
33         pass
34
35     def test0_saveAndExec(self):
36         """Execute twice the scheme. Each time the final state is dumped
37         and the scheme is written. The second exeuction is done with the
38         saved scheme file. Final state dumps and scheme files produced must
39         be identical for the 2 executions. Nodes are not always written in
40         the same order, so the comparison is done after sort of lines...
41         """
42         schemaList = []
43         schemaList += ["aschema","bschema","cschema","dschema","eschema","fschema"]
44         schemaList += ["bloc1","bloc2","bloc3","bloc4"]
45         schemaList += ["foreach1","foreach2","foreach4","foreach5"]
46         schemaList += ["foreach_LongCorba","foreach_LongPython"]
47         schemaList += ["forloop1","forloop2","forloop3","forloop4","forloop5","forloop6","forloop7"]
48         schemaList += ["forwhile1"]
49         schemaList += ["legendre7"]
50         schemaList += ["switch1","switch2","switch3","switch4","switch5","switch6","switch7","switch8","switch9"]
51         schemaList += ["while1","while2","while3"]
52         r = pilot.getRuntime()
53         l = loader.YACSLoader()
54         e = pilot.ExecutorSwig()
55         for schema in schemaList:
56             fileOrig = "samples/" + schema + ".xml"
57             saveSchema1 = "schema1_" + schema
58             dumpSchema1 = "dump1_" + schema
59             saveSchema2 = "schema2_" + schema
60             dumpSchema2 = "dump2_" + schema
61             try:
62                 p = l.load(fileOrig)
63                 s = pilot.SchemaSave(p)
64                 s.save(saveSchema1)
65                 e.RunW(p,0)
66                 e.saveState(dumpSchema1)
67                 p = l.load(saveSchema1)
68                 s = pilot.SchemaSave(p)
69                 s.save(saveSchema2)
70                 e.RunW(p,0)
71                 e.saveState(dumpSchema2)
72             except ValueError, ex:
73                 print "Value Error: ", ex
74                 pb = "problem on " + fileOrig + " : ValueError"
75                 self.fail(pb)
76             except pilot.Exception,ex:
77                 print ex.what()
78                 pb = "problem on " + fileOrig + " : " + ex.what()
79                 self.fail(pb)
80             except:
81                 pb = "unknown problem on " + fileOrig
82                 self.fail(pb)                
83             s1=open(saveSchema1,'r')
84             s2=open(saveSchema2,'r')
85             d1=open(dumpSchema1,'r')
86             d2=open(dumpSchema2,'r')
87             ls1 = s1.readlines().sort()
88             ls2 = s2.readlines().sort()
89             ld1 = d1.readlines().sort()
90             ld2 = d2.readlines().sort()
91             pb1 = "file schemes produced by successive executions are not identical: " + fileOrig 
92             pb2 = "final dump states produced by successive executions are not identical: " + fileOrig 
93             self.assertEqual(ls1,ls2,pb1)
94             self.assertEqual(ld1,ld2,pb2)            
95             pass
96         
97
98 import os
99 U = os.getenv('USER')
100 f=open("/tmp/" + U + "/UnitTestsResult", 'a')
101 f.write("  --- TEST src/yacsloader: testSave.py\n")
102 suite = unittest.makeSuite(TestSave)
103 result=unittest.TextTestRunner(f, descriptions=1, verbosity=1).run(suite)
104 f.close()
105 sys.exit(not result.wasSuccessful())