[Haskell-cafe] QuickCheck Generators

Anton Kholomiov anton.kholomiov at gmail.com
Thu Nov 22 13:30:54 CET 2012


An idea. You can make a type:

data TestContains = TestContains Tweet TweetSet

and the make an Arbitrary instance for it. When you do
a recursove call you have three different tweets one new tweet
and two from the sub-calls. Then you can place one of them in the
result. In the end you will have a random TweetSet with some value from it.

Here is a scratch of the implementation:

instance Arbitrary TestContains where
   arbitrary = sized set'
       where set' 0 = mkSingleTweet <$> (arbitrary :: Tweet)
             set' n = do
                  t0 <- arbitrary :: Tweet
                  TestContains t1 ts1 <- subTweets
                  TestContains t2 ts2 <- subTweets
                  t <- oneof [t0, t1, t2]
                  return $ TestContains t $ TweetSet t0 ts1 ts2

             subTweets = set' (n `div` 2)


2012/11/21 <graham at fatlazycat.com>

> I have
>
> data Tweet = Tweet {
>     user :: String,
>     text :: String,
>     retweets :: Double
>     } deriving (Show)
>
> data TweetSet = NoTweets | SomeTweets Tweet TweetSet TweetSet
>
> and trying to create some generators for testing, with
>
> instance Arbitrary Tweet where
>   arbitrary = liftM3 Tweet arbitrary arbitrary arbitrary
>
> instance Arbitrary TweetSet where
>   arbitrary = sized set'
>     where set' 0 = return NoTweets
>           set' n | n>0 = oneof[return NoTweets, liftM3 SomeTweets
>           arbitrary subTweets subTweets]
>             where subTweets = set' (n `div` 2)
>
> but wondering how I would go about generating a random TweetSet that
> contains a known random Tweet I later have reference to and I would also
> assume the known Tweet to be placed randomly.
>
> Then I could test a contains function.
>
> Thanks
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20121122/5478ea60/attachment.htm>


More information about the Haskell-Cafe mailing list