module type Number = sig ... end
Util module representing a number type, used for ad hoc polymorphism of some functions like
Shrink 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.
val float_towards : float -> float -> float Seq.t
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_towards function, tries to get as close as possible to the destination, e.g. the last value of
Gen.float_towards 50 100 may be
99.9969482421875 (or a similar value).
val int_aggressive_towards : int -> int -> int Seq.t
int_agressive_towards destination n gives all integers from
Be careful about time and memory as the resulting list can be huge
val int_aggressive : int -> int Seq.t