some basic tests for Future

This commit is contained in:
Simon Cruanes 2013-03-20 18:04:04 +01:00
parent 174f10e4f2
commit 170c1031a9
4 changed files with 43 additions and 5 deletions

View file

@ -6,10 +6,7 @@ TARGETS_LIB = containers.cmxa containers.cma
TARGET_THREAD_LIB = thread_containers.cmxa thread_containers.cma TARGET_THREAD_LIB = thread_containers.cmxa thread_containers.cma
OPTIONS = -use-ocamlfind OPTIONS = -use-ocamlfind
all: lib all: lib lib_thread
# like lib, but with thread-specific modules
all_thread: lib lib_thread
lib: lib:
ocamlbuild $(OPTIONS) $(TARGETS_LIB) ocamlbuild $(OPTIONS) $(TARGETS_LIB)

3
_tags
View file

@ -1 +1,2 @@
<future.*>: thread <**/*future.*>: thread
<tests/*.native>: thread

View file

@ -14,6 +14,7 @@ let suite =
Test_heap.suite; Test_heap.suite;
Test_graph.suite; Test_graph.suite;
Test_univ.suite; Test_univ.suite;
Test_future.suite;
] ]
let _ = let _ =

39
tests/test_future.ml Normal file
View file

@ -0,0 +1,39 @@
(** Test Future *)
open OUnit
let test_mvar () =
let box = Future.MVar.empty () in
let f = Future.spawn (fun () -> Future.MVar.take box + 1) in
Thread.delay 0.1;
OUnit.assert_bool "still waiting" (not (Future.is_done f));
Future.MVar.put box 1;
OUnit.assert_equal 2 (Future.get f);
()
let test_parallel () =
let open Enum.Infix in
let l = 1 -- 300
|> Enum.map (fun _ -> Future.spawn (fun () -> Thread.delay 0.1; 1))
|> Enum.to_list in
let l' = List.map Future.get l in
OUnit.assert_equal 300 (List.fold_left (+) 0 l');
()
let test_time () =
let start = Unix.gettimeofday () in
let f1 = Future.spawn (fun () -> Thread.delay 0.5) in
let f2 = Future.spawn (fun () -> Thread.delay 0.5) in
Future.get f1;
Future.get f2;
let stop = Unix.gettimeofday () in
OUnit.assert_bool "parallelism" (stop -. start < 0.75);
()
let suite =
"test_future" >:::
[ "test_mvar" >:: test_mvar;
"test_parallel" >:: test_parallel;
"test_time" >:: test_time;
]