Skip to content

Commit

Permalink
🧪 Add a test (currently fails)
Browse files Browse the repository at this point in the history
Effect commit is not correct now
  • Loading branch information
Zeta611 committed Jul 4, 2024
1 parent 9b42561 commit ac0cf14
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 7 deletions.
17 changes: 15 additions & 2 deletions bin/main.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
open! Base
open Stdio
open React_trace

let test_prog =
Expand All @@ -22,7 +23,18 @@ let test_prog =
(App
{
fn = Var "setS";
arg = Fn { param = "s"; body = Const (Int 43) };
arg =
Fn
{
param = "s";
body =
Bin_op
{
op = Plus;
left = Var "s";
right = Const (Int 1);
};
};
}),
View [ Const Unit ] );
});
Expand All @@ -34,5 +46,6 @@ let () =
Logs.set_reporter (Logs_fmt.reporter ());
Logs.set_level (Some Logs.Debug);
Sexp.pp_hum Stdlib.Format.std_formatter (Syntax.Prog.sexp_of_t test_prog);
Interp.run test_prog;
let { Interp.steps } = Interp.run ~fuel:4 test_prog in
printf "\nSteps: %d\n" steps;
Stdlib.exit (if Logs.err_count () > 0 then 1 else 0)
3 changes: 2 additions & 1 deletion dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
base
fmt
logs
stdio)
stdio
(alcotest :with-test))
(tags
(React "Static Analysis")))

Expand Down
12 changes: 8 additions & 4 deletions lib/interp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,9 @@ let step_path (path : Path.t) : bool =
if has_updates then commit_effs path;
has_updates

let run ?(step : int option) (prog : Prog.t) : unit =
type run_info = { steps : int }

let run ?(fuel : int option) (prog : Prog.t) : run_info =
Logger.run prog;
let driver () =
let cnt = ref 1 in
Expand All @@ -363,8 +365,10 @@ let run ?(step : int option) (prog : Prog.t) : unit =
Int.incr cnt;
Logs.info (fun m -> m "Step %d" !cnt);
if step_path path then
match step with Some n when !cnt >= n -> () | _ -> loop ()
match fuel with Some n when !cnt >= n -> () | _ -> loop ()
in
loop ()
loop ();
!cnt
in
mem_h driver () ~mem:Tree_mem.empty |> ignore
let steps = mem_h driver () ~mem:Tree_mem.empty |> fst in
{ steps }
1 change: 1 addition & 0 deletions react_trace.opam
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ depends: [
"fmt"
"logs"
"stdio"
"alcotest" {with-test}
"odoc" {with-doc}
]
build: [
Expand Down
1 change: 1 addition & 0 deletions test/dune
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
(test
(libraries alcotest base react_trace)
(name test_react_trace))
92 changes: 92 additions & 0 deletions test/test_react_trace.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
open! Base
open React_trace

let fuel = 100

let set_in_effect_step_two_times () =
let prog =
let open Syntax in
Prog.(
Comp
( {
name = "C";
param = "x";
body =
Expr.(
Stt
{
label = 0;
stt = "s";
set = "setS";
init = Const (Int 42);
body =
Seq
( Eff
(App
{
fn = Var "setS";
arg = Fn { param = "s"; body = Const (Int 43) };
}),
View [ Const Unit ] );
});
},
Expr Expr.(View [ App { fn = Var "C"; arg = Const Unit } ]) ))
in
let { Interp.steps } = Interp.run ~fuel prog in
Alcotest.(check' int) ~msg:"step two times" ~expected:2 ~actual:steps

let set_in_effect_step_indefinitely () =
let prog =
let open Syntax in
Prog.(
Comp
( {
name = "C";
param = "x";
body =
Expr.(
Stt
{
label = 0;
stt = "s";
set = "setS";
init = Const (Int 42);
body =
Seq
( Eff
(App
{
fn = Var "setS";
arg =
Fn
{
param = "s";
body =
Bin_op
{
op = Plus;
left = Var "s";
right = Const (Int 1);
};
};
}),
View [ Const Unit ] );
});
},
Expr Expr.(View [ App { fn = Var "C"; arg = Const Unit } ]) ))
in
let { Interp.steps } = Interp.run ~fuel prog in
Alcotest.(check' int) ~msg:"step indefintely" ~expected:fuel ~actual:steps

let () =
let open Alcotest in
run "Interpreter"
[
( "steps",
[
test_case "Set in effect should step two times" `Quick
set_in_effect_step_two_times;
test_case "Set in effect should step indefintely" `Quick
set_in_effect_step_indefinitely;
] );
]

0 comments on commit ac0cf14

Please sign in to comment.