summaryrefslogtreecommitdiff
path: root/Haskell-book/14/qc/tests/UsingQuickCheckTest.hs
blob: da5ddb55e4f75967f17c138a8aa5b8b448bca56e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
module Main where

import Data.List (sort)
import UsingQuickCheck
import Test.QuickCheck

prop_half :: (Eq a, Fractional a) => a -> Bool
prop_half x = (halfIdentity x) == x

associativeGen :: (Integer -> Integer -> Integer -> Bool) -> Gen Bool
associativeGen f = do
    x <- (arbitrary :: Gen Integer)
    y <- (arbitrary :: Gen Integer)
    z <- (arbitrary :: Gen Integer)
    elements [f x y z]

commutativeGen :: (Integer -> Integer -> Bool) -> Gen Bool
commutativeGen f = do
    x <- (arbitrary :: Gen Integer)
    y <- (arbitrary :: Gen Integer)
    elements [f x y]

assocNotNegGen :: (Int -> Int -> Int -> Bool) -> Gen Bool
assocNotNegGen f = do
    x <- choose (1 :: Int, 100)
    y <- choose (1 :: Int, 100)
    z <- choose (1 :: Int, 100)
    elements [f x y z]

commutNotNegGen :: (Int -> Int -> Bool) -> Gen Bool
commutNotNegGen f = do
    x <- choose (1 :: Int, 100)
    y <- choose (1 :: Int, 100)
    elements [f x y]
    

prop_quotRem :: Property
prop_quotRem =
    forAll (prop_quotRem') (\(x ,y) -> (quot x y) * y + (rem x y) == x)
    where prop_quotRem' = do
          x <- choose (1 :: Int, 10000)
          y <- choose (1 :: Int, 10000)
          return (x, y)

prop_divMod :: Property
prop_divMod =
    forAll (prop_divMod') (\(x ,y) -> (div x y) * y + (mod x y) == x)
    where prop_divMod' = do
          x <- choose (1 :: Int, 10000)
          y <- choose (1 :: Int, 10000)
          return (x, y)

prop_reverse :: Property
prop_reverse =
    forAll prop_reverse' (\xs -> (reverse . reverse) xs == id xs)
    where prop_reverse' = do
          x <- (arbitrary :: Gen [Integer])
          return x

prop_dollar :: Property
prop_dollar =
    forAll prop_dollar' (\x -> x)
    where prop_dollar' = do
          x <- (arbitrary :: Gen Integer)
          return ((id $ x) == (id x))

prop_point :: Property
prop_point =
    forAll prop_point' (\x -> x)
    where prop_point' = do
          x <- (arbitrary :: Gen Integer)
          let pointFunc = negate . id
          let appliedFunc = \y -> negate (id y)
          return (pointFunc x == appliedFunc x)

prop_foldr1 :: Property
prop_foldr1 =
    forAll prop_foldr1' (\x -> x)
    where prop_foldr1' = do
          x <- (arbitrary :: Gen [Integer])
          y <- (arbitrary :: Gen [Integer])
          return ((foldr (:) x y) == (x ++ y))

prop_foldr2 :: Property
prop_foldr2 =
    forAll prop_foldr2' (\x -> x)
    where prop_foldr2' = do
          x <- (arbitrary :: Gen [[Integer]])
          return ((foldr (++) [] x) == (concat x))

prop_length :: Property
prop_length =
    forAll prop_length' (\x -> x)
    where prop_length' = do
          n <- (arbitrary :: Gen Int)
          xs <- (arbitrary :: Gen [Integer])
          return ((length (take n xs)) == n)

prop_readShow :: Property
prop_readShow =
    forAll prop_readShow' (\x -> x)
    where prop_readShow' = do
          x <- (arbitrary :: Gen Integer)
          return ((read (show x)) == x)

main :: IO ()
main = do
    quickCheck (prop_half :: Double -> Bool)
    quickCheck $ (listOrdered :: [Int] -> Bool) . sort
    
    quickCheck $ associativeGen plusAssociative
    quickCheck $ commutativeGen plusCommutative
    quickCheck $ associativeGen mulAssociative
    quickCheck $ commutativeGen mulCommutative

    quickCheck prop_quotRem
    quickCheck prop_divMod

    quickCheck $ assocNotNegGen (\x y z -> x ^ (y ^ z) == (x ^ y) ^ z)
    quickCheck $ commutNotNegGen (\x y -> x ^ y == y ^ x)

    quickCheck prop_reverse
    quickCheck prop_dollar
    quickCheck prop_point
    quickCheck prop_foldr1
    quickCheck prop_foldr2
    quickCheck prop_length
    quickCheck prop_readShow