Coverage for skema/program_analysis/tests/test_goto_computed.py: 99%

202 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-04-30 17:15 +0000

1# import json NOTE: json and Path aren't used right now, 

2# from pathlib import Path but will be used in the future 

3from skema.program_analysis.multi_file_ingester import process_file_system 

4from skema.gromet.fn import ( 

5 GrometFNModuleCollection, 

6 FunctionType, 

7) 

8import ast 

9from skema.program_analysis.tests.utils_test import create_temp_file, delete_temp_file 

10 

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

12from skema.program_analysis.CAST2FN.model.cast import SourceRef 

13from skema.program_analysis.CAST2FN import cast 

14from skema.program_analysis.CAST2FN.cast import CAST 

15from skema.program_analysis.run_ann_cast_pipeline import ann_cast_pipeline 

16 

17def goto0(): 

18 return """ 

19PROGRAM ComputedGoTo 

20 INTEGER:: a, b, c 

21 

22 a = 1 

23 b = 2 

24 c = 3 

25 

26 GO TO (100, 200, 300, 400), MOD((a + b) * c / 2, 4) + 1 

27 

28 100 PRINT *, 'Resulting choice led to label 100' 

29 GO TO 500 

30 

31 200 PRINT *, 'Resulting choice led to label 200' 

32 GO TO 500 

33 

34 300 PRINT *, 'Resulting choice led to label 300' 

35 GO TO 500 

36 

37 400 PRINT *, 'Resulting choice led to label 400' 

38 GO TO 500 

39 

40 500 PRINT *, 'End of program' 

41 END PROGRAM ComputedGoTo  

42 """ 

43 

44def goto1(): 

45 return """ 

46 SUBROUTINE EXAMPLE(DA,C,S) 

47* .. Scalar Arguments .. 

48 DOUBLE PRECISION C,DA,S 

49* .. Local Scalars .. 

50 DOUBLE PRECISION R,ROE 

51 IF (DA.NE.10) GO TO 10 

52 C = 1 

53 R = 2 

54 GO TO 20 

55 10 C = 2 

56 R = 3 

57 20 DA = R 

58 S = C 

59 RETURN 

60 END 

61 """ 

62 

63 

64def generate_gromet(test_file_string): 

65 # How do we generate CAST for Fortran from here? 

66 create_temp_file(test_file_string, "f95") 

67 

68 ts2cast = TS2CAST("temp.f95") 

69 out_cast = ts2cast.out_cast[0] 

70 gromet = ann_cast_pipeline(out_cast, gromet=True, to_file=False, from_obj=True) 

71 

72 delete_temp_file("f95") 

73 

74 return gromet 

75 

76def test_goto0(): 

77 goto_gromet = generate_gromet(goto0()) 

78 ##### 

79 # Checking FN containing label 

80 base_fn = goto_gromet.fn 

81 assert base_fn.bf[3].function_type == FunctionType.GOTO 

82 assert base_fn.bf[6].function_type == FunctionType.GOTO 

83 assert base_fn.bf[9].function_type == FunctionType.GOTO 

84 assert base_fn.bf[12].function_type == FunctionType.GOTO 

85 assert base_fn.bf[15].function_type == FunctionType.GOTO 

86 

87 assert base_fn.bf[4].function_type == FunctionType.LABEL 

88 assert base_fn.bf[7].function_type == FunctionType.LABEL 

89 assert base_fn.bf[10].function_type == FunctionType.LABEL 

90 assert base_fn.bf[13].function_type == FunctionType.LABEL 

91 assert base_fn.bf[16].function_type == FunctionType.LABEL 

92 

93 assert base_fn.pif[0].box == 4 

94 assert base_fn.pif[1].box == 4 

95 assert base_fn.pif[2].box == 4 

96 

97 assert base_fn.pif[3].box == 5 

98 assert base_fn.pif[4].box == 5 

99 assert base_fn.pif[5].box == 5 

100 

101 assert base_fn.pif[6].box == 7 

102 assert base_fn.pif[7].box == 7 

103 assert base_fn.pif[8].box == 7 

104 

105 assert base_fn.pif[9].box == 8 

106 assert base_fn.pif[10].box == 8 

107 assert base_fn.pif[11].box == 8 

108 

109 assert base_fn.pif[12].box == 10 

110 assert base_fn.pif[13].box == 10 

111 assert base_fn.pif[14].box == 10 

112 

113 assert base_fn.pif[15].box == 11 

114 assert base_fn.pif[16].box == 11 

115 assert base_fn.pif[17].box == 11 

116 

117 assert base_fn.pif[18].box == 13 

118 assert base_fn.pif[19].box == 13 

119 assert base_fn.pif[20].box == 13 

120 

121 assert base_fn.pif[21].box == 14 

122 assert base_fn.pif[22].box == 14 

