]> gitweb.michael.orlitzky.com - hath.git/blobdiff - src/IPv4Address.hs
Added a QuickCheck property verifying that all Cidrs contain themselves.
[hath.git] / src / IPv4Address.hs
index 31ad7bd43965a56f7d243ab932ddbb4dc79dac33..55602aa8a9616197d6ecf038e6b071eec87e79d3 100644 (file)
@@ -1,5 +1,6 @@
 module IPv4Address
 ( ipv4address_from_octets,
+  ipv4address_tests,
   IPv4Address(None),
   max_octet1,
   max_octet2,
@@ -14,8 +15,10 @@ module IPv4Address
   octet2,
   octet3,
   octet4
-)
-  where
+) where
+
+import Test.HUnit
+import Test.QuickCheck
 
 import qualified Bit as B
 import Maskbits
@@ -41,6 +44,18 @@ instance Show IPv4Address where
           oct4 = (octet4 addr)
 
 
+instance Arbitrary IPv4Address where
+    arbitrary = do
+      oct1 <- arbitrary :: Gen Octet
+      oct2 <- arbitrary :: Gen Octet
+      oct3 <- arbitrary :: Gen Octet
+      oct4 <- arbitrary :: Gen Octet
+      return (IPv4Address oct1 oct2 oct3 oct4)
+
+    coarbitrary _ = variant 0
+
+
+
 -- We don't export our constructor so this function is the only
 -- way to construct an address from octets. As a result, we can
 -- return IPv4Address.None in response to being passed one of more
@@ -339,3 +354,30 @@ most_sig_bit_different addr1 addr2
     oct2b = (octet2 addr2)
     oct3b = (octet3 addr2)
     oct4b = (octet4 addr2)
+
+
+
+-- HUnit Tests
+mk_testaddr :: Int -> Int -> Int -> Int -> IPv4Address
+mk_testaddr a b c d =
+    IPv4Address oct1 oct2 oct3 oct4
+    where
+      oct1 = octet_from_int a
+      oct2 = octet_from_int b
+      oct3 = octet_from_int c
+      oct4 = octet_from_int d
+
+
+test_most_sig_bit_different1 :: Test
+test_most_sig_bit_different1 =
+    TestCase $ assertEqual "10.1.1.0 and 10.1.0.0 differ in bit 24" TwentyFour (most_sig_bit_different (mk_testaddr 10 1 1 0) (mk_testaddr 10 1 0 0))
+
+
+test_most_sig_bit_different2 :: Test
+test_most_sig_bit_different2 =
+    TestCase $ assertEqual "10.1.2.0 and 10.1.1.0 differ in bit 23" TwentyThree (most_sig_bit_different (mk_testaddr 10 1 2 0) (mk_testaddr 10 1 1 0))
+
+
+ipv4address_tests :: [Test]
+ipv4address_tests = [ test_most_sig_bit_different1,
+                      test_most_sig_bit_different2 ]