Coverage for skema/program_analysis/CAST/matlab/tests/test_switch.py: 100%
11 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-04-30 17:15 +0000
« 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 ModelIf,
6 Operator
7)
9def test_1_argument():
10 """ Test CAST from single argument case clause."""
11 source = """
12 switch s
13 case 'one'
14 n = 1;
15 case 'two'
16 n = 2;
17 x = y;
18 otherwise
19 n = 0;
20 end
21 """
22 # switch statement translated into conditional
23 check(
24 cast(source)[0],
25 ModelIf(
26 expr = Operator(op = "==", operands = ["s", "'one'"]),
27 body = [Assignment(left="n", right = 1)],
28 orelse = [
29 ModelIf(
30 expr = Operator(op = "==", operands = ["s", "'two'"]),
31 body = [
32 Assignment(left="n", right = 2),
33 Assignment(left="x", right = "y"),
34 ],
35 orelse = [Assignment(left="n", right = 0)]
36 )
37 ]
38 )
39 )
41def test_n_arguments():
42 """ Test CAST from multipe argument case clause."""
44 source = """
45 switch s
46 case {1, 1.0, one, 'one'}
47 n = 1;
48 otherwise
49 n = 0;
50 end
51 """
52 # switch statement translated into conditional
53 check(
54 cast(source)[0],
55 ModelIf(
56 expr = Operator(
57 op = "in",
58 operands = ["s", [1, 1.0, "one", "'one'"]]
59 ),
60 body = [Assignment(left="n", right = 1)],
61 orelse = [Assignment(left="n", right = 0)]
62 )
63 )
65def test_call_argument():
66 """ Test CAST using the value of a function call """
68 source = """
69 switch fd(i,j)
70 case 0
71 x = 5
72 end
74 """
75 # switch statement translated into conditional
76 check(
77 cast(source)[0],
78 ModelIf(
79 expr = Operator(
80 op = "==",
81 operands = [
82 Call (
83 func = "fd",
84 arguments = ["i","j"]
85 ),
86 0
87 ]
88 ),
89 body = [Assignment(left="x", right = 5)],
90 orelse = []
91 )
92 )