packages feed

papa-0.7.0: test/PropertiesContainers.hs

{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -Wall #-}

{- HLINT ignore "Avoid restricted function" -}
-- Reason: these properties compare Papa's total functions with the partial
-- ones of containers, whose names HLint restricts.

-- | The agreement of the total functions of "Papa.Containers" with the
-- partial ones of containers they replace: each is 'Nothing' exactly where
-- containers' throws, and containers' result everywhere else; each
-- order-checked builder is containers' result exactly where the input is in
-- order (where containers' result is a valid structure), and 'Nothing'
-- everywhere else.
module PropertiesContainers (
  propertiesContainers,
) where

import Agree (agrees)
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.Sequence as Sq
import qualified Data.Set as St
import Hedgehog (Gen, Group (Group), Property, PropertyName, forAll, forAllWith, property, (===))
import Hedgehog.Function (apply, fn)
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range
import Papa
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

-- | Every property of this module, in one group.
propertiesContainers :: Group
propertiesContainers =
  Group (fromString "papa: containers agreement") (partials <> builders)

-- | One named property.
prop :: String -> Property -> [(PropertyName, Property)]
prop name p =
  [(fromString name, p)]

-- Generators

genKey :: Gen Int
genKey =
  Gen.int (Range.linear (-20) 20)

genIndex :: Gen Int
genIndex =
  Gen.int (Range.linear (-3) 25)

-- | Lists of pairs, often in ascending or descending order of keys, with or
-- without duplicates.
genKVs :: Gen [(Int, Int)]
genKVs =
  Gen.choice
    [ raw
    , L.sortOn fst <$> raw
    , L.sortOn (Down . fst) <$> raw
    , L.nubBy (\a b -> fst a == fst b) . L.sortOn fst <$> raw
    , L.nubBy (\a b -> fst a == fst b) . L.sortOn (Down . fst) <$> raw
    ]
 where
  raw = Gen.list (Range.linear 0 20) ((,) <$> genKey <*> genKey)

genKeys :: Gen [Int]
genKeys =
  fmap (fmap fst) genKVs

genMap :: Gen (Map Int Int)
genMap =
  M.fromList <$> genKVs

genSet :: Gen (Set Int)
genSet =
  St.fromList <$> genKeys

genIntMap :: Gen (IntMap Int)
genIntMap =
  IM.fromList <$> genKVs

genIntSet :: Gen IntSet
genIntSet =
  IS.fromList <$> genKeys

genSeq :: Gen (Seq Int)
genSeq =
  Sq.fromList <$> genKeys

-- | A function, often strictly increasing.
genKeyFn :: Gen (String, Int -> Int)
genKeyFn =
  Gen.choice
    [ (\c -> ("(+ " <> show c <> ")", (+ c))) <$> genKey
    , (\c -> ("(* " <> show c <> ")", (* c))) <$> Gen.int (Range.linear (-3) 3)
    , (\f -> (show f, apply f)) <$> fn genKey
    ]

-- | Whether a list is in order by a relation of adjacent elements.
ordered :: (a -> a -> Bool) -> [a] -> Bool
ordered r xs =
  and (L.zipWith r xs (L.drop 1 xs))

-- Partial functions

-- | Each total function is containers' partial one, 'Nothing' exactly where
-- that throws.
partials :: [(PropertyName, Property)]
partials =
  prop "Map.!" (property (do m <- forAll genMap; k <- forAll genKey; agrees (m Map.! k) (m M.! k)))
    <> prop "Map.findMin" (property (do m <- forAll genMap; agrees (Map.findMin m) (M.findMin m)))
    <> prop "Map.findMax" (property (do m <- forAll genMap; agrees (Map.findMax m) (M.findMax m)))
    <> prop "Map.deleteFindMin" (property (do m <- forAll genMap; agrees (Map.deleteFindMin m) (M.deleteFindMin m)))
    <> prop "Map.deleteFindMax" (property (do m <- forAll genMap; agrees (Map.deleteFindMax m) (M.deleteFindMax m)))
    <> prop "Map.elemAt" (property (do m <- forAll genMap; i <- forAll genIndex; agrees (Map.elemAt i m) (M.elemAt i m)))
    <> prop "Map.findIndex" (property (do m <- forAll genMap; k <- forAll genKey; agrees (Map.findIndex k m) (M.findIndex k m)))
    <> prop "Map.deleteAt" (property (do m <- forAll genMap; i <- forAll genIndex; agrees (Map.deleteAt i m) (M.deleteAt i m)))
    <> prop "Map.updateAt" (property (do m <- forAll genMap; i <- forAll genIndex; f <- forAll (fn (Gen.maybe genKey)); agrees (Map.updateAt (\_ -> apply f) i m) (M.updateAt (\_ -> apply f) i m)))
    <> prop "Set.findMin" (property (do s <- forAll genSet; agrees (Set.findMin s) (St.findMin s)))
    <> prop "Set.findMax" (property (do s <- forAll genSet; agrees (Set.findMax s) (St.findMax s)))
    <> prop "Set.deleteFindMin" (property (do s <- forAll genSet; agrees (Set.deleteFindMin s) (St.deleteFindMin s)))
    <> prop "Set.deleteFindMax" (property (do s <- forAll genSet; agrees (Set.deleteFindMax s) (St.deleteFindMax s)))
    <> prop "Set.elemAt" (property (do s <- forAll genSet; i <- forAll genIndex; agrees (Set.elemAt i s) (St.elemAt i s)))
    <> prop "Set.findIndex" (property (do s <- forAll genSet; a <- forAll genKey; agrees (Set.findIndex a s) (St.findIndex a s)))
    <> prop "Set.deleteAt" (property (do s <- forAll genSet; i <- forAll genIndex; agrees (Set.deleteAt i s) (St.deleteAt i s)))
    <> prop "IntMap.!" (property (do m <- forAll genIntMap; k <- forAll genKey; agrees (m IntMap.! k) (m IM.! k)))
    <> prop "IntMap.findMin" (property (do m <- forAll genIntMap; agrees (IntMap.findMin m) (IM.findMin m)))
    <> prop "IntMap.findMax" (property (do m <- forAll genIntMap; agrees (IntMap.findMax m) (IM.findMax m)))
    <> prop "IntMap.deleteFindMin" (property (do m <- forAll genIntMap; agrees (IntMap.deleteFindMin m) (IM.deleteFindMin m)))
    <> prop "IntMap.deleteFindMax" (property (do m <- forAll genIntMap; agrees (IntMap.deleteFindMax m) (IM.deleteFindMax m)))
    <> prop "IntSet.findMin" (property (do s <- forAll genIntSet; agrees (IntSet.findMin s) (IS.findMin s)))
    <> prop "IntSet.findMax" (property (do s <- forAll genIntSet; agrees (IntSet.findMax s) (IS.findMax s)))
    <> prop "IntSet.deleteFindMin" (property (do s <- forAll genIntSet; agrees (IntSet.deleteFindMin s) (IS.deleteFindMin s)))
    <> prop "IntSet.deleteFindMax" (property (do s <- forAll genIntSet; agrees (IntSet.deleteFindMax s) (IS.deleteFindMax s)))
    <> prop "Seq.index" (property (do s <- forAll genSeq; i <- forAll genIndex; agrees (Seq.index s i) (Sq.index s i)))
    <> prop "Seq.replicate" (property (do n <- forAll genIndex; a <- forAll genKey; agrees (Seq.replicate n a) (Sq.replicate n a)))
    <> prop "Seq.replicateA" (property (do n <- forAll genIndex; a <- forAll (Gen.maybe genKey); agrees (Seq.replicateA n a) (Sq.replicateA n a)))
    <> prop "Seq.replicateM" (property (do n <- forAll genIndex; a <- forAll (Gen.maybe genKey); agrees (Seq.replicateM n a) (Sq.replicateM n a)))
    <> prop "Seq.iterateN" (property (do n <- forAll genIndex; f <- forAll (fn genKey); a <- forAll genKey; agrees (Seq.iterateN n (apply f) a) (Sq.iterateN n (apply f) a)))
    <> prop "Seq.fromFunction" (property (do n <- forAll genIndex; f <- forAll (fn genKey); agrees (Seq.fromFunction n (apply f)) (Sq.fromFunction n (apply f))))
    <> prop "Seq.chunksOf" (property (do n <- forAll (Gen.int (Range.linear (-3) 6)); s <- forAll genSeq; agrees (Seq.chunksOf n s) (Sq.chunksOf n s)))
    <> prop "Seq.cycleTaking" (property (do n <- forAll genIndex; s <- forAll genSeq; agrees (Seq.cycleTaking n s) (Sq.cycleTaking n s)))
    <> prop "Seq.scanl1" (property (do s <- forAll genSeq; agrees (Seq.scanl1 (+) s) (Sq.scanl1 (+) s)))
    <> prop "Seq.scanr1" (property (do s <- forAll genSeq; agrees (Seq.scanr1 (+) s) (Sq.scanr1 (+) s)))

-- Order-checked builders

-- | Each builder that checks the order of its input is containers' builder
-- exactly where the input is in order (where containers' result is valid),
-- and 'Nothing' everywhere else.
builders :: [(PropertyName, Property)]
builders =
  prop "Map.fromAscList" (property (do kvs <- forAll genKVs; let { inOrder = ordered (<=) (fmap fst kvs); r = M.fromAscList kvs }; Map.fromAscList kvs === bool Nothing (Just r) inOrder; when inOrder (M.valid r === True)))
    <> prop "Map.fromAscListWith" (property (do kvs <- forAll genKVs; let { inOrder = ordered (<=) (fmap fst kvs); r = M.fromAscListWith (-) kvs }; Map.fromAscListWith (-) kvs === bool Nothing (Just r) inOrder; when inOrder (M.valid r === True)))
    <> prop "Map.fromAscListWithKey" (property (do kvs <- forAll genKVs; let { inOrder = ordered (<=) (fmap fst kvs); f k a b = k + a - b; r = M.fromAscListWithKey f kvs }; Map.fromAscListWithKey f kvs === bool Nothing (Just r) inOrder; when inOrder (M.valid r === True)))
    <> prop "Map.fromDistinctAscList" (property (do kvs <- forAll genKVs; let { inOrder = ordered (<) (fmap fst kvs); r = M.fromDistinctAscList kvs }; Map.fromDistinctAscList kvs === bool Nothing (Just r) inOrder; when inOrder (M.valid r === True)))
    <> prop "Map.fromDescList" (property (do kvs <- forAll genKVs; let { inOrder = ordered (>=) (fmap fst kvs); r = M.fromDescList kvs }; Map.fromDescList kvs === bool Nothing (Just r) inOrder; when inOrder (M.valid r === True)))
    <> prop "Map.fromDescListWith" (property (do kvs <- forAll genKVs; let { inOrder = ordered (>=) (fmap fst kvs); r = M.fromDescListWith (-) kvs }; Map.fromDescListWith (-) kvs === bool Nothing (Just r) inOrder; when inOrder (M.valid r === True)))
    <> prop "Map.fromDescListWithKey" (property (do kvs <- forAll genKVs; let { inOrder = ordered (>=) (fmap fst kvs); f k a b = k + a - b; r = M.fromDescListWithKey f kvs }; Map.fromDescListWithKey f kvs === bool Nothing (Just r) inOrder; when inOrder (M.valid r === True)))
    <> prop "Map.fromDistinctDescList" (property (do kvs <- forAll genKVs; let { inOrder = ordered (>) (fmap fst kvs); r = M.fromDistinctDescList kvs }; Map.fromDistinctDescList kvs === bool Nothing (Just r) inOrder; when inOrder (M.valid r === True)))
    <> prop "Map.mapKeysMonotonic" (property (do m <- forAll genMap; (_, f) <- forAllWith fst genKeyFn; let { inOrder = ordered (<) (fmap f (M.keys m)); r = M.mapKeysMonotonic f m }; Map.mapKeysMonotonic f m === bool Nothing (Just r) inOrder; when inOrder (M.valid r === True)))
    <> prop "Set.fromAscList" (property (do xs <- forAll genKeys; let { inOrder = ordered (<=) xs; r = St.fromAscList xs }; Set.fromAscList xs === bool Nothing (Just r) inOrder; when inOrder (St.valid r === True)))
    <> prop "Set.fromDescList" (property (do xs <- forAll genKeys; let { inOrder = ordered (>=) xs; r = St.fromDescList xs }; Set.fromDescList xs === bool Nothing (Just r) inOrder; when inOrder (St.valid r === True)))
    <> prop "Set.fromDistinctAscList" (property (do xs <- forAll genKeys; let { inOrder = ordered (<) xs; r = St.fromDistinctAscList xs }; Set.fromDistinctAscList xs === bool Nothing (Just r) inOrder; when inOrder (St.valid r === True)))
    <> prop "Set.fromDistinctDescList" (property (do xs <- forAll genKeys; let { inOrder = ordered (>) xs; r = St.fromDistinctDescList xs }; Set.fromDistinctDescList xs === bool Nothing (Just r) inOrder; when inOrder (St.valid r === True)))
    <> prop "Set.mapMonotonic" (property (do s <- forAll genSet; (_, f) <- forAllWith fst genKeyFn; let { inOrder = ordered (<) (fmap f (St.toAscList s)); r = St.mapMonotonic f s }; Set.mapMonotonic f s === bool Nothing (Just r) inOrder; when inOrder (St.valid r === True)))
    <> prop "IntMap.fromAscList" (property (do kvs <- forAll genKVs; let { inOrder = ordered (<=) (fmap fst kvs); r = IM.fromAscList kvs }; IntMap.fromAscList kvs === bool Nothing (Just r) inOrder))
    <> prop "IntMap.fromAscListWith" (property (do kvs <- forAll genKVs; let { inOrder = ordered (<=) (fmap fst kvs); r = IM.fromAscListWith (-) kvs }; IntMap.fromAscListWith (-) kvs === bool Nothing (Just r) inOrder))
    <> prop "IntMap.fromAscListWithKey" (property (do kvs <- forAll genKVs; let { inOrder = ordered (<=) (fmap fst kvs); f k a b = k + a - b; r = IM.fromAscListWithKey f kvs }; IntMap.fromAscListWithKey f kvs === bool Nothing (Just r) inOrder))
    <> prop "IntMap.fromDistinctAscList" (property (do kvs <- forAll genKVs; let { inOrder = ordered (<) (fmap fst kvs); r = IM.fromDistinctAscList kvs }; IntMap.fromDistinctAscList kvs === bool Nothing (Just r) inOrder))
    <> prop "IntMap.fromDescList: fromList where descending" (property (do kvs <- forAll genKVs; let { inOrder = ordered (>=) (fmap fst kvs); r = IM.fromList kvs }; IntMap.fromDescList kvs === bool Nothing (Just r) inOrder))
    <> prop "IntMap.mapKeysMonotonic" (property (do m <- forAll genIntMap; (_, f) <- forAllWith fst genKeyFn; let { inOrder = ordered (<) (fmap f (IM.keys m)); r = IM.mapKeysMonotonic f m }; IntMap.mapKeysMonotonic f m === bool Nothing (Just r) inOrder))
    <> prop "IntSet.fromAscList" (property (do xs <- forAll genKeys; let { inOrder = ordered (<=) xs; r = IS.fromAscList xs }; IntSet.fromAscList xs === bool Nothing (Just r) inOrder))
    <> prop "IntSet.fromDescList: fromList where descending" (property (do xs <- forAll genKeys; let { inOrder = ordered (>=) xs; r = IS.fromList xs }; IntSet.fromDescList xs === bool Nothing (Just r) inOrder))
    <> prop "IntSet.fromDistinctAscList" (property (do xs <- forAll genKeys; let { inOrder = ordered (<) xs; r = IS.fromDistinctAscList xs }; IntSet.fromDistinctAscList xs === bool Nothing (Just r) inOrder))
    <> prop "IntSet.mapMonotonic" (property (do s <- forAll genIntSet; (_, f) <- forAllWith fst genKeyFn; let { inOrder = ordered (<) (fmap f (IS.toAscList s)); r = IS.mapMonotonic f s }; IntSet.mapMonotonic f s === bool Nothing (Just r) inOrder))