From e3076e478954e3385cc5248cdaad25831c08cfc1 Mon Sep 17 00:00:00 2001 From: Gabriel Scherer Date: Fri, 12 Jul 2019 22:18:40 +0200 Subject: [PATCH] Shrink: decompose Shrink.list into Shrink.list_spine and Shrink.list_elems --- src/core/QCheck.ml | 28 +++++++++++++++++----------- src/core/QCheck.mli | 6 ++++++ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/core/QCheck.ml b/src/core/QCheck.ml index 79aa27d5..fb468fa4 100644 --- a/src/core/QCheck.ml +++ b/src/core/QCheck.ml @@ -333,6 +333,7 @@ module Shrink = struct type 'a t = 'a -> 'a Iter.t let nil _ = Iter.empty + let unit = nil (* balanced shrinker for integers (non-exhaustive) *) @@ -394,7 +395,7 @@ module Shrink = struct ) done - let list ?shrink l yield = + let list_spine l yield = let n = List.length l in let chunk_size = ref (n/2) in @@ -424,18 +425,23 @@ module Shrink = struct in pos_loop [] l'; chunk_size := !chunk_size / 2; - done; + done + + let list_elems shrink l yield = + (* try to shrink each element of the list *) + let rec elem_loop rev_prefix suffix = match suffix with + | [] -> () + | x::xs -> + shrink x (fun x' -> yield (List.rev_append rev_prefix (x'::xs))); + elem_loop (x::rev_prefix) xs + in + elem_loop [] l + + let list ?shrink l yield = + list_spine l yield; match shrink with | None -> () - | Some f -> - (* try to shrink each element of the list *) - let rec elem_loop rev_prefix suffix = match suffix with - | [] -> () - | x::xs -> - f x (fun x' -> yield (List.rev_append rev_prefix (x'::xs))); - elem_loop (x::rev_prefix) xs - in - elem_loop [] l + | Some shrink -> list_elems shrink l yield let pair a b (x,y) yield = a x (fun x' -> yield (x',y)); diff --git a/src/core/QCheck.mli b/src/core/QCheck.mli index 84d03996..b5e65c14 100644 --- a/src/core/QCheck.mli +++ b/src/core/QCheck.mli @@ -467,6 +467,12 @@ module Shrink : sig the elements of the list themselves (e.g. in an [int list] one can try to decrease the integers). *) + val list_spine : 'a list t + (** Try to shrink lists by removing one or more elements. *) + + val list_elems : 'a t -> 'a list t + (** Shrinks the elements of a list, without changing the list size. *) + val array : ?shrink:'a t -> 'a array t (** Shrink an array. @param shrink see {!list} *)