-
Notifications
You must be signed in to change notification settings - Fork 2
/
qctest.ml
183 lines (159 loc) · 5.4 KB
/
qctest.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
open QCheck
let test_count = 10_000
(* A data type for symbolically representing calls to the Ptset API *)
type instr_tree =
| Empty
| Singleton of int
| Add of int * instr_tree
| Remove of int * instr_tree
| Union of instr_tree * instr_tree
| Inter of instr_tree * instr_tree
(* to_string : instr_tree -> string *)
let rec to_string a =
(*let int_to_string n = if n<0 then "(" ^ string_of_int n ^ ")" else string_of_int n in*)
let int_to_string = string_of_int in
match a with
| Empty -> "Empty"
| Singleton n -> "Singleton " ^ (int_to_string n)
| Add (n,t) -> "Add (" ^ (int_to_string n) ^ ", " ^ (to_string t) ^ ")"
| Remove (n,t) -> "Remove (" ^ (int_to_string n) ^ ", " ^ (to_string t) ^ ")"
| Union (t,t') -> "Union (" ^ (to_string t) ^ ", " ^ (to_string t') ^ ")"
| Inter (t,t') -> "Inter (" ^ (to_string t) ^ ", " ^ (to_string t') ^ ")"
(* interpreting calls over the Ptset module *)
let rec interpret t = match t with
| Empty -> Ptset.empty
| Singleton n -> Ptset.singleton n
| Add (n,t) -> Ptset.add n (interpret t)
| Remove (n,t) -> Ptset.remove n (interpret t)
| Union (t,t') ->
let s = interpret t in
let s' = interpret t' in
Ptset.union s s'
| Inter (t,t') ->
let s = interpret t in
let s' = interpret t' in
Ptset.inter s s'
(** A recursive (size-bounded) generator of trees *)
(* tree_gen : int Gen.t -> instr_tree Gen.t *)
let tree_gen int_gen =
Gen.sized
(Gen.fix (fun recgen n -> match n with
| 0 -> Gen.oneof [Gen.return Empty;
Gen.map (fun i -> Singleton i) int_gen]
| _ ->
Gen.frequency
[ (1, Gen.return Empty);
(1, Gen.map (fun i -> Singleton i) int_gen);
(2, Gen.map2 (fun i t -> Add (i,t)) int_gen (recgen (n-1)));
(2, Gen.map2 (fun i t -> Remove (i,t)) int_gen (recgen (n-1)));
(2, Gen.map2 (fun l r -> Union (l,r)) (recgen (n/2)) (recgen (n/2)));
(2, Gen.map2 (fun l r -> Inter (l,r)) (recgen (n/2)) (recgen (n/2)));
]))
let (<+>) = Iter.(<+>)
(* tshrink : instr_tree -> instr_tree Iter.t *)
let rec tshrink t = match t with
| Empty -> Iter.empty
| Singleton i ->
(Iter.return Empty)
<+> (Iter.map (fun i' -> Singleton i') (Shrink.int i))
| Add (i,t) ->
(Iter.of_list [Empty; t; Singleton i])
<+> (Iter.map (fun t' -> Add (i,t')) (tshrink t))
<+> (Iter.map (fun i' -> Add (i',t)) (Shrink.int i))
| Remove (i,t) ->
(Iter.of_list [Empty; t])
<+> (Iter.map (fun t' -> Remove (i,t')) (tshrink t))
<+> (Iter.map (fun i' -> Remove (i',t)) (Shrink.int i))
| Union (t0,t1) ->
(Iter.of_list [Empty;t0;t1])
<+> (Iter.map (fun t0' -> Union (t0',t1)) (tshrink t0))
<+> (Iter.map (fun t1' -> Union (t0,t1')) (tshrink t1))
| Inter (t0,t1) ->
(Iter.of_list [Empty;t0;t1])
<+> (Iter.map (fun t0' -> Inter (t0',t1)) (tshrink t0))
<+> (Iter.map (fun t1' -> Inter (t0,t1')) (tshrink t1))
(* An integer generator *)
let arb_int =
frequency
[(5,small_signed_int);
(3,int);
(1, oneofl [min_int;max_int])]
(* int *)
(* small_signed_int *)
(* arb_tree : instr_tree arbitrary *)
let arb_tree =
make ~print:to_string ~shrink:tshrink
(tree_gen arb_int.gen)
(** The model (identifiers are suffixed with _m) *)
let empty_m = []
let singleton_m i = [i]
let mem_m i s = List.mem i s
let add_m i s = if List.mem i s then s else List.sort compare (i::s)
let rec remove_m i s = match s with
| [] -> []
| j::s' -> if i=j then s' else j::(remove_m i s')
let rec union_m s s' = match s,s' with
| [], _ -> s'
| _, [] -> s
| i::is,j::js -> if i<j then i::(union_m is s') else
if i>j then j::(union_m s js) else
i::(union_m is js)
let rec inter_m s s' = match s with
| [] -> []
| e::s -> if List.mem e s' then e::(inter_m s s') else inter_m s s'
(*let abstract s = Ptset.elements s*)
let abstract s = List.sort compare (Ptset.fold (fun i a -> i::a) s [])
(** A bunch of agreement properties *)
let test_empty =
Test.make ~name:"empty" ~count:1
unit
(fun () ->
let s = Ptset.empty in
abstract s = empty_m)
let singleton_test =
Test.make ~name:"singleton test" ~count:test_count
arb_int
(fun n ->
abstract (Ptset.singleton n) = singleton_m n)
let mem_test =
Test.make ~name:"mem test" ~count:test_count
(pair arb_tree arb_int)
(fun (t,n) ->
let s = interpret t in
Ptset.mem n s = mem_m n (abstract s))
let add_test =
Test.make ~name:"add test" ~count:test_count
(pair arb_tree arb_int)
(fun (t,n) ->
let s = interpret t in
abstract (Ptset.add n s) = add_m n (abstract s))
let remove_test =
Test.make ~name:"remove test" ~count:test_count
(pair arb_tree arb_int)
(fun (t,n) ->
let s = interpret t in
abstract (Ptset.remove n s) = remove_m n (abstract s))
let union_test =
Test.make ~name:"union test" ~count:test_count
(pair arb_tree arb_tree)
(fun (t,t') ->
let s = interpret t in
let s' = interpret t' in
abstract (Ptset.union s s') = union_m (abstract s) (abstract s'))
let inter_test =
Test.make ~name:"inter test" ~count:test_count
(pair arb_tree arb_tree)
(fun (t,t') ->
let s = interpret t in
let s' = interpret t' in
abstract (Ptset.inter s s') = inter_m (abstract s) (abstract s'))
;;
QCheck_runner.run_tests(*_main*) ~verbose:true
[ test_empty;
singleton_test;
mem_test;
add_test;
remove_test;
union_test;
inter_test;
]