File: Utils.hs

package info (click to toggle)
haskell-zlib 0.6.3.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,300 kB
  • sloc: ansic: 7,058; haskell: 1,081; makefile: 6
file content (28 lines) | stat: -rw-r--r-- 800 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Utils where

import qualified Data.ByteString.Lazy as BL
import qualified Data.ByteString      as BS

import Test.QuickCheck

-------------------
-- QuickCheck Utils

maxStrSize :: Double
maxStrSize = 500

-- convert a QC size parameter into one for generating long lists,
-- growing inverse exponentially up to maxStrSize
strSize :: Int -> Int
strSize n = floor (maxStrSize * (1 - 2 ** (-fromIntegral n/100)))

instance Arbitrary BL.ByteString where
  arbitrary = sized $ \sz -> fmap BL.fromChunks $ listOf $ resize (sz `div` 2) arbitrary
  shrink = map BL.pack . shrink . BL.unpack

instance Arbitrary BS.ByteString where
  arbitrary = sized $ \sz -> resize (strSize sz) $ fmap BS.pack $ listOf $ arbitrary
  shrink = map BS.pack . shrink . BS.unpack