Guest User

Untitled

a guest
Jun 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. -- This is a snippet cut-and-paste from a larger code body, might not compile or
  2. -- work as expected as such
  3.  
  4. import Prelude hiding (pi)
  5.  
  6. import Data.Bits
  7. import Data.Word (Word32)
  8.  
  9. import Foreign.C.Types (CSize)
  10. import Foreign.Marshal.Array (withArrayLen)
  11. import Foreign.Marshal.Utils (with)
  12. import Foreign.Ptr (Ptr)
  13. import Foreign.Storable (peek)
  14.  
  15. import Test.QuickCheck
  16.  
  17. foreign import ccall unsafe "hashword" fHashWord
  18. :: Ptr Word32 -> CSize -> Word32 -> IO Word32
  19.  
  20. -- | A wrapper for native 'hashword'
  21. hashWord :: [Word32] -- ^ Input values to hash
  22. -> Word32 -- ^ Initial salt
  23. -> IO Word32 -- ^ Hash result
  24. hashWord as i = withArrayLen as $ \l p -> fHashWord p (fromIntegral l) i
  25.  
  26. foreign import ccall unsafe "hashword2" fHashWord2
  27. :: Ptr Word32 -> CSize -> Ptr Word32 -> Ptr Word32 -> IO ()
  28.  
  29. -- | A wrapper for native 'hashword2'
  30. hashWord2 :: [Word32] -- ^ Input values to hash
  31. -> Word32 -- ^ First salt
  32. -> Word32 -- ^ Second salt
  33. -> IO (Word32, Word32) -- ^ Hashes
  34. hashWord2 as i j = withArrayLen as $ \las pas ->
  35. with i $ \pi -> do
  36. j' <- with j $ \pj -> do
  37. fHashWord2 pas (fromIntegral las) pi pj
  38. peek pj
  39.  
  40. i' <- peek pi
  41. return (i', j')
  42.  
  43. prop_hashword_fst_hashword2 :: [Word32] -> Word32 -> IO Bool
  44. prop_hashword_fst_hashword2 a i = do
  45. h <- hashWord a i
  46. h' <- fst `fmap` hashWord2 a i 0
  47.  
  48. return $ h == h'
  49.  
  50. main = quickCheck (prop_hashword_fst_hashword2 :: [Word32] -> Word32 -> IO Bool)
Add Comment
Please, Sign In to add comment