]> gitweb.michael.orlitzky.com - hath.git/blobdiff - src/IPv4Address.hs
Add Ord instance tests for IPv4Address.
[hath.git] / src / IPv4Address.hs
index d262a5c0537ea47092d0255ab417b6516d1cdab8..7e502125722cbfb354a53d517acd206a3fe678ad 100644 (file)
@@ -6,25 +6,31 @@ module IPv4Address(
 where
 
 
-import Test.QuickCheck ( Gen ) -- Not re-exported by tasty
 import Test.Tasty ( TestTree, testGroup )
 import Test.Tasty.HUnit ( (@?=), testCase )
 import Test.Tasty.QuickCheck (
-  Arbitrary(..),
+  Arbitrary( arbitrary ),
+  Gen,
   Property,
   (==>),
   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
@@ -194,10 +200,10 @@ instance Enum IPv4Address where
       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
+      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,6 +341,10 @@ 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
@@ -358,10 +368,10 @@ 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 +432,46 @@ test_to_enum =
   where
     desc     = "192.168.0.0 in base-10 is 3232235520"
     expected = mk_testaddr 192 168 0 0
-    actual   = toEnum 3232235520
+    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