Revision ca7ff3f7
Added by Lélio Brun over 1 year ago
src/tools/stateflow/json-parser/test_json_parser_variables.ml | ||
---|---|---|
5 | 5 |
open LustreSpec |
6 | 6 |
open OUnit2 |
7 | 7 |
|
8 |
module ParseExt = |
|
9 |
struct |
|
8 |
module ParseExt = struct |
|
10 | 9 |
let parse_condition _ = Condition.tru |
11 |
let parse_action _ = Action.nil |
|
12 |
let parse_event json = Some Yojson.Basic.(json |> to_string) |
|
10 |
|
|
11 |
let parse_action _ = Action.nil |
|
12 |
|
|
13 |
let parse_event json = Some Yojson.Basic.(json |> to_string) |
|
13 | 14 |
end |
14 | 15 |
|
15 | 16 |
module Parse = Parser (ParseExt) |
... | ... | |
18 | 19 |
|
19 | 20 |
let string_of_var_type var_type = |
20 | 21 |
match var_type with |
21 |
| Tydec_bool -> "bool" |
|
22 |
| Tydec_int -> "int" |
|
23 |
| Tydec_real -> "real" |
|
24 |
| _ -> "other" |
|
22 |
| Tydec_bool -> |
|
23 |
"bool" |
|
24 |
| Tydec_int -> |
|
25 |
"int" |
|
26 |
| Tydec_real -> |
|
27 |
"real" |
|
28 |
| _ -> |
|
29 |
"other" |
|
25 | 30 |
|
26 | 31 |
let string_of_var_value value = |
27 | 32 |
match value with |
28 |
| Expr_const (Const_tag label) -> label |
|
29 |
| Expr_const (Const_int v) -> string_of_int v |
|
30 |
| Expr_const (Const_real (n, l, s)) -> (Num.string_of_num n) ^ |
|
31 |
" x 10^-" ^ |
|
32 |
(string_of_int l) ^ |
|
33 |
" (" ^ s ^ ")" |
|
34 |
| _ -> "other value (not possible)" |
|
33 |
| Expr_const (Const_tag label) -> |
|
34 |
label |
|
35 |
| Expr_const (Const_int v) -> |
|
36 |
string_of_int v |
|
37 |
| Expr_const (Const_real (n, l, s)) -> |
|
38 |
Num.string_of_num n ^ " x 10^-" ^ string_of_int l ^ " (" ^ s ^ ")" |
|
39 |
| _ -> |
|
40 |
"other value (not possible)" |
|
35 | 41 |
|
36 | 42 |
let test_var_skeleton var id var_type value = |
37 |
begin |
|
38 |
assert_bool |
|
39 |
"orig for user variables should be true" |
|
40 |
var.var_orig; |
|
41 |
assert_bool |
|
42 |
"user variables are considered as constants" |
|
43 |
var.var_dec_const; |
|
44 |
assert_equal |
|
45 |
~msg:("problem with variable " ^ var.var_id ^ " clock type") |
|
46 |
Ckdec_any |
|
47 |
var.var_dec_clock.ck_dec_desc; |
|
48 |
assert_equal |
|
49 |
~msg:("problem with variable " ^ var.var_id ^ " ident") |
|
50 |
~printer:(fun x -> x) |
|
51 |
id |
|
52 |
var.var_id; |
|
43 |
assert_bool "orig for user variables should be true" var.var_orig; |
|
44 |
assert_bool "user variables are considered as constants" var.var_dec_const; |
|
45 |
assert_equal |
|
46 |
~msg:("problem with variable " ^ var.var_id ^ " clock type") |
|
47 |
Ckdec_any var.var_dec_clock.ck_dec_desc; |
|
48 |
assert_equal |
|
49 |
~msg:("problem with variable " ^ var.var_id ^ " ident") |
|
50 |
~printer:(fun x -> x) |
|
51 |
id var.var_id; |
|
52 |
assert_equal |
|
53 |
~msg:("problem with variable " ^ var.var_id ^ " type") |
|
54 |
~printer:string_of_var_type var_type var.var_dec_type.ty_dec_desc; |
|
55 |
match var.var_dec_value with |
|
56 |
| Some { expr_desc = d } -> |
|
53 | 57 |
assert_equal |
54 |
~msg:("problem with variable " ^ var.var_id ^ " type") |
|
55 |
~printer:string_of_var_type |
|
56 |
var_type |
|
57 |
var.var_dec_type.ty_dec_desc; |
|
58 |
match var.var_dec_value with |
|
59 |
| Some { expr_desc = d } -> |
|
60 |
assert_equal |
|
61 |
~msg:("problem with variable " ^ var.var_id ^ " value") |
|
62 |
~printer:string_of_var_value |
|
63 |
value |
|
64 |
d |
|
65 |
| _ -> raise (OUnitTest.OUnit_failure |
|
66 |
"User variables should have an initial value") |
|
67 |
end |
|
58 |
~msg:("problem with variable " ^ var.var_id ^ " value") |
|
59 |
~printer:string_of_var_value value d |
|
60 |
| _ -> |
|
61 |
raise |
|
62 |
(OUnitTest.OUnit_failure "User variables should have an initial value") |
|
68 | 63 |
|
69 | 64 |
let test_simple_var_bool_false tests_ctxt = |
70 |
let prog = Parse.parse_prog |
|
71 |
(Yojson.Basic.from_file "../data-test/simple-var-bool-false.json") in |
|
65 |
let prog = |
|
66 |
Parse.parse_prog |
|
67 |
(Yojson.Basic.from_file "../data-test/simple-var-bool-false.json") |
|
68 |
in |
|
72 | 69 |
match prog with |
73 |
| Program ("simple_var_bool_false", [ ], [ x ]) -> |
|
74 |
test_var_skeleton x "my_bool_var_false" |
|
75 |
Tydec_bool (Expr_const (Const_tag tag_false)) |
|
76 |
| _ -> raise (OUnitTest.OUnit_failure |
|
77 |
"Program obtained from simple-var-bool-false.json is not correct") |
|
70 |
| Program ("simple_var_bool_false", [], [ x ]) -> |
|
71 |
test_var_skeleton x "my_bool_var_false" Tydec_bool |
|
72 |
(Expr_const (Const_tag tag_false)) |
|
73 |
| _ -> |
|
74 |
raise |
|
75 |
(OUnitTest.OUnit_failure |
|
76 |
"Program obtained from simple-var-bool-false.json is not correct") |
|
78 | 77 |
|
79 | 78 |
let test_simple_var_bool_true tests_ctxt = |
80 |
let prog = Parse.parse_prog |
|
81 |
(Yojson.Basic.from_file "../data-test/simple-var-bool-true.json") in |
|
79 |
let prog = |
|
80 |
Parse.parse_prog |
|
81 |
(Yojson.Basic.from_file "../data-test/simple-var-bool-true.json") |
|
82 |
in |
|
82 | 83 |
match prog with |
83 |
| Program ("simple_var_bool_true", [ ], [ x ]) -> |
|
84 |
test_var_skeleton x "my_bool_var_true" |
|
85 |
Tydec_bool (Expr_const (Const_tag tag_true)) |
|
86 |
| _ -> raise (OUnitTest.OUnit_failure |
|
87 |
"Program obtained from simple-var-bool-true.json is not correct") |
|
84 |
| Program ("simple_var_bool_true", [], [ x ]) -> |
|
85 |
test_var_skeleton x "my_bool_var_true" Tydec_bool |
|
86 |
(Expr_const (Const_tag tag_true)) |
|
87 |
| _ -> |
|
88 |
raise |
|
89 |
(OUnitTest.OUnit_failure |
|
90 |
"Program obtained from simple-var-bool-true.json is not correct") |
|
88 | 91 |
|
89 | 92 |
let test_simple_var_int_zero tests_ctxt = |
90 |
let prog = Parse.parse_prog |
|
91 |
(Yojson.Basic.from_file "../data-test/simple-var-int-zero.json") in |
|
93 |
let prog = |
|
94 |
Parse.parse_prog |
|
95 |
(Yojson.Basic.from_file "../data-test/simple-var-int-zero.json") |
|
96 |
in |
|
92 | 97 |
match prog with |
93 |
| Program ("simple_var_int_zero", [ ], [ x ]) -> |
|
94 |
test_var_skeleton x "my_int_var_zero" |
|
95 |
Tydec_int (Expr_const (Const_int 0)) |
|
96 |
| _ -> raise (OUnitTest.OUnit_failure |
|
97 |
"Program obtained from simple-var-int-zero.json is not correct") |
|
98 |
| Program ("simple_var_int_zero", [], [ x ]) -> |
|
99 |
test_var_skeleton x "my_int_var_zero" Tydec_int (Expr_const (Const_int 0)) |
|
100 |
| _ -> |
|
101 |
raise |
|
102 |
(OUnitTest.OUnit_failure |
|
103 |
"Program obtained from simple-var-int-zero.json is not correct") |
|
98 | 104 |
|
99 | 105 |
let test_simple_var_int_pos tests_ctxt = |
100 |
let prog = Parse.parse_prog |
|
101 |
(Yojson.Basic.from_file "../data-test/simple-var-int-pos.json") in |
|
106 |
let prog = |
|
107 |
Parse.parse_prog |
|
108 |
(Yojson.Basic.from_file "../data-test/simple-var-int-pos.json") |
|
109 |
in |
|
102 | 110 |
match prog with |
103 |
| Program ("simple_var_int_pos", [ ], [ x ]) -> |
|
104 |
test_var_skeleton x "my_int_var_pos" |
|
105 |
Tydec_int (Expr_const (Const_int 2)) |
|
106 |
| _ -> raise (OUnitTest.OUnit_failure |
|
107 |
"Program obtained from simple-var-int-pos.json is not correct") |
|
111 |
| Program ("simple_var_int_pos", [], [ x ]) -> |
|
112 |
test_var_skeleton x "my_int_var_pos" Tydec_int (Expr_const (Const_int 2)) |
|
113 |
| _ -> |
|
114 |
raise |
|
115 |
(OUnitTest.OUnit_failure |
|
116 |
"Program obtained from simple-var-int-pos.json is not correct") |
|
108 | 117 |
|
109 | 118 |
let test_simple_var_int_neg tests_ctxt = |
110 |
let prog = Parse.parse_prog |
|
111 |
(Yojson.Basic.from_file "../data-test/simple-var-int-neg.json") in |
|
119 |
let prog = |
|
120 |
Parse.parse_prog |
|
121 |
(Yojson.Basic.from_file "../data-test/simple-var-int-neg.json") |
|
122 |
in |
|
112 | 123 |
match prog with |
113 |
| Program ("simple_var_int_neg", [ ], [ x ]) -> |
|
114 |
test_var_skeleton x "my_int_var_neg" |
|
115 |
Tydec_int (Expr_const (Const_int (-5))) |
|
116 |
| _ -> raise (OUnitTest.OUnit_failure |
|
117 |
"Program obtained from simple-var-int-neg.json is not correct") |
|
124 |
| Program ("simple_var_int_neg", [], [ x ]) -> |
|
125 |
test_var_skeleton x "my_int_var_neg" Tydec_int (Expr_const (Const_int (-5))) |
|
126 |
| _ -> |
|
127 |
raise |
|
128 |
(OUnitTest.OUnit_failure |
|
129 |
"Program obtained from simple-var-int-neg.json is not correct") |
|
118 | 130 |
|
119 | 131 |
let test_simple_var_real_zero tests_ctxt = |
120 |
let prog = Parse.parse_prog |
|
121 |
(Yojson.Basic.from_file "../data-test/simple-var-real-zero.json") in |
|
132 |
let prog = |
|
133 |
Parse.parse_prog |
|
134 |
(Yojson.Basic.from_file "../data-test/simple-var-real-zero.json") |
|
135 |
in |
|
122 | 136 |
match prog with |
123 |
| Program ("simple_var_real_zero", [ ], [ x ]) -> |
|
124 |
test_var_skeleton x "my_real_var_zero" |
|
125 |
Tydec_real (Expr_const (Const_real (Num.num_of_int 0, 1, "0.0"))) |
|
126 |
| _ -> raise (OUnitTest.OUnit_failure |
|
127 |
"Program obtained from simple-var-real-zero.json is not correct") |
|
137 |
| Program ("simple_var_real_zero", [], [ x ]) -> |
|
138 |
test_var_skeleton x "my_real_var_zero" Tydec_real |
|
139 |
(Expr_const (Const_real (Num.num_of_int 0, 1, "0.0"))) |
|
140 |
| _ -> |
|
141 |
raise |
|
142 |
(OUnitTest.OUnit_failure |
|
143 |
"Program obtained from simple-var-real-zero.json is not correct") |
|
128 | 144 |
|
129 | 145 |
let test_simple_var_real_pos tests_ctxt = |
130 |
let prog = Parse.parse_prog |
|
131 |
(Yojson.Basic.from_file "../data-test/simple-var-real-pos.json") in |
|
146 |
let prog = |
|
147 |
Parse.parse_prog |
|
148 |
(Yojson.Basic.from_file "../data-test/simple-var-real-pos.json") |
|
149 |
in |
|
132 | 150 |
match prog with |
133 |
| Program ("simple_var_real_pos", [ ], [ x ]) -> |
|
134 |
test_var_skeleton x "my_real_var_pos" |
|
135 |
Tydec_real (Expr_const (Const_real (Num.num_of_int 2115, 2, "21.15"))) |
|
136 |
| _ -> raise (OUnitTest.OUnit_failure |
|
137 |
"Program obtained from simple-var-real-pos.json is not correct") |
|
151 |
| Program ("simple_var_real_pos", [], [ x ]) -> |
|
152 |
test_var_skeleton x "my_real_var_pos" Tydec_real |
|
153 |
(Expr_const (Const_real (Num.num_of_int 2115, 2, "21.15"))) |
|
154 |
| _ -> |
|
155 |
raise |
|
156 |
(OUnitTest.OUnit_failure |
|
157 |
"Program obtained from simple-var-real-pos.json is not correct") |
|
138 | 158 |
|
139 | 159 |
let test_simple_var_real_neg tests_ctxt = |
140 |
let prog = Parse.parse_prog |
|
141 |
(Yojson.Basic.from_file "../data-test/simple-var-real-neg.json") in |
|
160 |
let prog = |
|
161 |
Parse.parse_prog |
|
162 |
(Yojson.Basic.from_file "../data-test/simple-var-real-neg.json") |
|
163 |
in |
|
142 | 164 |
match prog with |
143 |
| Program ("simple_var_real_neg", [ ], [ x ]) -> |
|
144 |
test_var_skeleton x "my_real_var_neg" |
|
145 |
Tydec_real (Expr_const (Const_real (Num.num_of_int (-224), 2, "-2.24"))) |
|
146 |
| _ -> raise (OUnitTest.OUnit_failure |
|
147 |
"Program obtained from simple-var-real-neg.json is not correct") |
|
165 |
| Program ("simple_var_real_neg", [], [ x ]) -> |
|
166 |
test_var_skeleton x "my_real_var_neg" Tydec_real |
|
167 |
(Expr_const (Const_real (Num.num_of_int (-224), 2, "-2.24"))) |
|
168 |
| _ -> |
|
169 |
raise |
|
170 |
(OUnitTest.OUnit_failure |
|
171 |
"Program obtained from simple-var-real-neg.json is not correct") |
|
148 | 172 |
|
149 | 173 |
let test_simple_var_real_e tests_ctxt = |
150 |
let prog = Parse.parse_prog |
|
151 |
(Yojson.Basic.from_file "../data-test/simple-var-real-e.json") in |
|
174 |
let prog = |
|
175 |
Parse.parse_prog |
|
176 |
(Yojson.Basic.from_file "../data-test/simple-var-real-e.json") |
|
177 |
in |
|
152 | 178 |
match prog with |
153 |
| Program ("simple_var_real_e", [ ], [ x ]) -> |
|
154 |
test_var_skeleton x "my_real_var_e" |
|
155 |
Tydec_real (Expr_const (Const_real (Num.num_of_int (-2115), 4, "-21.15e-02"))) |
|
156 |
| _ -> raise (OUnitTest.OUnit_failure |
|
157 |
"Program obtained from simple-var-real-e.json is not correct") |
|
179 |
| Program ("simple_var_real_e", [], [ x ]) -> |
|
180 |
test_var_skeleton x "my_real_var_e" Tydec_real |
|
181 |
(Expr_const (Const_real (Num.num_of_int (-2115), 4, "-21.15e-02"))) |
|
182 |
| _ -> |
|
183 |
raise |
|
184 |
(OUnitTest.OUnit_failure |
|
185 |
"Program obtained from simple-var-real-e.json is not correct") |
|
158 | 186 |
|
159 | 187 |
let test_simple_var_real_wo_dec tests_ctxt = |
160 |
assert_raises (Parse.JSON_parse_error("Invalid real constant 2500"))
|
|
161 |
(fun _ -> Parse.parse_prog (Yojson.Basic.from_file
|
|
162 |
"../data-test/simple-var-real-wo-dec.json"))
|
|
188 |
assert_raises (Parse.JSON_parse_error "Invalid real constant 2500") (fun _ ->
|
|
189 |
Parse.parse_prog
|
|
190 |
(Yojson.Basic.from_file "../data-test/simple-var-real-wo-dec.json"))
|
|
163 | 191 |
|
164 | 192 |
let var_suite = |
165 |
"suite for variables" >::: |
|
166 |
[ "simple test for variable (boolean, false)" >:: |
|
167 |
test_simple_var_bool_false; |
|
168 |
"simple test for variable (boolean, true)" >:: |
|
169 |
test_simple_var_bool_true; |
|
170 |
"simple test for variable (int, 0)" >:: |
|
171 |
test_simple_var_int_zero; |
|
172 |
"simple test for variable (int, 2)" >:: |
|
173 |
test_simple_var_int_pos; |
|
174 |
"simple test for variable (int, -5)" >:: |
|
175 |
test_simple_var_int_neg; |
|
176 |
"simple test for variable (real, 0.0)" >:: |
|
177 |
test_simple_var_real_zero; |
|
178 |
"simple test for variable (real, 21.15)" >:: |
|
179 |
test_simple_var_real_pos; |
|
180 |
"simple test for variable (real, -2.24)" >:: |
|
181 |
test_simple_var_real_neg; |
|
182 |
"simple test for variable (real, -21.15e-02)" >:: |
|
183 |
test_simple_var_real_e; |
|
184 |
"simple test for variable (real, 2500)" >:: |
|
185 |
test_simple_var_real_wo_dec; |
|
186 |
] |
|
187 |
|
|
188 |
let _ = |
|
189 |
run_test_tt_main var_suite |
|
193 |
"suite for variables" |
|
194 |
>::: [ |
|
195 |
"simple test for variable (boolean, false)" |
|
196 |
>:: test_simple_var_bool_false; |
|
197 |
"simple test for variable (boolean, true)" |
|
198 |
>:: test_simple_var_bool_true; |
|
199 |
"simple test for variable (int, 0)" >:: test_simple_var_int_zero; |
|
200 |
"simple test for variable (int, 2)" >:: test_simple_var_int_pos; |
|
201 |
"simple test for variable (int, -5)" >:: test_simple_var_int_neg; |
|
202 |
"simple test for variable (real, 0.0)" >:: test_simple_var_real_zero; |
|
203 |
"simple test for variable (real, 21.15)" >:: test_simple_var_real_pos; |
|
204 |
"simple test for variable (real, -2.24)" >:: test_simple_var_real_neg; |
|
205 |
"simple test for variable (real, -21.15e-02)" |
|
206 |
>:: test_simple_var_real_e; |
|
207 |
"simple test for variable (real, 2500)" >:: test_simple_var_real_wo_dec; |
|
208 |
] |
|
209 |
|
|
210 |
let _ = run_test_tt_main var_suite |
Also available in: Unified diff
reformatting