Salome HOME
Updated copyright comment
[modules/yacs.git] / src / yacsloader_swig / Test / testSave.py
1 #!/usr/bin/env python3
2 # Copyright (C) 2006-2024  CEA, EDF
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, or (at your option) any later version.
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 import tempfile
25 import os
26
27 import SALOMERuntime
28 import loader
29 import pilot
30
31 class TestSave(unittest.TestCase):
32
33     def setUp(self):
34         SALOMERuntime.RuntimeSALOME_setRuntime(1)
35         self.workdir = tempfile.mkdtemp(suffix=".yacstest")
36         pass
37
38     def test0_saveAndExec(self):
39         """Execute twice the scheme. Each time the final state is dumped
40         and the scheme is written. The second exeuction is done with the
41         saved scheme file. Final state dumps and scheme files produced must
42         be identical for the 2 executions. Nodes are not always written in
43         the same order, so the comparison is done after sort of lines...
44         """
45         schemaList = []
46         schemaList += ["aschema","bschema","cschema","dschema","eschema","fschema"]
47         schemaList += ["bloc1","bloc2","bloc3","bloc4"]
48         schemaList += ["foreach1","foreach2","foreach4","foreach5"]
49         schemaList += ["foreach_LongCorba","foreach_LongPython"]
50         schemaList += ["forloop1","forloop2","forloop3","forloop4","forloop5","forloop6","forloop7"]
51         schemaList += ["forwhile1"]
52         schemaList += ["legendre7"]
53         schemaList += ["switch1","switch2","switch3","switch4","switch5","switch6","switch7","switch8","switch9"]
54         schemaList += ["while1","while2","while3"]
55         r = pilot.getRuntime()
56         l = loader.YACSLoader()
57         e = pilot.ExecutorSwig()
58         for schema in schemaList:
59             print(schema)
60             fileOrig = "samples/" + schema + ".xml"
61             saveSchema1 = os.path.join(self.workdir, "schema1_" + schema)
62             dumpSchema1 = os.path.join(self.workdir, "dump1_" + schema)
63             saveSchema2 = os.path.join(self.workdir, "schema2_" + schema)
64             dumpSchema2 = os.path.join(self.workdir, "dump2_" + schema)
65             try:
66                 p = l.load(fileOrig)
67                 s = pilot.SchemaSave(p)
68                 s.save(saveSchema1)
69                 e.RunW(p,0)
70                 e.saveState(dumpSchema1)
71                 p = l.load(saveSchema1)
72                 s = pilot.SchemaSave(p)
73                 s.save(saveSchema2)
74                 e.RunW(p,0)
75                 e.saveState(dumpSchema2)
76             except ValueError as ex:
77                 print("Value Error: ", ex)
78                 pb = "problem on " + fileOrig + " : ValueError"
79                 self.fail(pb)
80             except pilot.Exception as ex:
81                 print(ex.what())
82                 pb = "problem on " + fileOrig + " : " + ex.what()
83                 self.fail(pb)
84             except:
85                 pb = "unknown problem on " + fileOrig
86                 self.fail(pb)                
87             
88             with open(saveSchema1,'r') as s1:
89                 ls1 = s1.readlines().sort()
90             with open(saveSchema2,'r') as s2:
91                 ls2 = s2.readlines().sort()
92             with open(dumpSchema1,'r') as d1:
93                 ld1 = d1.readlines().sort()
94             with open(dumpSchema2,'r') as d2:
95                 ld2 = d2.readlines().sort()
96             pb1 = "file schemes produced by successive executions are not identical: " + fileOrig 
97             pb2 = "final dump states produced by successive executions are not identical: " + fileOrig 
98             self.assertEqual(ls1,ls2,pb1)
99             self.assertEqual(ld1,ld2,pb2)            
100             pass
101
102 if __name__ == '__main__':
103   import salome
104   import NamingService
105   import os
106   import subprocess
107   salome.salome_init()
108   ior = NamingService.NamingService.IOROfNS()
109   p = subprocess.Popen(["../yacsloader/echoSrv",ior])
110   import time
111   time.sleep(3)
112   with tempfile.TemporaryDirectory(suffix=".yacstest") as dir_test:
113     file_test = os.path.join(dir_test,"UnitTestsResult")
114     with open(file_test, 'a') as f:
115       f.write("  --- TEST src/yacsloader: testSave.py\n")
116       suite = unittest.makeSuite(TestSave)
117       result=unittest.TextTestRunner(f, descriptions=1, verbosity=1).run(suite)
118   p.terminate()
119   sys.exit(not result.wasSuccessful())