mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 03:05:28 -05:00
renamed tests/tests.ml into tests/run_tests.ml;
added some tests for Vector; added Sequence functions to Vector
This commit is contained in:
parent
9d556cb106
commit
ef08010c6f
5 changed files with 64 additions and 6 deletions
2
Makefile
2
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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
45
tests/test_vector.ml
Normal file
45
tests/test_vector.ml
Normal file
|
|
@ -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;
|
||||
]
|
||||
13
vector.ml
13
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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue