X-Git-Url: http://gitweb.michael.orlitzky.com/?a=blobdiff_plain;f=src%2FOctet.hs;h=55f075e8a5810247dc9d7df2342b741ea6f82d30;hb=eee156f562f9c1c1194a67cef12f146304d88ce9;hp=c472f8da6e8c85552c32256f5409e0daeb86c5b8;hpb=45ac9baca360d0f0dabe4576fb44c1634a50dbf7;p=hath.git diff --git a/src/Octet.hs b/src/Octet.hs index c472f8d..55f075e 100644 --- a/src/Octet.hs +++ b/src/Octet.hs @@ -1,15 +1,17 @@ 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.QuickCheck ( Gen ) -- Not re-exported by tasty +import Test.Tasty ( TestTree, testGroup ) +import Test.Tasty.HUnit ( (@?=), testCase ) +import Test.Tasty.QuickCheck ( + Arbitrary(..), + Property, + (==>), + testProperty ) import Bit as B (Bit(..)) import Maskable (Maskable(..)) @@ -136,76 +138,79 @@ instance Enum Octet where 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_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 :: TestTree test_octet_from_int1 = - testCase desc $ assertEqual desc oct1 oct2 + 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 + expected = Octet B.One B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero B.Zero + actual = toEnum 128 + -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 -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