-
Notifications
You must be signed in to change notification settings - Fork 456
/
Copy pathSet4aTest.hs
235 lines (196 loc) · 8.49 KB
/
Set4aTest.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
{-# LANGUAGE TemplateHaskell, ScopedTypeVariables #-}
module Set4aTest where
import Control.Monad
import Data.Array
import Data.List
import qualified Data.Map as Map
import Test.QuickCheck
import Mooc.Th
import Mooc.Test
import Set4a
main = score tests
tests = [(1,"allEqual",[ex1, ex1_one])
,(2,"distinct",[ex2_int, ex2_char])
,(3,"middle",[ex3_char, ex3_int])
,(4,"rangeOf",[ex4_int, ex4_integer, ex4_double])
,(5,"longest",[ex5_int, ex5_char])
,(6,"incrementKey",[ex6_bool_float, ex6_char_int])
,(7,"average",[ex7])
,(8,"winner",[ex8_winner, ex8_tie])
,(9,"freqs",[ex9_bool, ex9_int])
,(10,"transfer",[ex10_success, ex10_missing, ex10_notEnough])
,(11,"swap",[ex11_swap_small, ex11_swap_large])
,(12,"maxIndex",[ex12_bool, ex12_int])]
-- -- -- -- -- -- -- --
m_ex1_true example = forAll_ $ \(cnt,v) ->
$(testing [|allEqual (replicate cnt (v `asTypeOf` example))|]) (?==True)
m_ex1_false example = forAll_ $ \(v,NonEmpty vs) ->
not (elem (v `asTypeOf` example) vs) ==> $(testing [|allEqual (v:vs)|]) (?==False)
ex1 = conjoin [m_ex1_true True
,m_ex1_true 'a'
,m_ex1_false 'a'
,m_ex1_true (1::Int)
,m_ex1_false (1::Int)]
ex1_one = forAll_ $ \(Positive cnt,x::Int,y) ->
forAllBlind (shuffle (y:replicate cnt x)) $ \xs ->
x /= y ==>
$(testing [|allEqual xs|]) (?==False)
m_ex2 domain = forAllBlind (sublistOf domain) $ \xs ->
conjoin [forAllBlind (shuffle xs) $ \xs' -> $(testing [|distinct xs'|]) (?==True)
,not (null xs) ==> forAllBlind (shuffle (head xs:xs)) $ \xs' -> $(testing [|distinct xs'|]) (?==False)]
ex2_int = m_ex2 [1::Int,2,3,4]
ex2_char = m_ex2 ['a'..'z']
m_ex3 c =
let d = succ c
e = succ d
in forAllBlind (shuffle [c,d,e]) $ \[x,y,z] ->
$(testing [|middle x y z|]) (?==d)
ex3_char = forAllBlind (choose ('a','y')) m_ex3
ex3_int = forAllBlind (choose (0::Int,20)) m_ex3
genRangeOf delta = do
base <- arbitrary
vs <- listOf1 (choose (base, base+delta))
shuffle (base:base+delta:vs)
m_ex4 delta = forAllBlind (genRangeOf delta) $ \vs ->
$(testing [|rangeOf vs|]) (?==delta)
ex4_int = forAllBlind (choose (1::Int,10)) m_ex4
ex4_integer = forAllBlind (choose (1::Integer,10)) m_ex4
ex4_double = property $ do
delta <- elements [1::Int,3,5,7,9]
vs <- genRangeOf delta
return $ $(testing [|rangeOf (map conv vs)|]) (?~=conv delta)
where conv x = fromIntegral x / 2
ex5_int =
forAllBlind (choose (2,10)) $ \l ->
forAllBlind (vectorOf l (arbitrary :: Gen Int)) $ \ans ->
forAllBlind (listOf (choose (1,l - 1) >>= vector)) $ \rest ->
forAllBlind (shuffle (ans:rest)) $ \input ->
$(testing [|longest input|]) (?==ans)
ex5_char = property $ do
c <- choose ('a','k')
d <- choose ('a','z') `suchThat` (>c)
len <- choose (1,3)
crest <- vectorOf len (choose ('a','z'))
drest <- vectorOf len (choose ('a','z'))
wrong <- vectorOf len (choose ('a','z'))
input <- shuffle [c:crest, d:drest, wrong]
return $ $(testing [|longest input|]) (?==(c:crest))
ex6_bool_float =
property $ do
c <- choose (True,False)
let f i = fromIntegral i + 0.5
i <- f <$> choose (0,10::Int)
j <- f <$> choose (0,10::Int)
let inp = [(c,i),(c,j)]
return $ $(testing [|incrementKey c inp|]) (?==[(c,i+1),(c,j+1)])
ex6_char_int =
property $ do
is <- vectorOf 5 $ choose (0,20::Int)
ks <- vectorOf 5 $ choose ('a','g')
c <- choose ('a','h')
let inp = zip ks is
out = zipWith (\k i -> if k==c then (k,succ i) else (k,i)) ks is
return $ $(testing [|incrementKey c inp|]) (?== out)
ex7 = conjoin [forAll_ $ \(Small i) -> $(testing [|average [fromIntegral i :: Float]|]) (?==fromIntegral i)
,forAllBlind (choose (1,10)) $ \n -> forAll_ $ \(i::Rational) ->
$(testing [|average (replicate n i)|]) (?==i)
,forAll_ $ \(Small i) -> let f = fromIntegral i in $(testing [|average [f-1,f,f+1]|]) (?==f)
,forAll_ $ \(Small i,Small j) ->
let x = fromIntegral i :: Double
y = fromIntegral j :: Double
in $(testing [|average (replicate 10 x ++ replicate 10 y)|]) (?~=(x+y)/2)]
ex8_winner = property $
do names <- shuffle ["Mike","Bob","Lisa","Jane"]
let (win:lose:third:_) = names
loseScore <- choose (0,10000)
diff <- choose (1,100)
thirdScore <- choose (0,20000)
let inp = Map.fromList [(win,loseScore+diff),(lose,loseScore),(third,thirdScore)]
return $ conjoin [$(testing [|winner inp win lose|]) (?==win)
,$(testing [|winner inp lose win|]) (?==win)]
ex8_tie = property $
do names <- shuffle ["Mike","Bob","Lisa","Jane"]
let (first:second:third:_) = names
score <- choose (0,10000)
thirdScore <- choose (0,20000)
let inp = Map.fromList [(first,score),(second,score),(third,thirdScore)]
return $ $(testing [|winner inp first second|]) (?==first)
ex9_bool = forAllBlind (choose (0,10)) $ \n ->
forAllBlind (vectorOf n arbitrary) $ \bs ->
$(testing [|freqs bs|]) . was $ \m ->
let (t,f) = partition id bs
ntrue = length t
nfalse = length f
out = Map.assocs m
in conjoin [counterexample (" Number of True values should be "++show ntrue) $
null t || (True,ntrue) `elem` out
,counterexample (" Number of False values should be "++show nfalse) $
null f || (False,nfalse) `elem` out]
ex9_int = forAllBlind (choose (0,15)) $ \n ->
forAllBlind (vectorOf n arbitrary) $ \(is::[Int]) ->
$(testing [|freqs is|]) . was $ \out ->
let vals = nub is
in counterexample " Number of keys" (length (Map.keys out) ?== length vals)
.&&.
conjoin (map (ck out is) vals)
where ck out vals i = let exp = length (filter (==i) vals)
in counterexample (" Should contain "++show(i,exp)) $ (i,exp) `elem` (Map.assocs out)
ex10_success = property $
do names <- shuffle ["Mike","Bob","Lisa","Jane"]
let (from:to:third:_) = names
fromBal <- choose (0,100)
toBal <- choose (0,100)
thirdBal <- choose (0,100)
amount <- choose (0,100)
let inp = Map.fromList [(from,fromBal+amount),(to,toBal),(third,thirdBal)]
let out = Map.fromList [(from,fromBal),(to,toBal+amount),(third,thirdBal)]
return $ $(testing [|transfer from to amount inp|]) (?==out)
ex10_missing = property $
do names <- shuffle ["Mike","Bob","Lisa","Jane"]
let (present:missing:third:_) = names
bal <- oneof [return 0, choose (0,100)]
thirdBal <- choose (0,100)
amount <- choose (0,100)
let inp = Map.fromList [(present,bal),(third,thirdBal)]
return $ conjoin [$(testing [|transfer present missing amount inp|]) (?==inp)
,$(testing [|transfer missing present amount inp|]) (?==inp)]
ex10_notEnough = property $
do names <- shuffle ["Mike","Bob","Lisa","Jane"]
let (from:to:third:_) = names
fromBal <- choose (0,100)
toBal <- choose (0,100)
thirdBal <- choose (0,100)
amount <- choose (1,100)
let inp = Map.fromList [(from,fromBal),(to,toBal),(third,thirdBal)]
return $ conjoin [$(testing [|transfer from to (fromBal+amount) inp|]) (?==inp)
,$(testing [|transfer from to (negate amount) inp|]) (?==inp)]
ex11_swap_small = property $
do a <- choose ('a','z')
b <- choose ('a','z')
i <- choose (0,10::Int)
let inp = listArray (i,i+1) [a,b]
out = listArray (i,i+1) [b,a]
return $ $(testing [|swap i (i+1) inp|]) (?==out)
ex11_swap_large = property $
do base <- choose (0,10::Int)
len <- choose (3,6::Int)
let max = base+len-1
vals <- shuffle (take len [0..])
~(i:j:_) <- shuffle [base..max]
let vi = vals !! (i-base)
let vj = vals !! (j-base)
let inp = listArray (base,max) (vals::[Int])
let out = listArray (base,max) (map (\x -> if x==vi then vj else if x == vj then vi else x) vals)
return $ $(testing [|swap i j inp|]) (?==out)
ex12_bool = property $
do small <- choose (0,100::Int)
diff <- choose (1,100)
return $ conjoin [$(testing [|maxIndex (array (False,True) [(False,small),(True,small+diff)])|]) (?==True)
,$(testing [|maxIndex (array (False,True) [(True,small),(False,small+diff)])|]) (?==False)]
ex12_int = property $
do low <- choose (0,100::Int)
hi <- choose (low+1,low+10)
mid <- choose (low,hi)
val <- choose (0,100::Int)
let inp = array (low,hi) [(i,if i==mid then val+1 else val) | i <- [low..hi]]
return $ $(testing [|maxIndex inp|]) (?==mid)