papa-0.7.0: test/Properties.hs
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}
{- HLINT ignore "Avoid restricted function" -}
-- Reason: these are the properties of Papa's total head, last, init, tail,
-- maximum, minimum and (!!), and of Papa.Unsafe's partial functions compared
-- with them, whose names HLint restricts whatever module
-- they come from.
{- HLINT ignore "Evaluate" -}
{- HLINT ignore "Setter law: set twice" -}
{- HLINT ignore "Use lefts" -}
{- HLINT ignore "Use rights" -}
{- HLINT ignore "Use fromLeft" -}
{- HLINT ignore "Use fromRight" -}
{- HLINT ignore "Use mapM_" -}
{- HLINT ignore "Use traverse_" -}
{- HLINT ignore "Use fold" -}
{- HLINT ignore "Redundant map" -}
{- HLINT ignore "Use map once" -}
{- HLINT ignore "Redundant pair" -}
{- HLINT ignore "Redundant flip" -}
{- HLINT ignore "Use exitSuccess" -}
{- HLINT ignore "Use ix" -}
{- HLINT ignore "Use toList" -}
{- HLINT ignore "Lens law: read what you set" -}
{- HLINT ignore "Lens law: set what you read" -}
-- Reason: each property writes both sides of an equation that these hints
-- would rewrite one side into the other (CLAUDE.md, HLint Compliance: testing
-- laws).
{- HLINT ignore "Use maximumByOf" -}
{- HLINT ignore "Use minimumByOf" -}
-- Reason: Papa's maximumBy and minimumBy are Data.Foldable1's, which are
-- total; these hints are for Data.Foldable's, which throw for an empty
-- structure.
-- | Hedgehog properties of every function that Papa defines.
module Properties (
properties,
) where
import Control.Monad.Trans.Writer.CPS (runWriter, tell)
import qualified Data.Bifunctor as B
import qualified Data.IntMap.Strict as IM
import qualified Data.IntSet as IS
import qualified Data.List as L
import qualified Data.Map.Strict as M
import qualified Data.Monoid as Mo
import qualified Data.Semigroup as S
import qualified Data.Sequence as Sq
import qualified Data.Set as St
import Data.Tagged (Tagged)
import Hedgehog (Gen, Group (Group), Property, PropertyName, evalIO, forAll, property, (===))
import Hedgehog.Function (Fn, Vary, apply, fn)
import qualified Hedgehog.Function as F
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range
import Papa
import Papa.Base.Control.Arrow (firstA, leftA, rightA, secondA)
import qualified Papa.Containers.Data.IntMap as IntMap
import qualified Papa.Containers.Data.IntSet as IntSet
import qualified Papa.Containers.Data.Map as Map
import qualified Papa.Containers.Data.Seq as Seq
import qualified Papa.Containers.Data.Set as Set
import Papa.Unsafe.Numeric (Natural)
import qualified Papa.Unsafe.Numeric as U
import qualified Prelude as P
-- | Every property, in one group.
properties :: Group
properties =
Group "papa" (integral <> intCasts <> nonZeros <> enums <> numeric <> semigroup <> list <> foldable <> traversable <> functions <> monad <> eithers <> maybes <> fractional <> exits <> containers <> nonEmptyLenses <> optics <> bifunctors)
-- Generators
genInt :: Gen Int
genInt =
Gen.int (Range.linearFrom 0 (-1000) 1000)
genSmallInt :: Gen Int
genSmallInt =
Gen.int (Range.linear (-5) 25)
genInteger :: Gen Integer
genInteger =
Gen.integral (Range.linearFrom 0 (-1000) 1000)
genInts :: Gen [Int]
genInts =
Gen.list (Range.linear 0 20) genInt
genString :: Gen String
genString =
Gen.string (Range.linear 0 10) Gen.alpha
genNonEmpty :: Gen (NonEmpty Int)
genNonEmpty =
Gen.nonEmpty (Range.linear 1 20) genInt
genEither :: Gen (Either Int Char)
genEither =
Gen.either genInt Gen.alpha
genEithers :: Gen [Either Int Char]
genEithers =
Gen.list (Range.linear 0 20) genEither
genFn :: (F.Arg a, Vary a, Show b) => Gen b -> Gen (Fn a b)
genFn =
fn
prop :: PropertyName -> Property -> [(PropertyName, Property)]
prop name p =
[(name, p)]
-- Papa.Base.Data.Integral
integral :: [(PropertyName, Property)]
integral =
prop "divModNonZero: q * d + m == n" (property (do n <- forAll genInt; d <- forAll genNonZeroInt; (\(q, m) -> q * the divisor d + m) (divModNonZero n d) === n))
<> prop "divModNonZero: Prelude's for d /= -1" (property (do n <- forAll genInt; d <- forAll genNonZeroInt; let { d' = the divisor d }; divModNonZero n d === bool (U.divMod n d') (P.negate n, 0) (d' == -1)))
<> prop "divModNonZero: minBound / -1 does not throw" (property (fmap (divModNonZero (minBound :: Int)) (preview nonZero (-1 :: Int)) === Just (minBound, 0)))
<> prop "divModNonZero: Prelude's at Word8" (property (do n <- forAll (Gen.word8 Range.linearBounded); d <- forAll (Gen.just (preview nonZero <$> Gen.word8 Range.linearBounded)); divModNonZero n d === U.divMod n (the divisor d)))
<> prop "divModNonZero: Prelude's at Word" (property (do n <- forAll (Gen.word Range.linearBounded); d <- forAll (Gen.just (preview nonZero <$> Gen.frequency [(1, pure maxBound), (9, Gen.word Range.linearBounded)])); divModNonZero n d === U.divMod n (the divisor d)))
<> prop "divModNonZero: Prelude's at Natural" (property (do n <- forAll genNatural; d <- forAll (Gen.just (preview nonZero <$> genNatural)); divModNonZero n d === U.divMod n (the divisor d)))
<> prop "divNonZero, modNonZero: the components of divModNonZero" (property (do n <- forAll genInt; d <- forAll genNonZeroInt; (divNonZero n d, modNonZero n d) === divModNonZero n d))
<> prop "quotRemNonZero: q * d + r == n" (property (do n <- forAll genInt; d <- forAll genNonZeroInt; (\(q, r) -> q * the divisor d + r) (quotRemNonZero n d) === n))
<> prop "quotRemNonZero: Prelude's for d /= -1" (property (do n <- forAll genInt; d <- forAll genNonZeroInt; let { d' = the divisor d }; quotRemNonZero n d === bool (U.quotRem n d') (P.negate n, 0) (d' == -1)))
<> prop "quotRemNonZero: Prelude's at Word8" (property (do n <- forAll (Gen.word8 Range.linearBounded); d <- forAll (Gen.just (preview nonZero <$> Gen.word8 Range.linearBounded)); quotRemNonZero n d === U.quotRem n (the divisor d)))
<> prop "quotRemNonZero: Prelude's at Natural" (property (do n <- forAll genNatural; d <- forAll (Gen.just (preview nonZero <$> genNatural)); quotRemNonZero n d === U.quotRem n (the divisor d)))
<> prop "quotNonZero, remNonZero: the components of quotRemNonZero" (property (do n <- forAll genInt; d <- forAll genNonZeroInt; (quotNonZero n d, remNonZero n d) === quotRemNonZero n d))
<> prop "pow: x ^ (e + 1) == x ^ e * x" (property (do x <- forAll genInt; e <- forAll genSmallWord; pow x (e + 1) === pow x e * x))
<> prop "pow: Prelude's" (property (do x <- forAll genInt; e <- forAll genSmallWord; pow x e === (U.^) x e))
-- | A generator that also gives 0 often, for the smart constructor.
withZero :: (Num a) => Gen a -> Gen a
withZero g =
Gen.frequency [(1, pure 0), (4, g)]
genNatural :: Gen Natural
genNatural =
Gen.integral (Range.linear 0 1000)
genSmallWord :: Gen Word
genSmallWord =
Gen.integral (Range.linear 0 6)
-- Papa.Base.Data.NonZero: the classy optics laws (rule 13)
-- | Non-zero values, built through the smart constructor, with -1 often,
-- for the special case of division.
genNonZeroInt :: Gen (NonZero Int)
genNonZeroInt =
Gen.just (preview nonZero <$> Gen.frequency [(1, pure (-1)), (9, genInt)])
genNonZeroRational :: Gen (NonZero Rational)
genNonZeroRational =
Gen.just (preview nonZero <$> genRational)
-- | The prism laws of the smart constructor at a numeric type.
prismNonZero ::
forall a.
(Eq a, Num a, Show a, NonZeroLike (->) (Const (Mo.First (NonZero a))) a a a a, NonZeroLike (->) (Const Mo.Any) a a a a, NonZeroLike Tagged Identity a a a a) =>
PropertyName ->
Gen a ->
[(PropertyName, Property)]
prismNonZero name genA =
prop ("prism nonZero (" <> name <> "): preview (review x) == Just x") (property (do x <- forAll (Gen.just (preview nonZero <$> genA)); preview nonZero (review nonZero x :: a) === Just x))
<> prop ("prism nonZero (" <> name <> "): preview s == Just x implies review x == s") (property (do s <- forAll genA; for_ (preview nonZero s) (\x -> review nonZero x === s)))
<> prop ("prism nonZero (" <> name <> "): Just exactly where not zero") (property (do s <- forAll genA; has nonZero s === (s /= 0)))
nonZeros :: [(PropertyName, Property)]
nonZeros =
prop "identity nonZero" (property (do s <- forAll genNonZeroInt; t <- forAll genNonZeroInt; (the nonZero s, set nonZero t s, review nonZero s) === (s, t, s)))
<> prismNonZero "Int" (withZero genInt)
<> prismNonZero "Int8" (withZero (Gen.int8 Range.linearBounded))
<> prismNonZero "Int16" (withZero (Gen.int16 Range.linearBounded))
<> prismNonZero "Int32" (withZero (Gen.int32 Range.linearBounded))
<> prismNonZero "Int64" (withZero (Gen.int64 Range.linearBounded))
<> prismNonZero "Integer" (withZero genInteger)
<> prismNonZero "Word" (withZero (Gen.word Range.linearBounded))
<> prismNonZero "Word8" (withZero (Gen.word8 Range.linearBounded))
<> prismNonZero "Word16" (withZero (Gen.word16 Range.linearBounded))
<> prismNonZero "Word32" (withZero (Gen.word32 Range.linearBounded))
<> prismNonZero "Word64" (withZero (Gen.word64 Range.linearBounded))
<> prismNonZero "Natural" (withZero genNatural)
<> prismNonZero "Float" (withZero (Gen.float (Range.linearFracFrom 0 (-100) 100)))
<> prismNonZero "Double" (withZero (Gen.double (Range.linearFracFrom 0 (-100) 100)))
<> prismNonZero "Rational" (withZero genRational)
<> prop "Eq1 NonZero: liftEq (==) == (==)" (property (do x <- forAll genNonZeroInt; y <- forAll genNonZeroInt; liftEq (==) x y === (x == y)))
<> prop "Ord1 NonZero: liftCompare compare == compare" (property (do x <- forAll genNonZeroInt; y <- forAll genNonZeroInt; liftCompare compare x y === compare x y))
<> prop "Show1 NonZero: liftShowsPrec showsPrec showList == showsPrec" (property (do x <- forAll genNonZeroInt; d <- forAll (Gen.int (Range.linear 0 11)); liftShowsPrec showsPrec showList d x "" === showsPrec d x ""))
<> prop "Foldable NonZero: the one value" (property (do x <- forAll genNonZeroInt; (toList x, length x, null x, sum x, foldr (:) [] x) === ([the divisor x], 1, False, the divisor x, [the divisor x])))
<> prop "Foldable NonZero: foldMap == foldr" (property (do x <- forAll genNonZeroInt; f <- forAll (genFn genString); foldMap (apply f) x === foldr ((<>) . apply f) mempty x))
<> prop "Foldable1 NonZero: toNonEmpty, foldMap1 agree with Foldable" (property (do x <- forAll genNonZeroInt; f <- forAll (genFn genString); (toNonEmpty x, foldMap1 (apply f) x) === (the divisor x :| [], foldMap (apply f) x)))
<> prop "getter divisor" (property (do s <- forAll genNonZeroInt; toListOf divisor s === [the divisor s]))
<> prop "lawNamesNonZero == Just" (property (do s <- forAll genNonZeroInt; lawNamesNonZero s === Just s))
<> prop "lawNamesNonZero == Just (Rational)" (property (do s <- forAll genNonZeroRational; lawNamesNonZero s === Just s))
<> prop "validNonZero" (property (do n <- forAll genInt; validNonZero n === (n /= 0)))
-- Papa.Base.Data.IntCast: every lossless pair of the table, over the whole
-- range of its source type
intCasts :: [(PropertyName, Property)]
intCasts =
prop "intCast Int8 -> Int16: the value is unchanged" (property (do x <- forAll (Gen.int8 Range.linearBounded); toInteger (intCast x :: Int16) === toInteger x))
<> prop "intCast Int8 -> Int32: the value is unchanged" (property (do x <- forAll (Gen.int8 Range.linearBounded); toInteger (intCast x :: Int32) === toInteger x))
<> prop "intCast Int8 -> Int: the value is unchanged" (property (do x <- forAll (Gen.int8 Range.linearBounded); toInteger (intCast x :: Int) === toInteger x))
<> prop "intCast Int8 -> Int64: the value is unchanged" (property (do x <- forAll (Gen.int8 Range.linearBounded); toInteger (intCast x :: Int64) === toInteger x))
<> prop "intCast Int8 -> Integer: the value is unchanged" (property (do x <- forAll (Gen.int8 Range.linearBounded); toInteger (intCast x :: Integer) === toInteger x))
<> prop "intCast Int16 -> Int32: the value is unchanged" (property (do x <- forAll (Gen.int16 Range.linearBounded); toInteger (intCast x :: Int32) === toInteger x))
<> prop "intCast Int16 -> Int: the value is unchanged" (property (do x <- forAll (Gen.int16 Range.linearBounded); toInteger (intCast x :: Int) === toInteger x))
<> prop "intCast Int16 -> Int64: the value is unchanged" (property (do x <- forAll (Gen.int16 Range.linearBounded); toInteger (intCast x :: Int64) === toInteger x))
<> prop "intCast Int16 -> Integer: the value is unchanged" (property (do x <- forAll (Gen.int16 Range.linearBounded); toInteger (intCast x :: Integer) === toInteger x))
<> prop "intCast Int32 -> Int: the value is unchanged" (property (do x <- forAll (Gen.int32 Range.linearBounded); toInteger (intCast x :: Int) === toInteger x))
<> prop "intCast Int32 -> Int64: the value is unchanged" (property (do x <- forAll (Gen.int32 Range.linearBounded); toInteger (intCast x :: Int64) === toInteger x))
<> prop "intCast Int32 -> Integer: the value is unchanged" (property (do x <- forAll (Gen.int32 Range.linearBounded); toInteger (intCast x :: Integer) === toInteger x))
<> prop "intCast Int -> Int64: the value is unchanged" (property (do x <- forAll (Gen.int Range.linearBounded); toInteger (intCast x :: Int64) === toInteger x))
<> prop "intCast Int -> Integer: the value is unchanged" (property (do x <- forAll (Gen.int Range.linearBounded); toInteger (intCast x :: Integer) === toInteger x))
<> prop "intCast Int64 -> Integer: the value is unchanged" (property (do x <- forAll (Gen.int64 Range.linearBounded); toInteger (intCast x :: Integer) === toInteger x))
<> prop "intCast Word8 -> Word16: the value is unchanged" (property (do x <- forAll (Gen.word8 Range.linearBounded); toInteger (intCast x :: Word16) === toInteger x))
<> prop "intCast Word8 -> Word32: the value is unchanged" (property (do x <- forAll (Gen.word8 Range.linearBounded); toInteger (intCast x :: Word32) === toInteger x))
<> prop "intCast Word8 -> Word: the value is unchanged" (property (do x <- forAll (Gen.word8 Range.linearBounded); toInteger (intCast x :: Word) === toInteger x))
<> prop "intCast Word8 -> Word64: the value is unchanged" (property (do x <- forAll (Gen.word8 Range.linearBounded); toInteger (intCast x :: Word64) === toInteger x))
<> prop "intCast Word8 -> Natural: the value is unchanged" (property (do x <- forAll (Gen.word8 Range.linearBounded); toInteger (intCast x :: Natural) === toInteger x))
<> prop "intCast Word8 -> Integer: the value is unchanged" (property (do x <- forAll (Gen.word8 Range.linearBounded); toInteger (intCast x :: Integer) === toInteger x))
<> prop "intCast Word16 -> Word32: the value is unchanged" (property (do x <- forAll (Gen.word16 Range.linearBounded); toInteger (intCast x :: Word32) === toInteger x))
<> prop "intCast Word16 -> Word: the value is unchanged" (property (do x <- forAll (Gen.word16 Range.linearBounded); toInteger (intCast x :: Word) === toInteger x))
<> prop "intCast Word16 -> Word64: the value is unchanged" (property (do x <- forAll (Gen.word16 Range.linearBounded); toInteger (intCast x :: Word64) === toInteger x))
<> prop "intCast Word16 -> Natural: the value is unchanged" (property (do x <- forAll (Gen.word16 Range.linearBounded); toInteger (intCast x :: Natural) === toInteger x))
<> prop "intCast Word16 -> Integer: the value is unchanged" (property (do x <- forAll (Gen.word16 Range.linearBounded); toInteger (intCast x :: Integer) === toInteger x))
<> prop "intCast Word32 -> Word: the value is unchanged" (property (do x <- forAll (Gen.word32 Range.linearBounded); toInteger (intCast x :: Word) === toInteger x))
<> prop "intCast Word32 -> Word64: the value is unchanged" (property (do x <- forAll (Gen.word32 Range.linearBounded); toInteger (intCast x :: Word64) === toInteger x))
<> prop "intCast Word32 -> Natural: the value is unchanged" (property (do x <- forAll (Gen.word32 Range.linearBounded); toInteger (intCast x :: Natural) === toInteger x))
<> prop "intCast Word32 -> Integer: the value is unchanged" (property (do x <- forAll (Gen.word32 Range.linearBounded); toInteger (intCast x :: Integer) === toInteger x))
<> prop "intCast Word -> Word64: the value is unchanged" (property (do x <- forAll (Gen.word Range.linearBounded); toInteger (intCast x :: Word64) === toInteger x))
<> prop "intCast Word -> Natural: the value is unchanged" (property (do x <- forAll (Gen.word Range.linearBounded); toInteger (intCast x :: Natural) === toInteger x))
<> prop "intCast Word -> Integer: the value is unchanged" (property (do x <- forAll (Gen.word Range.linearBounded); toInteger (intCast x :: Integer) === toInteger x))
<> prop "intCast Word64 -> Natural: the value is unchanged" (property (do x <- forAll (Gen.word64 Range.linearBounded); toInteger (intCast x :: Natural) === toInteger x))
<> prop "intCast Word64 -> Integer: the value is unchanged" (property (do x <- forAll (Gen.word64 Range.linearBounded); toInteger (intCast x :: Integer) === toInteger x))
<> prop "intCast Natural -> Integer: the value is unchanged" (property (do x <- forAll ((Gen.integral (Range.linear 0 (pow 2 70)) :: Gen Natural)); toInteger (intCast x :: Integer) === toInteger x))
<> prop "intCast Word8 -> Int16: the value is unchanged" (property (do x <- forAll (Gen.word8 Range.linearBounded); toInteger (intCast x :: Int16) === toInteger x))
<> prop "intCast Word8 -> Int32: the value is unchanged" (property (do x <- forAll (Gen.word8 Range.linearBounded); toInteger (intCast x :: Int32) === toInteger x))
<> prop "intCast Word8 -> Int: the value is unchanged" (property (do x <- forAll (Gen.word8 Range.linearBounded); toInteger (intCast x :: Int) === toInteger x))
<> prop "intCast Word8 -> Int64: the value is unchanged" (property (do x <- forAll (Gen.word8 Range.linearBounded); toInteger (intCast x :: Int64) === toInteger x))
<> prop "intCast Word16 -> Int32: the value is unchanged" (property (do x <- forAll (Gen.word16 Range.linearBounded); toInteger (intCast x :: Int32) === toInteger x))
<> prop "intCast Word16 -> Int: the value is unchanged" (property (do x <- forAll (Gen.word16 Range.linearBounded); toInteger (intCast x :: Int) === toInteger x))
<> prop "intCast Word16 -> Int64: the value is unchanged" (property (do x <- forAll (Gen.word16 Range.linearBounded); toInteger (intCast x :: Int64) === toInteger x))
<> prop "intCast Word32 -> Int64: the value is unchanged" (property (do x <- forAll (Gen.word32 Range.linearBounded); toInteger (intCast x :: Int64) === toInteger x))
-- Papa.Base.Data.Enum, Papa.Base.Data.Char
enums :: [(PropertyName, Property)]
enums =
prop "succ: Integer, unbounded" (property (do n <- forAll genInteger; succ n === Just (n + 1)))
<> prop "succ: Natural, unbounded" (property (do n <- forAll genNatural; succ n === Just (n + 1)))
<> prop "succ: Nothing at maxBound, else Prelude's" (property (do n <- forAll (Gen.int Range.linearBounded); succ n === bool (Just (P.succ n)) Nothing (n == maxBound)))
<> prop "pred: Nothing at minBound, else Prelude's" (property (do n <- forAll (Gen.int Range.linearBounded); pred n === bool (Just (P.pred n)) Nothing (n == minBound)))
<> prop "succ >=> pred" (property (do n <- forAll (Gen.int Range.linearBounded); (succ >=> pred) n === bool (Just n) Nothing (n == maxBound)))
<> prop "chr . ord == Just" (property (do c <- forAll Gen.unicodeAll; chr (ord c) === Just c))
<> prop "chr: Nothing out of range" (property (do n <- forAll (Gen.choice [Gen.int (Range.linear (-1000) (-1)), Gen.int (Range.linear 0x110000 0x200000)]); chr n === Nothing))
-- Papa.Base.Data.Ratio, Papa.Base.Text.Read
numeric :: [(PropertyName, Property)]
numeric =
prop "ratioNonZero: Data.Ratio's" (property (do n <- forAll genInteger; d <- forAll (Gen.just (preview nonZero <$> genInteger)); ratioNonZero n d === (U.%) n (the divisor d)))
<> prop "readIO . show == pure . Just" (property (do n <- forAll genInt; r <- evalIO (readIO (display n)); r === Just n))
<> prop "readIO: Nothing for a non-number" (property (do r <- evalIO (readIO "x" :: IO (Maybe Int)); r === Nothing))
-- Papa.Base.Data.Semigroup
semigroup :: [(PropertyName, Property)]
semigroup =
prop "stimes: agrees with Data.Semigroup for n > 0" (property (do s <- forAll genString; n <- forAll (Gen.int (Range.linear 1 10)); stimes n s === Just (S.stimes n s)))
<> prop "stimes: Nothing for n <= 0" (property (do s <- forAll genString; n <- forAll (Gen.int (Range.linear (-10) 0)); stimes n s === Nothing))
<> prop "stimesMonoid: agrees for n >= 0, Nothing below" (property (do s <- forAll genString; n <- forAll (Gen.int (Range.linear (-10) 10)); stimesMonoid n s === bool (Just (S.stimesMonoid n s)) Nothing (n < 0)))
<> prop "stimesIdempotent: the value for n > 0, Nothing otherwise" (property (do a <- forAll genInt; n <- forAll (Gen.int (Range.linear (-10) 10)); stimesIdempotent n (Max a) === bool (Just (Max a)) Nothing (n <= 0)))
<> prop "stimesIdempotentMonoid: agrees for n >= 0, Nothing below" (property (do b <- forAll Gen.bool; n <- forAll (Gen.int (Range.linear (-10) 10)); stimesIdempotentMonoid n (Any b) === bool (Just (S.stimesIdempotentMonoid n (Any b))) Nothing (n < 0)))
<> prop "mtimesDefault == stimesMonoid" (property (do s <- forAll genString; n <- forAll (Gen.int (Range.linear (-10) 10)); mtimesDefault n s === stimesMonoid n s))
-- Papa.Base.Data.List
list :: [(PropertyName, Property)]
list =
prop "cycle: Nothing for [], else repeats" (property (do xs <- forAll genInts; k <- forAll (Gen.int (Range.linear 0 3)); fmap (take (k * length xs)) (cycle xs) === (P.concat (replicate k xs) <$ preview _head xs)))
<> prop "maximum: Foldable1, the largest" (property (do xs <- forAll genNonEmpty; Just (maximum xs) === maximumOf folded xs; all (<= maximum xs) xs === True))
<> prop "minimum: Foldable1, the smallest" (property (do xs <- forAll genNonEmpty; Just (minimum xs) === minimumOf folded xs; all (>= minimum xs) xs === True))
<> prop "lookup: agrees with Data.List" (property (do k <- forAll (Gen.int (Range.linear 0 5)); kvs <- forAll (Gen.list (Range.linear 0 10) ((,) <$> Gen.int (Range.linear 0 5) <*> Gen.alpha)); lookup k kvs === L.lookup k kvs))
<> prop "(!?): agrees with ix" (property (do xs <- forAll genInts; i <- forAll genSmallInt; (xs !? i) === preview (ix i) xs))
<> prop "(++): associative" (property (do xs <- forAll genInts; ys <- forAll genInts; zs <- forAll genInts; (xs ++ ys) ++ zs === xs ++ (ys ++ zs)))
<> prop "(++): Data.List's at lists" (property (do xs <- forAll genInts; ys <- forAll genInts; xs ++ ys === (L.++) xs ys))
<> prop "(++): infixr 5" (property (do x <- forAll genInt; xs <- forAll genInts; ys <- forAll genInts; (x `elem` xs ++ ys) === elem x (xs <> ys)))
<> prop "maximumBy: Foldable1, total" (property (do xs <- forAll genNonEmpty; Just (maximumBy (comparing negate) xs) === minimumOf folded xs))
<> prop "minimumBy: Foldable1, total" (property (do xs <- forAll genNonEmpty; Just (minimumBy (comparing negate) xs) === maximumOf folded xs))
-- Papa.Base.Data.Foldable, Papa.Base.Data.Monoid
foldable :: [(PropertyName, Property)]
foldable =
prop "mapM_ == traverse_" (property (do xs <- forAll genInts; f <- forAll (genFn genString); runWriter (mapM_ (tell . apply f) xs) === runWriter (traverse_ (tell . apply f) xs)))
<> prop "forM_ == for_" (property (do xs <- forAll genInts; f <- forAll (genFn genString); runWriter (forM_ xs (tell . apply f)) === runWriter (for_ xs (tell . apply f))))
<> prop "sequence_ == sequenceA_" (property (do ss <- forAll (Gen.list (Range.linear 0 10) genString); runWriter (sequence_ (fmap tell ss)) === runWriter (sequenceA_ (fmap tell ss))))
<> prop "concat: Foldable of lists" (property (do xss <- forAll (Gen.list (Range.linear 0 5) genInts); concat xss === foldr (<>) [] xss; concat (Just (P.concat xss)) === P.concat xss))
<> prop "concatMap: Foldable" (property (do xs <- forAll genNonEmpty; f <- forAll (genFn genInts); concatMap (apply f) xs === foldMap (apply f) xs))
<> prop "mconcat == foldr (<>) mempty" (property (do ss <- forAll (Gen.list (Range.linear 0 10) genString); mconcat ss === foldr (<>) mempty ss))
-- Papa.Base.Data.Traversable
traversable :: [(PropertyName, Property)]
traversable =
prop "mapM == traverse" (property (do xs <- forAll genInts; f <- forAll (genFn (Gen.maybe genInt)); mapM (apply f) xs === traverse (apply f) xs))
<> prop "forM == for" (property (do xs <- forAll genInts; f <- forAll (genFn (Gen.maybe genInt)); forM xs (apply f) === for xs (apply f)))
<> prop "sequence == sequenceA" (property (do xs <- forAll (Gen.list (Range.linear 0 10) (Gen.maybe genInt)); sequence xs === sequenceA xs))
-- Papa.Base.Data.Function, Papa.Base.Data.Functor, Papa.Base.Control.Applicative
functions :: [(PropertyName, Property)]
functions =
prop "flip: Prelude's at functions" (property (do f <- forAll (genFn (genFn genInt)); a <- forAll genInt; b <- forAll genInt; flip (apply . apply f) a b === P.flip (apply . apply f) a b))
<> prop "flip: fmap of application" (property (do fs <- forAll (Gen.list (Range.linear 0 5) (genFn genInt)); a <- forAll genInt; flip (fmap apply fs) a === fmap (`apply` a) fs))
<> prop "map: Functor identity" (property (do xs <- forAll genInts; map id xs === xs))
<> prop "map: Functor composition" (property (do xs <- forAll genInts; f <- forAll (genFn genInt); g <- forAll (genFn genInt); map (apply f . apply g) xs === map (apply f) (map (apply g) xs)))
<> prop "const: Prelude's at functions" (property (do a <- forAll genInt; b <- forAll Gen.alpha; const a b === P.const a b))
<> prop "const == pure" (property (do a <- forAll genInt; (const a :: Maybe Int) === pure a))
<> prop "replicateA: replicate in Maybe" (property (do n <- forAll (Gen.int (Range.linear (-5) 10)); a <- forAll genInt; replicateA n (Just a) === Just (replicate n a)))
<> prop "replicateA_: void of replicateA" (property (do n <- forAll (Gen.int (Range.linear (-5) 6)); replicateA_ n ("ab" :: String) === void (replicateA n ("ab" :: String))))
-- Papa.Base.Control.Monad
monad :: [(PropertyName, Property)]
monad =
prop "anyM == any" (property (do xs <- forAll genInts; p <- forAll (genFn Gen.bool); anyM (Just . apply p) xs === Just (any (apply p) xs)))
<> prop "allM == all" (property (do xs <- forAll genInts; p <- forAll (genFn Gen.bool); allM (Just . apply p) xs === Just (all (apply p) xs)))
<> prop "findM == find" (property (do xs <- forAll genInts; p <- forAll (genFn Gen.bool); findM (Just . apply p) xs === Just (find (apply p) xs)))
<> prop "anyM: stops at the first True" (property (do xs <- forAll genInts; p <- forAll (genFn Gen.bool); runWriter (anyM (\x -> apply p x <$ tell [x]) xs) === (any (apply p) xs, takeThrough (apply p) xs)))
<> prop "allM: stops at the first False" (property (do xs <- forAll genInts; p <- forAll (genFn Gen.bool); runWriter (allM (\x -> apply p x <$ tell [x]) xs) === (all (apply p) xs, takeThrough (not . apply p) xs)))
<> prop "findM: stops at the first True" (property (do xs <- forAll genInts; p <- forAll (genFn Gen.bool); runWriter (findM (\x -> apply p x <$ tell [x]) xs) === (find (apply p) xs, takeThrough (apply p) xs)))
-- | The elements up to and including the first that satisfies the
-- predicate.
takeThrough :: (a -> Bool) -> [a] -> [a]
takeThrough p xs =
let (as, bs) = break p xs
in as <> take 1 bs
-- Papa.Base.Data.Either
eithers :: [(PropertyName, Property)]
eithers =
prop "lefts: the Left values" (property (do xs <- forAll genEithers; toListOf lefts xs === [a | Left a <- xs]))
<> prop "rights: the Right values" (property (do xs <- forAll genEithers; toListOf rights xs === [b | Right b <- xs]))
<> prop "prism left: preview (review x) == Just x" (property (do x <- forAll genInt; preview left (review left x :: Either Int Char) === Just x))
<> prop "prism left: preview s == Just x implies review x == s" (property (do e <- forAll genEither; for_ (preview left e) (\x -> review left x === e)))
<> prop "prism right: preview (review x) == Just x" (property (do x <- forAll Gen.alpha; preview right (review right x :: Either Int Char) === Just x))
<> prop "prism right: preview s == Just x implies review x == s" (property (do e <- forAll genEither; for_ (preview right e) (\x -> review right x === e)))
<> prop "fromLeft: Data.Either's" (property (do d <- forAll genInt; e <- forAll genEither; fromLeft d e === P.either id (P.const d) e))
<> prop "fromRight: Data.Either's" (property (do d <- forAll Gen.alpha; e <- forAll genEither; fromRight d e === P.either (P.const d) id e))
<> prop "partitionEithers: lefts and rights" (property (do xs <- forAll genEithers; partitionEithers xs === (toListOf lefts xs, toListOf rights xs)))
-- Papa.Base.Data.Fractional, toIntegralSized
genRational :: Gen Rational
genRational =
Gen.realFrac_ (Range.linearFracFrom 0 (-100) 100)
fractional :: [(PropertyName, Property)]
fractional =
prop "divideNonZero: Prelude's" (property (do x <- forAll genRational; y <- forAll genNonZeroRational; divideNonZero x y === (U./) x (the divisor y)))
<> prop "divideNonZero: (x / y) * y == x" (property (do x <- forAll genRational; y <- forAll genNonZeroRational; divideNonZero x y * the divisor y === x))
<> prop "recipNonZero == divideNonZero 1" (property (do x <- forAll genNonZeroRational; recipNonZero x === divideNonZero 1 x))
<> prop "powNonZero: Prelude's" (property (do x <- forAll genNonZeroRational; e <- forAll (Gen.int (Range.linear (-5) 5)); powNonZero x e === (U.^^) (the divisor x) e))
<> prop "toIntegralSized: Just in range, Nothing outside" (property (do n <- forAll (Gen.int (Range.linear (-300) 300)); (toIntegralSized n :: Maybe Word8) === bool Nothing (Just (P.fromIntegral n)) (0 <= n && n <= 255)))
<> prop "toIntegralSized: to Natural" (property (do n <- forAll genInteger; (toIntegralSized n :: Maybe Natural) === bool Nothing (Just (P.fromInteger n)) (n >= 0)))
-- Papa.Base.System.Exit
exits :: [(PropertyName, Property)]
exits =
prop "exitWith: Nothing exactly for ExitFailure 0" (property (do n <- forAll (Gen.int (Range.linearFrom 0 (-255) 255)); has _Just (exitWith (ExitFailure n) :: Maybe (IO ())) === (n /= 0)))
<> prop "exitWith: Just for ExitSuccess" (property (has _Just (exitWith ExitSuccess :: Maybe (IO ())) === True))
-- Papa.Containers
genMap :: Gen (Map.Map Int Char)
genMap =
Map.fromList <$> Gen.list (Range.linear 0 20) ((,) <$> Gen.int (Range.linear 0 30) <*> Gen.alpha)
genSet :: Gen (Set.Set Int)
genSet =
Set.fromList <$> Gen.list (Range.linear 0 20) (Gen.int (Range.linear 0 30))
genIntMap :: Gen (IntMap.IntMap Char)
genIntMap =
IntMap.fromList <$> Gen.list (Range.linear 0 20) ((,) <$> Gen.int (Range.linear (-30) 30) <*> Gen.alpha)
genIntSet :: Gen IntSet.IntSet
genIntSet =
IntSet.fromList <$> Gen.list (Range.linear 0 20) (Gen.int (Range.linear (-30) 30))
genSeq :: Gen (Seq.Seq Int)
genSeq =
Seq.fromList <$> genInts
-- | Lists of pairs in ascending, descending, strictly ascending and any
-- order of keys, with keys in a small range so that some are equal.
genKVs :: Gen [(Int, Char)]
genKVs =
let kvs = Gen.list (Range.linear 0 10) ((,) <$> Gen.int (Range.linear 0 9) <*> Gen.alpha)
in Gen.choice [L.sortOn fst <$> kvs, L.sortOn (Down . fst) <$> kvs, L.nubBy (\a b -> fst a == fst b) . L.sortOn fst <$> kvs, kvs]
-- | Lists in ascending, descending, strictly ascending and any order, in a
-- small range so that some are equal.
genXs :: Gen [Int]
genXs =
let xs = Gen.list (Range.linear 0 10) (Gen.int (Range.linear 0 9))
in Gen.choice [L.sort <$> xs, L.sortOn Down <$> xs, L.nub . L.sort <$> xs, xs]
-- | A list action with two results.
ab :: String
ab =
"ab"
-- | Whether an index is in range of a structure of a size.
inRange :: Int -> Int -> Bool
inRange n i =
0 <= i && i < n
containers :: [(PropertyName, Property)]
containers =
prop "Map.!: lookup" (property (do m <- forAll genMap; k <- forAll (Gen.int (Range.linear 0 30)); (m Map.! k) === M.lookup k m))
<> prop "Map.findMin, findMax: lookupMin, lookupMax" (property (do m <- forAll genMap; (Map.findMin m, Map.findMax m) === (M.lookupMin m, M.lookupMax m)))
<> prop "Map.deleteFindMin, deleteFindMax: base's where non-empty" (property (do m <- forAll genMap; (Map.deleteFindMin m, Map.deleteFindMax m) === bool (Just (M.deleteFindMin m), Just (M.deleteFindMax m)) (Nothing, Nothing) (M.null m)))
<> prop "Map.elemAt, deleteAt, updateAt: base's in range" (property (do m <- forAll genMap; i <- forAll genSmallInt; (Map.elemAt i m, Map.deleteAt i m, Map.updateAt (\_ _ -> Just 'z') i m) === bool (Nothing, Nothing, Nothing) (Just (M.elemAt i m), Just (M.deleteAt i m), Just (M.updateAt (\_ _ -> Just 'z') i m)) (inRange (M.size m) i)))
<> prop "Map.findIndex: lookupIndex" (property (do m <- forAll genMap; k <- forAll (Gen.int (Range.linear 0 30)); Map.findIndex k m === M.lookupIndex k m))
<> prop "Set.findMin, findMax: lookupMin, lookupMax" (property (do s <- forAll genSet; (Set.findMin s, Set.findMax s) === (St.lookupMin s, St.lookupMax s)))
<> prop "Set.deleteFindMin, deleteFindMax: base's where non-empty" (property (do s <- forAll genSet; (Set.deleteFindMin s, Set.deleteFindMax s) === bool (Just (St.deleteFindMin s), Just (St.deleteFindMax s)) (Nothing, Nothing) (St.null s)))
<> prop "Set.elemAt, deleteAt: base's in range" (property (do s <- forAll genSet; i <- forAll genSmallInt; (Set.elemAt i s, Set.deleteAt i s) === bool (Nothing, Nothing) (Just (St.elemAt i s), Just (St.deleteAt i s)) (inRange (St.size s) i)))
<> prop "Set.findIndex: lookupIndex" (property (do s <- forAll genSet; a <- forAll (Gen.int (Range.linear 0 30)); Set.findIndex a s === St.lookupIndex a s))
<> prop "IntMap.!, findMin, findMax: lookup, lookupMin, lookupMax" (property (do m <- forAll genIntMap; k <- forAll (Gen.int (Range.linear (-30) 30)); (m IntMap.! k, IntMap.findMin m, IntMap.findMax m) === (IM.lookup k m, IM.lookupMin m, IM.lookupMax m)))
<> prop "IntMap.deleteFindMin, deleteFindMax: base's where non-empty" (property (do m <- forAll genIntMap; (IntMap.deleteFindMin m, IntMap.deleteFindMax m) === bool (Just (IM.deleteFindMin m), Just (IM.deleteFindMax m)) (Nothing, Nothing) (IM.null m)))
<> prop "IntSet: base's where non-empty" (property (do s <- forAll genIntSet; (IntSet.findMin s, IntSet.findMax s, IntSet.deleteFindMin s, IntSet.deleteFindMax s) === bool (Just (IS.findMin s), Just (IS.findMax s), Just (IS.deleteFindMin s), Just (IS.deleteFindMax s)) (Nothing, Nothing, Nothing, Nothing) (IS.null s)))
<> prop "Seq.index: lookup" (property (do s <- forAll genSeq; i <- forAll genSmallInt; Seq.index s i === Sq.lookup i s))
<> prop "Seq.replicate, iterateN, fromFunction: base's for n >= 0" (property (do n <- forAll (Gen.int (Range.linear (-5) 20)); a <- forAll genInt; f <- forAll (genFn genInt); (Seq.replicate n a, Seq.iterateN n (apply f) a, Seq.fromFunction n (apply f)) === bool (Nothing, Nothing, Nothing) (Just (Sq.replicate n a), Just (Sq.iterateN n (apply f) a), Just (Sq.fromFunction n (apply f))) (n >= 0)))
<> prop "Seq.replicateA, replicateM: base's for n >= 0" (property (do n <- forAll (Gen.int (Range.linear (-3) 4)); (Seq.replicateA n ab, Seq.replicateM n ab) === bool (Nothing, Nothing) (Just (Sq.replicateA n ab), Just (Sq.replicateM n ab)) (n >= 0)))
<> prop "Seq.chunksOf: base's for n > 0 or empty" (property (do n <- forAll (Gen.int (Range.linear (-3) 6)); s <- forAll genSeq; Seq.chunksOf n s === bool Nothing (Just (Sq.chunksOf n s)) (n > 0 || Sq.null s)))
<> prop "Seq.cycleTaking: base's for n <= 0 or non-empty" (property (do n <- forAll (Gen.int (Range.linear (-3) 30)); s <- forAll genSeq; Seq.cycleTaking n s === bool Nothing (Just (Sq.cycleTaking n s)) (n <= 0 || not (Sq.null s))))
<> prop "Seq.scanl1, scanr1: base's where non-empty" (property (do s <- forAll genSeq; (Seq.scanl1 (+) s, Seq.scanr1 (+) s) === bool (Just (Sq.scanl1 (+) s), Just (Sq.scanr1 (+) s)) (Nothing, Nothing) (Sq.null s)))
<> prop "Map.fromAscList: fromList where ascending, else Nothing" (property (do kvs <- forAll genKVs; Map.fromAscList kvs === bool Nothing (Just (Map.fromList kvs)) (L.sortOn fst kvs == kvs)))
<> prop "Map.fromAscListWith: fromListWith where ascending, else Nothing" (property (do kvs <- forAll genKVs; let { kss = fmap (fmap (: [])) kvs }; Map.fromAscListWith (<>) kss === bool Nothing (Just (Map.fromListWith (<>) kss)) (L.sortOn fst kvs == kvs)))
<> prop "Map.fromAscListWithKey: fromListWithKey where ascending, else Nothing" (property (do kvs <- forAll genKVs; let { kss = fmap (fmap (: [])) kvs; f k a b = show k <> a <> b }; Map.fromAscListWithKey f kss === bool Nothing (Just (Map.fromListWithKey f kss)) (L.sortOn fst kvs == kvs)))
<> prop "Map.fromDistinctAscList: fromList where strictly ascending, else Nothing" (property (do kvs <- forAll genKVs; Map.fromDistinctAscList kvs === bool Nothing (Just (Map.fromList kvs)) (L.sortOn fst kvs == kvs && L.nub (fmap fst kvs) == fmap fst kvs)))
<> prop "Map.fromDescList: fromList where descending, else Nothing" (property (do kvs <- forAll genKVs; Map.fromDescList kvs === bool Nothing (Just (Map.fromList kvs)) (L.sortOn (Down . fst) kvs == kvs)))
<> prop "Map.mapKeysMonotonic: mapKeys where strictly increasing, else Nothing" (property (do m <- forAll (fmap Map.fromList genKVs); c <- forAll (Gen.int (Range.linear (-2) 2)); Map.mapKeysMonotonic (* c) m === bool Nothing (Just (Map.mapKeys (* c) m)) (c > 0 || Map.size m <= 1)))
<> prop "IntMap.fromAscList: fromList where ascending, else Nothing" (property (do kvs <- forAll genKVs; IntMap.fromAscList kvs === bool Nothing (Just (IntMap.fromList kvs)) (L.sortOn fst kvs == kvs)))
<> prop "IntMap.fromAscListWith: fromListWith where ascending, else Nothing" (property (do kvs <- forAll genKVs; let { kss = fmap (fmap (: [])) kvs }; IntMap.fromAscListWith (<>) kss === bool Nothing (Just (IntMap.fromListWith (<>) kss)) (L.sortOn fst kvs == kvs)))
<> prop "IntMap.fromAscListWithKey: fromListWithKey where ascending, else Nothing" (property (do kvs <- forAll genKVs; let { kss = fmap (fmap (: [])) kvs; f k a b = show k <> a <> b }; IntMap.fromAscListWithKey f kss === bool Nothing (Just (IntMap.fromListWithKey f kss)) (L.sortOn fst kvs == kvs)))
<> prop "IntMap.fromDistinctAscList: fromList where strictly ascending, else Nothing" (property (do kvs <- forAll genKVs; IntMap.fromDistinctAscList kvs === bool Nothing (Just (IntMap.fromList kvs)) (L.sortOn fst kvs == kvs && L.nub (fmap fst kvs) == fmap fst kvs)))
<> prop "IntMap.fromDescList: fromList where descending, else Nothing" (property (do kvs <- forAll genKVs; IntMap.fromDescList kvs === bool Nothing (Just (IntMap.fromList kvs)) (L.sortOn (Down . fst) kvs == kvs)))
<> prop "IntMap.mapKeysMonotonic: mapKeys where strictly increasing, else Nothing" (property (do m <- forAll (fmap IntMap.fromList genKVs); c <- forAll (Gen.int (Range.linear (-2) 2)); IntMap.mapKeysMonotonic (* c) m === bool Nothing (Just (IntMap.mapKeys (* c) m)) (c > 0 || IntMap.size m <= 1)))
<> prop "Map.fromDescListWith: fromListWith where descending, else Nothing" (property (do kvs <- forAll genKVs; let { kss = fmap (fmap (: [])) kvs }; Map.fromDescListWith (<>) kss === bool Nothing (Just (Map.fromListWith (<>) kss)) (L.sortOn (Down . fst) kvs == kvs)))
<> prop "Map.fromDescListWithKey: fromListWithKey where descending, else Nothing" (property (do kvs <- forAll genKVs; let { kss = fmap (fmap (: [])) kvs; f k a b = show k <> a <> b }; Map.fromDescListWithKey f kss === bool Nothing (Just (Map.fromListWithKey f kss)) (L.sortOn (Down . fst) kvs == kvs)))
<> prop "Map.fromDistinctDescList: fromList where strictly descending, else Nothing" (property (do kvs <- forAll genKVs; Map.fromDistinctDescList kvs === bool Nothing (Just (Map.fromList kvs)) (L.sortOn (Down . fst) kvs == kvs && L.nub (fmap fst kvs) == fmap fst kvs)))
<> prop "Map: every built map is valid" (property (do kvs <- forAll genKVs; (all M.valid (Map.fromAscList kvs), all M.valid (Map.fromDescList kvs), all M.valid (Map.fromDistinctAscList kvs)) === (True, True, True)))
<> prop "Set.fromAscList: fromList where ascending, else Nothing" (property (do xs <- forAll genXs; Set.fromAscList xs === bool Nothing (Just (Set.fromList xs)) (L.sort xs == xs)))
<> prop "Set.fromDescList: fromList where descending, else Nothing" (property (do xs <- forAll genXs; Set.fromDescList xs === bool Nothing (Just (Set.fromList xs)) (L.sortOn Down xs == xs)))
<> prop "Set.fromDistinctAscList: fromList where strictly ascending, else Nothing" (property (do xs <- forAll genXs; Set.fromDistinctAscList xs === bool Nothing (Just (Set.fromList xs)) (L.sort xs == xs && L.nub xs == xs)))
<> prop "Set.mapMonotonic: map where strictly increasing, else Nothing" (property (do s <- forAll (fmap Set.fromList genXs); c <- forAll (Gen.int (Range.linear (-2) 2)); Set.mapMonotonic (* c) s === bool Nothing (Just (Set.map (* c) s)) (c > 0 || Set.size s <= 1)))
<> prop "IntSet.fromAscList: fromList where ascending, else Nothing" (property (do xs <- forAll genXs; IntSet.fromAscList xs === bool Nothing (Just (IntSet.fromList xs)) (L.sort xs == xs)))
<> prop "IntSet.fromDescList: fromList where descending, else Nothing" (property (do xs <- forAll genXs; IntSet.fromDescList xs === bool Nothing (Just (IntSet.fromList xs)) (L.sortOn Down xs == xs)))
<> prop "IntSet.fromDistinctAscList: fromList where strictly ascending, else Nothing" (property (do xs <- forAll genXs; IntSet.fromDistinctAscList xs === bool Nothing (Just (IntSet.fromList xs)) (L.sort xs == xs && L.nub xs == xs)))
<> prop "IntSet.mapMonotonic: map where strictly increasing, else Nothing" (property (do s <- forAll (fmap IntSet.fromList genXs); c <- forAll (Gen.int (Range.linear (-2) 2)); IntSet.mapMonotonic (* c) s === bool Nothing (Just (IntSet.map (* c) s)) (c > 0 || IntSet.size s <= 1)))
<> prop "Set.fromDistinctDescList: fromList where strictly descending, else Nothing" (property (do xs <- forAll genXs; Set.fromDistinctDescList xs === bool Nothing (Just (Set.fromList xs)) (L.sortOn Down xs == xs && L.nub xs == xs)))
<> prop "Set: every built set is valid" (property (do xs <- forAll genXs; (all St.valid (Set.fromAscList xs), all St.valid (Set.fromDescList xs)) === (True, True)))
-- Papa.Base.Data.Maybe
maybes :: [(PropertyName, Property)]
maybes =
prop "whenJust: never applies is id" (property (do f <- forAll (genFn genInt :: Gen (Fn Int Int)); a <- forAll genInt; whenJust (P.const Nothing) (apply f) a === a))
<> prop "whenJust: always applies is composition" (property (do f <- forAll (genFn genInt); g <- forAll (genFn genInt); a <- forAll genInt; whenJust (Just . apply g) (apply f) a === apply f (apply g a)))
<> prop "whenJust: maybe a f (p a)" (property (do p <- forAll (genFn (Gen.maybe genInt)); f <- forAll (genFn genInt); a <- forAll genInt; whenJust (apply p) (apply f) a === maybe a (apply f) (apply p a)))
<> prop "whenJustM: never applies is pure" (property (do f <- forAll (genFn (Gen.maybe genInt) :: Gen (Fn Int (Maybe Int))); a <- forAll genInt; whenJustM (\_ -> Just Nothing) (apply f) a === Just a))
<> prop "whenJustM: always applies is Kleisli composition" (property (do f <- forAll (genFn (Gen.maybe genInt)); g <- forAll (genFn (Gen.maybe genInt)); a <- forAll genInt; whenJustM (fmap Just . apply g) (apply f) a === (apply g >=> apply f) a))
<> prop "whenJustM: whenJust at Identity" (property (do p <- forAll (genFn (Gen.maybe genInt)); f <- forAll (genFn genInt); a <- forAll genInt; runIdentity (whenJustM (Identity . apply p) (Identity . apply f) a) === whenJust (apply p) (apply f) a))
<> prop "whenJustM: effects of the step, then of f where it applies" (property (do p <- forAll (genFn (Gen.maybe genInt)); f <- forAll (genFn genInt); a <- forAll genInt; runWriter (whenJustM (\x -> apply p x <$ tell [x]) (\b -> apply f b <$ tell [b]) a) === (maybe a (apply f) (apply p a), a : toList (apply p a))))
-- Papa.Base.Data.List.NonEmpty: the lens laws of head, tail, last, init
nonEmptyLenses :: [(PropertyName, Property)]
nonEmptyLenses =
lensLaws "head" head genInt
<> lensLaws "tail" tail genInts
<> lensLaws "last" last genInt
<> lensLaws "init" init genInts
<> prop "init <> [last] == toList" (property (do s <- forAll genNonEmpty; the init s <> [the last s] === toList s))
<> prop "head : tail == toList" (property (do s <- forAll genNonEmpty; the head s : the tail s === toList s))
-- | The three lens laws, of a lens into a 'NonEmpty' 'Int'.
lensLaws :: (Eq a, Show a) => PropertyName -> Lens' (NonEmpty Int) a -> Gen a -> [(PropertyName, Property)]
lensLaws name l genA =
prop (name <> ": the l (set l a s) == a") (property (do s <- forAll genNonEmpty; a <- forAll genA; the l (set l a s) === a))
<> prop (name <> ": set l (the l s) s == s") (property (do s <- forAll genNonEmpty; set l (the l s) s === s))
<> prop (name <> ": set l b (set l a s) == set l b s") (property (do s <- forAll genNonEmpty; a <- forAll genA; b <- forAll genA; set l b (set l a s) === set l b s))
-- Papa.Lens.Data.List, Papa.Lens.Data.Tuple
optics :: [(PropertyName, Property)]
optics =
prop "(!!): agrees with (!?) at lists" (property (do xs <- forAll genInts; i <- forAll genSmallInt; (xs !! i) === (xs !? i)))
<> prop "(!!): NonEmpty" (property (do xs <- forAll genNonEmpty; i <- forAll genSmallInt; (xs !! i) === (toList xs !? i)))
<> prop "fst: Prelude's at pairs" (property (do a <- forAll genInt; b <- forAll Gen.alpha; fst (a, b) === P.fst (a, b)))
<> prop "snd: Prelude's at pairs" (property (do a <- forAll genInt; b <- forAll Gen.alpha; snd (a, b) === P.snd (a, b)))
<> prop "fst, snd: triples" (property (do a <- forAll genInt; b <- forAll Gen.alpha; c <- forAll Gen.bool; (fst (a, b, c), snd (a, b, c)) === (a, b)))
-- Papa.Bifunctors.Data.Bifoldable, Papa.Base.Control.Arrow
bifunctors :: [(PropertyName, Property)]
bifunctors =
prop "biconcat: pairs" (property (do a <- forAll genString; b <- forAll genString; biconcat (a, b) === a <> b))
<> prop "biconcat: Either" (property (do e <- forAll (Gen.either genString genString); biconcat e === P.either id id e))
<> prop "biconcatMap == bifoldMap" (property (do f <- forAll (genFn genString); g <- forAll (genFn genString); e <- forAll (Gen.either genInt Gen.bool); biconcatMap (apply f) (apply g) e === bifoldMap (apply f) (apply g) e))
<> prop "leftA: Bifunctor's first at Either" (property (do f <- forAll (genFn genInt); e <- forAll genEither; leftA (apply f) e === B.first (apply f) e))
<> prop "rightA: Bifunctor's second at Either" (property (do f <- forAll (genFn genInt); e <- forAll (Gen.either Gen.alpha genInt); rightA (apply f) e === B.second (apply f) e))
<> prop "firstA: Bifunctor's first at functions" (property (do f <- forAll (genFn genInt); a <- forAll genInt; c <- forAll Gen.alpha; firstA (apply f) (a, c) === B.first (apply f) (a, c)))
<> prop "secondA: Bifunctor's second at functions" (property (do f <- forAll (genFn genInt); a <- forAll genInt; c <- forAll Gen.alpha; secondA (apply f) (c, a) === B.second (apply f) (c, a)))