Coverage for skema/program_analysis/CAST/matlab/tests/test_loop.py: 36%

14 statements  

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

1from skema.program_analysis.CAST.matlab.tests.utils import (check, cast) 

2from skema.program_analysis.CAST2FN.model.cast import ( 

3 Assignment, 

4 Call, 

5 Loop, 

6 Operator 

7) 

8 

9# Test the for loop incrementing by 1 

10def no_test_implicit_step(): 

11 """ Test the MATLAB for loop syntax elements""" 

12 source = """ 

13 for n = 0:10 

14 disp(n) 

15 end 

16 """ 

17 nodes = cast(source) 

18 check(nodes[0], 

19 Loop( 

20 pre = [Assignment(left = "n", right = 0)], 

21 expr = Operator(op = "<=", operands = ["n", 10]), 

22 body = [ 

23 Call( 

24 func = "disp", 

25 arguments = ["n"] 

26 ), 

27 Assignment( 

28 left = "n", 

29 right = Operator( 

30 op = "+", 

31 operands = ["n", 1] 

32 ) 

33 ) 

34 ], 

35 post = [] 

36 ) 

37 ) 

38 

39# Test the for loop incrementing by n 

40def no_test_explicit_step(): 

41 """ Test the MATLAB for loop syntax elements""" 

42 source = """ 

43 for n = 0:2:10  

44 disp(n) 

45 end 

46 """ 

47 nodes = cast(source) 

48 check(nodes[0], 

49 Loop( 

50 pre = [Assignment(left = "n", right = 0)], 

51 expr = Operator(op = "<=", operands = ["n", 10]), 

52 body = [ 

53 Call( 

54 func = "disp", 

55 arguments = ["n"] 

56 ), 

57 Assignment( 

58 left = "n", 

59 right = Operator( 

60 op = "+", 

61 operands = ["n", 2] 

62 ) 

63 ) 

64 ], 

65 post = [] 

66 ) 

67 ) 

68 

69 

70 

71 

72# Test the for loop using matrix steps 

73def no_test_matrix(): 

74 """ Test the MATLAB for loop syntax elements""" 

75 source = """ 

76 for k = [10 3 5 6] 

77 disp(k) 

78 end 

79 """ 

80 nodes = cast(source) 

81 check(nodes[0], 

82 Loop( 

83 pre = [ 

84 Assignment( 

85 left = "_mat", 

86 right = [10, 3, 5, 6] 

87 ), 

88 Assignment( 

89 left = "_mat_len", 

90 right = 4 

91 ), 

92 Assignment( 

93 left = "_mat_idx", 

94 right = 0 

95 ), 

96 Assignment( 

97 left = "k", 

98 right = 10 

99 ) 

100 ], 

101 expr = Operator(op = "<", operands = ["_mat_idx", "_mat_len"]), 

102 body = [ 

103 Call( 

104 func = "disp", 

105 arguments = ["k"] 

106 ), 

107 Assignment( 

108 left = "_mat_idx", 

109 right = Operator( 

110 op = "+", 

111 operands = ["_mat_idx", 1] 

112 ) 

113 ), 

114 Assignment( 

115 left = "k", 

116 right = "_mat[_mat_idx]" 

117 ) 

118 ], 

119 post = [] 

120 

121 ) 

122 ) 

123