Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#26] Property tests: rewind & duplicate #331

Merged
merged 1 commit into from
Nov 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions test/Test/Chapter1.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning: Redundant 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)

17 changes: 16 additions & 1 deletion test/Test/Chapter2.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning: Redundant 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]
Expand Down Expand Up @@ -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 = []