Skip to content

Commit

Permalink
address feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
bartoszmodelski committed May 31, 2023
1 parent a2e28fc commit ddae4aa
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 72 deletions.
1 change: 1 addition & 0 deletions dscheck.opam
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ depends: [
"tsort"
"oseq"
"alcotest" {>= "1.6.0" & with-test}
"cmdliner"
"odoc" {with-doc}
]
build: [
Expand Down
2 changes: 1 addition & 1 deletion dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
(package
(name dscheck)
(synopsis "Traced Atomics")
(depends (ocaml (>= 5.0.0)) dune containers tsort oseq (alcotest (and (>= 1.6.0) :with-test))))
(depends (ocaml (>= 5.0.0)) dune containers tsort oseq (alcotest (and (>= 1.6.0) :with-test)) cmdliner))


2 changes: 1 addition & 1 deletion src/tracedAtomic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ let rec explore_random func state =
let len = List.length enabled in
if len == 0 then ()
else
let random_index = Random.int (List.length enabled) in
let random_index = Random.int len in
let j = List.nth enabled random_index in
let j_proc = List.nth s.procs j in
let schedule =
Expand Down
2 changes: 1 addition & 1 deletion tests/dune
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@

(executable
(name gen_program)
(libraries dscheck)
(libraries dscheck cmdliner)
(modules gen_program))
181 changes: 112 additions & 69 deletions tests/gen_program.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,23 @@ module Atomic = Dscheck.TracedAtomic
module IntSet = Set.Make (Int)

type config = {
globals_count : int;
global_count : int;
value_limit : int;
operations_count : int;
thread_count : int;
operation_count : int;
domain_count : int;
generate_conditionals : bool;
print_tests : bool;
seed : int option;
seed : int;
}

let print_config t =
Printf.printf "CONFIG\n";
Printf.printf "globals_count: %d\n" t.globals_count;
Printf.printf "global_count: %d\n" t.global_count;
Printf.printf "value_limit: %d\n" t.value_limit;
Printf.printf "operations_count: %d\n" t.operations_count;
Printf.printf "thread_count: %d\n" t.thread_count;
Printf.printf "operations_count: %d\n" t.operation_count;
Printf.printf "domain_count: %d\n" t.domain_count;
Printf.printf "generate_conditionals: %b\n%!" t.generate_conditionals;
Printf.printf "seed: %s\n%!"
(Option.map Int.to_string t.seed |> Option.value ~default:"<random>")
Printf.printf "seed: %d\n%!" t.seed

let var_name i = Char.chr (i + 97)

Expand Down Expand Up @@ -177,7 +176,7 @@ module Step = struct
| Noop -> ()

let rec gen ~config ~fuel () =
let var_id = Random.int config.globals_count in
let var_id = Random.int config.global_count in
let next fuel =
if fuel > 1 then gen ~config ~fuel:(fuel - 1) () else Noop
in
Expand All @@ -196,10 +195,10 @@ module Step = struct
FetchAndAdd { var_id; delta; next = next fuel }
| 6 ->
let func_count =
min (max 1 fuel) (min config.globals_count (1 + Random.int 2))
min (max 1 fuel) (min config.global_count (1 + Random.int 2))
in
let var_ids =
List.init func_count (fun _ -> Random.int config.globals_count)
List.init func_count (fun _ -> Random.int config.global_count)
in
let conditional =
Conditional.gen func_count ~value_limit:config.value_limit
Expand Down Expand Up @@ -241,12 +240,10 @@ module Program = struct
end

