falsify-0.4.0: Property-based testing with internal integrated shrinking
Safe HaskellNone
LanguageHaskell2010

Test.Falsify.Range

Description

Numerical ranges

Intended for qualified import.

import Test.Falsify
import qualified Test.Falsify.Range as Range
Synopsis

Documentation

data Range a Source #

Range of values

Instances

Instances details
Functor Range Source # 
Instance details

Defined in Test.Falsify.Internal.Range

Methods

fmap :: (a -> b) -> Range a -> Range b #

(<$) :: a -> Range b -> Range a #

Constructors

Linear

uniform :: (Integral a, FiniteBits a, Bounded a) => Range a Source #

Uniform selection anywhere in the full range [minBound .. maxBound]

Shrinks towards zero.

If you don't need specific bounds, you should probably use uniform instead of inclusive, especially for large bit sizes, because we can more easily guarantee a true uniform selection here.

inclusive :: (Integral a, FiniteBits a) => (a, a) -> Range a Source #

Uniform selection inclusive the given bounds, shrinking towards first bound

NOTE: There is no requirement that the first bound is lower than the second; indeed, uniform (0, -1) and uniform (-1, 0) are different ranges: the former shrinks towards 0, the latter towards -1.

See also uniform.

enum :: Enum a => (a, a) -> Range a Source #

Variation on inclusive for types that are Enum but not Integral

This is useful for types such as Char. However, since this relies on Enum, it's limited by the precision of Int.

withOrigin :: (Integral a, FiniteBits a) => (a, a) -> a -> Range a Source #

Selection within the given bounds, shrinking towards the specified origin

All else being equal, prefers values in the second half of the range (in the common case of say withOrigin (-100, 100) 0, this means we prefer positive values).

Non-linear

skewedBy :: (FiniteBits a, Integral a) => Double -> (a, a) -> Range a Source #

Introduce skew (non-uniform selection)

A skew of s == 0 means no skew: uniform selection.

A positive skew (s > 0) introduces a bias towards smaller values (this is the typical use case). As example, for a skew of s == 1:

  • We will generate a value from the lower 20% of the range 60% of the time.
  • We will generate a value from the upper 60% of the range 20% of the time.

A negative skew (s < 0) introduces a bias towards larger values. For a skew of s == 1:

  • We will generate a value from the upper 20% of the range 60% of the time.
  • We will generate a value from the lower 60% of the range 20% of the time.

The table below lists values for the percentage of the range used, given a percentage of the time (a value of 0 means a single value from the range):

   | time%
 s | 50% | 90%
--------------
 0 |  50 |  90
 1 |  13 |  56
 2 |   4 |  35
 3 |   1 |  23
 4 |   0 |  16
 5 |   0 |  11
 6 |   0 |   8
 7 |   0 |   6
 8 |   0 |   5
 9 |   0 |   4
10 |   0 |   3

Will shrink towards x, independent of skew.

NOTE: The implementation currently uses something similar to μ-law encoding. As a consequence, the generator gets increased precision near the end of the range we skew towards, and less precision near the other end. This means that not all values in the range can be produced.

Queries

origin :: Range a -> a Source #

Origin of the range (value we shrink towards)

Primitive constructors

constant :: a -> Range a Source #

Range that is x everywhere

fromProperFraction :: Precision -> (ProperFraction -> a) -> Range a Source #

Construct a given a fraction

Precondition: f must be monotonically increasing or decreasing; i.e.

  • for all x <= y, f x <= f y, or
  • for all x <= y, f y <= f x

towards :: (Ord a, Num a) => a -> [Range a] -> Range a Source #

Generate value in any of the specified ranges, then choose the one that is closest to the specified origin

Precondition: the target must be within the bounds of all ranges.

Evalation

eval :: Applicative f => (Precision -> f WordN) -> Range a -> f a Source #

Evaluate a range, given an action to generate fractions

Most users will probably never need to call this function.