From: Michael Orlitzky Date: Fri, 14 Apr 2017 04:00:17 +0000 (-0400) Subject: Use the derived instance for Ord and add a test case. X-Git-Tag: 0.4.0~16 X-Git-Url: http://gitweb.michael.orlitzky.com/?p=hath.git;a=commitdiff_plain;h=2fcbd5144dae4837b26638baa170ae97f3ee1ea2 Use the derived instance for Ord and add a test case. There was a bug in my implementation of Ord for Octets that was pointed out by Michael McKibben. As a result, the octets corresponding to (for example) 2 and 4 would compare incorrectly. This is fixed by simply adopting the derived Ord instance for Octets. Why didn't I do that in the first place? With the bug fixed, I've added a QuickCheck property to ensure that the Ord instances for Octet and Int agree when the Int is between 0 and 255. --- diff --git a/src/Octet.hs b/src/Octet.hs index 34f9d4f..affce39 100644 --- a/src/Octet.hs +++ b/src/Octet.hs @@ -30,7 +30,7 @@ data Octet = b6 :: Bit, b7 :: Bit, b8 :: Bit } - deriving (Eq) + deriving (Eq, Ord) instance Show Octet where @@ -83,19 +83,6 @@ instance Maskable Octet where apply_mask oct _ _ = oct -instance Ord Octet where - (Octet x1 x2 x3 x4 x5 x6 x7 x8) <= (Octet y1 y2 y3 y4 y5 y6 y7 y8) - | x1 > y1 = False - | x2 > y2 = False - | x3 > y3 = False - | x4 > y4 = False - | x5 > y5 = False - | x6 > y6 = False - | x7 > y7 = False - | x8 > y8 = False - | otherwise = True - - instance Bounded Octet where -- | The octet with the least possible value. minBound = @@ -163,7 +150,8 @@ octet_properties = testGroup "Octet Properties " [ prop_from_enum_to_enum_inverses, - prop_read_show_inverses ] + prop_read_show_inverses, + prop_ord_instances_agree ] -- QuickCheck properties prop_from_enum_to_enum_inverses :: TestTree @@ -188,6 +176,20 @@ prop_read_show_inverses = x' :: Int x' = read $ show oct +-- | Ensure that the Ord instance for Octets agrees with the Ord +-- instance for Int (i.e. that numerical comparisons work). +prop_ord_instances_agree :: TestTree +prop_ord_instances_agree = + testProperty "the Octet and Int Ord instances agree" prop + where + prop :: Int -> Int -> Property + prop x y = (0 <= x) && (x <= 255) && (0 <= y) && (y <= 255) ==> ord == ord' + where + ord = (x <= y) + + oct1 = toEnum x :: Octet + oct2 = toEnum y :: Octet + ord' = (oct1 <= oct2) -- HUnit Tests test_octet_to_enum1 :: TestTree