Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

lustrec / src / tools / stateflow / json-parser / test_json_parser_variables.ml @ cbfee4a3

History | View | Annotate | Download (6.35 KB)

1
open Basetypes
2
open Corelang
3
open Datatype
4
open Json_parser
5
open LustreSpec
6
open OUnit2
7

    
8
module ParseExt =
9
struct
10
  let parse_condition _ = Condition.tru
11
  let parse_action    _ = Action.nil
12
  let parse_event json  = Some Yojson.Basic.(json |> to_string)
13
end
14

    
15
module Parse = Parser (ParseExt)
16

    
17
let location = Location.dummy_loc
18

    
19
let string_of_var_type var_type =
20
  match var_type with
21
  | Tydec_bool -> "bool"
22
  | Tydec_int  -> "int"
23
  | Tydec_real -> "real"
24
  | _          -> "other"
25

    
26
let string_of_var_value value =
27
  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)"
35

    
36
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;
53
    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
68

    
69
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
72
  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")
78

    
79
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
82
  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")
88

    
89
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
92
  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

    
99
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
102
  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")
108

    
109
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
112
  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")
118

    
119
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
122
  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")
128

    
129
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
132
  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")
138

    
139
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
142
  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")
148

    
149
let test_simple_var_real_wo_dec tests_ctxt =
150
  assert_raises (Parse.JSON_parse_error("Invalid real constant 2500"))
151
    (fun _ -> Parse.parse_prog (Yojson.Basic.from_file
152
                                  "../data-test/simple-var-real-wo-dec.json"))
153

    
154
let var_suite =
155
  "suite for variables" >:::
156
  [ "simple test for variable (boolean, false)" >::
157
    test_simple_var_bool_false;
158
    "simple test for variable (boolean, true)"  >::
159
    test_simple_var_bool_true;
160
    "simple test for variable (int, 0)"  >::
161
    test_simple_var_int_zero;
162
    "simple test for variable (int, 2)"  >::
163
    test_simple_var_int_pos;
164
    "simple test for variable (int, -5)"  >::
165
    test_simple_var_int_neg;
166
    "simple test for variable (real, 0.0)"  >::
167
    test_simple_var_real_zero;
168
    "simple test for variable (real, 21.15)"  >::
169
    test_simple_var_real_pos;
170
    "simple test for variable (real, -2.24)"  >::
171
    test_simple_var_real_neg;
172
    "simple test for variable (real, 2500)"  >::
173
    test_simple_var_real_wo_dec;
174
  ]
175

    
176
let _ =
177
  run_test_tt_main var_suite