X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FOctet.hs;h=b3e6a0f5d3c1310f38443ca99dc49c43de516e5a;hb=0be56eaa64ab03adf3aa1f3f3c14a191c09045c8;hp=e2e01ea93eadbc9d72d06a522c562227596fdced;hpb=d74254fd2a30214d7a8e4b9c407a6ec4fd6fed7f;p=hath.git diff --git a/src/Octet.hs b/src/Octet.hs index e2e01ea..b3e6a0f 100644 --- a/src/Octet.hs +++ b/src/Octet.hs @@ -1,5 +1,8 @@ module Octet where +import Test.HUnit +import Test.QuickCheck + import Bit -- An Octet consists of eight bits. For our purposes, the most @@ -21,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. @@ -67,3 +85,16 @@ min_octet = Octet Zero Zero Zero Zero Zero Zero Zero Zero -- The octet with the greatest possible value. max_octet :: Octet max_octet = Octet One One One One One One One One + + + +-- HUnit Tests +test_octet_from_int1 :: Test +test_octet_from_int1 = + TestCase $ assertEqual "octet_from_int 128 should parse as 10000000" oct1 (octet_from_int 128) + where + oct1 = Octet One Zero Zero Zero Zero Zero Zero Zero + + +octet_tests :: [Test] +octet_tests = [ test_octet_from_int1 ]