Created
June 10, 2013 12:44
Minimal failing test suite for hashable 1.2.0.7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: hashable-is-broken | |
version: 0.1.0.0 | |
build-type: Simple | |
cabal-version: >=1.10 | |
executable hashable-is-broken | |
main-is: Main.hs | |
build-depends: base == 4.5.*, | |
bytestring, | |
hashable == 1.2.0.7, | |
containers | |
default-language: Haskell2010 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE BangPatterns #-} | |
{-# LANGUAGE OverloadedStrings #-} | |
import Control.Monad (forM_) | |
import Data.Bits | |
import Data.ByteString.Char8 (ByteString) | |
import qualified Data.ByteString.Char8 as S | |
import Data.Hashable (hash) | |
import Data.List (foldl', intercalate) | |
import qualified Data.Map as Map | |
import System.Info | |
import Text.Printf | |
------------------------------------------------------------------------------ | |
-- A 10MB string | |
seedString :: ByteString | |
seedString = S.replicate 10000000 'a' | |
{-# NOINLINE seedString #-} | |
------------------------------------------------------------------------------ | |
chopEvery :: Int -> ByteString -> [ByteString] | |
chopEvery n = go id | |
where | |
go dl s | S.null s = dl [] | |
| otherwise = let (x, y) = S.splitAt n s | |
dl' = dl . (x :) | |
in go dl' y | |
------------------------------------------------------------------------------ | |
pad :: Int -> String -> String | |
pad n s = if l >= n then s else s ++ b | |
where | |
l = length s | |
b = replicate (n - l) ' ' | |
------------------------------------------------------------------------------ | |
testChop :: Int -> IO () | |
testChop n = analyse $! foldl' f Map.empty $ chopEvery n seedString | |
where | |
f !mp !s = let !h = hash s | |
in Map.insertWith' (+) h (1::Int) mp | |
prefix = "Chop " ++ show n | |
analyse mp | Map.size mp == 1 = putStrLn $ prefix ++ ": ok" | |
| otherwise = dumpFailure mp | |
dumpFailure mp = do | |
putStrLn $ prefix ++ ": FAILURE!" | |
putStrLn $ pad 18 "hashcode" ++ "count of occurrences" | |
forM_ (Map.toList mp) $ \(k,v) -> do | |
let a = printf "%016x" k | |
putStrLn $ pad 18 a ++ show v | |
------------------------------------------------------------------------------ | |
printInfo :: IO () | |
printInfo = putStrLn $ intercalate " " [ compilerName, show compilerVersion | |
, os , arch] | |
------------------------------------------------------------------------------ | |
main :: IO () | |
main = do | |
printInfo | |
forM_ [2..21] testChop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample output from my machine: