falsify-0.4.0: src/Test/Falsify/Internal/Generator/Simple.hs
-- | Simple (i.e., non-compound) generators
module Test.Falsify.Internal.Generator.Simple (
bool
, inRange
, int
) where
import Prelude hiding (properFraction)
import Data.Bits
import Data.Word
import Test.Falsify.Internal.Generator
import Test.Falsify.Internal.Generator.Precision
import Test.Falsify.Internal.Range
import Test.Falsify.SampleTree (Sample(..))
import qualified Test.Falsify.Range as Range
import qualified Test.Falsify.SampleTree as SampleTree
{-------------------------------------------------------------------------------
Simple generators
-------------------------------------------------------------------------------}
-- | Generate random bool, shrink towards the given value
--
-- Chooses with equal probability between 'True' and 'False'.
bool :: Bool -> Gen Bool
bool target = aux . SampleTree.sampleValue <$> primWith shrinker
where
aux :: Word64 -> Bool
aux x | msbSet x = not target
| otherwise = target
msbSet :: forall a. FiniteBits a => a -> Bool
msbSet x = testBit x (finiteBitSize (undefined :: a) - 1)
shrinker :: Sample -> [Word64]
shrinker (Shrunk 0) = []
shrinker _ = [0]
{-------------------------------------------------------------------------------
Integral ranges
-------------------------------------------------------------------------------}
-- | Generate value in the specified range
inRange :: Range a -> Gen a
inRange r = Range.eval wordN r
-- | Type-specialization of 'inRange'
int :: Range Int -> Gen Int
int = inRange