Project

General

Profile

Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.67 KB)

1 550e6a83 Christophe Garion
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 test_var_skeleton var id var_type value =
20
  begin
21
    assert_bool
22
      "orig for user variables should be true"
23
      var.var_orig;
24
    assert_bool
25
      "user variables are considered as constants"
26
      var.var_dec_const;
27
    assert_equal
28
      ~msg:("problem with variable " ^ var.var_id ^ " clock type")
29 89ac5599 Christophe Garion
      Ckdec_any
30
      var.var_dec_clock.ck_dec_desc;
31 550e6a83 Christophe Garion
    assert_equal
32
      ~msg:("problem with variable " ^ var.var_id ^ " ident")
33 89ac5599 Christophe Garion
      id
34
      var.var_id;
35 550e6a83 Christophe Garion
    assert_equal
36
      ~msg:("problem with variable " ^ var.var_id ^ " type")
37 89ac5599 Christophe Garion
      var_type
38
      var.var_dec_type.ty_dec_desc;
39 550e6a83 Christophe Garion
    match var.var_dec_value with
40
    | Some { expr_desc = d } ->
41
      assert_equal
42
        ~msg:("problem with variable " ^ var.var_id ^ " value")
43
        value
44 89ac5599 Christophe Garion
        d
45 550e6a83 Christophe Garion
    | _       -> raise (OUnitTest.OUnit_failure
46
                          "User variables should have an initial value")
47
  end
48
49 f5c1e4c1 Christophe Garion
let test_simple_var_bool_false tests_ctxt =
50 550e6a83 Christophe Garion
  let prog = Parse.parse_prog
51 f5c1e4c1 Christophe Garion
      (Yojson.Basic.from_file "../data-test/simple-var-bool-false.json") in
52 550e6a83 Christophe Garion
  match prog with
53 0291fcfd Christophe Garion
  | Program ("simple_var_bool_false", [ ], [ x ]) ->
54 2b95979d Christophe Garion
    test_var_skeleton x "my_bool_var_false"
55
      Tydec_bool (Expr_const (Const_tag tag_false))
56 550e6a83 Christophe Garion
  | _ -> raise (OUnitTest.OUnit_failure
57 f5c1e4c1 Christophe Garion
                  "Program obtained from simple-var-bool-false.json is not correct")
58 550e6a83 Christophe Garion
59 0291fcfd Christophe Garion
let test_simple_var_bool_true tests_ctxt =
60
  let prog = Parse.parse_prog
61
      (Yojson.Basic.from_file "../data-test/simple-var-bool-true.json") in
62
  match prog with
63
  | Program ("simple_var_bool_true", [ ], [ x ]) ->
64 2b95979d Christophe Garion
    test_var_skeleton x "my_bool_var_true"
65
      Tydec_bool (Expr_const (Const_tag tag_true))
66 0291fcfd Christophe Garion
  | _ -> raise (OUnitTest.OUnit_failure
67
                  "Program obtained from simple-var-bool-true.json is not correct")
68
69 5c0d6e4e Christophe Garion
let test_simple_var_int_zero tests_ctxt =
70
  let prog = Parse.parse_prog
71
      (Yojson.Basic.from_file "../data-test/simple-var-int-zero.json") in
72
  match prog with
73
  | Program ("simple_var_int_zero", [ ], [ x ]) ->
74
    test_var_skeleton x "my_int_var_zero"
75
      Tydec_int (Expr_const (Const_int 0))
76
  | _ -> raise (OUnitTest.OUnit_failure
77
                  "Program obtained from simple-var-int-zero.json is not correct")
78
79
let test_simple_var_int_pos tests_ctxt =
80
  let prog = Parse.parse_prog
81
      (Yojson.Basic.from_file "../data-test/simple-var-int-pos.json") in
82
  match prog with
83
  | Program ("simple_var_int_pos", [ ], [ x ]) ->
84
    test_var_skeleton x "my_int_var_pos"
85
      Tydec_int (Expr_const (Const_int 2))
