diff --git a/test/dune b/test/dune index b31ced1f..47ff53ce 100644 --- a/test/dune +++ b/test/dune @@ -1,3 +1,3 @@ (tests - (names t_fib t_bench1) + (names t_fib t_bench1 t_fib_rec) (libraries moonpool)) diff --git a/test/t_fib_rec.ml b/test/t_fib_rec.ml new file mode 100644 index 00000000..87f136f0 --- /dev/null +++ b/test/t_fib_rec.ml @@ -0,0 +1,46 @@ +open Moonpool + +let rec fib_direct x = + if x <= 1 then + 1 + else + fib_direct (x - 1) + fib_direct (x - 2) + +let rec fib ~on x : int Fut.t = + if x <= 18 then + Fut.spawn ~on (fun () -> fib_direct x) + else + let open Fut.Infix_local in + let+ t1 = fib ~on (x - 1) and+ t2 = fib ~on (x - 2) in + t1 + t2 + +let () = assert (List.init 10 fib_direct = [ 1; 1; 2; 3; 5; 8; 13; 21; 34; 55 ]) + +let fib_40 : int = + let pool = Pool.create ~min:8 () in + fib ~on:pool 40 |> Fut.wait_block_exn + +let run_test () = + let pool = Pool.create ~min:8 () in + + assert ( + List.init 10 (fib ~on:pool) + |> Fut.join_list |> Fut.wait_block_exn + = [ 1; 1; 2; 3; 5; 8; 13; 21; 34; 55 ]); + + let fibs = Array.init 5 (fun _ -> fib ~on:pool 40) in + + let res = Fut.join_array fibs |> Fut.wait_block in + Pool.shutdown pool; + + assert (res = Ok (Array.make 5 fib_40)) + +let () = + Printf.printf "fib 40 = %d\n%!" fib_40; + for _i = 1 to 2 do + run_test () + done; + + (* now make sure we can do this with multiple pools in parallel *) + let jobs = Array.init 4 (fun _ -> Thread.create run_test ()) in + Array.iter Thread.join jobs