From: Michael Orlitzky Date: Fri, 14 May 2010 05:16:34 +0000 (-0400) Subject: Added Arbitrary instance definitions for the main data types. X-Git-Tag: 0.0.1~43 X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=6257f1c59c5c9663f8d95996c1c35362417def71;p=hath.git Added Arbitrary instance definitions for the main data types. --- diff --git a/src/Bit.hs b/src/Bit.hs index d7fb679..3799516 100644 --- a/src/Bit.hs +++ b/src/Bit.hs @@ -1,5 +1,8 @@ module Bit where +import Test.QuickCheck + + data Bit = None | Zero | One deriving (Eq) @@ -8,7 +11,12 @@ instance Show Bit where show Zero = "0" show One = "1" - + +instance Arbitrary Bit where + arbitrary = elements [ Zero, One ] + coarbitrary _ = variant 0 + + bit_to_int :: Bit -> Int bit_to_int None = -1 bit_to_int Zero = 0 diff --git a/src/Cidr.hs b/src/Cidr.hs index c0c639a..7bb3ac2 100644 --- a/src/Cidr.hs +++ b/src/Cidr.hs @@ -7,6 +7,7 @@ module Cidr import Data.List (nubBy) import Test.HUnit +import Test.QuickCheck import IPv4Address import ListUtils @@ -25,6 +26,15 @@ instance Show Cidr where show cidr = (show (ipv4address cidr)) ++ "/" ++ (show (maskbits cidr)) +instance Arbitrary Cidr where + arbitrary = do + ipv4 <- arbitrary :: Gen IPv4Address + mask <- arbitrary :: Gen Maskbits + return (Cidr ipv4 mask) + + coarbitrary _ = variant 0 + + -- Two CIDR ranges are equivalent if they have the same network bits -- and the masks are the same. equivalent :: Cidr -> Cidr -> Bool diff --git a/src/IPv4Address.hs b/src/IPv4Address.hs index e91e673..55602aa 100644 --- a/src/IPv4Address.hs +++ b/src/IPv4Address.hs @@ -18,6 +18,7 @@ module IPv4Address ) where import Test.HUnit +import Test.QuickCheck import qualified Bit as B import Maskbits @@ -43,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 diff --git a/src/Maskbits.hs b/src/Maskbits.hs index ed358b9..59efc2d 100644 --- a/src/Maskbits.hs +++ b/src/Maskbits.hs @@ -4,6 +4,8 @@ module Maskbits maskbits_from_string ) where +import Test.QuickCheck + -- A type representing the number of bits in a CIDR netmask. data Maskbits = None | Zero @@ -79,6 +81,44 @@ instance Show Maskbits where show ThirtyTwo = "32" +instance Arbitrary Maskbits where + arbitrary = elements [ 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 ] + + coarbitrary _ = variant 0 + + -- There are only 32 bits in an IPv4 address, so there -- can't be more bits than that in the mask. diff --git a/src/Octet.hs b/src/Octet.hs index 04caae5..b3e6a0f 100644 --- a/src/Octet.hs +++ b/src/Octet.hs @@ -1,6 +1,7 @@ module Octet where import Test.HUnit +import Test.QuickCheck import Bit @@ -23,6 +24,21 @@ instance Show Octet where show oct = show (octet_to_int oct) +instance Arbitrary Octet where + arbitrary = do + a1 <- arbitrary :: Gen Bit + a2 <- arbitrary :: Gen Bit + a3 <- arbitrary :: Gen Bit + a4 <- arbitrary :: Gen Bit + a5 <- arbitrary :: Gen Bit + a6 <- arbitrary :: Gen Bit + a7 <- arbitrary :: Gen Bit + a8 <- arbitrary :: Gen Bit + return (Octet a1 a2 a3 a4 a5 a6 a7 a8) + + coarbitrary _ = variant 0 + + -- Convert each bit to its integer value, and multiply by the -- appropriate power of two. Sum them up, and we should get an integer -- between 0 and 255.