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

109 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 GrometFNModuleCollection 

5from skema.gromet.fn import FunctionType 

6import ast 

7 

8from skema.program_analysis.CAST.pythonAST import py_ast_to_cast 

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

10from skema.program_analysis.CAST2FN import cast 

11from skema.program_analysis.CAST2FN.cast import CAST 

12from skema.program_analysis.run_ann_cast_pipeline import ann_cast_pipeline 

13 

14# NOTE: these examples are very trivial for the realm of recursion 

15# more complex ones will follow later as needed 

16 

17def while1(): 

18 return """ 

19x = 2 

20while x < 5: 

21 x = x + 1 

22 """ 

23 

24def while2(): 

25 return """ 

26x = 2 

27y = 3 

28 

29while x < 5: 

30 x = x + y 

31 """ 

32 

33def while3(): 

34 return """ 

35x = 2 

36y = 3 

37 

38while x < 5: 

39 x = x + 1 

40 x = x + y 

41 """ 

42 

43def generate_gromet(test_file_string): 

44 # use ast.Parse to get Python AST 

45 contents = ast.parse(test_file_string) 

46 

47 # use Python to CAST 

48 line_count = len(test_file_string.split("\n")) 

49 convert = py_ast_to_cast.PyASTToCAST("temp") 

50 C = convert.visit(contents, {}, {}) 

51 C.source_refs = [SourceRef("temp", None, None, 1, line_count)] 

52 out_cast = cast.CAST([C], "python") 

53 

54 # use AnnCastPipeline to create GroMEt 

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

56 

57 return gromet 

58 

59def test_while1(): 

60 while_gromet = generate_gromet(while1()) 

61 

62 base_fn = while_gromet.fn 

63 predicate_fn = while_gromet.fn_array[1] 

64 

65 # Base FN with loop 

66 

67 assert len(base_fn.wfl) == 1 

68 assert len(base_fn.pil) == 1 

69 assert len(base_fn.pol) == 1 

70 assert len(base_fn.bl) == 1 

71 

72 # Check Ports 

73 assert base_fn.pof[0].name == "x" and base_fn.pof[0].box == 1 

74 assert base_fn.pil[0].name == "x" and base_fn.pil[0].name == "x" 

75 

76 # Check boxes 

77 assert len(base_fn.bl) == 1 

78 assert base_fn.bl[0].condition == 2 and base_fn.bl[0].body == 3 

79 

80 # Check Wires 

81 assert len(base_fn.wfl) == 1 

82 assert base_fn.wfl[0].src == 1 and base_fn.wfl[0].tgt == 1 

83 

84 ############################################### 

85 assert predicate_fn.b[0].function_type == FunctionType.PREDICATE 

86 

87 # Check port counts 

88 assert len(predicate_fn.opi) == 1 

89 assert len(predicate_fn.opo) == 2 

90 assert len(predicate_fn.wopio) == 1 

91 assert len(predicate_fn.wfopi) == 1 

92 assert len(predicate_fn.wff) == 1 

93 assert len(predicate_fn.wfopo) == 1 

94 

95 # Check bf count 

96 assert len(predicate_fn.bf) == 2 

97 

98 # Check wires 

99 assert predicate_fn.wopio[0].src == 1 and predicate_fn.wopio[0].tgt == 1 

100 assert predicate_fn.wfopo[0].src == 2 and predicate_fn.wfopo[0].tgt == 2 

101 

102 # Check bf 

103 assert predicate_fn.bf[1].name == "ast.Lt" 

104 

105 

106def test_while2(): 

107 while_gromet = generate_gromet(while2()) 

108 base_fn = while_gromet.fn 

109 predicate_fn = while_gromet.fn_array[2] 

110 

111 assert predicate_fn.b[0].function_type == FunctionType.PREDICATE 

112 

113 # Base fn with loop 

114 # Check gromet element counts 

115 assert len(base_fn.bf) == 2 

116 assert len(base_fn.wfl) == 2 

117 assert len(base_fn.bl) == 1 

118 

119 # Check wires 

120 assert base_fn.wfl[0].src == 1 and base_fn.wfl[0].tgt == 1 

121 assert base_fn.wfl[1].src == 2 and base_fn.wfl[1].tgt == 2 

