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

97 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 

9 

10from skema.program_analysis.CAST.pythonAST import py_ast_to_cast 

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

12from skema.program_analysis.CAST2FN import cast 

13from skema.program_analysis.CAST2FN.cast import CAST 

14from skema.program_analysis.run_ann_cast_pipeline import ann_cast_pipeline 

15 

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

17# more complex ones will follow later as needed 

18 

19def primitive1(): 

20 return """ 

21x = 10 

22y = 5 

23z = min(x,y) 

24 """ 

25 

26def primitive2(): 

27 return """ 

28def foo(x,y): 

29 z = min(x,y) + y 

30 return z 

31 

32foo(10,2) 

33 """ 

34 

35def primitive3(): 

36 return """ 

37min(10,5) 

38 """ 

39 

40def generate_gromet(test_file_string): 

41 # use ast.Parse to get Python AST 

42 contents = ast.parse(test_file_string) 

43 

44 # use Python to CAST 

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

46 convert = py_ast_to_cast.PyASTToCAST("temp") 

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

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

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

50 

51 # use AnnCastPipeline to create GroMEt 

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

53 

54 return gromet 

55 

56def test_primitive1(): 

57 exp_gromet = generate_gromet(primitive1()) 

58 

59 base_fn = exp_gromet.fn 

60 

61 assert len(base_fn.b) == 1 

62 assert len(base_fn.bf) == 3 

63 

64 assert len(base_fn.pif) == 2 

65 assert len(base_fn.pof) == 3 

66 assert len(base_fn.wff) == 2 

67 

68 assert base_fn.bf[2].function_type == FunctionType.LANGUAGE_PRIMITIVE 

69 

70 assert base_fn.pif[0].box == 3 

71 assert base_fn.pif[1].box == 3 

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

73 

74 assert base_fn.wff[0].src == 1 and base_fn.wff[0].tgt == 1 

75 assert base_fn.wff[1].src == 2 and base_fn.wff[1].tgt == 2 

76 

77 

78def test_primitive2(): 

79 exp_gromet = generate_gromet(primitive2()) 

80 

81 base_fn = exp_gromet.fn 

82 primitive_fn = exp_gromet.fn_array[1] 

83 add_fn = exp_gromet.fn_array[2] 

84 

85 assert len(base_fn.b) == 1 

86 

87 assert base_fn.pif[0].box == 1 

88 assert base_fn.pif[1].box == 1 

89 

90 assert base_fn.pof[0].box == 2 

91 assert base_fn.pof[1].box == 3 

92 

93 assert base_fn.wff[0].src == 1 and base_fn.wff[0].tgt == 1 

94 assert base_fn.wff[1].src == 2 and base_fn.wff[1].tgt == 2 

95 

96 ############################################## 

97 assert len(primitive_fn.opi) == 2 

98 assert len(primitive_fn.opo) == 1 

99 

100 assert len(primitive_fn.bf) == 1 

101 

102 assert len(primitive_fn.pif) == 2 

103 assert len(primitive_fn.pof) == 1 

104 

105 assert len(primitive_fn.wfopi) == 2 

106 

107 assert primitive_fn.bf[0].function_type == FunctionType.LANGUAGE_PRIMITIVE 

108 

109 assert primitive_fn.wfopi[0].src == 1 and primitive_fn.wfopi[0].tgt == 1 

110 assert primitive_fn.wfopi[1].src == 2 and primitive_fn.wfopi[1].tgt == 2 

111 

112 assert primitive_fn.wfopo[0].src == 1 and primitive_fn.wfopo[0].tgt == 1 

113 

114 ############################################## 

115 assert len(add_fn.opi) == 2 

116 assert len(add_fn.opo) == 1 

117 

118 assert len(add_fn.bf) == 2 

119 assert len(add_fn.pif) == 4 

120 assert len(add_fn.pof) == 2 

121 

122 assert len(add_fn.wfopi) == 3 

123 assert len(add_fn.wff) == 1 

124 assert len(add_fn.wfopo) == 1 

125 

126 # Check bfs 

127 assert add_fn.bf[0].body == 2 

128 assert add_fn.bf[1].function_type == FunctionType.LANGUAGE_PRIMITIVE 

129 

130 # Check wiring 

131 assert add_fn.wfopi[0].src == 1 and add_fn.wfopi[0].tgt == 1 

132 assert add_fn.wfopi[1].src == 2 and add_fn.wfopi[1].tgt == 2 

133 assert add_fn.wfopi[2].src == 4 and add_fn.wfopi[2].tgt == 2 

134 

135 assert add_fn.wff[0].src == 3 and add_fn.wfopi[0].tgt == 1 

136 

137 assert add_fn.wfopo[0].src == 1 and add_fn.wfopo[0].tgt == 2 

138 

139 

140def test_primitive3(): 

141 exp_gromet = generate_gromet(primitive3()) 

142 

143 base_fn = exp_gromet.fn 

144 primitive_fn = exp_gromet.fn_array[0] 

145 

146 assert len(base_fn.b) == 1 

147 assert len(base_fn.bf) == 1 

148 

149 assert primitive_fn.bf[0].function_type == FunctionType.LANGUAGE_PRIMITIVE 

150 assert primitive_fn.bf[1].function_type == FunctionType.LITERAL 

151 assert primitive_fn.bf[2].function_type == FunctionType.LITERAL 

152 

153 assert primitive_fn.opo[0].box == 1 

154 

155 assert primitive_fn.pif[0].box == 1 

156 assert primitive_fn.pif[1].box == 1 

157 

158 assert primitive_fn.pof[0].box == 1 

159 assert primitive_fn.pof[1].box == 2 

160 assert primitive_fn.pof[2].box == 3 

161 

162 assert primitive_fn.wff[0].src == 1 and primitive_fn.wff[0].tgt == 2 

163 assert primitive_fn.wff[1].src == 2 and primitive_fn.wff[1].tgt == 3 

164 assert primitive_fn.wfopo[0].src == 1 and primitive_fn.wfopo[0].tgt == 1 

165 

166 

167def test_primitive(): 

168 test_primitive1() 

169 test_primitive2() 

170 test_primitive3() 

171 

172 return