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

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 Operator, 

11 CASTLiteralValue, 

12 ScalarType 

13) 

14 

15def binop1(): 

16 return """ 

17program binop1 

18integer :: x = 2 + 3 

19end program binop1 

20 """ 

21 

22def binop2(): 

23 return """ 

24program binop2 

25integer :: x = 2 

26integer :: y = x + 3 

27end program binop2 

28 """ 

29 

30def binop3(): 

31 return """ 

32program binop3 

33integer :: x = 1 

34integer :: y = 2 

35integer :: z = x + y - (y * x) / x 

36end program binop3 

37 """ 

38 

39def unary1(): 

40 return """ 

41program unary1 

42integer :: x = -1 

43end program unary1 

44 """ 

45 

46def unary2(): 

47 return """ 

48program unary2 

49integer :: x = 1 

50integer :: y = -x 

51end program unary2 

52 """ 

53 

54 

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 

60 

61 return out_cast[0] 

62 

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] 

67 

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" 

72 

73 assert isinstance(binop_node.right, Operator) 

74 assert binop_node.right.op == "+" 

75 

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 

79 

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 

83 

84def test_binop2(): 

85 exp_cast = generate_cast(binop2()) 

86 

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 

94 

95 assert isinstance(binop_node.right, CASTLiteralValue) 

96 assert binop_node.right.value_type == ScalarType.INTEGER 

97 assert binop_node.right.value == '2' 

98 

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 

106 

107 assert isinstance(binop_node.right, Operator) 

108 assert binop_node.right.op == "+" 

109 

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 

113 

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 

117 

118def test_binop3(): 

119 exp_cast = generate_cast(binop3()) 

120 

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 

128 

129 assert isinstance(binop_node.right, CASTLiteralValue) 

130 assert binop_node.right.value_type == ScalarType.INTEGER 

131 assert binop_node.right.value == '1' 

132 

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 

140 

141 assert isinstance(binop_node.right, CASTLiteralValue) 

142 assert binop_node.right.value_type == ScalarType.INTEGER 

143 assert binop_node.right.value == '2' 

144 

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 

152 

153 assert isinstance(binop_node.right, Operator) 

154 assert binop_node.right.op == "-" 

155 

156 binop_node_1 = binop_node.right.operands[0] 

157 assert binop_node_1.op == "+" 

158 

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 

162 

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 

166 

167 binop_node_2 = binop_node.right.operands[1] 

168 assert binop_node_2.op == "/" 

169 assert isinstance(binop_node_2.operands[0], Operator) 

170 

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 

174 

175 binop_node_3 = binop_node_2.operands[0] 

176 assert binop_node_3.op == "*" 

177 

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 

181 

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 

185 

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] 

190 

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 

196 

197 assert isinstance(unary_node.right, Operator) 

198 assert unary_node.right.op == "-" 

199 

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 

203 

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] 

208 

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 

214 

215 assert isinstance(unary_node.right, CASTLiteralValue) 

216 assert unary_node.right.value == '1' 

217 assert unary_node.right.value_type == ScalarType.INTEGER 

218 

219 unary_node = exp_cast.nodes[0].body[1] 

220 

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 

226 

227 assert isinstance(unary_node.right, Operator) 

228 assert unary_node.right.op == "-" 

229 

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