Coverage for skema/program_analysis/CAST/fortran/tests/test_for_cast_fortran.py: 100%
70 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 StructureType
17)
19def for1():
20 return """
21program for1
22integer :: x = 7
23integer :: i
25do i=1, 10
26x = x + 1
27end do
28end program for1
29 """
33def generate_cast(test_file_string):
34 with TemporaryDirectory() as temp:
35 source_path = Path(temp) / "source.f95"
36 source_path.write_text(test_file_string)
37 out_cast = TS2CAST(str(source_path)).out_cast
39 return out_cast[0]
41def test_for1():
42 cast = generate_cast(for1())
44 asg_node = cast.nodes[0].body[0]
45 loop_node = cast.nodes[0].body[2]
47 assert isinstance(asg_node, Assignment)
48 assert isinstance(asg_node.left, Var)
49 assert isinstance(asg_node.left.val, Name)
50 assert asg_node.left.val.name == "x"
52 assert isinstance(asg_node.right, CASTLiteralValue)
53 assert asg_node.right.value_type == ScalarType.INTEGER
54 assert asg_node.right.value == '7'
56 assert isinstance(loop_node, Loop)
57 print(loop_node)
58 assert len(loop_node.pre) == 2
60 # Loop Pre
61 loop_pre = loop_node.pre
62 assert isinstance(loop_pre[0], Assignment)
63 assert isinstance(loop_pre[0].left, Var)
64 assert loop_pre[0].left.val.name == "generated_iter_0"
66 assert isinstance(loop_pre[0].right, Call)
67 assert loop_pre[0].right.func.name == "iter"
68 iter_args = loop_pre[0].right.arguments
70 assert len(iter_args) == 1
71 assert isinstance(iter_args[0], Call)
72 assert iter_args[0].func.name == "range"
73 assert len(iter_args[0].arguments) == 3
75 assert isinstance(iter_args[0].arguments[0], CASTLiteralValue)
76 assert iter_args[0].arguments[0].value == "1"
77 assert isinstance(iter_args[0].arguments[1], CASTLiteralValue)
78 assert iter_args[0].arguments[1].value == "10"
79 assert isinstance(iter_args[0].arguments[2], CASTLiteralValue)
80 assert iter_args[0].arguments[2].value == "1"
82 assert isinstance(loop_pre[1], Assignment)
83 assert isinstance(loop_pre[1].left, CASTLiteralValue)
84 assert loop_pre[1].left.value_type == StructureType.TUPLE
86 assert isinstance(loop_pre[1].left.value[0], Var)
87 assert loop_pre[1].left.value[0].val.name == "i"
88 assert isinstance(loop_pre[1].left.value[1], Var)
89 assert loop_pre[1].left.value[1].val.name == "generated_iter_0"
90 assert isinstance(loop_pre[1].left.value[2], Var)
91 assert loop_pre[1].left.value[2].val.name == "sc_0"
93 assert isinstance(loop_pre[1].right, Call)
94 assert loop_pre[1].right.func.name == "next"
95 assert len(loop_pre[1].right.arguments) == 1
96 assert loop_pre[1].right.arguments[0].val.name == "generated_iter_0"
98 # Loop Test
99 loop_test = loop_node.expr
100 assert isinstance(loop_test, Operator)
101 assert loop_test.op == "!="
102 assert isinstance(loop_test.operands[0], Name)
103 assert loop_test.operands[0].name == "sc_0"
105 assert isinstance(loop_test.operands[1], CASTLiteralValue)
106 assert loop_test.operands[1].value_type == ScalarType.BOOLEAN
108 # Loop Body
109 loop_body = loop_node.body
110 next_call = loop_body[-1]
111 assert isinstance(next_call, Assignment)
112 assert isinstance(next_call.right, Call)
113 assert next_call.right.func.name == "next"
114 assert next_call.right.arguments[0].val.name == "generated_iter_0"