From c26c7a454e561837407c39219a1a9240de0f429a Mon Sep 17 00:00:00 2001 From: Veronika Romashkina Date: Sun, 1 Nov 2020 15:07:19 +0000 Subject: [PATCH] [#26] Property tests: rewind & duplicate --- test/Test/Chapter1.hs | 21 ++++++++++++--------- test/Test/Chapter2.hs | 17 ++++++++++++++++- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/test/Test/Chapter1.hs b/test/Test/Chapter1.hs index 5bac65c0..df6b189b 100644 --- a/test/Test/Chapter1.hs +++ b/test/Test/Chapter1.hs @@ -5,14 +5,13 @@ module Test.Chapter1 ) where import Test.Hspec (Spec, describe, it, shouldBe) - -import qualified Hedgehog.Range as Range (linear) -import qualified Hedgehog.Gen as Gen (int) - import Test.Hspec.Hedgehog (hedgehog, (===), forAll) import Chapter1 +import qualified Hedgehog.Range as Range (linear) +import qualified Hedgehog.Gen as Gen (int) + chapter1 :: Spec chapter1 = describe "Chapter1" $ do @@ -54,6 +53,8 @@ chapter1normal = describe "Chapter1Normal" $ do it "negatives mix " $ mid (-20) (-30) (-10) `shouldBe` (-20) it "all equal" $ mid 1 1 1 `shouldBe` 1 it "all equal, except 1" $ mid 1 1 2 `shouldBe` 1 + it "all equal, except 1" $ mid 2 1 2 `shouldBe` 2 + it "all equal, except 1" $ mid 1 2 2 `shouldBe` 2 describe "Task8: isVowel" $ do it "true for vowels" $ all isVowel "aeiou" `shouldBe` True it "false for non-vowels" $ isVowel 'c' `shouldBe` False @@ -66,13 +67,9 @@ chapter1normal = describe "Chapter1Normal" $ do it "sumLast2 0 > -10" $ sumLast2 (-9) `shouldBe` 9 it "sumLast2 -10 > -100" $ sumLast2 (-56) `shouldBe` 11 it "sumLast2 -100 > -1000" $ sumLast2 (-987) `shouldBe` 15 - describe "Task 4 & 5 : first and last digit" $ do - it "last digit is the first digit of the reversed number" $ hedgehog $ do - x <- forAll $ Gen.int (Range.linear (-200) 200) - (firstDigit x :: Int) === (lastDigit (reverseInt x) :: Int) chapter1advanced :: Spec -chapter1advanced = describe "Chapter1Advanced" $ +chapter1advanced = describe "Chapter1Advanced" $ do describe "Task 10*" $ do it "first digit 0" $ firstDigit 0 `shouldBe` 0 it "first digit 0 < 10" $ firstDigit 9 `shouldBe` 9 @@ -82,3 +79,9 @@ chapter1advanced = describe "Chapter1Advanced" $ it "first digit 0 > -10" $ firstDigit (-9) `shouldBe` 9 it "first digit -10 > -100" $ firstDigit (-58) `shouldBe` 5 it "first digit -100 > -1000" $ firstDigit (-158) `shouldBe` 1 + describe "Task 4 & 5 : first and last digit" $ do + it "last digit is the first digit of the reversed number" $ hedgehog $ do + xGen <- forAll $ Gen.int (Range.linear (-200) 200) + digit <- forAll $ Gen.int (Range.linear 1 9) + let x = if xGen `rem` 10 == 0 then xGen + digit else xGen + (firstDigit x :: Int) === (lastDigit (reverseInt x) :: Int) diff --git a/test/Test/Chapter2.hs b/test/Test/Chapter2.hs index 857e9e2b..03ce9246 100644 --- a/test/Test/Chapter2.hs +++ b/test/Test/Chapter2.hs @@ -5,9 +5,13 @@ module Test.Chapter2 ) where import Test.Hspec (Spec, describe, it, shouldBe) +import Test.Hspec.Hedgehog (hedgehog, (===), forAll) import Chapter2 +import qualified Hedgehog.Range as Range +import qualified Hedgehog.Gen as Gen + chapter2 :: Spec chapter2 = describe "Chapter2" $ do @@ -41,6 +45,10 @@ chapter2normal = describe "Chapter2Normal" $ do it "one elem" $ duplicate [0] `shouldBe` [0, 0] it "two elems" $ duplicate [-1, 0] `shouldBe` [-1, -1, 0, 0] it "many elems" $ duplicate [0..5] `shouldBe` [0,0,1,1,2,2,3,3,4,4,5,5] + describe "Task6: duplicate property" $ do + it "length (duplicate xs) = 2 * length xs" $ hedgehog $ do + xs <- forAll $ Gen.list (Range.linear 0 10) Gen.bool + length (duplicate xs) === 2 * length xs describe "Task7: takeEven" $ do it "empty" $ takeEven emptyInts `shouldBe` emptyInts it "one elem" $ takeEven [1] `shouldBe` [1] @@ -69,12 +77,19 @@ chapter2normal = describe "Chapter2Normal" $ do it "empty on negative" $ rotate (-5) [1..5] `shouldBe` emptyInts chapter2advanced :: Spec -chapter2advanced = describe "Chapter2Advanced" $ +chapter2advanced = describe "Chapter2Advanced" $ do describe "Task12*: rewind" $ do it "empty" $ rewind emptyInts `shouldBe` emptyInts it "one elem" $ rewind [1] `shouldBe` [1] it "many elems" $ rewind [1..10] `shouldBe` [10,9..1] it "many elems random" $ rewind [5,1,9,56,32,7,11] `shouldBe` [11,7,32,56,9,1,5] + describe "Task12*: rewind Properties" $ do + it "rewind == reverse" $ hedgehog $ do + xs <- forAll $ Gen.list (Range.linear 0 10) Gen.bool + rewind xs === reverse xs + it "length rewind == length" $ hedgehog $ do + xs <- forAll $ Gen.list (Range.linear 0 10) Gen.bool + length (rewind xs :: [Bool]) === length xs emptyInts :: [Int] emptyInts = []