Skip to content

Commit 456ed7c

Browse files
committed
Add a QCheck-STM test for Stack
1 parent a62e823 commit 456ed7c

File tree

8 files changed

+112
-1
lines changed

8 files changed

+112
-1
lines changed

dune-project

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@
8282
(and
8383
(>= 1.7.0)
8484
:with-test))
85+
(qcheck-core
86+
(and
87+
(>= 0.21.2)
88+
:with-test))
89+
(qcheck-stm
90+
(and
91+
(>= 0.3)
92+
:with-test))
8593
(mdx
8694
(and
8795
(>= 2.3.0)

kcas_data.opam

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ depends: [
2020
"domain_shims" {>= "0.1.0" & with-test}
2121
"mtime" {>= "2.0.0" & with-test}
2222
"alcotest" {>= "1.7.0" & with-test}
23+
"qcheck-core" {>= "0.21.2" & with-test}
24+
"qcheck-stm" {>= "0.3" & with-test}
2325
"mdx" {>= "2.3.0" & with-test}
2426
"yojson" {>= "2.1.0" & with-test}
2527
"odoc" {>= "2.2.0" & with-doc}

test/kcas_data/dune

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
(rule
2+
(enabled_if %{lib-available:qcheck-stm.domain})
3+
(action
4+
(copy stm_run.ocaml5.ml stm_run.ml)))
5+
6+
(rule
7+
(enabled_if
8+
(not %{lib-available:qcheck-stm.domain}))
9+
(action
10+
(copy stm_run.ocaml4.ml stm_run.ml)))
11+
112
(tests
213
(names
314
dllist_test
@@ -6,6 +17,21 @@
617
mvar_test
718
queue_test
819
stack_test
20+
stack_test_stm
921
xt_test)
10-
(libraries alcotest kcas kcas_data domain_shims)
22+
(libraries
23+
alcotest
24+
kcas
25+
kcas_data
26+
domain_shims
27+
qcheck-core
28+
qcheck-core.runner
29+
qcheck-stm.stm
30+
qcheck-stm.sequential
31+
qcheck-stm.thread
32+
(select
33+
empty.ml
34+
from
35+
(qcheck-stm.domain -> empty.ocaml5.ml)
36+
(-> empty.ocaml4.ml)))
1137
(package kcas_data))

test/kcas_data/empty.ocaml4.ml

Whitespace-only changes.

test/kcas_data/empty.ocaml5.ml

Whitespace-only changes.

test/kcas_data/stack_test_stm.ml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
open Kcas_data
2+
3+
module Spec = struct
4+
type cmd = Push of int | Pop_opt | Top_opt | Length
5+
6+
let show_cmd = function
7+
| Push x -> "Push " ^ string_of_int x
8+
| Pop_opt -> "Pop_opt"
9+
| Top_opt -> "Top_opt"
10+
| Length -> "Length"
11+
12+
type state = int list
13+
type sut = int Stack.t
14+
15+
let arb_cmd _s =
16+
QCheck.(
17+
[
18+
Gen.int |> Gen.map (fun x -> Push x);
19+
Gen.return Pop_opt;
20+
Gen.return Top_opt;
21+
Gen.return Length;
22+
]
23+
|> Gen.oneof |> make ~print:show_cmd)
24+
25+
let init_state = []
26+
let init_sut () = Stack.create ()
27+
let cleanup _ = ()
28+
29+
let next_state c s =
30+
match c with
31+
| Push x -> x :: s
32+
| Pop_opt -> ( match s with [] -> [] | _ :: s -> s)
33+
| Top_opt -> s
34+
| Length -> s
35+
36+
let precond _ _ = true
37+
38+
let run c d =
39+
let open STM in
40+
match c with
41+
| Push x -> Res (unit, Stack.push x d)
42+
| Pop_opt -> Res (option int, Stack.pop_opt d)
43+
| Top_opt -> Res (option int, Stack.top_opt d)
44+
| Length -> Res (int, Stack.length d)
45+
46+
let postcond c (s : state) res =
47+
let open STM in
48+
match (c, res) with
49+
| Push _x, Res ((Unit, _), ()) -> true
50+
| Pop_opt, Res ((Option Int, _), res) -> (
51+
res = match s with [] -> None | x :: _ -> Some x)
52+
| Top_opt, Res ((Option Int, _), res) -> (
53+
res = match s with [] -> None | x :: _ -> Some x)
54+
| Length, Res ((Int, _), res) -> res = List.length s
55+
| _, _ -> false
56+
end
57+
58+
let () =
59+
Stm_run.run ~count:1000 ~verbose:true ~name:"Stack" (module Spec) |> exit

test/kcas_data/stm_run.ocaml4.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
let run ~verbose ~count ~name (module Spec : STM.Spec) =
2+
let module Seq = STM_sequential.Make (Spec) in
3+
let module Con = STM_thread.Make (Spec) [@alert "-experimental"] in
4+
QCheck_base_runner.run_tests ~verbose
5+
[
6+
Seq.agree_test ~count ~name:(name ^ " sequential");
7+
Con.agree_test_conc ~count ~name:(name ^ " concurrent");
8+
]

test/kcas_data/stm_run.ocaml5.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
let run ~verbose ~count ~name (module Spec : STM.Spec) =
2+
let module Seq = STM_sequential.Make (Spec) in
3+
let module Dom = STM_domain.Make (Spec) in
4+
QCheck_base_runner.run_tests ~verbose
5+
[
6+
Seq.agree_test ~count ~name:(name ^ " sequential");
7+
Dom.agree_test_par ~count ~name:(name ^ " parallel");
8+
]

0 commit comments

Comments
 (0)