@@ -5,9 +5,13 @@ module Test.Chapter2
5
5
) where
6
6
7
7
import Test.Hspec (Spec , describe , it , shouldBe )
8
+ import Test.Hspec.Hedgehog (hedgehog , (===) , forAll )
8
9
9
10
import Chapter2
10
11
12
+ import qualified Hedgehog.Range as Range
13
+ import qualified Hedgehog.Gen as Gen
14
+
11
15
12
16
chapter2 :: Spec
13
17
chapter2 = describe " Chapter2" $ do
@@ -41,6 +45,10 @@ chapter2normal = describe "Chapter2Normal" $ do
41
45
it " one elem" $ duplicate [0 ] `shouldBe` [0 , 0 ]
42
46
it " two elems" $ duplicate [- 1 , 0 ] `shouldBe` [- 1 , - 1 , 0 , 0 ]
43
47
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
44
52
describe " Task7: takeEven" $ do
45
53
it " empty" $ takeEven emptyInts `shouldBe` emptyInts
46
54
it " one elem" $ takeEven [1 ] `shouldBe` [1 ]
@@ -69,12 +77,19 @@ chapter2normal = describe "Chapter2Normal" $ do
69
77
it " empty on negative" $ rotate (- 5 ) [1 .. 5 ] `shouldBe` emptyInts
70
78
71
79
chapter2advanced :: Spec
72
- chapter2advanced = describe " Chapter2Advanced" $
80
+ chapter2advanced = describe " Chapter2Advanced" $ do
73
81
describe " Task12*: rewind" $ do
74
82
it " empty" $ rewind emptyInts `shouldBe` emptyInts
75
83
it " one elem" $ rewind [1 ] `shouldBe` [1 ]
76
84
it " many elems" $ rewind [1 .. 10 ] `shouldBe` [10 ,9 .. 1 ]
77
85
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
78
93
79
94
emptyInts :: [Int ]
80
95
emptyInts = []
0 commit comments