+ oct1 = fromEnum (octet1 addr)
+ 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)
+
+-- | Given two addresses, find the number of the most significant bit
+-- where they differ. If the addresses are the same, return
+-- Maskbits.Zero.
+most_sig_bit_different :: IPv4Address -> IPv4Address -> Maskbits
+most_sig_bit_different addr1 addr2
+ | addr1 == addr2 = Maskbits.Zero
+ | m1 /= n1 = Maskbits.One
+ | m2 /= n2 = Two
+ | m3 /= n3 = Three
+ | m4 /= n4 = Four
+ | m5 /= n5 = Five
+ | m6 /= n6 = Six
+ | m7 /= n7 = Seven
+ | m8 /= n8 = Eight
+ | m9 /= n9 = Nine
+ | m10 /= n10 = Ten
+ | m11 /= n11 = Eleven
+ | m12 /= n12 = Twelve
+ | m13 /= n13 = Thirteen
+ | m14 /= n14 = Fourteen
+ | m15 /= n15 = Fifteen
+ | m16 /= n16 = Sixteen
+ | m17 /= n17 = Seventeen
+ | m18 /= n18 = Eighteen
+ | m19 /= n19 = Nineteen
+ | m20 /= n20 = Twenty
+ | m21 /= n21 = TwentyOne
+ | m22 /= n22 = TwentyTwo
+ | m23 /= n23 = TwentyThree
+ | m24 /= n24 = TwentyFour
+ | m25 /= n25 = TwentyFive
+ | m26 /= n26 = TwentySix
+ | m27 /= n27 = TwentySeven
+ | m28 /= n28 = TwentyEight
+ | m29 /= n29 = TwentyNine
+ | m30 /= n30 = Thirty
+ | m31 /= n31 = ThirtyOne
+ | m32 /= n32 = ThirtyTwo
+ | otherwise = Maskbits.Zero
+ where
+ m1 = (b1 oct1a)
+ m2 = (b2 oct1a)
+ m3 = (b3 oct1a)
+ m4 = (b4 oct1a)
+ m5 = (b5 oct1a)
+ m6 = (b6 oct1a)
+ m7 = (b7 oct1a)
+ m8 = (b8 oct1a)
+ m9 = (b1 oct2a)
+ m10 = (b2 oct2a)
+ m11 = (b3 oct2a)
+ m12 = (b4 oct2a)
+ m13 = (b5 oct2a)
+ m14 = (b6 oct2a)
+ m15 = (b7 oct2a)
+ m16 = (b8 oct2a)
+ m17 = (b1 oct3a)
+ m18 = (b2 oct3a)
+ m19 = (b3 oct3a)
+ m20 = (b4 oct3a)
+ m21 = (b5 oct3a)
+ m22 = (b6 oct3a)
+ m23 = (b7 oct3a)
+ m24 = (b8 oct3a)
+ m25 = (b1 oct4a)
+ m26 = (b2 oct4a)
+ m27 = (b3 oct4a)
+ m28 = (b4 oct4a)
+ m29 = (b5 oct4a)
+ m30 = (b6 oct4a)
+ m31 = (b7 oct4a)
+ m32 = (b8 oct4a)
+ oct1a = (octet1 addr1)
+ oct2a = (octet2 addr1)
+ oct3a = (octet3 addr1)
+ oct4a = (octet4 addr1)
+ n1 = (b1 oct1b)
+ n2 = (b2 oct1b)
+ n3 = (b3 oct1b)
+ n4 = (b4 oct1b)
+ n5 = (b5 oct1b)
+ n6 = (b6 oct1b)
+ n7 = (b7 oct1b)
+ n8 = (b8 oct1b)
+ n9 = (b1 oct2b)
+ n10 = (b2 oct2b)
+ n11 = (b3 oct2b)
+ n12 = (b4 oct2b)
+ n13 = (b5 oct2b)
+ n14 = (b6 oct2b)
+ n15 = (b7 oct2b)
+ n16 = (b8 oct2b)
+ n17 = (b1 oct3b)
+ n18 = (b2 oct3b)
+ n19 = (b3 oct3b)
+ n20 = (b4 oct3b)
+ n21 = (b5 oct3b)
+ n22 = (b6 oct3b)
+ n23 = (b7 oct3b)
+ n24 = (b8 oct3b)
+ n25 = (b1 oct4b)
+ n26 = (b2 oct4b)
+ n27 = (b3 oct4b)
+ n28 = (b4 oct4b)
+ n29 = (b5 oct4b)
+ n30 = (b6 oct4b)
+ n31 = (b7 oct4b)
+ n32 = (b8 oct4b)
+ oct1b = (octet1 addr2)
+ oct2b = (octet2 addr2)
+ oct3b = (octet3 addr2)
+ oct4b = (octet4 addr2)
+
+
+-- Test lists.
+ipv4address_tests :: TestTree
+ipv4address_tests =
+ testGroup "IPv4 Address Tests" [
+ test_enum,
+ test_maxBound,
+ 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 ]
+
+-- QuickCheck properties
+prop_from_enum_to_enum_inverses :: TestTree
+prop_from_enum_to_enum_inverses =
+ testProperty "fromEnum and toEnum are inverses" prop
+ where
+ prop :: Int -> Property
+ prop x =
+ (0 <= x) && (x <= 2^(32 :: Integer) - 1) ==>
+ fromEnum (toEnum 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 :: Octet
+ oct2 = toEnum b :: Octet
+ oct3 = toEnum c :: Octet
+ oct4 = toEnum d :: Octet
+
+
+test_minBound :: TestTree
+test_minBound =
+ testCase desc $ actual @?= expected
+ where
+ desc = "minBound should be 0.0.0.0"
+ expected = mk_testaddr 0 0 0 0
+ actual = minBound :: IPv4Address
+
+
+test_maxBound :: TestTree
+test_maxBound =
+ testCase desc $ actual @?= expected
+ where
+ desc = "maxBound should be 255.255.255.255"
+ expected = mk_testaddr 255 255 255 255
+ actual = maxBound :: IPv4Address
+
+
+test_enum :: TestTree
+test_enum =
+ testCase desc $ actual @?= expected
+ where
+ desc = "enumerating a /24 gives the correct addresses"
+ expected = ["192.168.0." ++ (show x) | x <- [0..255::Int] ]
+ lb = mk_testaddr 192 168 0 0
+ ub = mk_testaddr 192 168 0 255
+ actual = map show [lb..ub]
+
+
+test_most_sig_bit_different1 :: TestTree
+test_most_sig_bit_different1 =
+ testCase desc $ actual @?= expected
+ where
+ desc = "10.1.1.0 and 10.1.0.0 differ in bit 24"
+ addr1 = mk_testaddr 10 1 1 0
+ addr2 = (mk_testaddr 10 1 0 0)
+ expected = TwentyFour
+ actual = most_sig_bit_different addr1 addr2
+
+
+
+test_most_sig_bit_different2 :: TestTree
+test_most_sig_bit_different2 =
+ testCase desc $ actual @?= expected
+ where
+ desc = "10.1.2.0 and 10.1.1.0 differ in bit 23"
+ addr1 = mk_testaddr 10 1 2 0
+ addr2 = mk_testaddr 10 1 1 0
+ expected = TwentyThree
+ actual = most_sig_bit_different addr1 addr2
+
+
+test_to_enum :: TestTree
+test_to_enum =
+ testCase desc $ actual @?= expected
+ where
+ desc = "192.168.0.0 in base-10 is 3232235520"
+ expected = mk_testaddr 192 168 0 0
+ actual = toEnum 3232235520 :: 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