module Octet (
Octet(..),
octet_properties,
- octet_tests,
- )
+ octet_tests )
where
-import Test.HUnit (assertEqual)
-import Test.Framework (Test, testGroup)
-import Test.Framework.Providers.HUnit (testCase)
-import Test.Framework.Providers.QuickCheck2 (testProperty)
-import Test.QuickCheck (Arbitrary(..), Gen, Property, (==>))
+import Test.Tasty ( TestTree, testGroup )
+import Test.Tasty.HUnit ( (@?=), testCase )
+import Test.Tasty.QuickCheck (
+ Arbitrary( arbitrary ),
+ Gen,
+ Property,
+ (==>),
+ testProperty )
-import Bit as B (Bit(..))
-import Maskable (Maskable(..))
-import Maskbits (Maskbits(..))
+import Bit as B( Bit( Zero, One) )
+import Maskable( Maskable( apply_mask) )
+import Maskbits(
+ Maskbits( Zero, One, Two, Three, Four, Five, Six, Seven, Eight ) )
-- | An Octet consists of eight bits. For our purposes, the most
-- significant bit will come "first." That is, b1 is in the 2^7
b6 :: Bit,
b7 :: Bit,
b8 :: Bit }
- deriving (Eq)
+ deriving (Eq, Ord)
instance Show 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 =
instance Read Octet where
- readsPrec _ = \s ->
+ readsPrec _ s =
case (reads s :: [(Int, String)]) of
[] -> []
(x,leftover):_ -> go x leftover
where
go :: Int -> String -> [(Octet, String)]
- go y s
- | y < minBound || y > maxBound = []
- | otherwise = [(toEnum y, s)]
+ go x' leftover'
+ | x' < minBound || x' > maxBound = []
+ | otherwise = [(toEnum x', leftover')]
-- Test lists.
-octet_tests :: Test
+octet_tests :: TestTree
octet_tests =
testGroup "Octet Tests" [
- test_octet_from_int1,
+ test_octet_to_enum1,
test_octet_mask1,
test_octet_mask2 ]
-octet_properties :: Test
+octet_properties :: TestTree
octet_properties =
testGroup
"Octet Properties "
- [ testProperty
- "fromEnum/toEnum are inverses"
- prop_from_enum_to_enum_inverses,
- testProperty
- "read/show are inverses"
- prop_read_show_inverses ]
+ [ prop_from_enum_to_enum_inverses,
+ prop_read_show_inverses,
+ prop_ord_instances_agree ]
-- QuickCheck properties
-prop_from_enum_to_enum_inverses :: Int -> Property
-prop_from_enum_to_enum_inverses x =
- (0 <= x) && (x <= 255) ==>
- fromEnum (toEnum x :: Octet) == x
-
-prop_read_show_inverses :: Int -> Property
-prop_read_show_inverses x =
- (0 <= x) && (x <= 255) ==> x' == x
+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 <= 255) ==>
+ fromEnum (toEnum x :: Octet) == x
+
+prop_read_show_inverses :: TestTree
+prop_read_show_inverses =
+ testProperty "read and show are inverses" prop
where
- oct :: Octet
- oct = read $ show x
+ prop :: Int -> Property
+ prop x = (0 <= x) && (x <= 255) ==> x' == x
+ where
+ oct :: Octet
+ oct = read $ show x
+
+ 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)
- x' :: Int
- x' = read $ show oct
+ oct1 = toEnum x :: Octet
+ oct2 = toEnum y :: Octet
+ ord' = (oct1 <= oct2)
-- HUnit Tests
-test_octet_from_int1 :: Test
-test_octet_from_int1 =
- testCase desc $ assertEqual desc oct1 oct2
+test_octet_to_enum1 :: TestTree
+test_octet_to_enum1 =
+ testCase desc $ actual @?= expected
where
- desc = "octet_from_int 128 should parse as 10000000"
- oct1 = Octet B.One B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero
- oct2 = toEnum 128
+ desc = "toEnum 128 should parse as 10000000"
+ expected = Octet B.One B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero
+ actual = toEnum 128 :: Octet
+
-test_octet_mask1 :: Test
+test_octet_mask1 :: TestTree
test_octet_mask1 =
- testCase desc $
- assertEqual desc oct2 (apply_mask oct1 Four B.Zero)
+ testCase desc $ actual @?= expected
where
desc = "The network bits of 255/4 should equal 240"
- oct1 = toEnum 255
- oct2 = toEnum 240 :: Octet
+ expected = toEnum 240 :: Octet
+ actual = apply_mask (toEnum 255) Four B.Zero :: Octet
-test_octet_mask2 :: Test
+test_octet_mask2 :: TestTree
test_octet_mask2 =
- testCase desc $
- assertEqual desc oct2 (apply_mask oct1 Maskbits.One B.Zero)
+ testCase desc $ actual @?= expected
where
desc = "The network bits of 255/1 should equal 128"
- oct1 = toEnum 255
- oct2 = toEnum 128 :: Octet
+ expected = toEnum 128 :: Octet
+ actual = apply_mask (toEnum 255) Maskbits.One B.Zero :: Octet