Skip to content

Commit df41332

Browse files
authored
Merge pull request #419 from ocaml-multicore/semaphore-expand
Expand STM Semaphore tests
2 parents 2450280 + 1311e74 commit df41332

File tree

3 files changed

+95
-5
lines changed

3 files changed

+95
-5
lines changed

src/semaphore/dune

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
;; Tests of Semaphore.Counting
2+
(test
3+
(name stm_tests_counting)
4+
(modules stm_tests_counting)
5+
(package multicoretests)
6+
(libraries qcheck-stm.sequential qcheck-stm.domain)
7+
(action (run %{test} --verbose))
8+
)
29

10+
;; Tests of Semaphore.Binary
311
(test
4-
(name stm_tests)
5-
(modules stm_tests)
12+
(name stm_tests_binary)
13+
(modules stm_tests_binary)
614
(package multicoretests)
715
(libraries qcheck-stm.sequential qcheck-stm.domain)
816
(action (run %{test} --verbose))

src/semaphore/stm_tests_binary.ml

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
open QCheck
2+
open STM
3+
4+
(** parallel STM tests of Semaphore.Binary *)
5+
6+
(* Semaphore API these tests will exercise
7+
8+
val make : bool -> t
9+
val release : t -> unit
10+
val acquire : t -> unit
11+
val try_acquire : t -> bool
12+
13+
*)
14+
15+
module SBConf =
16+
struct
17+
type sut = Semaphore.Binary.t
18+
type state = int
19+
20+
type cmd =
21+
| Release
22+
| Acquire
23+
| TryAcquire
24+
25+
let pp_cmd _ fmt x =
26+
let open Util.Pp in
27+
match x with
28+
| Release -> cst0 "Release" fmt
29+
| Acquire -> cst0 "Acquire" fmt
30+
| TryAcquire -> cst0 "TryAcquire" fmt
31+
32+
let show_cmd = Util.Pp.to_show pp_cmd
33+
34+
let init_state = 1
35+
let init_sut () = Semaphore.Binary.make true
36+
let cleanup _ = ()
37+
38+
let arb_cmd s =
39+
let cmds = [ Release; TryAcquire; ] in
40+
let cmds = if s = 1 then Acquire :: cmds else cmds in
41+
QCheck.make ~print:show_cmd (Gen.oneofl cmds)
42+
43+
let next_state c s = match c with
44+
| Release -> 1
45+
| Acquire -> 0
46+
| TryAcquire -> if s = 1 then 0 else 0
47+
48+
let run c sem =
49+
match c with
50+
| Release -> Res (unit, Semaphore.Binary.release sem)
51+
| Acquire -> Res (unit, Semaphore.Binary.acquire sem)
52+
| TryAcquire -> Res (bool, Semaphore.Binary.try_acquire sem)
53+
54+
let precond c s =
55+
match c with
56+
| Acquire -> s = 1
57+
| _ -> true
58+
let postcond c s res =
59+
match c,res with
60+
| Release, Res ((Unit,_), _)
61+
| Acquire, Res ((Unit,_), _) -> true
62+
| TryAcquire, Res ((Bool,_),r) -> r = (s = 1)
63+
| _ -> false
64+
end
65+
66+
module SBTest_seq = STM_sequential.Make(SBConf)
67+
module SBTest_dom = STM_domain.Make(SBConf)
68+
69+
let _ =
70+
QCheck_base_runner.run_tests_main
71+
(let count = 500 in
72+
[SBTest_seq.agree_test ~count ~name:"STM Semaphore.Binary test sequential";
73+
SBTest_dom.agree_test_par ~count ~name:"STM Semaphore.Binary test parallel";
74+
])

src/semaphore/stm_tests.ml renamed to src/semaphore/stm_tests_counting.ml

+11-3
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,19 @@ module SCConf =
7373
end
7474

7575
module SCTest_seq = STM_sequential.Make(SCConf)
76-
module SCTest_dom = STM_domain.Make(SCConf)
76+
module SCTest_dom_gv = STM_domain.Make(SCConf)
77+
module SCTest_dom = STM_domain.Make(struct
78+
include SCConf
79+
let arb_cmd s =
80+
let cmds = [ Release; TryAcquire; ] in (* No GetValue *)
81+
let cmds = if s > 0 then Acquire :: cmds else cmds in
82+
QCheck.make ~print:show_cmd (Gen.oneofl cmds)
83+
end)
7784

7885
let _ =
7986
QCheck_base_runner.run_tests_main
8087
(let count = 200 in
81-
[SCTest_seq.agree_test ~count ~name:"STM Semaphore.Counting test sequential";
82-
SCTest_dom.agree_test_par ~count ~name:"STM Semaphore.Counting test parallel";
88+
[SCTest_seq.agree_test ~count ~name:"STM Semaphore.Counting test sequential";
89+
SCTest_dom_gv.agree_test_par ~count ~name:"STM Semaphore.Counting test parallel (w/ get_value)";
90+
SCTest_dom.agree_test_par ~count ~name:"STM Semaphore.Counting test parallel (w/o get_value)";
8391
])

0 commit comments

Comments
 (0)