falsify-0.2.0: Property-based testing with internal integrated shrinking
Safe HaskellSafe-Inferred
LanguageHaskell2010

Test.Falsify.Range

Description

Numerical ranges

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

between :: forall a. (Integral a, FiniteBits a) => (a, a) -> Range a Source #

Uniform selection between the given bounds, shrinking towards first bound

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

Variation on between 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 :: forall a. (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

data ProperFraction where Source #

Value x such that 0 <= x < 1

Bundled Patterns

pattern ProperFraction :: Double -> ProperFraction 

Instances

Instances details
Num ProperFraction Source # 
Instance details

Defined in Test.Falsify.Internal.Range

Fractional ProperFraction Source # 
Instance details

Defined in Test.Falsify.Internal.Range

Show ProperFraction Source #

Show instance relies on the ProperFraction pattern synonym

Instance details

Defined in Test.Falsify.Internal.Range

Eq ProperFraction Source # 
Instance details

Defined in Test.Falsify.Internal.Range

Ord ProperFraction Source # 
Instance details

Defined in Test.Falsify.Internal.Range

newtype Precision Source #

Precision (in bits)

Constructors

Precision Word8 

Instances

Instances details
Enum Precision Source # 
Instance details

Defined in Test.Falsify.Internal.Range

Num Precision Source # 
Instance details

Defined in Test.Falsify.Internal.Range

Show Precision Source # 
Instance details

Defined in Test.Falsify.Internal.Range

Eq Precision Source # 
Instance details

Defined in Test.Falsify.Internal.Range

Ord Precision Source # 
Instance details

Defined in Test.Falsify.Internal.Range

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 :: forall a. (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 :: forall f a. Applicative f => (Precision -> f ProperFraction) -> Range a -> f a Source #

Evaluate a range, given an action to generate fractions

Most users will probably never need to call this function.