packages feed

xxhash-0.0.1: tests/TestSuite.hs

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

module TestSuite where

import Test.Hspec
import Test.Hspec.QuickCheck
import Test.QuickCheck

import Data.Digest.XXHash
import qualified Data.ByteString.Char8 as B
import qualified Data.ByteString.Lazy as L

instance Arbitrary L.ByteString where
    arbitrary = L.pack `fmap` arbitrary
    shrink xs = L.pack `fmap` shrink (L.unpack xs)

suite :: Spec
suite = do
    describe "Hashing a bytestring" $ do
        it "does not explode with sizes up to 2048" $ do
            let strings = take 2048 $ iterate ('A':) "" in
                (length $ map (xxHash' . B.pack) strings) `shouldBe` 2048

        it "works with known pairs" $ do
            xxHash "" `shouldBe` 46947589
            xxHash "the quick brown fox jumps over a pony" `shouldBe` 2069436874
            xxHash' "small" `shouldBe` 2387791210
            xxHash' "" `shouldBe` 46947589
            let as = B.pack . take 500 $ repeat 'A' in
                xxHash' as  `shouldBe` 1719491600

    describe "Fuzzing random strings" $ do
       prop "is deterministic" $  \string ->
            let h = xxHash string in
                (xxHash' $ L.toStrict string) == h &&
                (c_xxHash' $ L.toStrict string) == h