86
  | _ -> raise (OUnitTest.OUnit_failure
87
                  "Program obtained from simple-var-int-pos.json is not correct")
88
89
let test_simple_var_int_neg tests_ctxt =
90
  let prog = Parse.parse_prog
91
      (Yojson.Basic.from_file "../data-test/simple-var-int-neg.json") in
92
  match prog with
93
  | Program ("simple_var_int_neg", [ ], [ x ]) ->
94
    test_var_skeleton x "my_int_var_neg"
95
      Tydec_int (Expr_const (Const_int (-5)))
96
  | _ -> raise (OUnitTest.OUnit_failure
97
                  "Program obtained from simple-var-int-neg.json is not correct")
98
99
let test_simple_var_real_zero tests_ctxt =
100
  let prog = Parse.parse_prog
101
      (Yojson.Basic.from_file "../data-test/simple-var-real-zero.json") in
102
  match prog with
103
  | Program ("simple_var_real_zero", [ ], [ x ]) ->
104
    test_var_skeleton x "my_real_var_zero"
105
      Tydec_real (Expr_const (Const_real (Num.num_of_int 0, 1, "0.0")))
106
  | _ -> raise (OUnitTest.OUnit_failure
107
                  "Program obtained from simple-var-real-zero.json is not correct")
108
109
let test_simple_var_real_pos tests_ctxt =
110
  let prog = Parse.parse_prog
111
      (Yojson.Basic.from_file "../data-test/simple-var-real-pos.json") in
112
  match prog with
113
  | Program ("simple_var_real_pos", [ ], [ x ]) ->
114
    test_var_skeleton x "my_real_var_pos"
115
      Tydec_real (Expr_const (Const_real (Num.num_of_int 2115, 2, "21.15")))
116
  | _ -> raise (OUnitTest.OUnit_failure
117
                  "Program obtained from simple-var-real-pos.json is not correct")
118
119
let test_simple_var_real_neg tests_ctxt =
120
  let prog = Parse.parse_prog
121
      (Yojson.Basic.from_file "../data-test/simple-var-real-neg.json") in
122
  match prog with
123
  | Program ("simple_var_real_neg", [ ], [ x ]) ->
124
    test_var_skeleton x "my_real_var_neg"
125
      Tydec_real (Expr_const (Const_real (Num.num_of_int (-224), 2, "-2.24")))
126
  | _ -> raise (OUnitTest.OUnit_failure
127
                  "Program obtained from simple-var-real-neg.json is not correct")
128
129
let test_simple_var_real_wo_dec tests_ctxt =
130 c3d8313d Christophe Garion
  assert_raises (Parse.JSON_parse_error("Invalid real constant 2500"))
131
    (fun _ -> Parse.parse_prog (Yojson.Basic.from_file
132
                                  "../data-test/simple-var-real-wo-dec.json"))
133 5c0d6e4e Christophe Garion
134 550e6a83 Christophe Garion
let var_suite =
135
  "suite for variables" >:::
136 0291fcfd Christophe Garion
  [ "simple test for variable (boolean, false)" >::
137
    test_simple_var_bool_false;
138
    "simple test for variable (boolean, true)"  >::
139 5c0d6e4e Christophe Garion
    test_simple_var_bool_true;
140
    "simple test for variable (int, 0)"  >::
141
    test_simple_var_int_zero;
142
    "simple test for variable (int, 2)"  >::
143
    test_simple_var_int_pos;
144
    "simple test for variable (int, -5)"  >::
145
    test_simple_var_int_neg;
146
    "simple test for variable (real, 0.0)"  >::
147
    test_simple_var_real_zero;
148
    "simple test for variable (real, 21.15)"  >::
149
    test_simple_var_real_pos;
150
    "simple test for variable (real, -2.24)"  >::
151
    test_simple_var_real_neg;
152
    "simple test for variable (real, 2500)"  >::
153
    test_simple_var_real_wo_dec;
154 0291fcfd Christophe Garion
  ]
155 550e6a83 Christophe Garion
156
let _ =
157
  run_test_tt_main var_suite