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 Ord Octet where
87 (Octet x1 x2 x3 x4 x5 x6 x7 x8) <= (Octet y1 y2 y3 y4 y5 y6 y7 y8)
99 instance Bounded Octet where
100 -- | The octet with the least possible value.
102 Octet B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero
104 -- | The octet with the greatest possible value.
106 Octet B.One B.One B.One B.One B.One B.One B.One B.One
109 instance Enum Octet where
111 -- | Create an 'Octet' from an 'Int'. The docs for Enum say we
112 -- should throw a runtime error on out-of-bounds, so we do.
114 | x < minBound || x > maxBound = error "octet out of bounds"
115 | otherwise = Octet a1 a2 a3 a4 a5 a6 a7 a8
117 a1 = if (x >= 128) then B.One else B.Zero
118 a2 = if ((x `mod` 128) >= 64) then B.One else B.Zero
119 a3 = if ((x `mod` 64) >= 32) then B.One else B.Zero
120 a4 = if ((x `mod` 32) >= 16) then B.One else B.Zero
121 a5 = if ((x `mod` 16) >= 8) then B.One else B.Zero
122 a6 = if ((x `mod` 8) >= 4) then B.One else B.Zero
123 a7 = if ((x `mod` 4) >= 2) then B.One else B.Zero
124 a8 = if ((x `mod` 2) == 1) then B.One else B.Zero
126 -- | Convert each bit to its integer value, and multiply by the
127 -- appropriate power of two. Sum them up, and we should get an integer
128 -- between 0 and 255.
130 128 * (fromEnum (b1 x)) +
131 64 * (fromEnum (b2 x)) +
132 32 * (fromEnum (b3 x)) +
133 16 * (fromEnum (b4 x)) +
134 8 * (fromEnum (b5 x)) +
135 4 * (fromEnum (b6 x)) +
136 2 * (fromEnum (b7 x)) +
137 1 * (fromEnum (b8 x))
141 instance Read Octet where
143 case (reads s :: [(Int, String)]) of
145 (x,leftover):_ -> go x leftover
147 go :: Int -> String -> [(Octet, String)]
149 | x' < minBound || x' > maxBound = []
150 | otherwise = [(toEnum x', leftover')]
154 octet_tests :: TestTree
156 testGroup "Octet Tests" [
161 octet_properties :: TestTree
165 [ prop_from_enum_to_enum_inverses,
166 prop_read_show_inverses ]
168 -- QuickCheck properties
169 prop_from_enum_to_enum_inverses :: TestTree
170 prop_from_enum_to_enum_inverses =
171 testProperty "fromEnum and toEnum are inverses" prop
173 prop :: Int -> Property
175 (0 <= x) && (x <= 255) ==>
176 fromEnum (toEnum x :: Octet) == x
178 prop_read_show_inverses :: TestTree
179 prop_read_show_inverses =
180 testProperty "read and show are inverses" prop
182 prop :: Int -> Property
183 prop x = (0 <= x) && (x <= 255) ==> x' == x
193 test_octet_to_enum1 :: TestTree
194 test_octet_to_enum1 =
195 testCase desc $ actual @?= expected
197 desc = "toEnum 128 should parse as 10000000"
198 expected = Octet B.One B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero
199 actual = toEnum 128 :: Octet
202 test_octet_mask1 :: TestTree
204 testCase desc $ actual @?= expected
206 desc = "The network bits of 255/4 should equal 240"
207 expected = toEnum 240 :: Octet
208 actual = apply_mask (toEnum 255) Four B.Zero :: Octet
211 test_octet_mask2 :: TestTree
213 testCase desc $ actual @?= expected
215 desc = "The network bits of 255/1 should equal 128"
216 expected = toEnum 128 :: Octet
217 actual = apply_mask (toEnum 255) Maskbits.One B.Zero :: Octet