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 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
|
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Data.Streaming.ByteString.BuilderSpec
( spec
) where
import qualified Data.ByteString as S
import Data.ByteString.Char8 ()
import qualified Data.ByteString.Builder as B
import Data.ByteString.Builder (Builder)
import qualified Data.ByteString.Builder.Internal as B
import qualified Data.ByteString.Lazy as L
import Data.ByteString.Lazy.Char8 ()
import Data.IORef
import Data.Maybe
import Data.Monoid
import Test.Hspec
import Test.Hspec.QuickCheck (prop)
import Data.Streaming.ByteString.Builder
tester :: BufferAllocStrategy -> [Builder] -> IO [S.ByteString]
tester strat builders0 = do
(recv, finish) <- newBuilderRecv strat
let loop front [] = do
mbs <- finish
return $ front $ maybe [] return mbs
loop front0 (bu:bus) = do
popper <- recv bu
let go front = do
bs <- popper
if S.null bs
then loop front bus
else go (front . (bs:))
go front0
loop id builders0
testerFlush :: BufferAllocStrategy -> [Maybe Builder] -> IO [Maybe S.ByteString]
testerFlush strat builders0 = do
(recv, finish) <- newBuilderRecv strat
let loop front [] = do
mbs <- finish
return $ front $ maybe [] (return . Just) mbs
loop front0 (mbu:bus) = do
popper <- recv $ fromMaybe B.flush mbu
let go front = do
bs <- popper
if S.null bs
then
case mbu of
Nothing -> loop (front . (Nothing:)) bus
Just _ -> loop front bus
else go (front . (Just bs:))
go front0
loop id builders0
builderSpec :: Spec
builderSpec = do
prop "idempotent to toLazyByteString" $ \bss' -> do
let bss = map S.pack bss'
let builders = map B.byteString bss
let lbs = B.toLazyByteString $ mconcat builders
outBss <- tester defaultStrategy builders
L.fromChunks outBss `shouldBe` lbs
it "works for large input" $ do
let builders = replicate 10000 (B.byteString "hello world!")
let lbs = B.toLazyByteString $ mconcat builders
outBss <- tester defaultStrategy builders
L.fromChunks outBss `shouldBe` lbs
it "works for lazy bytestring insertion" $ do
let builders = replicate 10000 (B.lazyByteStringInsert "hello world!")
let lbs = B.toLazyByteString $ mconcat builders
outBss <- tester defaultStrategy builders
L.fromChunks outBss `shouldBe` lbs
prop "works for strict bytestring insertion" $ \bs' -> do
let bs = S.pack bs'
let builders = replicate 10000 (B.byteStringCopy bs `Data.Monoid.mappend` B.byteStringInsert bs)
let lbs = B.toLazyByteString $ mconcat builders
outBss <- tester defaultStrategy builders
L.fromChunks outBss `shouldBe` lbs
it "flush shouldn't bring in empty strings." $ do
let dat = ["hello", "world"]
builders = map ((`mappend` B.flush) . B.byteString) dat
out <- tester defaultStrategy builders
dat `shouldBe` out
prop "flushing" $ \bss' -> do
let bss = concatMap (\bs -> [Just $ S.pack bs, Nothing]) $ filter (not . null) bss'
let builders = map (fmap B.byteString) bss
outBss <- testerFlush defaultStrategy builders
outBss `shouldBe` bss
it "large flush input" $ do
let lbs = L.pack $ concat $ replicate 100000 [0..255]
chunks = map (Just . B.byteString) (L.toChunks lbs)
bss <- testerFlush defaultStrategy chunks
L.fromChunks (catMaybes bss) `shouldBe` lbs
spec :: Spec
spec =
describe "Data.Streaming.ByteString.Builder" $ do
builderSpec
let prop_idempotent i bss' = do
let bss = mconcat (map (B.byteString . S.pack) bss')
ior <- newIORef []
toByteStringIOWith 16
(\s -> do let s' = S.copy s
s' `seq` modifyIORef ior (s' :))
bss
chunks <- readIORef ior
let have = L.unpack (L.fromChunks (reverse chunks))
want = L.unpack (B.toLazyByteString bss)
(i, have) `shouldBe` (i, want)
prop "toByteStringIO idempotent to toLazyByteString" (prop_idempotent (0::Int))
it "toByteStringIO idempotent to toLazyBytestring, specific case" $ do
let bss' = replicate 10 [0..255]
mapM_ (\i -> prop_idempotent i bss') [(1::Int)..100]
|