Coverage for skema/program_analysis/tests/test_goto_basic.py: 100%

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 """ 

19 SUBROUTINE GoToExample 

20 INTEGER:: i 

21 

22 i = 1 

2310 CONTINUE 

24 i = i + 1 

25 

26 IF (i <= 5) GO TO 10 

27 

28 END SUBROUTINE GoToExample 

29 """ 

30 

31def goto1(): 

32 return """ 

33 SUBROUTINE EXAMPLE(DA,C,S) 

34* .. Scalar Arguments .. 

35 DOUBLE PRECISION C,DA,S 

36* .. Local Scalars .. 

37 DOUBLE PRECISION R,ROE 

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

39 C = 1 

40 R = 2 

41 GO TO 20 

42 10 C = 2 

43 R = 3 

44 20 DA = R 

45 S = C 

46 RETURN 

47 END 

48 """ 

49 

50 

51def generate_gromet(test_file_string): 

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

53 create_temp_file(test_file_string, "f95") 

54 

55 ts2cast = TS2CAST("temp.f95") 

56 out_cast = ts2cast.out_cast[0] 

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

58 

59 delete_temp_file("f95") 

60 

61 return gromet 

62 

63def test_goto0(): 

64 goto_gromet = generate_gromet(goto0()) 

65 ##### 

66 # Checking FN containing label 

67 label_fn = goto_gromet.fn_array[0] 

68 assert len(label_fn.bf) == 5 

69 assert label_fn.bf[1].function_type == FunctionType.LABEL 

70 assert label_fn.bf[1].name == "10" 

71 

72 assert len(label_fn.pif) == 2 

73 assert label_fn.pif[0].box == 2 

74 assert label_fn.pif[1].box == 4 

75 

76 assert len(label_fn.pof) == 4 

77 assert label_fn.pof[0].box == 1 

78 assert label_fn.pof[0].name == "i" 

79 

80 assert label_fn.pof[1].box == 2 

81 assert label_fn.pof[1].name == "i" 

82 

83 assert label_fn.pof[2].box == 4 

84 assert label_fn.pof[2].name == "i" 

85 

86 assert label_fn.pof[3].box == 5 

87 

88 assert len(label_fn.wff) == 2 

89 assert label_fn.wff[0].src == 1 

90 assert label_fn.wff[0].tgt == 1 

91 

92 assert label_fn.wff[1].src == 2 

93 assert label_fn.wff[1].tgt == 2 

94 

95 assert len(label_fn.wfc) == 1 

96 assert label_fn.wfc[0].src == 1 

97 assert label_fn.wfc[0].tgt == 3 

98 

99 assert len(label_fn.bc) == 1 

100 assert label_fn.bc[0].condition == 5 

101 assert label_fn.bc[0].body_if == 6 

102 

103 assert len(label_fn.pic) == 1 

104 assert label_fn.pic[0].box == 1 

105 assert label_fn.pic[0].name == "i" 

106 

107 assert len(label_fn.poc) == 1 

108 assert label_fn.poc[0].box == 1 

109 assert label_fn.poc[0].name == "i" 

110 

111 ##### 

112 # Checking goto call 

113 goto_call_fn = goto_gromet.fn_array[5] 

114 assert len(goto_call_fn.opi) == 1 

115 assert goto_call_fn.opi[0].box == 1 

116 

117 assert len(goto_call_fn.opo) == 1 

118 assert goto_call_fn.opo[0].box == 1 

119 assert goto_call_fn.opo[0].name == "i" 

120 

121 assert len(goto_call_fn.wfopi) == 1 

122 assert goto_call_fn.wfopi[0].src == 1 

123 assert goto_call_fn.wfopi[0].tgt == 1 

124 

125 assert len(goto_call_fn.wopio) == 1 

126 assert goto_call_fn.wopio[0].src == 1 

127 assert goto_call_fn.wopio[0].tgt == 1 

128 

129 assert len(goto_call_fn.bf) == 1 

130 assert goto_call_fn.bf[0].function_type == FunctionType.GOTO 

131 assert goto_call_fn.bf[0].body == 7 

132 

133 assert len(goto_call_fn.pif) == 1 

134 assert goto_call_fn.pif[0].box == 1 

135 

136 ##### 

137 # Checking basic label computation 

138 goto_expr_fn = goto_gromet.fn_array[6] 

139 assert len(goto_expr_fn.opi) == 1 

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

141 

142 assert len(goto_expr_fn.opo) == 2 

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

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

145 

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

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

148 

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

150 assert len(goto_expr_fn.bf) == 2 

151 assert goto_expr_fn.bf[0].value == 1 

152 assert goto_expr_fn.bf[1].value == "10" 

153 

154 assert len(goto_expr_fn.pof) == 2 

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

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

157 

158 assert len(goto_expr_fn.wfopo) == 2 

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

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

161 

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

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

164 

165def test_goto1(): 

166 goto_gromet = generate_gromet(goto1()) 

167 ##### 

168 # Checking FN containing labels and one GOTO call 

169 label_fn = goto_gromet.fn_array[0] 

170 assert len(label_fn.bf) == 10 

171 assert label_fn.bf[2].function_type == FunctionType.GOTO 

172 assert label_fn.bf[2].body == 7 

173 assert label_fn.bf[2].name == "20" 

174 

175 assert label_fn.bf[3].function_type == FunctionType.LABEL 

176 assert label_fn.bf[3].name == "10" 

177 

178 assert label_fn.bf[6].function_type == FunctionType.LABEL 

179 assert label_fn.bf[6].name == "20" 

180 

181 assert len(label_fn.pif) == 15 

