From 170c1031a9ff8c08807a8f63970081bf9a86d580 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 20 Mar 2013 18:04:04 +0100 Subject: [PATCH] some basic tests for Future --- Makefile | 5 +---- _tags | 3 ++- tests/run_tests.ml | 1 + tests/test_future.ml | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 5 deletions(-) create mode 100644 tests/test_future.ml diff --git a/Makefile b/Makefile index 67db17ce..9b704d54 100644 --- a/Makefile +++ b/Makefile @@ -6,10 +6,7 @@ TARGETS_LIB = containers.cmxa containers.cma TARGET_THREAD_LIB = thread_containers.cmxa thread_containers.cma OPTIONS = -use-ocamlfind -all: lib - -# like lib, but with thread-specific modules -all_thread: lib lib_thread +all: lib lib_thread lib: ocamlbuild $(OPTIONS) $(TARGETS_LIB) diff --git a/_tags b/_tags index ec418e40..bbc655fb 100644 --- a/_tags +++ b/_tags @@ -1 +1,2 @@ -: thread +<**/*future.*>: thread +: thread diff --git a/tests/run_tests.ml b/tests/run_tests.ml index 6421ed94..d00aef38 100644 --- a/tests/run_tests.ml +++ b/tests/run_tests.ml @@ -14,6 +14,7 @@ let suite = Test_heap.suite; Test_graph.suite; Test_univ.suite; + Test_future.suite; ] let _ = diff --git a/tests/test_future.ml b/tests/test_future.ml new file mode 100644 index 00000000..ee674e27 --- /dev/null +++ b/tests/test_future.ml @@ -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; + ]