Coverage for skema/program_analysis/CAST/fortran/tests/test_conditional_cast_fortran.py: 100%

102 statements  

« 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 

4 

5from skema.program_analysis.CAST.fortran.ts2cast import TS2CAST 

6from skema.program_analysis.CAST2FN.model.cast import ( 

7 Assignment, 

8 Var, 

9 Name, 

10 CASTLiteralValue, 

11 ModelIf, 

12 Operator, 

13 ScalarType 

14) 

15 

16def cond1(): 

17 return """ 

18program cond1 

19 implicit none 

20 integer :: x = 2 

21 

22 if (x < 5) then 

23 x = x + 1 

24 else 

25 x = x - 3 

26 end if 

27end program cond1 

28""" 

29 

30def cond2(): 

31 return """ 

32program cond2 

33 implicit none 

34 integer :: x=2, y=3 

35 

36 if (x < 5) then 

37 x = 1 

38 y = 2 

39 x = x * y 

40 else 

41 x = x - 3 

42 end if 

43end program cond2 

44""" 

45 

46def generate_cast(test_file_string): 

47 with TemporaryDirectory() as temp: 

48 source_path = Path(temp) / "source.f95" 

49 source_path.write_text(test_file_string) 

50 out_cast = TS2CAST(str(source_path)).out_cast 

51 

52 return out_cast[0] 

53 

54def test_cond1(): 

55 exp_cast = generate_cast(cond1()) 

56 

57 # Test basic conditional 

58 asg_node = exp_cast.nodes[0].body[0] 

59 cond_node = exp_cast.nodes[0].body[1] 

60 

61 assert isinstance(asg_node, Assignment) 

62 assert isinstance(asg_node.left, Var) 

63 assert isinstance(asg_node.left.val, Name) 

64 assert asg_node.left.val.name == "x" 

65 

66 assert isinstance(asg_node.right, CASTLiteralValue) 

67 assert asg_node.right.value_type == ScalarType.INTEGER 

68 assert asg_node.right.value == '2' 

69 

70 assert isinstance(cond_node, ModelIf) 

71 cond_expr = cond_node.expr 

72 cond_body = cond_node.body 

73 cond_else = cond_node.orelse 

74 

75 assert isinstance(cond_expr, Operator) 

76 assert cond_expr.op == "<" 

77 assert isinstance(cond_expr.operands[0], Name) 

78 assert isinstance(cond_expr.operands[1], CASTLiteralValue) 

79 

80 assert len(cond_body) == 1 

81 assert isinstance(cond_body[0], Assignment) 

82 assert isinstance(cond_body[0].left, Var) 

83 assert isinstance(cond_body[0].right, Operator) 

84 assert cond_body[0].right.op == "+" 

85 

86 assert len(cond_else) == 1 

87 assert isinstance(cond_else[0], Assignment) 

88 assert isinstance(cond_else[0].left, Var) 

89 assert isinstance(cond_else[0].right, Operator) 

90 assert cond_else[0].right.op == "-" 

91 

92 

93def test_cond2(): 

94 exp_cast = generate_cast(cond2()) 

95 

96 # Test multiple variable conditional 

97 asg_node = exp_cast.nodes[0].body[0] 

98 cond_node = exp_cast.nodes[0].body[2] 

99 

100 assert isinstance(asg_node, Assignment) 

101 assert isinstance(asg_node.left, Var) 

102 assert isinstance(asg_node.left.val, Name) 

103 assert asg_node.left.val.name == "x" 

104 assert asg_node.left.val.id == 0 

105 

106 assert isinstance(asg_node.right, CASTLiteralValue) 

107 assert asg_node.right.value_type == ScalarType.INTEGER 

108 assert asg_node.right.value == '2' 

109 

110 asg_node = exp_cast.nodes[0].body[1] 

111 assert isinstance(asg_node, Assignment) 

112 assert isinstance(asg_node.left, Var) 

113 assert isinstance(asg_node.left.val, Name) 

114 assert asg_node.left.val.name == "y" 

115 assert asg_node.left.val.id == 1 

116 

117 assert isinstance(asg_node.right, CASTLiteralValue) 

118 assert asg_node.right.value_type == ScalarType.INTEGER 

119 assert asg_node.right.value == '3' 

120 

121 assert isinstance(cond_node, ModelIf) 

122 cond_expr = cond_node.expr 

123 cond_body = cond_node.body 

124 cond_else = cond_node.orelse 

125 

126 assert isinstance(cond_expr, Operator) 

127 assert cond_expr.op == "<" 

128 assert isinstance(cond_expr.operands[0], Name) 

129 assert cond_expr.operands[0].name == "x" 

130 assert isinstance(cond_expr.operands[1], CASTLiteralValue) 

131 assert cond_expr.operands[1].value_type == ScalarType.INTEGER 

132 assert cond_expr.operands[1].value == "5" 

133 

134 assert len(cond_body) == 3 

135 assert isinstance(cond_body[0], Assignment) 

136 assert isinstance(cond_body[0].left, Var) 

137 assert cond_body[0].left.val.name == "x" 

138 assert isinstance(cond_body[0].right, CASTLiteralValue) 

139 assert cond_body[0].right.value == "1" 

140 

141 assert isinstance(cond_body[1], Assignment) 

142 assert isinstance(cond_body[1].left, Var) 

143 assert cond_body[1].left.val.name == "y" 

144 assert isinstance(cond_body[1].right, CASTLiteralValue) 

145 assert cond_body[1].right.value == "2" 

146 

147 assert isinstance(cond_body[2], Assignment) 

148 assert isinstance(cond_body[2].left, Var) 

149 assert isinstance(cond_body[2].right, Operator) 

150 

151 assert cond_body[2].right.op == "*" 

152 

153 assert isinstance(cond_body[2].right.operands[0], Name) 

154 assert cond_body[2].right.operands[0].name == "x" 

155 assert cond_body[2].right.operands[0].id == 0 

156 assert isinstance(cond_body[2].right.operands[1], Name) 

157 assert cond_body[2].right.operands[1].name == "y" 

158 assert cond_body[2].right.operands[1].id == 1 

159 

160 assert len(cond_else) == 1 

161 assert isinstance(cond_else[0], Assignment) 

162 assert isinstance(cond_else[0].left, Var) 

163 assert isinstance(cond_else[0].right, Operator) 

164 assert cond_else[0].right.op == "-"