182 assert label_fn.pif[0].box == 3 

183 assert label_fn.pif[1].box == 3 

184 assert label_fn.pif[2].box == 3 

185 assert label_fn.pif[3].box == 3 

186 

187 assert label_fn.pif[4].box == 4 

188 assert label_fn.pif[5].box == 4 

189 assert label_fn.pif[6].box == 4 

190 assert label_fn.pif[7].box == 4 

191 

192 assert label_fn.pif[8].box == 7 

193 assert label_fn.pif[9].box == 7 

194 assert label_fn.pif[10].box == 7 

195 assert label_fn.pif[11].box == 7 

196 

197 assert label_fn.pif[12].box == 10 

198 assert label_fn.pif[13].box == 10 

199 assert label_fn.pif[14].box == 10 

200 

201 assert len(label_fn.pof) == 15 

202 assert label_fn.pof[2].box == 4 

203 assert label_fn.pof[2].name == "DA" 

204 

205 assert label_fn.pof[3].box == 4 

206 assert label_fn.pof[3].name == "C" 

207 

208 assert label_fn.pof[4].box == 4 

209 assert label_fn.pof[4].name == "S" 

210 

211 assert label_fn.pof[5].box == 4 

212 assert label_fn.pof[5].name == "R" 

213 

214 assert label_fn.pof[8].box == 7 

215 assert label_fn.pof[8].name == "DA" 

216 

217 assert label_fn.pof[9].box == 7 

218 assert label_fn.pof[9].name == "C" 

219 

220 assert label_fn.pof[10].box == 7 

221 assert label_fn.pof[10].name == "S" 

222 

223 assert label_fn.pof[11].box == 7 

224 assert label_fn.pof[11].name == "R" 

225 

226 assert len(label_fn.wfopi) == 4 

227 assert len(label_fn.wff) == 11 

228 assert len(label_fn.wfopo) == 1 

229 assert len(label_fn.wcopi) == 1 

230 

231 assert len(label_fn.bc) == 1 

232 assert label_fn.bc[0].condition == 2 

233 assert label_fn.bc[0].body_if == 3 

234 

235 assert len(label_fn.pic) == 1 

236 assert label_fn.pic[0].box == 1 

237 assert label_fn.pic[0].name == "DA" 

238 

239 assert len(label_fn.poc) == 1 

240 assert label_fn.poc[0].box == 1 

241 assert label_fn.poc[0].name == "DA" 

242 

243 ##### 

244 # Checking goto call for first GOTO 

245 goto_call_fn = goto_gromet.fn_array[2] 

246 assert len(goto_call_fn.opi) == 1 

247 assert goto_call_fn.opi[0].box == 1 

248 

249 assert len(goto_call_fn.opo) == 1 

250 assert goto_call_fn.opo[0].box == 1 

251 assert goto_call_fn.opo[0].name == "DA" 

252 

253 assert len(goto_call_fn.wfopi) == 1 

254 assert goto_call_fn.wfopi[0].src == 1 

255 assert goto_call_fn.wfopi[0].tgt == 1 

256 

257 assert len(goto_call_fn.wopio) == 1 

258 assert goto_call_fn.wopio[0].src == 1 

259 assert goto_call_fn.wopio[0].tgt == 1 

260 

261 assert len(goto_call_fn.bf) == 1 

262 assert goto_call_fn.bf[0].function_type == FunctionType.GOTO 

263 assert goto_call_fn.bf[0].body == 4 

264 

265 assert len(goto_call_fn.pif) == 1 

266 assert goto_call_fn.pif[0].box == 1 

267 

268 ##### 

269 # Checking basic label computation for first GOTO 

270 goto_expr_fn = goto_gromet.fn_array[3] 

271 assert len(goto_expr_fn.opi) == 1 

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

273 

274 assert len(goto_expr_fn.opo) == 2 

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

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

277 

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

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

280 

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

282 assert len(goto_expr_fn.bf) == 2 

283 assert goto_expr_fn.bf[0].value == 1 

284 assert goto_expr_fn.bf[1].value == "10" 

285 

286 assert len(goto_expr_fn.pof) == 2 

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

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

289 

290 assert len(goto_expr_fn.wfopo) == 2 

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

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

293 

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

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

296 

297 ##### 

298 # Checking basic label computation for second GOTO 

299 goto_expr_fn2 = goto_gromet.fn_array[6] 

300 assert len(goto_expr_fn2.opi) == 4 

301 assert goto_expr_fn2.opi[0].box == 1 

302 

303 assert len(goto_expr_fn2.opo) == 2 

304 assert goto_expr_fn2.opo[0].box == 1 

305 assert goto_expr_fn2.opo[0].name == "fn_idx" 

306 

307 assert goto_expr_fn2.opo[1].box == 1 

308 assert goto_expr_fn2.opo[1].name == "label" 

309 

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

311 assert len(goto_expr_fn2.bf) == 2 

312 assert goto_expr_fn2.bf[0].value == 1 

313 assert goto_expr_fn2.bf[1].value == "20" 

314 

315 assert len(goto_expr_fn2.pof) == 2 

316 assert goto_expr_fn2.pof[0].box == 1 

317 assert goto_expr_fn2.pof[1].box == 2 

318 

319 assert len(goto_expr_fn2.wfopo) == 2 

320 assert goto_expr_fn2.wfopo[0].src == 1 

321 assert goto_expr_fn2.wfopo[0].tgt == 1 

322 

323 assert goto_expr_fn2.wfopo[1].src == 2 

324 assert goto_expr_fn2.wfopo[1].tgt == 2