Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional arbitrary instances #372

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/Test/QuickCheck/Arbitrary.hs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ import System.IO
#endif
#endif

#if defined(MIN_VERSION_base)
#if MIN_VERSION_base(4,9,0)
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.Semigroup as Semigroup
#endif
#endif

import Control.Monad
( liftM
, liftM2
Expand All @@ -157,6 +164,7 @@ import Control.Monad

import Data.Int(Int8, Int16, Int32, Int64)
import Data.Word(Word, Word8, Word16, Word32, Word64)
import Data.Ord
import System.Exit (ExitCode(..))
import Foreign.C.Types

Expand Down Expand Up @@ -1007,6 +1015,18 @@ instance Arbitrary a => Arbitrary (Monoid.Last a) where
shrink = map Monoid.Last . shrink . Monoid.getLast
#endif

#if defined(MIN_VERSION_base)
#if MIN_VERSION_base(4,9,0)
instance Arbitrary a => Arbitrary (Semigroup.Min a) where
arbitrary = fmap Semigroup.Min arbitrary
shrink = map Semigroup.Min . shrink . Semigroup.getMin

instance Arbitrary a => Arbitrary (Semigroup.Max a) where
arbitrary = fmap Semigroup.Max arbitrary
shrink = map Semigroup.Max . shrink . Semigroup.getMax
#endif
#endif

#if MIN_VERSION_base(4,8,0)
instance Arbitrary (f a) => Arbitrary (Monoid.Alt f a) where
arbitrary = fmap Monoid.Alt arbitrary
Expand Down Expand Up @@ -1061,6 +1081,29 @@ instance Arbitrary NewlineMode where
#endif
#endif

#if defined(MIN_VERSION_base)
#if MIN_VERSION_base(4,6,0)
instance Arbitrary1 Down where
liftArbitrary = fmap Down
liftShrink shr (Down a) = Down <$> shr a

instance Arbitrary a => Arbitrary (Down a) where
arbitrary = arbitrary1
shrink = shrink1
#endif
#endif

#if defined(MIN_VERSION_base)
#if MIN_VERSION_base(4,9,0)
instance Arbitrary a => Arbitrary (NonEmpty a) where
arbitrary = (:|) <$> arbitrary <*> arbitrary
shrink (a :| bs) =
[ a' :| bs | a' <- shrink a ] ++
[ b :| bs' | b : bs' <- [bs] ] ++
[ a :| bs' | bs' <- shrink bs ]
#endif
#endif

-- ** Helper functions for implementing arbitrary

-- | Apply a binary function to random arguments.
Expand Down Expand Up @@ -1395,6 +1438,20 @@ instance CoArbitrary a => CoArbitrary [a] where
instance (Integral a, CoArbitrary a) => CoArbitrary (Ratio a) where
coarbitrary r = coarbitrary (numerator r,denominator r)

#if defined(MIN_VERSION_base)
#if MIN_VERSION_base(4,6,0)
instance CoArbitrary a => CoArbitrary (Down a) where
coarbitrary (Down x) = coarbitrary x
#endif
#endif

#if defined(MIN_VERSION_base)
#if MIN_VERSION_base(4,9,0)
instance CoArbitrary a => CoArbitrary (NonEmpty a) where
coarbitrary (a :| as) = coarbitrary (a, as)
#endif
#endif

#ifndef NO_FIXED
instance HasResolution a => CoArbitrary (Fixed a) where
coarbitrary = coarbitraryReal
Expand Down Expand Up @@ -1540,6 +1597,15 @@ instance CoArbitrary a => CoArbitrary (Monoid.Last a) where
coarbitrary = coarbitrary . Monoid.getLast
#endif

#if MIN_VERSION_base(4,9,0)
instance CoArbitrary a => CoArbitrary (Semigroup.Min a) where
coarbitrary = coarbitrary . Semigroup.getMin

instance CoArbitrary a => CoArbitrary (Semigroup.Max a) where
coarbitrary = coarbitrary . Semigroup.getMax
#endif


#if MIN_VERSION_base(4,8,0)
instance CoArbitrary (f a) => CoArbitrary (Monoid.Alt f a) where
coarbitrary = coarbitrary . Monoid.getAlt
Expand Down
36 changes: 36 additions & 0 deletions src/Test/QuickCheck/Function.hs
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,14 @@ module Test.QuickCheck.Function

import Test.QuickCheck.Arbitrary
import Test.QuickCheck.Poly
import Test.QuickCheck.Modifiers (Small(..))

import Control.Applicative
import Data.Char
import Data.Word
import Data.List( intersperse )
import Data.Ratio
import Data.Ord
import qualified Data.IntMap as IntMap
import qualified Data.IntSet as IntSet
import qualified Data.Map as Map
Expand Down Expand Up @@ -101,6 +103,13 @@ import Data.Fixed
import GHC.Generics hiding (C)
#endif

#if defined(MIN_VERSION_base)
#if MIN_VERSION_base(4,9,0)
import Data.List.NonEmpty (NonEmpty(..))
import qualified Data.Semigroup as Semigroup
#endif
#endif

--------------------------------------------------------------------------
-- concrete functions

Expand Down Expand Up @@ -261,6 +270,13 @@ instance Function a => Function [a] where
h (Left _) = []
h (Right (x,xs)) = x:xs

#if defined(MIN_VERSION_base)
#if MIN_VERSION_base(4,9,0)
instance Function a => Function (NonEmpty a) where
function = functionMap (\(a :| as) -> (a, as)) (uncurry (:|))
#endif
#endif

instance Function a => Function (Maybe a) where
function = functionMap g h
where
Expand Down Expand Up @@ -423,11 +439,26 @@ instance Function a => Function (Monoid.First a) where
instance Function a => Function (Monoid.Last a) where
function = functionMap Monoid.getLast Monoid.Last

#if defined(MIN_VERSION_base)
#if MIN_VERSION_base(4,6,0)
instance Function a => Function (Down a) where
function = functionMap (\(Down a) -> a) Down
#endif

#if MIN_VERSION_base(4,8,0)
instance Function (f a) => Function (Monoid.Alt f a) where
function = functionMap Monoid.getAlt Monoid.Alt
#endif

#if MIN_VERSION_base(4,9,0)
instance Function a => Function (Semigroup.Min a) where
function = functionMap Semigroup.getMin Semigroup.Min

instance Function a => Function (Semigroup.Max a) where
function = functionMap Semigroup.getMax Semigroup.Max
#endif
#endif

-- poly instances

instance Function A where
Expand All @@ -448,6 +479,11 @@ instance Function OrdB where
instance Function OrdC where
function = functionMap unOrdC OrdC

-- Instances for modifiers

instance Function a => Function (Small a) where
function = functionMap getSmall Small

-- instance Arbitrary

instance (Function a, CoArbitrary a, Arbitrary b) => Arbitrary (a:->b) where
Expand Down
3 changes: 3 additions & 0 deletions src/Test/QuickCheck/Modifiers.hs
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,9 @@ instance Integral a => Arbitrary (Small a) where
arbitrary = fmap Small arbitrarySizedIntegral
shrink (Small x) = map Small (shrinkIntegral x)

instance CoArbitrary a => CoArbitrary (Small a) where
coarbitrary (Small a) = coarbitrary a

--------------------------------------------------------------------------
-- | @Shrink2 x@: allows 2 shrinking steps at the same time when shrinking x
newtype Shrink2 a = Shrink2 {getShrink2 :: a}
Expand Down
Loading