Skip to content

Commit

Permalink
test: more tests for vector
Browse files Browse the repository at this point in the history
  • Loading branch information
c-cube committed Jul 25, 2018
1 parent e510e15 commit 551c837
Showing 1 changed file with 66 additions and 7 deletions.
73 changes: 66 additions & 7 deletions src/core/CCVector.ml
Original file line number Diff line number Diff line change
Expand Up @@ -293,16 +293,31 @@ let equal eq v1 v2 =
equal (=) (of_list l1) (of_list l2) = (l1=l2))
*)

(*$QR
Q.(pair (small_list small_int)(small_list small_int)) (fun (l1,l2) ->
let v1 = of_list l1 in
let v2 = of_list l2 in
equal (=) v1 v2 = (l1=l2))
*)

let compare cmp v1 v2 =
let n = min v1.size v2.size in
let rec check i =
if i = n
then compare v1.size v2.size
else
else (
let c = cmp (get v1 i) (get v2 i) in
if c = 0 then check (i+1) else c
)
in check 0

(*$QR
Q.(pair (small_list small_int)(small_list small_int)) (fun (l1,l2) ->
let v1 = of_list l1 in
let v2 = of_list l2 in
compare Pervasives.compare v1 v2 = CCList.compare Pervasives.compare l1 l2)
*)

exception Empty

let pop_exn v =
Expand Down Expand Up @@ -349,6 +364,13 @@ let copy v = {
OUnit.assert_bool "not_empty" (not (is_empty v));
*)

(*$QR
Q.(small_list small_int) (fun l ->
let v = of_list l in
let v' = copy v in
equal (=) v v')
*)

let shrink v n =
if n < v.size then v.size <- n

Expand Down Expand Up @@ -532,13 +554,25 @@ let exists p v =
else p v.vec.(i) || check (i+1)
in check 0

(*$QR
Q.(pair (fun1 Observable.int bool) (small_list small_int)) (fun (Q.Fun (_,f),l) ->
let v = of_list l in
exists f v = List.exists f l)
*)

let for_all p v =
let n = v.size in
let rec check i =
if i = n then true
else p v.vec.(i) && check (i+1)
in check 0

(*$QR
Q.(pair (fun1 Observable.int bool) (small_list small_int)) (fun (Q.Fun (_,f),l) ->
let v = of_list l in
for_all f v = List.for_all f l)
*)

let member ~eq x v =
exists (eq x) v

Expand All @@ -556,6 +590,12 @@ let find p v =
try Some (find_exn p v)
with Not_found -> None

(*$QR
Q.(pair (fun1 Observable.int bool) (small_list small_int)) (fun (Q.Fun (_,f),l) ->
let v = of_list l in
find f v = CCList.find_pred f l)
*)

let find_map f v =
let n = v.size in
let rec search i =
Expand All @@ -578,10 +618,16 @@ let filter_map f v =
iter
(fun x -> match f x with
| None -> ()
| Some y -> push v' y
) v;
| Some y -> push v' y)
v;
v'

(*$QR
Q.(pair (fun1 Observable.int (option bool)) (small_list small_int)) (fun (Q.Fun (_,f),l) ->
let v = of_list l in
to_list (filter_map f v) = CCList.filter_map f l)
*)

let flat_map f v =
let v' = create () in
iter (fun x -> iter (push v') (f x)) v;
Expand All @@ -592,17 +638,17 @@ let flat_map_seq f v =
iter
(fun x ->
let seq = f x in
append_seq v' seq;
) v;
append_seq v' seq)
v;
v'

let flat_map_list f v =
let v' = create () in
iter
(fun x ->
let l = f x in
append_list v' l;
) v;
append_list v' l)
v;
v'

let (>>=) x f = flat_map f x
Expand All @@ -622,6 +668,13 @@ let rev_in_place v =
done
)

(*$QR
Q.(small_list small_int) (fun l ->
let v = of_list l in
rev_in_place v;
to_list v = List.rev l)
*)

let rev v =
let v' = copy v in
rev_in_place v';
Expand All @@ -633,6 +686,12 @@ let rev v =
rev (create ()) |> to_list = []
*)

(*$QR
Q.(small_list small_int) (fun l ->
let v = of_list l in
to_list (rev v) = List.rev l)
*)

let rev_iter f v =
for i = v.size-1 downto 0 do
f v.vec.(i)
Expand Down

0 comments on commit 551c837

Please sign in to comment.