123 assert base_fn.pif[23].box == 14 

124 

125 assert base_fn.pif[24].box == 16 

126 assert base_fn.pif[25].box == 16 

127 assert base_fn.pif[26].box == 16 

128 

129 assert base_fn.pif[27].box == 17 

130 assert base_fn.pif[28].box == 17 

131 assert base_fn.pif[29].box == 17 

132 

133 assert base_fn.pof[0].box == 1 

134 assert base_fn.pof[1].box == 2 

135 assert base_fn.pof[2].box == 3 

136 

137 assert base_fn.pof[3].box == 5 

138 assert base_fn.pof[4].box == 5 

139 assert base_fn.pof[5].box == 5 

140 

141 assert base_fn.pof[6].box == 8 

142 assert base_fn.pof[7].box == 8 

143 assert base_fn.pof[8].box == 8 

144 

145 assert base_fn.pof[9].box == 11 

146 assert base_fn.pof[10].box == 11 

147 assert base_fn.pof[11].box == 11 

148 

149 assert base_fn.pof[12].box == 14 

150 assert base_fn.pof[13].box == 14 

151 assert base_fn.pof[14].box == 14 

152 

153 assert base_fn.pof[15].box == 17 

154 assert base_fn.pof[16].box == 17 

155 assert base_fn.pof[17].box == 17 

156 

157 

158 # First Goto computation with expression 

159 ######### 

160 goto_expr_fn = goto_gromet.fn_array[3] 

161 assert len(goto_expr_fn.bf) == 2 

162 assert goto_expr_fn.bf[0].function_type == FunctionType.EXPRESSION 

163 assert goto_expr_fn.bf[0].body == 6 

164 

165 assert goto_expr_fn.bf[1].function_type == FunctionType.LITERAL 

166 assert goto_expr_fn.bf[1].value.value_type == "None" 

167 assert goto_expr_fn.bf[1].value.value == "None" 

168 

169 assert len(goto_expr_fn.opi) == 3 

170 assert goto_expr_fn.opi[0].box == 1 

171 assert goto_expr_fn.opi[1].box == 1 

172 assert goto_expr_fn.opi[2].box == 1 

173 

174 assert len(goto_expr_fn.opo) == 2 

175 assert goto_expr_fn.opo[0].box == 1 

176 assert goto_expr_fn.opo[0].name == "fn_idx" 

177 

178 assert goto_expr_fn.opo[1].box == 1 

179 assert goto_expr_fn.opo[1].name == "label" 

180 

181 assert len(goto_expr_fn.pif) == 3 

182 assert goto_expr_fn.pif[0].box == 1 

183 assert goto_expr_fn.pif[1].box == 1 

184 assert goto_expr_fn.pif[2].box == 1 

185 

186 assert len(goto_expr_fn.pof) == 2 

187 assert goto_expr_fn.pof[0].box == 1 

188 assert goto_expr_fn.pof[1].box == 2 

189 

190 assert len(goto_expr_fn.wfopi) == 3 

191 assert goto_expr_fn.wfopi[0].src == 1 

192 assert goto_expr_fn.wfopi[0].tgt == 1 

193 

194 assert goto_expr_fn.wfopi[1].src == 2 

195 assert goto_expr_fn.wfopi[1].tgt == 2 

196 

197 assert goto_expr_fn.wfopi[2].src == 3 

198 assert goto_expr_fn.wfopi[2].tgt == 3 

199 

200 assert len(goto_expr_fn.wfopo) == 2 

201 assert goto_expr_fn.wfopo[0].src == 1 

202 assert goto_expr_fn.wfopo[0].tgt == 1 

203 

204 assert goto_expr_fn.wfopo[1].src == 2 

205 assert goto_expr_fn.wfopo[1].tgt == 2 

206 

207 ##### 

208 # Checking basic label computation 

209 # Multiples of these exist in the FN but they're identical 

210 goto_expr_fn = goto_gromet.fn_array[7] 

211 assert len(goto_expr_fn.opi) == 3 

212 assert goto_expr_fn.opi[0].box == 1 

213 assert goto_expr_fn.opi[1].box == 1 

214 assert goto_expr_fn.opi[2].box == 1 

215 

216 assert len(goto_expr_fn.opo) == 2 

217 assert goto_expr_fn.opo[0].box == 1 

218 assert goto_expr_fn.opo[0].name == "fn_idx" 

219 

220 assert goto_expr_fn.opo[1].box == 1 

221 assert goto_expr_fn.opo[1].name == "label" 

222 

223 # Checks the correct FN is grabbed in the label computation 

224 assert len(goto_expr_fn.bf) == 2 

225 assert goto_expr_fn.bf[0].value == 0 

226 assert goto_expr_fn.bf[1].value == "500" 

227 

228 assert len(goto_expr_fn.pof) == 2 

229 assert goto_expr_fn.pof[0].box == 1 

230 assert goto_expr_fn.pof[1].box == 2 

231 

232 assert len(goto_expr_fn.wfopo) == 2 

233 assert goto_expr_fn.wfopo[0].src == 1 

234 assert goto_expr_fn.wfopo[0].tgt == 1 

235 

236 assert goto_expr_fn.wfopo[1].src == 2 

237 assert goto_expr_fn.wfopo[1].tgt == 2 

238 

239 ##### 

240 # Checking computed label computation 

241 goto_expr_fn = goto_gromet.fn_array[5] 

242 assert len(goto_expr_fn.opi) == 3 

243 assert goto_expr_fn.opi[0].box == 1 

244 assert goto_expr_fn.opi[1].box == 1 

245 assert goto_expr_fn.opi[2].box == 1 

246 

247 assert len(goto_expr_fn.opo) == 1 

248 assert goto_expr_fn.opo[0].box == 1 

249 

250 # Checks the label generation for the computed GOTO is correct 

251 assert len(goto_expr_fn.bf) == 10 

252 assert goto_expr_fn.bf[0].function_type == FunctionType.LANGUAGE_PRIMITIVE 

253 assert goto_expr_fn.bf[0].name == "_get" 

254 

255 assert goto_expr_fn.bf[2].function_type == FunctionType.IMPORTED_METHOD 

256 assert goto_expr_fn.bf[2].body == 5 

257 

258 assert len(goto_expr_fn.pif) == 12 

259 assert goto_expr_fn.pif[0].box == 1 

260 assert goto_expr_fn.pif[1].box == 4 

261 assert goto_expr_fn.pif[2].box == 4 

262 assert goto_expr_fn.pif[3].box == 5 

263 assert goto_expr_fn.pif[4].box == 5 

264 assert goto_expr_fn.pif[5].box == 7 

265 assert goto_expr_fn.pif[6].box == 7 

266 assert goto_expr_fn.pif[7].box == 3 

267 assert goto_expr_fn.pif[8].box == 3 

268 assert goto_expr_fn.pif[9].box == 10 

269 assert goto_expr_fn.pif[10].box == 10 

270 assert goto_expr_fn.pif[11].box == 1 

271 

272 assert len(goto_expr_fn.pof) == 10 

273 assert goto_expr_fn.pof[0].box == 1 

274 assert goto_expr_fn.pof[1].box == 2 

275 assert goto_expr_fn.pof[2].box == 4 

276 assert goto_expr_fn.pof[3].box == 5 

277 assert goto_expr_fn.pof[4].box == 6 

278 assert goto_expr_fn.pof[5].box == 7 

279 assert goto_expr_fn.pof[6].box == 8 

280 assert goto_expr_fn.pof[7].box == 3 

281 assert goto_expr_fn.pof[8].box == 9 

282 assert goto_expr_fn.pof[9].box == 10 

283 

284 assert len(goto_expr_fn.wfopi) == 3 

285 assert goto_expr_fn.wfopi[0].src == 2 

286 assert goto_expr_fn.wfopi[0].tgt == 1 

287 

288 assert goto_expr_fn.wfopi[1].src == 3 

289 assert goto_expr_fn.wfopi[1].tgt == 2 

290 

291 assert goto_expr_fn.wfopi[2].src == 5 

292 assert goto_expr_fn.wfopi[2].tgt == 3 

293 

294 assert len(goto_expr_fn.wff) == 9 

295 assert goto_expr_fn.wff[0].src == 1 

296 assert goto_expr_fn.wff[0].tgt == 2 

297 

298 assert goto_expr_fn.wff[1].src == 4 

299 assert goto_expr_fn.wff[1].tgt == 3 

300 

301 assert goto_expr_fn.wff[2].src == 6 

302 assert goto_expr_fn.wff[2].tgt == 4 

303 

304 assert goto_expr_fn.wff[3].src == 7 

305 assert goto_expr_fn.wff[3].tgt == 5 

306 

307 assert goto_expr_fn.wff[4].src == 8 

308 assert goto_expr_fn.wff[4].tgt == 6 

309 

310 assert goto_expr_fn.wff[5].src == 9 

311 assert goto_expr_fn.wff[5].tgt == 7 

312 

313 assert goto_expr_fn.wff[6].src == 10 

314 assert goto_expr_fn.wff[6].tgt == 8 

315 

316 assert goto_expr_fn.wff[7].src == 11 

317 assert goto_expr_fn.wff[7].tgt == 9 

318 

319 assert goto_expr_fn.wff[8].src == 12 

320 assert goto_expr_fn.wff[8].tgt == 10 

321 

322 assert len(goto_expr_fn.wfopo) == 1 

323 assert goto_expr_fn.wfopo[0].src == 1 

324 assert goto_expr_fn.wfopo[0].tgt == 1