{-|
Following the convention from "Data.ByteString.Lazy", this module is intended to be
imported @qualified@. For example:

> import qualified Test.SmallCheck.Series.ByteString.Lazy as L.Series
-}
module Test.SmallCheck.Series.ByteString.Lazy
  (
  -- * Replication
    replicateA
  , replicate0
  , replicateW8
  -- * Enumeration
  , enumW8s
  , enumAlphabet
  , enumList
  -- * Printing
  , jack
  ) where

import Data.Char (ord)
import Data.List (inits)
import Data.Word (Word8)
import Data.ByteString.Lazy (ByteString, pack)
import qualified Data.ByteString.Lazy.Char8 as L8 (pack)
import Test.SmallCheck.Series

-- | A 'Data.ByteString.Lazy.ByteString' 'Series' that grows by replicating
--   the @97@ 'Word8', which encodes the @\'a\'@ 'Char' in @ASCII@.
--
-- >>> list 4 replicateA
-- ["","a","aa","aaa","aaaa"]
--
-- Use this when you don't care about the 'Word8' inside 'Data.ByteString.Lazy.ByteString'.
replicateA :: Series m ByteString
replicateA :: Series m ByteString
replicateA = Word8 -> Series m ByteString
forall (m :: * -> *). Word8 -> Series m ByteString
replicateW8 Word8
97

-- | A 'Data.ByteString.Lazy.ByteString' 'Series' that grows by replicating
--   the @0@ 'Word8'.
--
-- >>> list 4 replicate0
-- ["","\NUL","\NUL\NUL","\NUL\NUL\NUL","\NUL\NUL\NUL\NUL"]
replicate0 :: Series m ByteString
replicate0 :: Series m ByteString
replicate0 = Word8 -> Series m ByteString
forall (m :: * -> *). Word8 -> Series m ByteString
replicateW8 Word8
0

-- | A 'Data.ByteString.Lazy.ByteString' 'Series' that grows by replicating
--   the given 'Word8'.
--
-- >>> list 4 $ replicateW8 64
-- ["","@","@@","@@@","@@@@"]
replicateW8 :: Word8 -> Series m ByteString
replicateW8 :: Word8 -> Series m ByteString
replicateW8 Word8
b =
    (Depth -> [ByteString]) -> Series m ByteString
forall a (m :: * -> *). (Depth -> [a]) -> Series m a
generate ((Depth -> [ByteString]) -> Series m ByteString)
-> (Depth -> [ByteString]) -> Series m ByteString
forall a b. (a -> b) -> a -> b
$ \Depth
d -> ([Word8] -> ByteString) -> [[Word8]] -> [ByteString]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Word8] -> ByteString
pack ([[Word8]] -> [ByteString])
-> ([Word8] -> [[Word8]]) -> [Word8] -> [ByteString]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Word8] -> [[Word8]]
forall a. [a] -> [[a]]
inits ([Word8] -> [ByteString]) -> [Word8] -> [ByteString]
forall a b. (a -> b) -> a -> b
$ (Depth -> Word8 -> [Word8]
forall a. Depth -> a -> [a]
replicate (Depth -> Word8 -> [Word8])
-> (Depth -> Depth) -> Depth -> Word8 -> [Word8]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Depth -> Depth
forall a b. (Integral a, Num b) => a -> b
fromIntegral) Depth
d Word8
b

-- | A 'Data.ByteString.Lazy.ByteString' 'Series' that grows by enumerating
--   every 'Word8'.
--
-- >>> list 4 enumW8s
-- ["","\NUL","\NUL\SOH","\NUL\SOH\STX","\NUL\SOH\STX\ETX"]
enumW8s :: Series m ByteString
enumW8s :: Series m ByteString
enumW8s = [Word8] -> Series m ByteString
forall (m :: * -> *). [Word8] -> Series m ByteString
enumList [Word8
0..Word8
255]

