Coverage for skema/program_analysis/CAST/fortran/tests/test_while_cast_fortran.py: 100%
96 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 Call,
10 Name,
11 CASTLiteralValue,
12 ModelIf,
13 Loop,
14 Operator,
15 ScalarType
16)
18def while1():
19 return """
20program while1
21integer :: x = 2
22do while(x<5)
23x=x+1
24end do
25end program while1
26 """
28def while2():
29 return """
30program while2
31integer :: x = 2
32integer :: y = 3
33do while(x<5)
34x=x+1
35x=x+y
36end do
37end program while2
38 """
40def generate_cast(test_file_string):
41 with TemporaryDirectory() as temp:
42 source_path = Path(temp) / "source.f95"
43 source_path.write_text(test_file_string)
44 out_cast = TS2CAST(str(source_path)).out_cast
46 return out_cast[0]
48def test_while1():
49 cast = generate_cast(while1())
51 asg_node = cast.nodes[0].body[0]
52 loop_node = cast.nodes[0].body[1]
54 assert isinstance(asg_node, Assignment)
55 assert isinstance(asg_node.left, Var)
56 assert isinstance(asg_node.left.val, Name)
57 assert asg_node.left.val.name == "x"
59 assert isinstance(asg_node.right, CASTLiteralValue)
60 assert asg_node.right.value_type == ScalarType.INTEGER
61 assert asg_node.right.value == '2'
63 assert isinstance(loop_node, Loop)
64 assert len(loop_node.pre) == 0
66 # Loop Test
67 loop_test = loop_node.expr
68 assert isinstance(loop_test, Operator)
69 assert loop_test.op == "<"
70 assert isinstance(loop_test.operands[0], Name)
71 assert loop_test.operands[0].name == "x"
73 assert isinstance(loop_test.operands[1], CASTLiteralValue)
74 assert loop_test.operands[1].value_type == ScalarType.INTEGER
75 assert loop_test.operands[1].value == "5"
77 # Loop Body
78 loop_body = loop_node.body
79 asg = loop_body[0]
80 assert isinstance(asg, Assignment)
81 assert isinstance(asg.left, Var)
82 assert asg.left.val.name == "x"
84 assert isinstance(asg.right, Operator)
85 assert asg.right.op == "+"
86 assert isinstance(asg.right.operands[0], Name)
87 assert isinstance(asg.right.operands[1], CASTLiteralValue)
88 assert asg.right.operands[1].value == "1"
90def test_while2():
91 cast = generate_cast(while2())
93 asg_node = cast.nodes[0].body[0]
94 asg_node_2 = cast.nodes[0].body[1]
95 loop_node = cast.nodes[0].body[2]
97 assert isinstance(asg_node, Assignment)
98 assert isinstance(asg_node.left, Var)
99 assert isinstance(asg_node.left.val, Name)
100 assert asg_node.left.val.name == "x"
102 assert isinstance(asg_node.right, CASTLiteralValue)
103 assert asg_node.right.value_type == ScalarType.INTEGER
104 assert asg_node.right.value == '2'
106 assert isinstance(asg_node_2, Assignment)
107 assert isinstance(asg_node_2.left, Var)
108 assert isinstance(asg_node_2.left.val, Name)
109 assert asg_node_2.left.val.name == "y"
111 assert isinstance(asg_node_2.right, CASTLiteralValue)
112 assert asg_node_2.right.value_type == ScalarType.INTEGER
113 assert asg_node_2.right.value == '3'
115 assert isinstance(loop_node, Loop)
116 assert len(loop_node.pre) == 0
118 # Loop Test
119 loop_test = loop_node.expr
120 assert isinstance(loop_test, Operator)
121 assert loop_test.op == "<"
122 assert isinstance(loop_test.operands[0], Name)
123 assert loop_test.operands[0].name == "x"
125 assert isinstance(loop_test.operands[1], CASTLiteralValue)
126 assert loop_test.operands[1].value_type == ScalarType.INTEGER
127 assert loop_test.operands[1].value == "5"
129 # Loop Body
130 loop_body = loop_node.body
131 asg = loop_body[0]
132 assert isinstance(asg, Assignment)
133 assert isinstance(asg.left, Var)
134 assert asg.left.val.name == "x"
136 assert isinstance(asg.right, Operator)
137 assert asg.right.op == "+"
138 assert isinstance(asg.right.operands[0], Name)
139 assert asg.right.operands[0].name == "x"
141 assert isinstance(asg.right.operands[1], CASTLiteralValue)
142 assert asg.right.operands[1].value == "1"
144 asg = loop_body[1]
145 assert isinstance(asg, Assignment)
146 assert isinstance(asg.left, Var)
147 assert asg.left.val.name == "x"
149 assert isinstance(asg.right, Operator)
150 assert asg.right.op == "+"
151 assert isinstance(asg.right.operands[0], Name)
152 assert asg.right.operands[0].name == "x"
154 assert isinstance(asg.right.operands[1], Name)
155 assert asg.right.operands[1].name == "y"