Skip to content

Commit 0edb18b

Browse files
authored
Merge pull request #558 from ocaml-multicore/weak-re-revision
STM Weak test re-revision
2 parents 1d21daf + d1ae3f9 commit 0edb18b

2 files changed

Lines changed: 52 additions & 66 deletions

File tree

src/weak/stm_tests_hashset_spec.ml

Lines changed: 27 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ type cmd =
1616
| Find_all of data
1717
| Count
1818
| Stats
19-
and data = int64
19+
and data = string
2020

2121
let pp_cmd par fmt x =
2222
let open Util.Pp in
23-
let pp_data = pp_int64 in
23+
let pp_data = pp_string in
2424
match x with
2525
| Clear -> cst0 "Clear" fmt
2626
| Merge x -> cst1 pp_data "Merge" par fmt x
@@ -37,19 +37,10 @@ let show_cmd = Util.Pp.to_show pp_cmd
3737

3838
type state = data list
3939

40-
module Int64 =
41-
struct
42-
[@@@warning "-unused-value-declaration"]
43-
(* support Int64.hash added in 5.1, without triggering an 'unused hash' error *)
44-
external seeded_hash_param :
45-
int -> int -> int -> 'a -> int = "caml_hash" [@@noalloc]
46-
let hash x = seeded_hash_param 10 100 0 x
47-
include Stdlib.Int64
48-
end
49-
module WHS = Weak.Make(Int64)
40+
module WHS = Weak.Make(String)
5041
type sut = WHS.t
5142

52-
let shrink_data d = Shrink.int64 d
43+
let shrink_data d = Shrink.string ~shrink:Shrink.nil d
5344

5445
let _shrink_cmd c = match c with
5546
| Clear -> Iter.empty
@@ -64,14 +55,15 @@ let _shrink_cmd c = match c with
6455
| Stats -> Iter.empty
6556

