|
| 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 | + ]) |
0 commit comments