X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FIPv4Address.hs;h=38aec8dd39c5250279a05357db753c8aa2247cf2;hb=HEAD;hp=85d86616023b19f3cc936ac2f69f1b65020df880;hpb=71f626d17e4308e00a894d088626bb72322dedec;p=hath.git diff --git a/src/IPv4Address.hs b/src/IPv4Address.hs index 85d8661..f9fd8c2 100644 --- a/src/IPv4Address.hs +++ b/src/IPv4Address.hs @@ -5,14 +5,17 @@ module IPv4Address( most_sig_bit_different ) where -import Data.Word (Word32) +import Data.Int ( Int32, Int64 ) +import Data.Word ( Word32 ) import Test.Tasty ( TestTree, testGroup ) import Test.Tasty.HUnit ( (@?=), testCase ) import Test.Tasty.QuickCheck ( Arbitrary( arbitrary ), Gen, + Large, Property, + Small, (==>), testProperty ) @@ -189,7 +192,7 @@ instance Enum IPv4Address where -- | Convert an 'Int' @x@ to an 'IPv4Address'. Each octet of @x@ is -- right-shifted by the appropriate number of bits, and the fractional -- part is dropped. - toEnum y = + toEnum signed_x = IPv4Address oct1 oct2 oct3 oct4 where -- Convert the input Int to a Word32 before we proceed. On x86, @@ -198,17 +201,17 @@ instance Enum IPv4Address where -- below doesn't work. The Word32 type is unsigned, so we do the -- math on that and then convert everything back to Int later on -- once we have four much-smaller non-negative numbers. - x = fromIntegral y :: Word32 + x = fromIntegral signed_x :: Word32 -- Chop off the higher octets. x1 = x `mod` 2^32, would be -- redundant. - x2 = x `mod` 2^(24 :: Integer) - x3 = x `mod` 2^(16 :: Integer) - x4 = (fromIntegral $ x `mod` 2^(8 :: Integer)) :: Int + x2 = x `mod` (2 ^ (24 :: Integer)) + x3 = x `mod` (2 ^ (16 :: Integer)) + x4 = (fromIntegral $ x `mod` (2 ^ (8 :: Integer))) :: Int -- Perform right-shifts. x4 doesn't need a shift. - shifted_x1 = (fromIntegral $ x `quot` 2^(24 :: Integer)) :: Int - shifted_x2 = (fromIntegral $ x2 `quot` 2^(16 :: Integer)) :: Int - shifted_x3 = fromIntegral $ x3 `quot` 2^(8 :: Integer) :: Int + shifted_x1 = (fromIntegral $ x `quot` (2 ^ (24 :: Integer))) :: Int + shifted_x2 = (fromIntegral $ x2 `quot` (2 ^ (16 :: Integer))) :: Int + shifted_x3 = (fromIntegral $ x3 `quot` (2 ^ (8 :: Integer))) :: Int oct1 = toEnum shifted_x1 :: Octet oct2 = toEnum shifted_x2 :: Octet oct3 = toEnum shifted_x3 :: Octet @@ -223,9 +226,9 @@ instance Enum IPv4Address where oct2 = fromEnum (octet2 addr) oct3 = fromEnum (octet3 addr) oct4 = fromEnum (octet4 addr) - shifted_oct1 = oct1 * 2^(24 :: Integer) - shifted_oct2 = oct2 * 2^(16 :: Integer) - shifted_oct3 = oct3 * 2^(8 :: Integer) + shifted_oct1 = oct1 * (2 ^ (24 :: Integer)) + shifted_oct2 = oct2 * (2 ^ (16 :: Integer)) + shifted_oct3 = oct3 * (2 ^ (8 :: Integer)) -- | Given two addresses, find the number of the most significant bit -- where they differ. If the addresses are the same, return @@ -360,17 +363,42 @@ ipv4address_properties :: TestTree ipv4address_properties = testGroup "IPv4 Address Properties " - [ prop_from_enum_to_enum_inverses ] + [ prop_from_enum_to_enum_inverses_x32, + prop_from_enum_to_enum_inverses_x64 ] -- QuickCheck properties -prop_from_enum_to_enum_inverses :: TestTree -prop_from_enum_to_enum_inverses = - testProperty "fromEnum and toEnum are inverses" prop +-- +-- We have two different tests to show that toEnum and fromEnum are +-- inverses of one another. This part of the code isn't really +-- type-safe, because the stupid Enum class insists that we use a +-- machine 'Int' for our representation. Since IPv4 addresses can +-- correspond to very large 32-bit integers, there's a possibility +-- that our math is wrong in 32- but not 64-bits, and vice-versa. +-- +-- tl;dr we want to ensure that this test passes when the 'Int' type +-- is both 32-bit and 64-bit. + +-- Generate "Small" 64-bit numbers, because almost all 64-bit integers are +-- too large to satisfy our predicate (i.e. also be 32-bit integers). +prop_from_enum_to_enum_inverses_x64 :: TestTree +prop_from_enum_to_enum_inverses_x64 = + testProperty "fromEnum and toEnum are inverses (x64)" prop where - prop :: Int -> Property + prop :: (Small Int64) -> Property prop x = - (0 <= x) && (x <= 2^(32 :: Integer) - 1) ==> - fromEnum (toEnum x :: IPv4Address) == x + 0 <= x && x <= (2 ^ (32 :: Integer)) - 1 ==> + fromIntegral (fromEnum (toEnum (fromIntegral x) :: IPv4Address)) == x + +-- According to the QuickCheck documentation, we need the "Large" +-- modifier to ensure that the test cases are drawn from the entire +-- range of Int32 values. +prop_from_enum_to_enum_inverses_x32 :: TestTree +prop_from_enum_to_enum_inverses_x32 = + testProperty "fromEnum and toEnum are inverses (x32)" prop + where + prop :: (Large Int32) -> Bool + prop x = + fromIntegral (fromEnum (toEnum (fromIntegral x) :: IPv4Address)) == x -- HUnit Tests mk_testaddr :: Int -> Int -> Int -> Int -> IPv4Address