Salome HOME
796e9576452633b0899900f7bbe8770c44558a9d
[modules/yacs.git] / src / py2yacs / Test / testDeco.py
1 #!/usr/bin/env python3
2 # Copyright (C) 2006-2020  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, 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 unittest
22 import tempfile
23 import os
24 import subprocess
25
26 import SALOMERuntime
27 import loader
28 import pilot
29
30 dir_test = tempfile.mkdtemp(suffix=".yacstest")
31
32 class TestDeco(unittest.TestCase):
33
34     def setUp(self):
35       SALOMERuntime.RuntimeSALOME_setRuntime()
36
37     def test_t1(self):
38       """
39       Schema:
40       jdd -> foreach -> post
41            > f2
42          /
43       f1 -> f3 -> f1
44       """
45       import testforeach
46       expected_1, expected_2 = testforeach.main()
47       yacs_schema_file = os.path.join(dir_test, "schema_t1.xml")
48       yacs_build_command = "yacsbuild.py"
49       test_script = "testforeach.py"
50       main_function_name = "main"
51       subprocess.run([yacs_build_command,
52                       test_script, main_function_name, yacs_schema_file])
53       l = loader.YACSLoader()
54       ex = pilot.ExecutorSwig()
55       proc = l.load(yacs_schema_file)
56       ex.RunW(proc,0)
57       obtained_1 = proc.getChildByName("post_0").getOutputPort("s").getPyObj()
58       obtained_2 = proc.getChildByName("f1_1").getOutputPort("r").getPyObj()
59       self.assertEqual(expected_1, obtained_1)
60       self.assertEqual(expected_2, obtained_2)
61
62     def test_t2(self):
63       """
64       Foreach initialized by value.
65       """
66       import testforeach
67       expected_1, expected_2 = testforeach.mainbloc()
68       yacs_schema_file = os.path.join(dir_test, "schema_t2.xml")
69       yacs_build_command = "yacsbuild.py"
70       test_script = "testforeach.py"
71       main_function_name = "mainbloc"
72       subprocess.run([yacs_build_command,
73                       test_script, main_function_name, yacs_schema_file])
74       l = loader.YACSLoader()
75       ex = pilot.ExecutorSwig()
76       proc = l.load(yacs_schema_file)
77       ex.RunW(proc,0)
78       obtained_1 = proc.getChildByName("output_fr_0").getOutputPort("s_0").getPyObj()
79       obtained_2 = proc.getChildByName("output_fr_0").getOutputPort("p_1").getPyObj()
80       self.assertEqual(expected_1, obtained_1)
81       self.assertEqual(expected_2, obtained_2)
82
83     def test_t3(self):
84       """
85       Foreach on 2 levels.
86       """
87       import testforeach
88       expected = testforeach.maindoublefr()
89       yacs_schema_file = os.path.join(dir_test, "schema_t3.xml")
90       yacs_build_command = "yacsbuild.py"
91       test_script = "testforeach.py"
92       main_function_name = "maindoublefr"
93       subprocess.run([yacs_build_command,
94                       test_script, main_function_name, yacs_schema_file])
95       l = loader.YACSLoader()
96       ex = pilot.ExecutorSwig()
97       proc = l.load(yacs_schema_file)
98       ex.RunW(proc,0)
99       obtained = proc.getChildByName("output_doublefr_0").getOutputPort("r_0_0").getPyObj()
100       self.assertEqual(expected, obtained)
101
102     def test_t4(self):
103       """
104       Using specific containers.
105       """
106       import yacsdecorator
107       cm = yacsdecorator.ContainerManager()
108       cm.addContainer("c1", 1, False)
109       cm.addContainer("c2", 4, True)
110       cm.addContainer(yacsdecorator.ContainerManager.defaultContainerName, 1, False)
111       cont_file = os.path.join(dir_test, "containers_t4.json")
112       cm.saveFile(cont_file)
113       script = """import yacsdecorator
114 @yacsdecorator.leaf("c1")
115 def f_c1(x,y):
116   s = x + y
117   return s
118
119 @yacsdecorator.leaf("c2")
120 def f_c2(x,y):
121   p = x * y
122   return p
123
124 @yacsdecorator.leaf
125 def f_def(x,y):
126   d = x - y
127   return d
128
129 @yacsdecorator.bloc
130 def main():
131   s1 = f_c1(3,4)
132   p1 = f_c2(5,6)
133   r = f_def(p1, s1)
134 """
135       script_file = os.path.join(dir_test, "script_t4.py")
136       with open(script_file, "w") as f:
137         f.write(script)
138       yacs_build_command = "yacsbuild.py"
139       main_function_name = "main"
140       yacs_schema_file = os.path.join(dir_test, "schema_t4.xml")
141       subprocess.run([yacs_build_command,
142                       script_file, main_function_name, yacs_schema_file,
143                       "-c", cont_file])
144       l = loader.YACSLoader()
145       ex = pilot.ExecutorSwig()
146       proc = l.load(yacs_schema_file)
147       ex.RunW(proc,0)
148       self.assertEqual(proc.getState(),pilot.DONE)
149       c1 = proc.getChildByName("f_c1_0").getContainer()
150       self.assertFalse(c1.isUsingPythonCache())
151       self.assertEqual(c1.getProperty("nb_parallel_procs"), "1")
152       c2 = proc.getChildByName("f_c2_0").getContainer()
153       self.assertTrue(c2.isUsingPythonCache())
154       self.assertEqual(c2.getProperty("nb_parallel_procs"), "4")
155       c3 = proc.getChildByName("f_def_0").getContainer()
156       self.assertFalse(c3.isUsingPythonCache())
157       self.assertEqual(c3.getProperty("nb_parallel_procs"), "1")
158
159 if __name__ == '__main__':
160   file_test = os.path.join(dir_test,"UnitTestsResult")
161   with open(file_test, 'a') as f:
162       f.write("  --- TEST src/py2yacs: testDeco.py\n")
163       suite = unittest.makeSuite(TestDeco)
164       result=unittest.TextTestRunner(f, descriptions=1, verbosity=1).run(suite)
165   sys.exit(not result.wasSuccessful())