122 assert base_fn.bl[0].condition == 3 and base_fn.bl[0].body == 4 

123 

124 # Check ports 

125 assert base_fn.pil[0].name == "x" and base_fn.pil[1].name == "y" 

126 assert base_fn.pol[0].name == "x" and base_fn.pol[1].name == "y" 

127 

128 # Check predicate 

129 # Check port counts 

130 assert len(predicate_fn.opi) == 2 

131 assert len(predicate_fn.opo) == 3 

132 assert len(predicate_fn.wopio) == 2 

133 assert len(predicate_fn.wfopi) == 1 

134 assert len(predicate_fn.wff) == 1 

135 assert len(predicate_fn.wfopo) == 1 

136 

137 # Check bf count 

138 assert len(predicate_fn.bf) == 2 

139 

140 # Check wires 

141 assert predicate_fn.wopio[0].src == 1 and predicate_fn.wopio[0].tgt == 1 

142 assert predicate_fn.wopio[1].src == 2 and predicate_fn.wopio[1].tgt == 2 

143 

144 assert predicate_fn.wfopi[0].src == 1 and predicate_fn.wfopi[0].tgt == 1 

145 assert predicate_fn.wff[0].src == 2 and predicate_fn.wff[0].tgt == 1 

146 assert predicate_fn.wfopo[0].src == 3 and predicate_fn.wfopo[0].tgt == 2 

147 

148 # Check bf 

149 assert predicate_fn.bf[1].name == "ast.Lt" 

150 

151 

152def test_while3(): 

153 while_gromet = generate_gromet(while3()) 

154 base_fn = while_gromet.fn 

155 predicate_fn = while_gromet.fn_array[2] 

156 

157 assert predicate_fn.b[0].function_type == FunctionType.PREDICATE 

158 

159 # Base fn with loop 

160 # Check gromet element counts 

161 assert len(base_fn.bf) == 2 

162 assert len(base_fn.wfl) == 2 

163 assert len(base_fn.bl) == 1 

164 

165 # Check wires 

166 assert base_fn.wfl[0].src == 1 and base_fn.wfl[0].tgt == 1 

167 assert base_fn.wfl[1].src == 2 and base_fn.wfl[1].tgt == 2 

168 assert base_fn.bl[0].condition == 3 and base_fn.bl[0].body == 4 

169 

170 # Check ports 

171 assert base_fn.pil[0].name == "x" and base_fn.pil[1].name == "y" 

172 assert base_fn.pol[0].name == "x" and base_fn.pol[1].name == "y" 

173 

174 

175 # Check predicate 

176 # Check port counts 

177 assert len(predicate_fn.opi) == 2 

178 assert len(predicate_fn.opo) == 3 

179 assert len(predicate_fn.wopio) == 2 

180 assert len(predicate_fn.wfopi) == 1 

181 assert len(predicate_fn.wff) == 1 

182 assert len(predicate_fn.wfopo) == 1 

183 assert len(predicate_fn.pif) == 2 

184 assert len(predicate_fn.pof) == 2 

185 

186 # Check port boxes 

187 assert predicate_fn.pif[0].box == 2 and predicate_fn.pif[1].box == 2 

188 assert predicate_fn.pof[0].box == 1 and predicate_fn.pof[1].box == 2 

189 

190 # Check bf count 

191 assert len(predicate_fn.bf) == 2 

192 

193 # Check wires 

194 assert predicate_fn.wopio[0].src == 1 and predicate_fn.wopio[0].tgt == 1 

195 assert predicate_fn.wopio[1].src == 2 and predicate_fn.wopio[1].tgt == 2 

196 

197 assert predicate_fn.wfopi[0].src == 1 and predicate_fn.wfopi[0].tgt == 1 

198 assert predicate_fn.wff[0].src == 2 and predicate_fn.wff[0].tgt == 1 

199 assert predicate_fn.wfopo[0].src == 3 and predicate_fn.wfopo[0].tgt == 2 

200 

201 # Check bf 

202 assert predicate_fn.bf[1].name == "ast.Lt" 

203 

204 

205 

206def test_conditional(): 

207 test_while1() 

208 test_while2() 

209 test_while3()