Skip to content

Commit

Permalink
improve Shrink.{array,list} (see #32)
Browse files Browse the repository at this point in the history
  • Loading branch information
c-cube committed May 10, 2017
1 parent 2634e67 commit e7b44ac
Showing 1 changed file with 13 additions and 22 deletions.
35 changes: 13 additions & 22 deletions src/QCheck.ml
Original file line number Diff line number Diff line change
Expand Up @@ -347,11 +347,17 @@ module Shrink = struct
done

let array ?shrink a yield =
for i=0 to Array.length a-1 do
let a' = Array.init (Array.length a-1)
(fun j -> if j< i then a.(j) else a.(j+1))
in
yield a'
let n = Array.length a in
let chunk_size = ref n in
while !chunk_size > 0 do
for i=0 to n - !chunk_size do
(* remove elements in [i .. i+!chunk_size] *)
let a' = Array.init (n - !chunk_size)
(fun j -> if j< i then a.(j) else a.(j + !chunk_size))
in
yield a'
done;
chunk_size := !chunk_size / 2;
done;
match shrink with
| None -> ()
Expand All @@ -366,23 +372,8 @@ module Shrink = struct
done

let list ?shrink l yield =
let rec aux l r = match r with
| [] -> ()
| x :: r' ->
yield (List.rev_append l r');
aux (x::l) r'
in
aux [] l;
match shrink with
| None -> ()
| Some f ->
let rec aux l r = match r with
| [] -> ()
| x :: r' ->
f x (fun x' -> yield (List.rev_append l (x' :: r')));
aux (x :: l) r'
in
aux [] l
array ?shrink (Array.of_list l)
(fun a -> yield (Array.to_list a))

let pair a b (x,y) yield =
a x (fun x' -> yield (x',y));
Expand Down

0 comments on commit e7b44ac

Please sign in to comment.