Skip to content

Commit eda5722

Browse files
committed
Add QCheck-STM test for Accumulator
1 parent 456ed7c commit eda5722

File tree

3 files changed

+63
-10
lines changed

3 files changed

+63
-10
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
open QCheck
2+
open STM
3+
open Kcas_data
4+
5+
module Spec = struct
6+
type cmd = Incr | Decr | Get | Set of int
7+
8+
let show_cmd = function
9+
| Incr -> "Incr"
10+
| Decr -> "Decr"
11+
| Get -> "Get"
12+
| Set v -> "Set " ^ string_of_int v
13+
14+
type state = int
15+
type sut = Accumulator.t
16+
17+
let arb_cmd _s =
18+
[
19+
Gen.return Incr;
20+
Gen.return Decr;
21+
Gen.return Get;
22+
Gen.map (fun i -> Set i) Gen.nat;
23+
]
24+
|> Gen.oneof |> make ~print:show_cmd
25+
26+
let init_state = 0
27+
let init_sut () = Accumulator.make 0
28+
let cleanup _ = ()
29+
30+
let next_state c s =
31+
match c with Incr -> s + 1 | Decr -> s - 1 | Get -> s | Set v -> v
32+
33+
let precond _ _ = true
34+
35+
let run c d =
36+
match c with
37+
| Incr -> Res (unit, Accumulator.incr d)
38+
| Decr -> Res (unit, Accumulator.decr d)
39+
| Get -> Res (int, Accumulator.get d)
40+
| Set v -> Res (unit, Accumulator.set d v)
41+
42+
let postcond c (s : state) res =
43+
match (c, res) with
44+
| Incr, Res ((Unit, _), ()) -> true
45+
| Decr, Res ((Unit, _), ()) -> true
46+
| Set _, Res ((Unit, _), ()) -> true
47+
| Get, Res ((Int, _), res) -> res = s
48+
| _, _ -> false
49+
end
50+
51+
let () =
52+
Stm_run.run ~count:1000 ~verbose:true ~name:"Accumulator" (module Spec)
53+
|> exit

test/kcas_data/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
(tests
1313
(names
14+
accumulator_test_stm
1415
dllist_test
1516
hashtbl_test
1617
lru_cache_example

test/kcas_data/stack_test_stm.ml

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
open QCheck
2+
open STM
13
open Kcas_data
24

35
module Spec = struct
@@ -13,14 +15,13 @@ module Spec = struct
1315
type sut = int Stack.t
1416

1517
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)
18+
[
19+
Gen.int |> Gen.map (fun x -> Push x);
20+
Gen.return Pop_opt;
21+
Gen.return Top_opt;
22+
Gen.return Length;
23+
]
24+
|> Gen.oneof |> make ~print:show_cmd
2425

2526
let init_state = []
2627
let init_sut () = Stack.create ()
@@ -36,15 +37,13 @@ module Spec = struct
3637
let precond _ _ = true
3738

3839
let run c d =
39-
let open STM in
4040
match c with
4141
| Push x -> Res (unit, Stack.push x d)
4242
| Pop_opt -> Res (option int, Stack.pop_opt d)
4343
| Top_opt -> Res (option int, Stack.top_opt d)
4444
| Length -> Res (int, Stack.length d)
4545

4646
let postcond c (s : state) res =
47-
let open STM in
4847
match (c, res) with
4948
| Push _x, Res ((Unit, _), ()) -> true
5049
| Pop_opt, Res ((Option Int, _), res) -> (

0 commit comments

Comments
 (0)