1
1
module Addition where
2
2
3
3
import Test.Hspec
4
+ import Test.QuickCheck
5
+
6
+
7
+ trivialInt :: Gen Int
8
+ trivialInt = return 1
9
+
10
+
11
+ oneThroughThree :: Gen Int
12
+ oneThroughThree =
13
+ -- elements [1, 2, 3] -- each Int has the same probability of showing up
14
+ -- here the probabilities are:
15
+ -- 1: 1/6
16
+ -- 2: 4/6 = 2/3
17
+ -- 3: 1/6
18
+ elements [1 , 2 , 2 , 2 , 2 , 3 ]
19
+
20
+
21
+ genBool :: Gen Bool
22
+ genBool = choose (False , True )
23
+
24
+
25
+ genBool' :: Gen Bool
26
+ genBool' = elements [False , True ]
27
+
28
+
29
+ genOrdering :: Gen Ordering
30
+ genOrdering = elements [LT , EQ , GT ]
31
+
32
+
33
+ genChar :: Gen Char
34
+ genChar = elements [' a' .. ' z' ]
35
+
36
+
37
+ genTuple :: (Arbitrary a , Arbitrary b ) => Gen (a , b )
38
+ genTuple = do
39
+ a <- arbitrary
40
+ b <- arbitrary
41
+ return (a, b)
42
+
43
+
44
+ genThreeple :: (Arbitrary a , Arbitrary b , Arbitrary c ) => Gen (a , b , c )
45
+ genThreeple = do
46
+ a <- arbitrary
47
+ b <- arbitrary
48
+ c <- arbitrary
49
+ return (a, b, c)
50
+
51
+
52
+ genEither :: (Arbitrary a , Arbitrary b ) => Gen (Either a b )
53
+ genEither = do
54
+ a <- arbitrary
55
+ b <- arbitrary
56
+ elements [Left a, Right b]
57
+
58
+
59
+ -- equal probability
60
+ genMaybe :: Arbitrary a => Gen (Maybe a )
61
+ genMaybe = do
62
+ a <- arbitrary
63
+ elements [Nothing , Just a]
64
+
65
+
66
+ -- What QuickCheck actually does
67
+ -- so you get more Just values
68
+ genMaybe' :: Arbitrary a => Gen (Maybe a )
69
+ genMaybe' = do
70
+ a <- arbitrary
71
+ frequency [ (1 , return Nothing )
72
+ , (3 , return (Just a))
73
+ ]
74
+ -- frequency :: [(Int, Gen a)] -> Gen a
75
+
76
+
77
+ prop_additionGreater :: Int -> Bool
78
+ prop_additionGreater x = x + 1 > x
79
+ -- = x + 0 > x -- asserting something untrue
80
+
81
+
82
+ runQc :: IO ()
83
+ runQc = quickCheck prop_additionGreater
84
+
85
+
86
+ main :: IO ()
87
+ main = hspec $ do
88
+ describe " Addition" $ do
89
+ it " 1 + 1 is greater than 1" $ do
90
+ (1 + 1 ) > 1 `shouldBe` True
91
+ it " 2 + 2 is equal to 4" $ do
92
+ 2 + 2 `shouldBe` 4
93
+
94
+ it " x + 1 is always greater than x" $ do
95
+ property $ \ x -> x + 1 > (x :: Int )
96
+
97
+ describe " Division" $ do
98
+ it " 15 dividedBy 3 is 5" $ do
99
+ dividedBy 15 3 `shouldBe` (5 , 0 )
100
+ it " 22 dividedBy 5 is 4 remainder 2" $ do
101
+ dividedBy 22 5 `shouldBe` (4 , 2 )
102
+
103
+ -- Intermission: Short Exercise
104
+ -- see myMult
105
+ describe " Multiplication" $ do
106
+ it " 6 * 3 is 18" $ do
107
+ myMult 6 3 `shouldBe` 18
108
+ it " 3 * 6 is 18" $ do
109
+ myMult 3 6 `shouldBe` 18
110
+ it " 6 * 0 is 0" $ do
111
+ myMult 6 0 `shouldBe` 0
112
+ it " 0 * 6 is 0" $ do
113
+ myMult 0 6 `shouldBe` 0
114
+ it " 6 * -3 is -18" $ do
115
+ myMult 6 (- 3 ) `shouldBe` (- 18 )
116
+ it " -3 * 6 is -18" $ do
117
+ myMult (- 3 ) 6 `shouldBe` (- 18 )
118
+ it " -6 * -3 is 18" $ do
119
+ myMult (- 6 ) (- 3 ) `shouldBe` 18
120
+ it " -3 * -6 is 18" $ do
121
+ myMult (- 3 ) (- 6 ) `shouldBe` 18
122
+
123
+
124
+ sayHello :: IO ()
125
+ sayHello = putStrLn " hello!"
126
+
127
+
128
+ dividedBy :: Integral a => a -> a -> (a , a )
129
+ dividedBy num denom = go num denom 0
130
+ where
131
+ go n d count
132
+ | n < d = (count, n)
133
+ | otherwise = go (n - d) d (count + 1 )
134
+
4
135
5
- -- Example 1
6
- -- main :: IO ()
7
- -- main = hspec $ do
8
- -- describe "Addition" $ do
9
- -- it "1 + 1 is greater than 1" $ do
10
- -- (1 + 1) > 1 `shouldBe` True
11
- -- it "2 + 2 is equal to 4" $ do
12
- -- 2 + 2 `shouldBe` 4
13
-
14
- -- Example 2
15
- -- dividedBy :: Integral a => a -> a -> (a, a)
16
- -- dividedBy num denom = go num denom 0
17
- -- where
18
- -- go n d count
19
- -- | n < d = (count, n)
20
- -- | otherwise = go (n - d) d (count + 1)
21
- --
22
- -- main :: IO ()
23
- -- main = hspec $ do
24
- -- describe "Addition" $ do
25
- -- it "15 divided by 3 is 5" $ do
26
- -- dividedBy 15 3 `shouldBe` (5, 0)
27
- -- it "22 divided by 5 is 4 remainder 2" $ do
28
- -- dividedBy 22 5 `shouldBe` (4, 2)
29
-
30
- -- Short Exercise
31
136
myMult :: (Eq a , Num a ) => a -> a -> a
32
137
myMult x y
33
138
| sy == 0 || sy == 1 = go x y 0
@@ -38,21 +143,5 @@ myMult x y
38
143
sy = signum y
39
144
ax = abs x
40
145
ay = abs y
41
- go _ 0 ab = ab
42
- go a b ab = go a (b - 1 ) (a + ab)
43
-
44
- main :: IO ()
45
- main = hspec $ do
46
- describe " Multiplication" $ do
47
- it " 5 multiplied by 0 is 0" $ do
48
- myMult 5 0 `shouldBe` 0
49
- it " 0 multiplied by 5 is 0" $ do
50
- myMult 0 5 `shouldBe` 0
51
- it " 2 multiplied by 3 is 6" $ do
52
- myMult 2 3 `shouldBe` 6
53
- it " -2 multiplied by 3 is -6" $ do
54
- myMult (- 2 ) 3 `shouldBe` (- 6 )
55
- it " 2 multiplied by -3 is -6" $ do
56
- myMult 2 (- 3 ) `shouldBe` (- 6 )
57
- it " -2 multiplied by -3 is 6" $ do
58
- myMult (- 2 ) (- 3 ) `shouldBe` 6
146
+ go x 0 xy = xy
147
+ go x y xy = go x (y - 1 ) (x + xy)
0 commit comments