-- | A 'Data.ByteString.Lazy.ByteString' 'Series' that grows by enumerating
--   the 'Word8's which encode the latin alphabet in @ASCII@.
--
-- >>> list 4 enumAlphabet
-- ["","a","ab","abc","abcd"]
enumAlphabet :: Series m ByteString
enumAlphabet :: Series m ByteString
enumAlphabet = [Word8] -> Series m ByteString
forall (m :: * -> *). [Word8] -> Series m ByteString
enumList ([Word8] -> Series m ByteString) -> [Word8] -> Series m ByteString
forall a b. (a -> b) -> a -> b
$ (Char -> Word8) -> [Char] -> [Word8]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Depth -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Depth -> Word8) -> (Char -> Depth) -> Char -> Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Depth
ord) [Char
'a'..Char
'z']

-- | A 'Data.ByteString.ByteString' 'Series' that grows by enumerating
--   every 'Word8' in the given list.
--
-- >>> list 4 . enumList $ fmap (fromIntegral . ord) "abc"
-- ["","a","ab","abc"]
enumList  :: [Word8] -> Series m ByteString
enumList :: [Word8] -> Series m ByteString
enumList [Word8]
cs = (Depth -> [ByteString]) -> Series m ByteString
forall a (m :: * -> *). (Depth -> [a]) -> Series m a
generate ((Depth -> [ByteString]) -> Series m ByteString)
-> (Depth -> [ByteString]) -> Series m ByteString
forall a b. (a -> b) -> a -> b
$ \Depth
d -> ([Word8] -> ByteString) -> [[Word8]] -> [ByteString]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap [Word8] -> ByteString
pack ([[Word8]] -> [ByteString])
-> ([Word8] -> [[Word8]]) -> [Word8] -> [ByteString]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Word8] -> [[Word8]]
forall a. [a] -> [[a]]
inits ([Word8] -> [ByteString]) -> [Word8] -> [ByteString]
forall a b. (a -> b) -> a -> b
$ Depth -> [Word8] -> [Word8]
forall a. Depth -> [a] -> [a]
take Depth
d [Word8]
cs

-- | A 'Data.ByteString.Lazy.ByteString' 'Series' that grows with @ASCII@
--   dummy English words encoded in @ASCII@.
--
--   This is useful when you want to print 'Series'.
--
-- >>> let s = list 20 jack
-- >>> take 3 s
-- ["","All","All work"]
-- >>> last s
-- "All work and no play makes Jack a dull boy. All work and no play makes Jack a dull boy."
jack :: Series m ByteString
jack :: Series m ByteString
jack = (Depth -> [ByteString]) -> Series m ByteString
forall a (m :: * -> *). (Depth -> [a]) -> Series m a
generate ((Depth -> [ByteString]) -> Series m ByteString)
-> (Depth -> [ByteString]) -> Series m ByteString
forall a b. (a -> b) -> a -> b
$ \Depth
d ->
    ([[Char]] -> ByteString) -> [[[Char]]] -> [ByteString]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ([Char] -> ByteString
L8.pack ([Char] -> ByteString)
-> ([[Char]] -> [Char]) -> [[Char]] -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Char]] -> [Char]
unwords) ([[[Char]]] -> [ByteString])
-> ([Char] -> [[[Char]]]) -> [Char] -> [ByteString]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Char]] -> [[[Char]]]
forall a. [a] -> [[a]]
inits ([[Char]] -> [[[Char]]])
-> ([Char] -> [[Char]]) -> [Char] -> [[[Char]]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Depth -> [[Char]] -> [[Char]]
forall a. Depth -> [a] -> [a]
take Depth
d ([[Char]] -> [[Char]])
-> ([Char] -> [[Char]]) -> [Char] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [[Char]] -> [[Char]]
forall a. [a] -> [a]
cycle ([[Char]] -> [[Char]])
-> ([Char] -> [[Char]]) -> [Char] -> [[Char]]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Char] -> [[Char]]
words ([Char] -> [ByteString]) -> [Char] -> [ByteString]
forall a b. (a -> b) -> a -> b
$
        [Char]
"All work and no play makes Jack a dull boy."