X-Git-Url: https://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FOctet.hs;h=34f9d4f821e61997ccbdb2f1438c10a06c71b49a;hb=3e3f98ade03919d30e1dca739d14eec96a6bb990;hp=6d6c8c85f79265ab95eb6013082a8ef61c4af5ee;hpb=cca5aba75c313151878a98344c289827c94151e7;p=hath.git diff --git a/src/Octet.hs b/src/Octet.hs index 6d6c8c8..34f9d4f 100644 --- a/src/Octet.hs +++ b/src/Octet.hs @@ -1,19 +1,22 @@ 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 @@ -148,64 +151,67 @@ instance Read Octet where -- 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 ] -- 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 - oct :: Octet - oct = read $ show x + 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 + prop :: Int -> Property + prop x = (0 <= x) && (x <= 255) ==> x' == x + where + oct :: Octet + oct = read $ show x + + x' :: Int + x' = read $ show oct - x' :: Int - x' = read $ show oct -- 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