Skip to content

Commit 7b52f12

Browse files
tests
1 parent 02071f5 commit 7b52f12

File tree

6 files changed

+93
-41
lines changed

6 files changed

+93
-41
lines changed

dscheck.opam

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ opam-version: "2.0"
33
synopsis: "Traced Atomics"
44
maintainer: ["Sadiq Jaffer"]
55
authors: ["Sadiq Jaffer"]
6-
homepage: "https://github.com/ocaml-multicore/dscheck"
7-
bug-reports: "https://github.com/ocaml-multicore/dscheck/issues"
6+
homepage: "https://github.com/sadiqj/dscheck"
7+
bug-reports: "https://github.com/sadiqj/dscheck/issues"
88
depends: [
99
"ocaml" {>= "5.0.0"}
1010
"dune" {>= "2.9"}
1111
"containers"
1212
"oseq"
13+
"alcotest" {>= "1.6.0"}
1314
"odoc" {with-doc}
1415
]
1516
build: [
@@ -28,4 +29,4 @@ build: [
2829
]
2930
["dune" "install" "-p" name "--create-install-files" name]
3031
]
31-
dev-repo: "git+https://github.com/ocaml-multicore/dscheck.git"
32+
dev-repo: "git+https://github.com/sadiqj/dscheck.git"

dune-project

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
(package
1111
(name dscheck)
1212
(synopsis "Traced Atomics")
13-
(depends (ocaml (>= 5.0.0)) dune containers oseq))
13+
(depends (ocaml (>= 5.0.0)) dune containers oseq (alcotest (>= 1.6.0))))
14+
1415

src/tracedAtomic.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,17 @@ let check f =
267267
end;
268268
tracing := tracing_at_start
269269

270+
let reset_state () =
271+
finished_processes := 0;
272+
atomics_counter := 1;
273+
num_runs := 0;
274+
schedule_for_checks := [];
275+
CCVector.clear processes;
276+
;;
277+
270278

271279
let trace func =
280+
reset_state ();
272281
let empty_state = do_run func [(0, Start, None)] :: [] in
273282
let empty_clock = IntMap.empty in
274283
let empty_last_access = IntMap.empty in

tests/dune

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
(executables
2-
(names test_list test_naive_counter)
3-
(modules test_list test_naive_counter)
4-
(libraries dscheck))
1+
(test
2+
(name test_list)
3+
(libraries dscheck alcotest)
4+
(modules test_list))
5+
6+
(test
7+
(name test_naive_counter)
8+
(libraries dscheck alcotest)
9+
(modules test_naive_counter))

tests/test_list.ml

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,46 +2,66 @@ module Atomic = Dscheck.TracedAtomic
22

33
(* a simple concurrent list *)
44

5-
type conc_list = { value: int; next: conc_list option }
5+
type conc_list = { value : int; next : conc_list option }
66

7-
let rec add_node list_head n =
7+
let rec add_node_naive list_head n =
88
(* try to add a new node to head *)
99
let old_head = Atomic.get list_head in
10-
let new_node = { value = n ; next = (Some old_head) } in
11-
(* introduce bug *)
12-
if Atomic.get list_head = old_head then begin
13-
Atomic.set list_head new_node;
14-
true
15-
end
16-
else
17-
add_node list_head n
10+
let new_node = { value = n; next = Some old_head } in
11+
(* introduce bug *)
12+
if Atomic.get list_head = old_head then (
13+
Atomic.set list_head new_node;
14+
true)
15+
else add_node_naive list_head n
16+
17+
let rec add_node_safe list_head n =
18+
let old_head = Atomic.get list_head in
19+
let new_node = { value = n; next = Some old_head } in
20+
if Atomic.compare_and_set list_head old_head new_node then true
21+
else add_node_safe list_head n
1822

1923
let check_node list_head n =
2024
let rec check_from_node node =
2125
match (node.value, node.next) with
22-
| (v, _) when v = n -> true
23-
| (_, None) -> false
24-
| (_ , Some(next_node)) -> begin
25-
check_from_node next_node
26-
end
26+
| v, _ when v = n -> true
27+
| _, None -> false
28+
| _, Some next_node -> check_from_node next_node
2729
in
2830
(* try to find the node *)
29-
check_from_node (Atomic.get list_head)
30-
31-
let add_and_check list_head n () =
32-
assert(add_node list_head n);
33-
assert(check_node list_head n)
31+
check_from_node (Atomic.get list_head)
3432

35-
let create_test upto () =
36-
let list_head = Atomic.make { value = 0 ; next = None } in
33+
let create_test add_node_f upto () =
34+
let list_head = Atomic.make { value = 0; next = None } in
3735
for x = 1 to upto do
38-
Atomic.spawn (add_and_check list_head x);
36+
Atomic.spawn (fun () ->
37+
assert (add_node_f list_head x);
38+
assert (check_node list_head x))
3939
done;
4040
Atomic.final (fun () ->
41-
for x = 1 to upto do
42-
Atomic.check(fun () -> check_node list_head x)
43-
done
44-
)
41+
for x = 1 to upto do
42+
Atomic.check (fun () -> check_node list_head x)
43+
done)
44+
45+
let test_list_naive_single_domain () =
46+
Atomic.trace (create_test add_node_naive 1)
47+
48+
let test_list_naive domains () =
49+
match Atomic.trace (create_test add_node_naive domains) with
50+
| exception _ -> ()
51+
| _ -> failwith "expected failure"
52+
53+
let test_list_safe () = Atomic.trace (create_test add_node_safe 3)
54+
(* 4 takes over 10 Gb of RAM *)
4555

4656
let () =
47-
Atomic.trace (create_test 8)
57+
let open Alcotest in
58+
run "dscheck"
59+
[
60+
( "list",
61+
[
62+
test_case "naive-1-domain" `Quick (test_list_naive_single_domain);
63+
test_case "naive-2-domains" `Quick (test_list_naive 8);
64+
test_case "naive-8-domains" `Quick (test_list_naive 8);
65+
test_case "safe" `Quick test_list_safe;
66+
] );
67+
]

tests/test_naive_counter.ml

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,26 @@
11
module Atomic = Dscheck.TracedAtomic
22

3-
let test_counter () =
3+
let counter incr () =
44
let counter = Atomic.make 0 in
5-
let incr () = Atomic.set counter (Atomic.get counter + 1) in
6-
Atomic.spawn incr;
7-
Atomic.spawn incr;
5+
Atomic.spawn (fun () -> incr counter);
6+
Atomic.spawn (fun () -> incr counter);
87
Atomic.final (fun () -> Atomic.check (fun () -> Atomic.get counter == 2))
98

10-
let () = Atomic.trace test_counter
9+
let test_naive_counter () =
10+
let naive_incr counter = Atomic.set counter (Atomic.get counter + 1) in
11+
match Atomic.trace (counter naive_incr) with
12+
| exception _ -> ()
13+
| _ -> failwith "expected failure"
14+
15+
let test_safe_counter () = Atomic.trace (counter Atomic.incr)
16+
17+
let () =
18+
let open Alcotest in
19+
run "dscheck"
20+
[
21+
( "counter",
22+
[
23+
test_case "naive" `Quick test_naive_counter;
24+
test_case "safe" `Quick test_safe_counter;
25+
] );
26+
]

0 commit comments

Comments
 (0)