6657
let arb_cmd s =
58+
let string_gen = Gen.(string_small_of printable) in
6759
let data_gen = match s with
68-
| [] -> Gen.(map Int64.of_int small_int)
69-
| _::_ -> Gen.(oneof [oneofl s; map Int64.of_int small_int]) in
60+
| [] -> string_gen
61+
| _::_ -> Gen.(oneof [oneofl s; string_gen]) in
7062
QCheck.make ~print:show_cmd (*~shrink:shrink_cmd*)
7163
Gen.(frequency
7264
[ 1,return Clear;
73-
1,map (fun d -> Merge d) data_gen;
74-
1,map (fun d -> Add d) data_gen;
65+
2,map (fun d -> Merge d) data_gen;
66+
2,map (fun d -> Add d) data_gen;
7567
1,map (fun d -> Remove d) data_gen;
7668
1,map (fun d -> Find d) data_gen;
7769
1,map (fun d -> Find_opt d) data_gen;
@@ -104,8 +96,7 @@ let weak_size = 16
10496
let init_sut () = Gc.minor (); WHS.create weak_size
10597
let cleanup _ = ()
10698

107-
let precond c _s = match c with
108-
| _ -> true
99+
let precond _c _s = true
109100

110101
type _ ty += Tup6 : 'a ty * 'b ty * 'c ty * 'd ty * 'e ty * 'f ty -> ('a * 'b * 'c * 'd * 'e * 'f) ty
111102

@@ -121,39 +112,35 @@ let tup6 spec_a spec_b spec_c spec_d spec_e spec_f =
121112

122113
let run c hs = match c with
123114
| Clear -> Res (unit, WHS.clear hs)
124-
| Merge d -> Res (result int64 exn, protect (WHS.merge hs) d)
125-
| Add d -> Res (result unit exn, protect (WHS.add hs) d)
126-
| Remove d -> Res (result unit exn, protect (WHS.remove hs) d)
127-
| Find d -> Res (result int64 exn, protect (WHS.find hs) d)
128-
| Find_opt d -> Res (result (option int64) exn, protect (WHS.find_opt hs) d)
129-
| Find_all d -> Res (result (list int64) exn, protect (WHS.find_all hs) d)
130-
| Mem d -> Res (result bool exn, protect (WHS.mem hs) d)
115+
| Merge d -> Res (result string exn, protect (fun () -> WHS.merge hs d) ())
116+
| Add d -> Res (result unit exn, protect (fun () -> WHS.add hs d) ())
117+
| Remove d -> Res (result unit exn, protect (fun () -> WHS.remove hs d) ())
118+
| Find d -> Res (result string exn, protect (fun () -> WHS.find hs d) ())
119+
| Find_opt d -> Res (result (option string) exn, protect (fun () -> WHS.find_opt hs d) ())
120+
| Find_all d -> Res (result (list string) exn, protect (fun () -> WHS.find_all hs d) ())
121+
| Mem d -> Res (result bool exn, protect (fun () -> WHS.mem hs d) ())
131122
| Count -> Res (int, WHS.count hs)
132123
| Stats -> Res (tup6 int int int int int int, WHS.stats hs)
133124

134125
let postcond c (s:data list) res = match c, res with
135126
| Clear, Res ((Unit,_),()) -> true
136-
| Merge d, Res ((Result (Int64,Exn),_),r) ->
127+
| Merge d, Res ((Result (String,Exn),_),r) ->
137128
(match r with
138-
| Error e -> e = Invalid_argument "index out of bounds"
129+
| Error _ -> false
139130
| Ok r -> if List.mem d s then r = d else r == d)
140131
| Add _, Res ((Result (Unit,Exn),_),r) ->
141-
r = Error (Invalid_argument "index out of bounds") || r = Ok ()
132+
r = Ok ()
142133
| Remove _, Res ((Result (Unit,Exn),_),r) ->
143-
r = Error (Invalid_argument "index out of bounds") || r = Ok ()
144-
| Find d, Res ((Result (Int64,Exn),_),r) ->
145-
r = Error (Invalid_argument "index out of bounds") ||
146-
r = Error Not_found ||
147-
(List.mem d s && r = Ok d)
148-
| Find_opt d, Res ((Result (Option Int64,Exn),_),r) ->
149-
r = Error (Invalid_argument "index out of bounds") ||
150-
r = Ok None || r = Ok (Some d)
151-
| Find_all d, Res ((Result (List Int64,Exn),_),r) ->
134+
r = Ok ()
135+
| Find d, Res ((Result (String,Exn),_),r) ->
136+
r = Error Not_found || (List.mem d s && r = Ok d)
137+
| Find_opt d, Res ((Result (Option String,Exn),_),r) ->
138+
r = Ok None || (List.mem d s && r = Ok (Some d))
139+
| Find_all d, Res ((Result (List String,Exn),_),r) ->
152140
(match r with
153-
| Error e -> e = Invalid_argument "index out of bounds"
141+
| Error _ -> false
154142
| Ok r -> List.for_all (fun d' -> d' = d) r)
155143
| Mem d, Res ((Result (Bool,Exn),_),r) ->
156-
r = Error (Invalid_argument "index out of bounds") ||
157144
r = Ok (List.mem d s) || r = Ok false
158145
| Count, Res ((Int,_),r) ->
159146
r <= List.length s

src/weak/stm_tests_weak_spec.ml

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,18 @@ type cmd =
1010
| Get_copy of int
1111
| Check of int
1212
| Fill of int * int * data option
13-
and data = int64
13+
and data = string
1414

1515
let pp_cmd par fmt x =
1616
let open Util.Pp in
1717
match x with
1818
| Length -> cst0 "Length" fmt
19-
| Set (x, y) -> cst2 pp_int (pp_option pp_int64) "Set" par fmt x y
19+
| Set (x, y) -> cst2 pp_int (pp_option pp_string) "Set" par fmt x y
2020
| Get x -> cst1 pp_int "Get" par fmt x
2121
| Get_copy x -> cst1 pp_int "Get_copy" par fmt x
2222
| Check x -> cst1 pp_int "Check" par fmt x
2323
| Fill (x, y, z) ->
24-
cst3 pp_int pp_int (pp_option pp_int64) "Fill" par fmt x y z
24+
cst3 pp_int pp_int (pp_option pp_string) "Fill" par fmt x y z
2525

2626
let show_cmd = Util.Pp.to_show pp_cmd
2727

@@ -41,66 +41,65 @@ let _shrink_cmd c = match c with
4141

4242
let arb_cmd s =
4343
let int_gen = Gen.(oneof [small_nat; int_bound (List.length s - 1)]) in
44-
let int64_gen = Gen.(map Int64.of_int small_int) in
44+
let data_gen = Gen.(string_small_of printable) in
4545
QCheck.make ~print:show_cmd (*~shrink:shrink_cmd*)
4646
Gen.(frequency
4747
[ 1,return Length;
48-
1,map2 (fun i c -> Set (i,c)) int_gen (option int64_gen);
48+
1,map2 (fun i d_opt -> Set (i,d_opt)) int_gen (option data_gen);
4949
2,map (fun i -> Get i) int_gen;
5050
2,map (fun i -> Get_copy i) int_gen;
5151
2,map (fun i -> Check i) int_gen;
52-
1,map3 (fun i len c -> Fill (i,len,c)) int_gen int_gen (option int64_gen); (* hack: reusing int_gen for length *)
52+
2,map3 (fun i len d_opt -> Fill (i,len,d_opt)) int_gen int_gen (option data_gen); (* hack: reusing int_gen for length *)
5353
])
5454

55-
let weak_size = 16
55+
let weak_size = 10
5656

5757
let init_state = List.init weak_size (fun _ -> None)
5858

5959
let next_state c s = match c with
6060
| Length -> s
61-
| Set (i,c) ->
62-
List.mapi (fun j c' -> if i=j then c else c') s
63-
| Get _ -> s
64-
| Get_copy _ -> s
61+
| Set (i,d_opt) ->
62+
List.mapi (fun j d_opt' -> if i=j then d_opt else d_opt') s
63+
| Get _ -> s
64+
| Get_copy _ -> s
6565
| Check _ -> s
66-
| Fill (i,l,c) ->
66+
| Fill (i,l,d_opt) ->
6767
if i >= 0 && l >= 0 && i+l-1 < List.length s
6868
then
69-
List.mapi (fun j c' -> if i <= j && j <= i+l-1 then c else c') s
69+
List.mapi (fun j d_opt' -> if i <= j && j <= i+l-1 then d_opt else d_opt') s
7070
else s
7171

7272
let init_sut () = Gc.minor (); Weak.create weak_size
7373
let cleanup _ = ()
7474

75-
let precond c _s = match c with
76-
| _ -> true
75+
let precond _c _s = true
7776

7877
let run c a = match c with
7978
| Length -> Res (int, Weak.length a)
80-
| Set (i,c) -> Res (result unit exn, protect (Weak.set a i) c)
81-
| Get i -> Res (result (option int64) exn, protect (Weak.get a) i)
82-
| Get_copy i -> Res (result (option int64) exn, protect (Weak.get_copy a) i)
83-
| Check i -> Res (result bool exn, protect (Weak.check a) i)
84-
| Fill (i,l,c) -> Res (result unit exn, protect (Weak.fill a i l) c)
79+
| Set (i,d_opt) -> Res (result unit exn, protect (fun () -> Weak.set a i d_opt) ())
80+
| Get i -> Res (result (option string) exn, protect (fun () -> Weak.get a i) ())
81+
| Get_copy i -> Res (result (option string) exn, protect (fun () -> Weak.get_copy a i) ())
82+
| Check i -> Res (result bool exn, protect (fun () -> Weak.check a i) ())
83+
| Fill (i,l,d_opt) -> Res (result unit exn, protect (fun () -> Weak.fill a i l d_opt) ())
8584

86-
let postcond c (s:int64 option list) res = match c, res with
85+
let postcond c (s:state) res = match c, res with
8786
| Length, Res ((Int,_),i) -> i = List.length s
8887
| Set (i,_), Res ((Result (Unit,Exn),_), r) ->
8988
if i < 0 || i >= List.length s
9089
then r = Error (Invalid_argument "Weak.set")
9190
else r = Ok ()
92-
| Get i, Res ((Result (Option Int64,Exn),_), r) ->
91+
| Get i, Res ((Result (Option String,Exn),_), r) ->
9392
if i < 0 || i >= List.length s
9493
then r = Error (Invalid_argument "Weak.get")
95-
else r = Ok (List.nth s i)
96-
| Get_copy i, Res ((Result (Option Int64,Exn),_), r) ->
94+
else r = Ok None || r = Ok (List.nth s i)
95+
| Get_copy i, Res ((Result (Option String,Exn),_), r) ->
9796
if i < 0 || i >= List.length s
9897
then r = Error (Invalid_argument "Weak.get_copy")
99-
else r = Ok (List.nth s i)
98+
else r = Ok None || r = Ok (List.nth s i)
10099
| Check i, Res ((Result (Bool,Exn),_),r) ->
101100
if i < 0 || i >= List.length s
102101
then r = Error (Invalid_argument "Weak.check")
103-
else r = Ok (None <> List.nth s i)
102+
else r = Ok false || r = Ok (None <> List.nth s i)
104103
| Fill (i,l,_), Res ((Result (Unit,Exn),_), r) ->
105104
if i < 0 || l < 0 || i+l > List.length s
106105
then r = Error (Invalid_argument "Weak.fill")

0 commit comments

Comments
 (0)