7 import Test.Tasty ( TestTree, testGroup )
8 import Test.Tasty.HUnit ( (@?=), testCase )
9 import Test.Tasty.QuickCheck (
10 Arbitrary( arbitrary ),
16 import Bit as B( Bit( Zero, One) )
17 import Maskable( Maskable( apply_mask) )
19 Maskbits( Zero, One, Two, Three, Four, Five, Six, Seven, Eight ) )
21 -- | An Octet consists of eight bits. For our purposes, the most
22 -- significant bit will come "first." That is, b1 is in the 2^7
23 -- place while b8 is in the 2^0 place.
36 instance Show Octet where
37 show oct = show (fromEnum oct)
40 instance Arbitrary Octet where
42 a1 <- arbitrary :: Gen Bit
43 a2 <- arbitrary :: Gen Bit
44 a3 <- arbitrary :: Gen Bit
45 a4 <- arbitrary :: Gen Bit
46 a5 <- arbitrary :: Gen Bit
47 a6 <- arbitrary :: Gen Bit
48 a7 <- arbitrary :: Gen Bit
49 a8 <- arbitrary :: Gen Bit
50 return (Octet a1 a2 a3 a4 a5 a6 a7 a8)
53 instance Maskable Octet where
54 apply_mask oct Eight _ = oct
56 apply_mask oct Seven bit =
59 apply_mask oct Six bit =
60 oct { b8 = bit, b7 = bit }
62 apply_mask oct Five bit =
63 oct { b8 = bit, b7 = bit, b6 = bit }
65 apply_mask oct Four bit =
66 oct { b8 = bit, b7 = bit, b6 = bit, b5 = bit }
68 apply_mask oct Three bit =
69 oct { b8 = bit, b7 = bit, b6 = bit, b5 = bit, b4 = bit }
71 apply_mask oct Two bit =
72 oct { b8 = bit, b7 = bit, b6 = bit, b5 = bit, b4 = bit, b3 = bit }
74 apply_mask oct Maskbits.One bit =
75 oct { b8 = bit, b7 = bit, b6 = bit, b5 = bit,
76 b4 = bit, b3 = bit, b2 = bit }
78 apply_mask oct Maskbits.Zero bit =
79 oct { b8 = bit, b7 = bit, b6 = bit, b5 = bit,
80 b4 = bit, b3 = bit, b2 = bit, b1 = bit }
82 -- The Maskbits must be in [Eight..ThirtyTwo].
83 apply_mask oct _ _ = oct
86 instance Bounded Octet where
87 -- | The octet with the least possible value.
89 Octet B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero
91 -- | The octet with the greatest possible value.
93 Octet B.One B.One B.One B.One B.One B.One B.One B.One
96 instance Enum Octet where
98 -- | Create an 'Octet' from an 'Int'. The docs for Enum say we
99 -- should throw a runtime error on out-of-bounds, so we do.
101 | x < minBound || x > maxBound = error "octet out of bounds"
102 | otherwise = Octet a1 a2 a3 a4 a5 a6 a7 a8
104 a1 = if (x >= 128) then B.One else B.Zero
105 a2 = if ((x `mod` 128) >= 64) then B.One else B.Zero
106 a3 = if ((x `mod` 64) >= 32) then B.One else B.Zero
107 a4 = if ((x `mod` 32) >= 16) then B.One else B.Zero
108 a5 = if ((x `mod` 16) >= 8) then B.One else B.Zero
109 a6 = if ((x `mod` 8) >= 4) then B.One else B.Zero
110 a7 = if ((x `mod` 4) >= 2) then B.One else B.Zero
111 a8 = if ((x `mod` 2) == 1) then B.One else B.Zero
113 -- | Convert each bit to its integer value, and multiply by the
114 -- appropriate power of two. Sum them up, and we should get an integer
115 -- between 0 and 255.
117 128 * (fromEnum (b1 x)) +
118 64 * (fromEnum (b2 x)) +
119 32 * (fromEnum (b3 x)) +
120 16 * (fromEnum (b4 x)) +
121 8 * (fromEnum (b5 x)) +
122 4 * (fromEnum (b6 x)) +
123 2 * (fromEnum (b7 x)) +
124 1 * (fromEnum (b8 x))
128 instance Read Octet where
130 case (reads s :: [(Int, String)]) of
132 (x,leftover):_ -> go x leftover
134 go :: Int -> String -> [(Octet, String)]
136 | x' < minBound || x' > maxBound = []
137 | otherwise = [(toEnum x', leftover')]
141 octet_tests :: TestTree
143 testGroup "Octet Tests" [
148 octet_properties :: TestTree
152 [ prop_from_enum_to_enum_inverses,
153 prop_read_show_inverses,
154 prop_ord_instances_agree ]
156 -- QuickCheck properties
157 prop_from_enum_to_enum_inverses :: TestTree
158 prop_from_enum_to_enum_inverses =
159 testProperty "fromEnum and toEnum are inverses" prop
161 prop :: Int -> Property
163 (0 <= x) && (x <= 255) ==>
164 fromEnum (toEnum x :: Octet) == x
166 prop_read_show_inverses :: TestTree
167 prop_read_show_inverses =
168 testProperty "read and show are inverses" prop
170 prop :: Int -> Property
171 prop x = (0 <= x) && (x <= 255) ==> x' == x
179 -- | Ensure that the Ord instance for Octets agrees with the Ord
180 -- instance for Int (i.e. that numerical comparisons work).
181 prop_ord_instances_agree :: TestTree
182 prop_ord_instances_agree =
183 testProperty "the Octet and Int Ord instances agree" prop
185 prop :: Int -> Int -> Property
186 prop x y = (0 <= x) && (x <= 255) && (0 <= y) && (y <= 255) ==> ord == ord'
190 oct1 = toEnum x :: Octet
191 oct2 = toEnum y :: Octet
192 ord' = (oct1 <= oct2)
195 test_octet_to_enum1 :: TestTree
196 test_octet_to_enum1 =
197 testCase desc $ actual @?= expected
199 desc = "toEnum 128 should parse as 10000000"
200 expected = Octet B.One B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero
201 actual = toEnum 128 :: Octet
204 test_octet_mask1 :: TestTree
206 testCase desc $ actual @?= expected
208 desc = "The network bits of 255/4 should equal 240"
209 expected = toEnum 240 :: Octet
210 actual = apply_mask (toEnum 255) Four B.Zero :: Octet
213 test_octet_mask2 :: TestTree
215 testCase desc $ actual @?= expected
217 desc = "The network bits of 255/1 should equal 128"
218 expected = toEnum 128 :: Octet
219 actual = apply_mask (toEnum 255) Maskbits.One B.Zero :: Octet