File: ByteBufferSpec.hs

package info (click to toggle)
haskell-store 0.7.18-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 284 kB
  • sloc: haskell: 2,663; makefile: 9
file content (54 lines) | stat: -rw-r--r-- 2,319 bytes parent folder | download | duplicates (4)
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
{-# LANGUAGE DeriveDataTypeable #-}
{-# LANGUAGE OverloadedStrings #-}
module System.IO.ByteBufferSpec where

import           Control.Exception
import qualified Data.ByteString as BS
import           Data.Typeable (Typeable)
import qualified System.IO.ByteBuffer as BB
import           Test.Hspec

data MyException = MyException
    deriving (Eq, Show, Typeable)
instance Exception MyException

spec :: Spec
spec = describe "ByteBuffer" $ do
    it "can grow to store a value and return it." $ BB.with (Just 0) $ \ bb -> do
        let bs = "some bytestring"
        BB.copyByteString bb bs
        bs' <- BB.consume bb (BS.length bs)
        bs' `shouldBe` Right bs
        bbIsEmpty bb
    it "should request more input when needed." $ BB.with (Just 0) $ \ bb -> do
        let bs = "some bytestring"
        BB.copyByteString bb bs
        bs' <- BB.consume bb (2 * BS.length bs)
        bs' `shouldBe` Left (BS.length bs)
        BB.copyByteString bb bs
        bs'' <- BB.consume bb (2 * BS.length bs)
        bs'' `shouldBe` Right (BS.concat [bs, bs])
        bbIsEmpty bb
    it "should not grow if bytes can be freed." $
        let bs1 = "12345"
            bs2 = "67810" -- what about nine? 7 8 9!
        in BB.with (Just $ BS.length bs1) $ \ bb -> do
            BB.copyByteString bb bs1
            bs1' <- BB.consume bb (BS.length bs1)
            BB.copyByteString bb bs2
            bs2' <- BB.consume bb (BS.length bs2)
            bs1' `shouldBe` Right bs1
            bs2' `shouldBe` Right bs2
            bbSize <- BB.totalSize bb
            bbSize `shouldBe` BS.length bs1
            bbIsEmpty bb
    it "should raise a ByteBufferException when used after freed" $ BB.with Nothing $ \bb -> do
        BB.free bb
        BB.totalSize bb `shouldThrow` \(BB.ByteBufferException loc e) ->
            loc == "free" && e == "ByteBuffer has explicitly been freed and is no longer valid."
    it "should raise a ByteBufferException after a failed operation" $ BB.with Nothing $ \bb -> do
        BB.copyByteString bb (throw MyException) `shouldThrow` (\MyException -> True)
        BB.consume bb 10 `shouldThrow` \(BB.ByteBufferException loc e) ->
            loc == "copyByteString" && e == show MyException
bbIsEmpty :: BB.ByteBuffer -> Expectation
bbIsEmpty bb = BB.isEmpty bb >>= (`shouldBe` True)