2019-06-13 21:12:40 +03:00
|
|
|
module Generators
|
|
|
|
( source
|
2019-08-07 18:26:54 +03:00
|
|
|
, integerScientific
|
2019-08-08 18:44:57 +03:00
|
|
|
, rationalScientific
|
2019-08-08 20:55:23 +03:00
|
|
|
, floatingScientific
|
|
|
|
, classifyScientific
|
2019-06-13 21:12:40 +03:00
|
|
|
) where
|
|
|
|
|
2019-09-20 22:51:49 +03:00
|
|
|
import Data.Ratio ((%))
|
|
|
|
import Data.Scientific (Scientific)
|
2019-08-08 18:44:57 +03:00
|
|
|
import qualified Data.Scientific as Scientific
|
2019-09-20 22:51:49 +03:00
|
|
|
import Hedgehog
|
|
|
|
import qualified Hedgehog.Gen as Gen
|
|
|
|
import qualified Source.Source
|
2019-08-07 18:26:54 +03:00
|
|
|
|
2019-06-13 21:12:40 +03:00
|
|
|
|
2019-09-20 22:51:49 +03:00
|
|
|
source :: MonadGen m => Hedgehog.Range Int -> m Source.Source.Source
|
2019-08-06 23:38:41 +03:00
|
|
|
source r = Gen.frequency [ (1, empty), (20, nonEmpty) ]
|
|
|
|
where empty = pure mempty
|
2019-09-20 22:51:49 +03:00
|
|
|
nonEmpty = Source.Source.fromUTF8 <$> Gen.utf8 r (Gen.frequency [ (1, pure '\r'), (1, pure '\n'), (20, Gen.unicode) ])
|
2019-08-07 18:26:54 +03:00
|
|
|
|
|
|
|
integerScientific :: MonadGen m => Hedgehog.Range Integer -> m Scientific
|
|
|
|
integerScientific = fmap fromIntegral . Gen.integral
|
2019-08-08 18:44:57 +03:00
|
|
|
|
|
|
|
rationalScientific :: MonadGen m => Hedgehog.Range Integer -> Hedgehog.Range Integer -> m Scientific
|
|
|
|
rationalScientific nrange drange = do
|
|
|
|
num <- Gen.integral nrange
|
|
|
|
den <- Gen.integral drange
|
|
|
|
let goodDen = if den == 0 then 1 else den
|
|
|
|
let digitLimit = Just 25
|
|
|
|
case Scientific.fromRationalRepetend digitLimit (num % goodDen) of
|
|
|
|
Left (sci, _) -> pure sci
|
|
|
|
Right (sci, _) -> pure sci
|
|
|
|
|
2019-08-08 20:55:23 +03:00
|
|
|
floatingScientific :: MonadGen m => Hedgehog.Range Double -> m Scientific
|
|
|
|
floatingScientific = fmap Scientific.fromFloatDigits . Gen.double
|
|
|
|
|
|
|
|
classifyScientific :: MonadTest m => Scientific -> m ()
|
|
|
|
classifyScientific sci = do
|
|
|
|
classify "negative" $ sci < 0
|
|
|
|
classify "small" $ (sci > 0 && sci <= 1)
|
|
|
|
classify "medium" $ (sci > 1 && sci <= 10000)
|
|
|
|
classify "large" $ sci > 10000
|