----------------------------------------------------------------------------- -- | -- Module : Etherbunny.Packet -- Copyright : (c) Nicholas Burlett 2007 -- License : GPL (see the file LICENSE) -- -- Maintainer : nickburlett@mac.com -- Stability : experimental -- Portability : ghc -- -- Etherbunny tests. -- ----------------------------------------------------------------------------- import Network.Etherbunny.Packet import List import Test.QuickCheck import Text.Printf import Bits import Data.Word import Monad main :: IO () main = mapM_ (\(s,a) -> printf "%-25s: " s >> a) tests wordsFromInt :: (Num a, Bits t, Integral t) => t -> [a] wordsFromInt w = reverse $ unfoldr buildwords (w, bitSize w `div` 8) where buildwords (_,0) = Nothing buildwords (x,c) = Just ( fromIntegral (x .&. 0xFF), (x `shiftR` 8, c -1)) -- | Test that the bytes from wordsToInt are the same as the input bytes -- prop_wordsToIntBytes s = wordsToIntBytes (len s) s == prop_wordstofrom32 :: Word32 -> Bool prop_wordstofrom32 x = (wordsToWord32 . wordsFromInt) x == x prop_wordstofrom16 :: Word16 -> Bool prop_wordstofrom16 x = (wordsToWord16 . wordsFromInt) x == x instance Arbitrary Word32 where arbitrary = liftM fromIntegral (arbitrary :: Gen Int) coarbitrary n = variant 0 . coarbitrary (fromIntegral n ::Word32) instance Arbitrary Word16 where arbitrary = liftM fromIntegral (arbitrary :: Gen Int) coarbitrary n = variant 0 . coarbitrary (fromIntegral n ::Word16) -- and add this to the tests list tests :: [(String, IO ())] tests = [("wordsToWord32 . wordsFromInt", test prop_wordstofrom32) ,("wordsToWord16 . wordsFromInt", test prop_wordstofrom16) ]