module Agda.Utils.TestHelpers
(
associative
, commutative
, idempotent
, isZero
, identity
, leftDistributive
, rightDistributive
, distributive
, natural
, positive
, maybeGen
, maybeCoGen
, listOfElements
, elementsUnlessEmpty
, two
, three
, runTests
)
where
import Control.Monad
import Data.Functor
import Test.QuickCheck
associative :: (Arbitrary a, Eq a, Show a)
=> (a -> a -> a)
-> a -> a -> a -> Bool
associative (+) = \x y z ->
x + (y + z) == (x + y) + z
commutative :: (Arbitrary a, Eq a, Show a)
=> (a -> a -> a)
-> a -> a -> Bool
commutative (+) = \x y ->
x + y == y + x
idempotent :: (Arbitrary a, Eq a, Show a)
=> (a -> a -> a)
-> a -> Bool
idempotent (/\) = \ x ->
(x /\ x) == x
isZero :: (Arbitrary a, Eq a, Show a)
=> a -> (a -> a -> a)
-> a -> Bool
isZero zer (*) = \x ->
(zer * x == zer)
&&
(x * zer == zer)
identity :: (Arbitrary a, Eq a, Show a)
=> a -> (a -> a -> a)
-> a -> Bool
identity one (*) = \x ->
(one * x == x)
&&
(x * one == x)
leftDistributive
:: (Arbitrary a, Eq a, Show a)
=> (a -> a -> a) -> (a -> a -> a)
-> a -> a -> a -> Bool
leftDistributive (*) (+) = \x y z ->
x * (y + z) == (x * y) + (x * z)
rightDistributive
:: (Arbitrary a, Eq a, Show a)
=> (a -> a -> a) -> (a -> a -> a)
-> a -> a -> a -> Bool
rightDistributive (*) (+) = \x y z ->
(x + y) * z == (x * z) + (y * z)
distributive
:: (Arbitrary a, Eq a, Show a)
=> (a -> a -> a) -> (a -> a -> a)
-> a -> a -> a -> Bool
distributive (*) (+) = \ x y z ->
leftDistributive (*) (+) x y z &&
rightDistributive (*) (+) x y z
natural :: (Integral i) => Gen i
natural = fromInteger . abs <$> arbitrary
positive :: (Integral i) => Gen i
positive = succ <$> natural
listOfElements :: [a] -> Gen [a]
listOfElements [] = return []
listOfElements xs = listOf $ elements xs
elementsUnlessEmpty :: Arbitrary a => [a] -> Gen a
elementsUnlessEmpty [] = arbitrary
elementsUnlessEmpty xs = elements xs
maybeGen :: Gen a -> Gen (Maybe a)
maybeGen gen = frequency [ (1, return Nothing)
, (9, Just <$> gen)
]
maybeCoGen :: (a -> Gen b -> Gen b) -> (Maybe a -> Gen b -> Gen b)
maybeCoGen f Nothing = variant 0
maybeCoGen f (Just x) = variant 1 . f x
two :: Gen a -> Gen (a, a)
two gen = liftM2 (,) gen gen
three :: Gen a -> Gen (a, a, a)
three gen = liftM3 (,,) gen gen gen
runTests :: String
-> [IO Bool]
-> IO Bool
runTests name tests = do
putStrLn name
and <$> sequence tests