let run_random config () =
(match config.seed with
| None -> Random.self_init ()
| Some seed -> Random.init seed);
let globals = CCVector.of_list (List.init config.globals_count Fun.id) in
let thread_f = Step.gen ~config ~fuel:config.operations_count in
let threads = List.init config.thread_count (fun _ -> thread_f ()) in
Random.init config.seed;
let globals = CCVector.of_list (List.init config.global_count Fun.id) in
let thread_f = Step.gen ~config ~fuel:config.operation_count in
let threads = List.init config.domain_count (fun _ -> thread_f ()) in
let program = ({ globals; threads } : Program.t) in
if config.print_tests then Program.print program;
let random = Program.run ~impl:(`Random 100) program in
Expand All @@ -260,61 +257,107 @@ let run_random config () =

let run config test_count =
Printf.printf "\n\n";
for i = 0 to test_count do
for i = 1 to test_count do
Printf.printf "----run: %d/%d\r%!" i test_count;
run_random config ()
done;
Printf.printf "\nall generated programs passed\n%!"

(* cmd *)
let _ =
let test_count = ref 100 in
let globals_count = ref 3 in
let value_limit = ref 3 in
let operations_count = ref 3 in
let thread_count = ref 3 in
let generate_conditionals = ref true in
let print_tests = ref false in
let seed = ref 0 in
let speclist =
[
( "-test-count",
Arg.Set_int test_count,
"number of programs to generate and test" );
("-print-tests", Arg.Set print_tests, "print all tests");
( "-global-vars-count",
Arg.Set_int globals_count,
"number of shared atomic variables (the more, the higher the reduction)"
);
( "-value-limit",
Arg.Set_int value_limit,
"range of values used by generated operations" );
( "-operations-count",
Arg.Set_int operations_count,
"number of operations per thread" );
("-thread-count", Arg.Set_int thread_count, "number of threads");
( "-generate-conditionals",
Arg.Set generate_conditionals,
"enable/disable generation of conditional statements" );
("-seed", Arg.Set_int seed, "random seed for generation");
]
(* Command line interface *)
open Cmdliner

let test_count =
let default = 100 in
let info =
Arg.info [ "t"; "test-count" ] ~docv:"INT"
~doc:"Number of programs to generate and test."
in
Arg.value (Arg.opt Arg.int default info)

let global_count =
let default = 3 in
let info =
Arg.info [ "g"; "global-count" ] ~docv:"INT"
~doc:"Number of global atomic variables in generated programs."
in
Arg.value (Arg.opt Arg.int default info)

let print_tests =
let info = Arg.info [ "p"; "print-tests" ] ~doc:"Print generated tests." in
Arg.value (Arg.flag info)

let value_limit =
let default = 3 in
let info =
Arg.info [ "l"; "value-limit" ] ~docv:"INT"
~doc:
"Values of atomic operations stay (mostly) between zero and this value."
in
Arg.value (Arg.opt Arg.int default info)

let operation_count =
let default = 3 in
let info =
Arg.info [ "o"; "operation-count" ] ~docv:"INT"
~doc:"Number of operations generated for every domain."
in
Arg.parse speclist
(fun _ -> ())
"gen_program.exe [-test-count INT] [-global-vars-count INT] [-value-limit \
INT] [-operations-count INT] [-thread-count INT] [-generate-conditionals \
BOOL]";
let config =
({
globals_count = !globals_count;
value_limit = !value_limit;
operations_count = !operations_count;
thread_count = !thread_count;
generate_conditionals = !generate_conditionals;
print_tests = !print_tests;
seed = (if !seed > 0 then Some !seed else None);
}
: config)
Arg.value (Arg.opt Arg.int default info)

let domain_count =
let default = 3 in
let info =
Arg.info [ "d"; "domain-count" ] ~docv:"INT"
~doc:"Number of domains in generated tests."
in
Arg.value (Arg.opt Arg.int default info)

let generate_conditionals =
let info =
Arg.info
[ "c"; "generate-conditionals" ]
~doc:"Generate tests with conditional statements."
in
print_config config;
run config !test_count
Arg.value (Arg.flag info)

let seed_opt =
let info = Arg.info [ "s"; "random-seed" ] ~docv:"INT" ~doc:"Random seed" in
Arg.value (Arg.opt (Arg.some Arg.int) None info)

let cmd =
let open Term in
const
(fun
test_count
global_count
print_tests
value_limit
operation_count
domain_count
generate_conditionals
seed_opt
->
let seed =
match seed_opt with
| Some seed -> seed
| None ->
Random.self_init ();
Random.bits ()
in
let config =
({
global_count;
value_limit;
operation_count;
domain_count;
generate_conditionals;
print_tests;
seed;
}
: config)
in
print_config config;
run config test_count)
$ test_count $ global_count $ print_tests $ value_limit $ operation_count
$ domain_count $ generate_conditionals $ seed_opt

let () = exit @@ Cmd.eval @@ Cmd.v (Cmd.info ~doc:"Test generator for DSCheck" "gen_program") cmd

0 comments on commit ddae4aa

Please sign in to comment.