snappy / tests / Properties.hs

{-# LANGUAGE FlexibleInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

import Control.Applicative ((<$>), (<*>))
import Functions (rechunk)
import Test.Framework (defaultMain)
import Test.Framework.Providers.QuickCheck2 (testProperty)
import Test.QuickCheck (Arbitrary(..))
import qualified Codec.Compression.Snappy as B
import qualified Codec.Compression.Snappy.Lazy as L
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L

instance Arbitrary B.ByteString where
    arbitrary = B.pack <$> arbitrary

instance Arbitrary L.ByteString where
    arbitrary = smallChunk <$> arbitrary <*> arbitrary

s_roundtrip bs = B.decompress (B.compress bs) == bs

newtype Compressed a = Compressed { compressed :: a }
    deriving (Eq, Ord)

instance Show a => Show (Compressed a)
    where show (Compressed a) = "Compressed " ++ show a

instance Arbitrary (Compressed B.ByteString) where
    arbitrary = (Compressed . B.compress) <$> arbitrary

compress_eq n bs = L.fromChunks [B.compress bs] == L.compress (smallChunk n bs)
decompress_eq n (Compressed bs) =
    L.fromChunks [B.decompress bs] == L.decompress (smallChunk n bs)

t_rechunk n bs = L.fromChunks [bs] == smallChunk n bs

l_roundtrip bs = L.decompress (L.compress bs) == bs

smallChunk n = rechunk ((n `mod` 63) + 1)

main = defaultMain tests

tests = [
    testProperty "s_roundtrip" s_roundtrip
  , testProperty "t_rechunk" t_rechunk
  , testProperty "compress_eq" compress_eq
  , testProperty "decompress_eq" decompress_eq
  , testProperty "l_roundtrip" l_roundtrip
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.