]> gitweb.michael.orlitzky.com - hath.git/blobdiff - src/IPv4Address.hs
src/IPv4Address.hs: fix the inverse toEnum/fromEnum property on x86.
[hath.git] / src / IPv4Address.hs
index 78bceabc703b39202823d6f9882d3c3fb5d523d1..4399b05f3ecb1728eca31be6eae5e833a23a3b5b 100644 (file)
@@ -5,26 +5,36 @@ module IPv4Address(
   most_sig_bit_different )
 where
 
+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( arbitrary ),
   Gen,
+  Large,
   Property,
+  Small,
   (==>),
   testProperty )
 
-import Maskable (Maskable(..))
-import Maskbits (Maskbits(..))
-import Octet (Octet(..))
+import Maskable ( Maskable( apply_mask) )
+import Maskbits (
+  Maskbits(
+    Zero, One, Two, Three, Four, Five, Six, Seven, Eight,
+    Nine, Ten, Eleven, Twelve, Thirteen, Fourteen, Fifteen, Sixteen,
+    Seventeen, Eighteen, Nineteen, Twenty, TwentyOne, TwentyTwo, TwentyThree,
+    TwentyFour, TwentyFive, TwentySix, TwentySeven, TwentyEight, TwentyNine,
+    Thirty, ThirtyOne, ThirtyTwo ) )
+import Octet ( Octet( b1, b2, b3, b4, b5, b6, b7, b8) )
 
 data IPv4Address =
   IPv4Address { octet1 :: Octet,
                 octet2 :: Octet,
                 octet3 :: Octet,
                 octet4 :: Octet }
-    deriving (Eq)
+    deriving (Eq, Ord)
 
 
 instance Show IPv4Address where
@@ -182,22 +192,30 @@ 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 x =
+  toEnum y =
     IPv4Address oct1 oct2 oct3 oct4
     where
+      -- Convert the input Int to a Word32 before we proceed. On x86,
+      -- the Int that we get could be negative (half of all IP
+      -- addresses correspond to negative numbers), and then the magic
+      -- 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
+
       -- 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 = x `mod` 2^(8  :: Integer)
+      x4 = (fromIntegral $ x `mod` 2^(8  :: Integer)) :: Int
       -- Perform right-shifts. x4 doesn't need a shift.
-      shifted_x1 = x `quot` 2^(24 :: Integer)
-      shifted_x2 = x2 `quot` 2^(16 :: Integer)
-      shifted_x3 = x3 `quot` 2^(8 :: Integer)
-      oct1 = toEnum shifted_x1
-      oct2 = toEnum shifted_x2
-      oct3 = toEnum shifted_x3
-      oct4 = toEnum x4
+      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
+      oct4 = toEnum x4 :: Octet
 
   -- | Convert @addr@ to an 'Int' by converting each octet to an 'Int'
   --   and shifting the result to the left by 0,8.16, or 24 bits.
@@ -335,33 +353,62 @@ ipv4address_tests =
     test_minBound,
     test_most_sig_bit_different1,
     test_most_sig_bit_different2,
+    test_ord_instance1,
+    test_ord_instance2,
+    test_ord_instance3,
+    test_ord_instance4,
     test_to_enum ]
 
 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 :: (Small Int64) -> Property
+    prop 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 :: Int -> Property
+    prop :: (Large Int32) -> Bool
     prop x =
-      (0 <= x) && (x <= 2^(32 :: Integer) - 1) ==>
-        fromEnum (toEnum x :: IPv4Address) == x
+      fromIntegral (fromEnum (toEnum (fromIntegral x) :: IPv4Address)) == x
 
 -- HUnit Tests
 mk_testaddr :: Int -> Int -> Int -> Int -> IPv4Address
 mk_testaddr a b c d =
   IPv4Address oct1 oct2 oct3 oct4
   where
-    oct1 = toEnum a
-    oct2 = toEnum b
-    oct3 = toEnum c
-    oct4 = toEnum d
+    oct1 = toEnum a :: Octet
+    oct2 = toEnum b :: Octet
+    oct3 = toEnum c :: Octet
+    oct4 = toEnum d :: Octet
 
 
 test_minBound :: TestTree
@@ -422,4 +469,50 @@ test_to_enum =
   where
     desc     = "192.168.0.0 in base-10 is 3232235520"
     expected = mk_testaddr 192 168 0 0
-    actual   = toEnum 3232235520
+    -- We declare the big number as Word32 because otherwise, on x86,
+    -- we get a warning that it's too big to fit in a 32-bit integer.
+    -- Ultimately we convert it to a (negative) Int on those systems
+    -- anyway, but the gymnastics declare our intent to the compiler.
+    actual   = toEnum (fromIntegral (3232235520 :: Word32)) :: IPv4Address
+
+
+test_ord_instance1 :: TestTree
+test_ord_instance1 =
+  testCase desc $ actual @?= expected
+  where
+    desc     = "127.0.0.0 is less than 127.0.0.1"
+    addr1 = mk_testaddr 127 0 0 0
+    addr2 = mk_testaddr 127 0 0 1
+    expected = True
+    actual   = addr1 <= addr2
+
+
+test_ord_instance2 :: TestTree
+test_ord_instance2 =
+  testCase desc $ actual @?= expected
+  where
+    desc     = "127.0.0.0 is less than 127.0.1.0"
+    addr1 = mk_testaddr 127 0 0 0
+    addr2 = mk_testaddr 127 0 1 0
+    expected = True
+    actual   = addr1 <= addr2
+
+test_ord_instance3 :: TestTree
+test_ord_instance3 =
+  testCase desc $ actual @?= expected
+  where
+    desc     = "127.0.0.0 is less than 127.1.0.0"
+    addr1 = mk_testaddr 127 0 0 0
+    addr2 = mk_testaddr 127 1 0 0
+    expected = True
+    actual   = addr1 <= addr2
+
+test_ord_instance4 :: TestTree
+test_ord_instance4 =
+  testCase desc $ actual @?= expected
+  where
+    desc     = "127.0.0.0 is less than 128.0.0.0"
+    addr1 = mk_testaddr 127 0 0 0
+    addr2 = mk_testaddr 128 0 0 0
+    expected = True
+    actual   = addr1 <= addr2