Coverage for skema/program_analysis/CAST/fortran/tests/test_operation_cast_fortran.py: 100%
146 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-30 17:15 +0000
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-30 17:15 +0000
1import pytest
2from tempfile import TemporaryDirectory
3from pathlib import Path
5from skema.program_analysis.CAST.fortran.ts2cast import TS2CAST
6from skema.program_analysis.CAST2FN.model.cast import (
7 Assignment,
8 Var,
9 Name,
10 Operator,
11 CASTLiteralValue,
12 ScalarType
13)
15def binop1():
16 return """
17program binop1
18integer :: x = 2 + 3
19end program binop1
20 """
22def binop2():
23 return """
24program binop2
25integer :: x = 2
26integer :: y = x + 3
27end program binop2
28 """
30def binop3():
31 return """
32program binop3
33integer :: x = 1
34integer :: y = 2
35integer :: z = x + y - (y * x) / x
36end program binop3
37 """
39def unary1():
40 return """
41program unary1
42integer :: x = -1
43end program unary1
44 """
46def unary2():
47 return """
48program unary2
49integer :: x = 1
50integer :: y = -x
51end program unary2
52 """
55def generate_cast(test_file_string):
56 with TemporaryDirectory() as temp:
57 source_path = Path(temp) / "source.f95"
58 source_path.write_text(test_file_string)
59 out_cast = TS2CAST(str(source_path)).out_cast
61 return out_cast[0]
63def test_binop1():
64 exp_cast = generate_cast(binop1())
65 # Test basic properties of assignment node
66 binop_node = exp_cast.nodes[0].body[0]
68 assert isinstance(binop_node, Assignment)
69 assert isinstance(binop_node.left, Var)
70 assert isinstance(binop_node.left.val, Name)
71 assert binop_node.left.val.name == "x"
73 assert isinstance(binop_node.right, Operator)
74 assert binop_node.right.op == "+"
76 assert isinstance(binop_node.right.operands[0], CASTLiteralValue)
77 assert binop_node.right.operands[0].value == '2'
78 assert binop_node.right.operands[0].value_type == ScalarType.INTEGER
80 assert isinstance(binop_node.right.operands[1], CASTLiteralValue)
81 assert binop_node.right.operands[1].value == '3'
82 assert binop_node.right.operands[1].value_type == ScalarType.INTEGER
84def test_binop2():
85 exp_cast = generate_cast(binop2())
87 # Test basic properties of two assignment nodes
88 binop_node = exp_cast.nodes[0].body[0]
89 assert isinstance(binop_node, Assignment)
90 assert isinstance(binop_node.left, Var)
91 assert isinstance(binop_node.left.val, Name)
92 assert binop_node.left.val.name == "x"
93 assert binop_node.left.val.id == 0
95 assert isinstance(binop_node.right, CASTLiteralValue)
96 assert binop_node.right.value_type == ScalarType.INTEGER
97 assert binop_node.right.value == '2'
99 # ------
100 binop_node = exp_cast.nodes[0].body[1]
101 assert isinstance(binop_node, Assignment)
102 assert isinstance(binop_node.left, Var)
103 assert isinstance(binop_node.left.val, Name)
104 assert binop_node.left.val.name == "y"
105 assert binop_node.left.val.id == 1
107 assert isinstance(binop_node.right, Operator)
108 assert binop_node.right.op == "+"
110 assert isinstance(binop_node.right.operands[0], Name)
111 assert binop_node.right.operands[0].name == 'x'
112 assert binop_node.right.operands[0].id == 0
114 assert isinstance(binop_node.right.operands[1], CASTLiteralValue)
115 assert binop_node.right.operands[1].value == '3'
116 assert binop_node.right.operands[1].value_type == ScalarType.INTEGER
118def test_binop3():
119 exp_cast = generate_cast(binop3())
121 # Test basic properties of two assignment nodes
122 binop_node = exp_cast.nodes[0].body[0]
123 assert isinstance(binop_node, Assignment)
124 assert isinstance(binop_node.left, Var)
125 assert isinstance(binop_node.left.val, Name)
126 assert binop_node.left.val.name == "x"
127 assert binop_node.left.val.id == 0
129 assert isinstance(binop_node.right, CASTLiteralValue)
130 assert binop_node.right.value_type == ScalarType.INTEGER
131 assert binop_node.right.value == '1'
133 # ------
134 binop_node = exp_cast.nodes[0].body[1]
135 assert isinstance(binop_node, Assignment)
136 assert isinstance(binop_node.left, Var)
137 assert isinstance(binop_node.left.val, Name)
138 assert binop_node.left.val.name == "y"
139 assert binop_node.left.val.id == 1
141 assert isinstance(binop_node.right, CASTLiteralValue)
142 assert binop_node.right.value_type == ScalarType.INTEGER
143 assert binop_node.right.value == '2'
145 # ------
146 binop_node = exp_cast.nodes[0].body[2]
147 assert isinstance(binop_node, Assignment)
148 assert isinstance(binop_node.left, Var)
149 assert isinstance(binop_node.left.val, Name)
150 assert binop_node.left.val.name == "z"
151 assert binop_node.left.val.id == 2
153 assert isinstance(binop_node.right, Operator)
154 assert binop_node.right.op == "-"
156 binop_node_1 = binop_node.right.operands[0]
157 assert binop_node_1.op == "+"
159 assert isinstance(binop_node_1.operands[0], Name)
160 assert binop_node_1.operands[0].name == "x"
161 assert binop_node_1.operands[0].id == 0
163 assert isinstance(binop_node_1.operands[1], Name)
164 assert binop_node_1.operands[1].name == "y"
165 assert binop_node_1.operands[1].id == 1
167 binop_node_2 = binop_node.right.operands[1]
168 assert binop_node_2.op == "/"
169 assert isinstance(binop_node_2.operands[0], Operator)
171 assert isinstance(binop_node_2.operands[1], Name)
172 assert binop_node_2.operands[1].name == "x"
173 assert binop_node_2.operands[1].id == 0
175 binop_node_3 = binop_node_2.operands[0]
176 assert binop_node_3.op == "*"
178 assert isinstance(binop_node_3.operands[0], Name)
179 assert binop_node_3.operands[0].name == "y"
180 assert binop_node_3.operands[0].id == 1
182 assert isinstance(binop_node_3.operands[1], Name)
183 assert binop_node_3.operands[1].name == "x"
184 assert binop_node_3.operands[1].id == 0
186def test_unary1():
187 exp_cast = generate_cast(unary1())
188 # Test basic properties of assignment node
189 unary_node = exp_cast.nodes[0].body[0]
191 assert isinstance(unary_node, Assignment)
192 assert isinstance(unary_node.left, Var)
193 assert isinstance(unary_node.left.val, Name)
194 assert unary_node.left.val.name == "x"
195 assert unary_node.left.val.id == 0
197 assert isinstance(unary_node.right, Operator)
198 assert unary_node.right.op == "-"
200 assert isinstance(unary_node.right.operands[0], CASTLiteralValue)
201 assert unary_node.right.operands[0].value == '1'
202 assert unary_node.right.operands[0].value_type == ScalarType.INTEGER
204def test_unary2():
205 exp_cast = generate_cast(unary2())
206 # Test basic properties of assignment node
207 unary_node = exp_cast.nodes[0].body[0]
209 assert isinstance(unary_node, Assignment)
210 assert isinstance(unary_node.left, Var)
211 assert isinstance(unary_node.left.val, Name)
212 assert unary_node.left.val.name == "x"
213 assert unary_node.left.val.id == 0
215 assert isinstance(unary_node.right, CASTLiteralValue)
216 assert unary_node.right.value == '1'
217 assert unary_node.right.value_type == ScalarType.INTEGER
219 unary_node = exp_cast.nodes[0].body[1]
221 assert isinstance(unary_node, Assignment)
222 assert isinstance(unary_node.left, Var)
223 assert isinstance(unary_node.left.val, Name)
224 assert unary_node.left.val.name == "y"
225 assert unary_node.left.val.id == 1
227 assert isinstance(unary_node.right, Operator)
228 assert unary_node.right.op == "-"
230 assert isinstance(unary_node.right.operands[0], Name)
231 assert unary_node.right.operands[0].name == 'x'
232 assert unary_node.right.operands[0].id == 0