Skip to content

Commit 57fabbb

Browse files
authored
[kowainik#26] Property tests: rewind & duplicate (kowainik#331)
1 parent 38a8186 commit 57fabbb

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

test/Test/Chapter1.hs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ module Test.Chapter1
55
) where
66

77
import Test.Hspec (Spec, describe, it, shouldBe)
8-
9-
import qualified Hedgehog.Range as Range (linear)
10-
import qualified Hedgehog.Gen as Gen (int)
11-
128
import Test.Hspec.Hedgehog (hedgehog, (===), forAll)
139

1410
import Chapter1
1511

12+
import qualified Hedgehog.Range as Range (linear)
13+
import qualified Hedgehog.Gen as Gen (int)
14+
1615

1716
chapter1 :: Spec
1817
chapter1 = describe "Chapter1" $ do
@@ -54,6 +53,8 @@ chapter1normal = describe "Chapter1Normal" $ do
5453
it "negatives mix " $ mid (-20) (-30) (-10) `shouldBe` (-20)
5554
it "all equal" $ mid 1 1 1 `shouldBe` 1
5655
it "all equal, except 1" $ mid 1 1 2 `shouldBe` 1
56+
it "all equal, except 1" $ mid 2 1 2 `shouldBe` 2
57+
it "all equal, except 1" $ mid 1 2 2 `shouldBe` 2
5758
describe "Task8: isVowel" $ do
5859
it "true for vowels" $ all isVowel "aeiou" `shouldBe` True
5960
it "false for non-vowels" $ isVowel 'c' `shouldBe` False
@@ -66,13 +67,9 @@ chapter1normal = describe "Chapter1Normal" $ do
6667
it "sumLast2 0 > -10" $ sumLast2 (-9) `shouldBe` 9
6768
it "sumLast2 -10 > -100" $ sumLast2 (-56) `shouldBe` 11
6869
it "sumLast2 -100 > -1000" $ sumLast2 (-987) `shouldBe` 15
69-
describe "Task 4 & 5 : first and last digit" $ do
70-
it "last digit is the first digit of the reversed number" $ hedgehog $ do
71-
x <- forAll $ Gen.int (Range.linear (-200) 200)
72-
(firstDigit x :: Int) === (lastDigit (reverseInt x) :: Int)
7370

7471
chapter1advanced :: Spec
75-
chapter1advanced = describe "Chapter1Advanced" $
72+
chapter1advanced = describe "Chapter1Advanced" $ do
7673
describe "Task 10*" $ do
7774
it "first digit 0" $ firstDigit 0 `shouldBe` 0
7875
it "first digit 0 < 10" $ firstDigit 9 `shouldBe` 9
@@ -82,3 +79,9 @@ chapter1advanced = describe "Chapter1Advanced" $
8279
it "first digit 0 > -10" $ firstDigit (-9) `shouldBe` 9
8380
it "first digit -10 > -100" $ firstDigit (-58) `shouldBe` 5
8481
it "first digit -100 > -1000" $ firstDigit (-158) `shouldBe` 1
82+
describe "Task 4 & 5 : first and last digit" $ do
83+
it "last digit is the first digit of the reversed number" $ hedgehog $ do
84+
xGen <- forAll $ Gen.int (Range.linear (-200) 200)
85+
digit <- forAll $ Gen.int (Range.linear 1 9)
86+
let x = if xGen `rem` 10 == 0 then xGen + digit else xGen
87+
(firstDigit x :: Int) === (lastDigit (reverseInt x) :: Int)

test/Test/Chapter2.hs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ module Test.Chapter2
55
) where
66

77
import Test.Hspec (Spec, describe, it, shouldBe)
8+
import Test.Hspec.Hedgehog (hedgehog, (===), forAll)
89

910
import Chapter2
1011

12+
import qualified Hedgehog.Range as Range
13+
import qualified Hedgehog.Gen as Gen
14+
1115

1216
chapter2 :: Spec
1317
chapter2 = describe "Chapter2" $ do
@@ -41,6 +45,10 @@ chapter2normal = describe "Chapter2Normal" $ do
4145
it "one elem" $ duplicate [0] `shouldBe` [0, 0]
4246
it "two elems" $ duplicate [-1, 0] `shouldBe` [-1, -1, 0, 0]
4347
it "many elems" $ duplicate [0..5] `shouldBe` [0,0,1,1,2,2,3,3,4,4,5,5]
48+
describe "Task6: duplicate property" $ do
49+
it "length (duplicate xs) = 2 * length xs" $ hedgehog $ do
50+
xs <- forAll $ Gen.list (Range.linear 0 10) Gen.bool
51+
length (duplicate xs) === 2 * length xs
4452
describe "Task7: takeEven" $ do
4553
it "empty" $ takeEven emptyInts `shouldBe` emptyInts
4654
it "one elem" $ takeEven [1] `shouldBe` [1]
@@ -69,12 +77,19 @@ chapter2normal = describe "Chapter2Normal" $ do
6977
it "empty on negative" $ rotate (-5) [1..5] `shouldBe` emptyInts
7078

7179
chapter2advanced :: Spec
72-
chapter2advanced = describe "Chapter2Advanced" $
80+
chapter2advanced = describe "Chapter2Advanced" $ do
7381
describe "Task12*: rewind" $ do
7482
it "empty" $ rewind emptyInts `shouldBe` emptyInts
7583
it "one elem" $ rewind [1] `shouldBe` [1]
7684
it "many elems" $ rewind [1..10] `shouldBe` [10,9..1]
7785
it "many elems random" $ rewind [5,1,9,56,32,7,11] `shouldBe` [11,7,32,56,9,1,5]
86+
describe "Task12*: rewind Properties" $ do
87+
it "rewind == reverse" $ hedgehog $ do
88+
xs <- forAll $ Gen.list (Range.linear 0 10) Gen.bool
89+
rewind xs === reverse xs
90+
it "length rewind == length" $ hedgehog $ do
91+
xs <- forAll $ Gen.list (Range.linear 0 10) Gen.bool
92+
length (rewind xs :: [Bool]) === length xs
7893

7994
emptyInts :: [Int]
8095
emptyInts = []

0 commit comments

Comments
 (0)