-
Notifications
You must be signed in to change notification settings - Fork 12
/
Testing.hs
169 lines (128 loc) · 4.13 KB
/
Testing.hs
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
module Testing where
import Test.QuickCheck
import qualified Data.Set as Set
import Control.Monad(liftM, liftM2)
import Data.List((\\))
import InsertionSort
{-
quickCheck :: Testable prop => prop -> IO ()
class Testable prop where
property :: prop -> Property
instance Testable Bool
instance (Arbitrary a, Show a, Testable prop) =>
Testable (a -> prop)
class Testable prop where
property :: prop -> Property
property :: Testable prop => prop -> Property
instance Testable Bool
instance (Arbitrary a, Show a, Testable prop) =>
Testable (a -> prop)
-}
sortAscending :: Bool
sortAscending = isort [2,1] == [1,2]
sortDescending :: Bool
sortDescending = isort [2,1] == [2,1]
test1 :: IO ()
test1 = quickCheck sortAscending
test2 :: IO ()
test2 = quickCheck sortDescending
sortPreservesLength :: ([Int] -> [Int]) -> [Int] -> Bool
sortPreservesLength sort xs = length (sort xs) == length xs
test3 :: IO ()
test3 = quickCheck (sortPreservesLength isort)
setSort :: [Int] -> [Int]
setSort = Set.toList . Set.fromList
test4 :: IO ()
test4 = quickCheck (sortPreservesLength setSort)
sortOrders :: [Int] -> Bool
sortOrders xs = ordered (isort xs)
sortPreservesElements :: [Int] -> Bool
sortPreservesElements xs = sameElements xs (isort xs)
sameElements :: Eq a => [a] -> [a] -> Bool
sameElements xs ys = null (xs \\ ys) && null (ys \\ xs)
-- collect :: (Testable prop, Show a) => a -> prop -> Property
sPLen :: [Int] -> Bool
sPLen = sortPreservesLength isort
test5 :: IO ()
test5 = quickCheck (\ xs -> collect (null xs) (sPLen xs))
test6 :: IO ()
test6 = quickCheck (\ xs -> collect (length xs `div` 10) (sPLen xs))
test7 :: IO ()
test7 = quickCheck (\ xs -> collect xs (sPLen xs))
implies :: Bool -> Bool -> Bool
implies x y = not x || y
insertPreservesOrdered :: Int -> [Int] -> Bool
insertPreservesOrdered x xs =
ordered xs `implies` ordered (insert x xs)
test8 :: IO ()
test8 = quickCheck insertPreservesOrdered
iPO :: Int -> [Int] -> Bool
iPO = insertPreservesOrdered
test9 :: IO ()
test9 = quickCheck (\x xs -> collect (ordered xs) (iPO x xs))
-- (==>) :: (Testable prop) => Bool -> prop -> Property
-- instance Testable Property
iPO2 :: Int -> [Int] -> Property
iPO2 x xs = ordered xs ==> ordered (insert x xs)
test92 :: IO ()
test92 = quickCheck (\ x xs -> collect (ordered xs) (iPO2 x xs))
{-
data Args = Args {
replay :: Maybe (StdGen, Int)
maxSuccess :: Int
maxDiscard :: Int
maxSize :: Int
}
stdArgs :: Args
stdArgs = Args {replay = Nothing,
maxSuccess = 100,
maxDiscard = 500,
maxSize = 100}
quickCheckWith :: Testable prop => Args -> prop -> IO ()
class Arbitrary a where
arbitrary :: Gen a
shrink :: a -> [a]
choose :: Random a => (a,a) -> Gen a
oneof :: [Gen a] -> Gen a
frequency :: [(Int, Gen a)] -> Gen a
elements :: [a] -> Gen a
sized :: (Int -> Gen a) -> Gen a
instance Arbitrary Bool where
arbitrary = choose (False, True)
instance (Arbitrary a, Arbitrary b) => Arbitrary (a,b) where
arbitrary = do
x <- arbitrary
y <- arbitrary
return (x,y)
-}
data Dir = North | East | South | West
instance Arbitrary Dir where
arbitrary = elements [North, East, South, West]
{-
instance Arbitrary Int where
arbitrary = choose (-20,20)
instance Arbitrary Int where
arbitrary = sized (\ n -> choose (-n,n))
-}
data Tree a = Leaf a | Node (Tree a) (Tree a)
{-
instance Arbitrary a => Arbitrary (Tree a) where
arbitrary = arbTreeBad
-}
arbTreeBad :: Arbitrary a => Gen (Tree a)
arbTreeBad =
frequency [ (1, liftM Leaf arbitrary),
(2, liftM2 Node arbitrary arbitrary) ]
{-
liftM :: (a -> b) -> Gen a -> Gen b
liftM2 :: (a -> b -> c) -> Gen a -> Gen b -> Gen c
-}
instance Arbitrary a => Arbitrary (Tree a) where
arbitrary = sized arbitraryTree
arbitraryTree :: Arbitrary a => Int -> Gen (Tree a)
arbitraryTree 0 = liftM Leaf arbitrary
arbitraryTree n = frequency [ (1, liftM Leaf arbitrary),
(4, liftM2 Node t t) ]
where t = arbitraryTree (n `div` 2)
-- See also GenTree.hs
-- shrink :: (Arbitrary a) => a -> [a]