mirror of
https://github.com/Lysxia/generic-random.git
synced 2024-09-11 06:35:24 +03:00
Add tour-example
This commit is contained in:
parent
f8ce9e2ba0
commit
d202e04550
@ -25,3 +25,15 @@ executable text-example
|
||||
text,
|
||||
generic-random
|
||||
default-language: Haskell2010
|
||||
|
||||
executable tour-example
|
||||
main-is: tour.hs
|
||||
ghc-options: -Wall
|
||||
if impl(ghc < 7.10)
|
||||
ghc-options: -fcontext-stack=30
|
||||
build-depends:
|
||||
base,
|
||||
QuickCheck,
|
||||
text,
|
||||
generic-random
|
||||
default-language: Haskell2010
|
||||
|
45
examples/tour.hs
Normal file
45
examples/tour.hs
Normal file
@ -0,0 +1,45 @@
|
||||
-- Just another toy example
|
||||
|
||||
{-# LANGUAGE DataKinds #-}
|
||||
{-# LANGUAGE DeriveGeneric #-}
|
||||
{-# LANGUAGE FlexibleInstances #-}
|
||||
{-# LANGUAGE InstanceSigs #-}
|
||||
{-# LANGUAGE LambdaCase #-}
|
||||
{-# LANGUAGE TypeApplications #-}
|
||||
|
||||
import Control.Monad
|
||||
import GHC.Generics
|
||||
import Test.QuickCheck
|
||||
|
||||
import Generic.Random
|
||||
|
||||
data MyType
|
||||
= OneThing Int
|
||||
| TwoThings Double String
|
||||
| ThreeThings (Maybe Integer) [()] (Bool -> Word)
|
||||
deriving (Show, Generic)
|
||||
|
||||
custom :: GenList '[Maybe Integer]
|
||||
custom = (Just <$> arbitrary) :@ Nil
|
||||
|
||||
instance Arbitrary MyType where
|
||||
arbitrary :: Gen MyType
|
||||
arbitrary = genericArbitraryG custom (1 % 4 % 4 % ())
|
||||
-- arbitrary = frequency
|
||||
-- [ (1, OneThing <$> arbitrary)
|
||||
-- , (4, TwoThings <$> arbitrary <*> arbitrary)
|
||||
-- , (4, ThreeThings <$> (Just <$> arbitrary) <*> arbitrary <*> arbitrary)
|
||||
-- ]
|
||||
|
||||
main = do
|
||||
-- Print some examples
|
||||
sample (arbitrary @MyType)
|
||||
|
||||
-- Check the property that ThreeThings contains three things.
|
||||
quickCheck $ \case
|
||||
ThreeThings Nothing _ _ -> False
|
||||
_ -> True
|
||||
|
||||
-- Ew. Sorry.
|
||||
instance Show a => Show (Bool -> a) where
|
||||
show f = "<True -> " ++ show (f True) ++ ",False -> " ++ show (f False) ++ ">"
|
Loading…
Reference in New Issue
Block a user