From ef08010c6f8fa2ad544f436572bba3dddbd54ab3 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Sun, 17 Mar 2013 18:36:51 +0100 Subject: [PATCH] renamed tests/tests.ml into tests/run_tests.ml; added some tests for Vector; added Sequence functions to Vector --- Makefile | 2 +- tests/{tests.ml => run_tests.ml} | 1 + tests/test_vector.ml | 45 ++++++++++++++++++++++++++++++++ vector.ml | 13 ++++++--- vector.mli | 9 +++++-- 5 files changed, 64 insertions(+), 6 deletions(-) rename tests/{tests.ml => run_tests.ml} (92%) create mode 100644 tests/test_vector.ml diff --git a/Makefile b/Makefile index 68757984..a1a667d8 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ all: ocamlbuild $(OPTIONS) $(TARGETS_LIB) tests: - ocamlbuild $(OPTIONS) -package oUnit -I . tests/tests.native + ocamlbuild $(OPTIONS) -package oUnit -I . tests/run_tests.native bench: ocamlbuild $(OPTIONS) -package bench -package unix -I . tests/benchs.native diff --git a/tests/tests.ml b/tests/run_tests.ml similarity index 92% rename from tests/tests.ml rename to tests/run_tests.ml index 8c0f01f3..08cb52d3 100644 --- a/tests/tests.ml +++ b/tests/run_tests.ml @@ -5,6 +5,7 @@ open OUnit let suite = "all_tests" >::: [ Test_pHashtbl.suite; + Test_vector.suite; Test_deque.suite; Test_fHashtbl.suite; Test_fQueue.suite; diff --git a/tests/test_vector.ml b/tests/test_vector.ml new file mode 100644 index 00000000..4032b2ae --- /dev/null +++ b/tests/test_vector.ml @@ -0,0 +1,45 @@ + +open OUnit + +open Sequence.Infix + +let test_clear () = + let v = Vector.of_seq (1 -- 10) in + OUnit.assert_equal 10 (Vector.size v); + Vector.clear v; + OUnit.assert_equal 0 (Vector.size v); + OUnit.assert_bool "empty_after_clear" (Sequence.is_empty (Vector.to_seq v)); + () + +let test_append () = + let a = Vector.of_seq (1 -- 5) in + let b = Vector.of_seq (6 -- 10) in + Vector.append a b; + OUnit.assert_equal 10 (Vector.size a); + OUnit.assert_equal (Sequence.to_array (1 -- 10)) (Vector.to_array a); + OUnit.assert_equal (Sequence.to_array (6 -- 10)) (Vector.to_array b); + () + +let test_copy () = + let v = Vector.of_seq (1 -- 100) in + OUnit.assert_equal 100 (Vector.size v); + let v' = Vector.copy v in + OUnit.assert_equal 100 (Vector.size v'); + Vector.clear v'; + OUnit.assert_bool "empty" (Vector.is_empty v'); + OUnit.assert_bool "not_empty" (not (Vector.is_empty v)); + () + +let test_shrink () = + let v = Vector.of_seq (1 -- 10) in + Vector.shrink v 5; + OUnit.assert_equal [1;2;3;4;5] (Vector.to_list v); + () + +let suite = + "test_vector" >::: + [ "test_clear" >:: test_clear; + "test_append" >:: test_append; + "test_copy" >:: test_copy; + "test_shrink" >:: test_shrink; + ] diff --git a/vector.ml b/vector.ml index 4c60f853..4261ba33 100644 --- a/vector.ml +++ b/vector.ml @@ -23,7 +23,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *) -(** Growable, mutable vector *) +(** {1 Growable, mutable vector} *) (** a vector of 'a. *) type 'a t = { @@ -186,6 +186,15 @@ let set v i x = let size v = v.size +let unsafe_get_array v = v.vec + +let of_seq ?(init=create 10) seq = + Sequence.iter (fun x -> push init x) seq; + init + +let to_seq t = + Sequence.from_iter (fun k -> iter t k) + let from_array a = let c = Array.length a in let v = create c in @@ -201,8 +210,6 @@ let from_list l = let to_array v = Array.sub v.vec 0 v.size -let get_array v = v.vec - let to_list v = let l = ref [] in for i = 0 to v.size - 1 do diff --git a/vector.mli b/vector.mli index 50b13cc4..f0452e98 100644 --- a/vector.mli +++ b/vector.mli @@ -23,7 +23,7 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *) -(** Growable, mutable vector *) +(** {1 Growable, mutable vector} *) type 'a t (** the type of a vector of 'a *) @@ -97,9 +97,14 @@ val set : 'a t -> int -> 'a -> unit val size : 'a t -> int (** number of elements in vector *) +val unsafe_get_array : 'a t -> 'a array + (** Access the underlying *shared* array (do not modify!) *) + +val of_seq : ?init:'a t -> 'a Sequence.t -> 'a t +val to_seq : 'a t -> 'a Sequence.t + val from_array : 'a array -> 'a t val from_list : 'a list -> 'a t val to_array : 'a t -> 'a array -val get_array : 'a t -> 'a array (* get underlying *shared* array *) val to_list : 'a t -> 'a list