Module QCheck2.Shrink
Shrinking helper functions.
module type Number = sig ... endUtil module representing a number type, used for ad hoc polymorphism of some functions like
number_towards.
val number_towards : (module Number with type t = 'a) -> destination:'a -> 'a -> 'a Stdlib.Seq.tShrink a number by edging towards a destination.
The destination is always the first value for optimal shrinking.
let int64_towards_list destination x = List.of_seq @@ Gen.number_towards (module Int64) ~destination x in assert (int64_towards_list 0L 100L = [0L; 50L; 75L; 88L; 94L; 97L; 99L]); assert (int64_towards_list 500L 1000L = [500L; 750L; 875L; 938L; 969L; 985L; 993L; 997L; 999L]); assert (int64_towards_list (-50L) (-26L) = [-50L; -38L; -32L; -29L; -28L; -27L])This generic function is exposed to let users reuse this shrinking technique for their custom number types. More specialized, convenient functions are provided below, e.g.
int_towards.
val int_towards : int -> int -> int Stdlib.Seq.tnumber_towardsspecialized toint.
val int32_towards : int32 -> int32 -> int32 Stdlib.Seq.tnumber_towardsspecialized toint32.
val int64_towards : int64 -> int64 -> int64 Stdlib.Seq.tnumber_towardsspecialized toint64.
val float_towards : float -> float -> float Stdlib.Seq.tnumber_towardsspecialized tofloat.There are various ways to shrink a float:
- try removing floating digits, i.e. towards integer values
- try to get as close as possible to the destination, no matter the number of digits
- a mix of both
This implementation, as it relies on the generic
number_towardsfunction, tries to get as close as possible to the destination, e.g. the last value ofGen.float_towards 50 100may be99.9969482421875(or a similar value).