@@ -20,11 +20,12 @@ main = score tests
20
20
tests = [(1 ," doublify" ,[ex1_finite, ex1_infinite])
21
21
,(2 ," interleave" ,[ex2_finite, ex2_infinite_1, ex2_infinite_2])
22
22
,(3 ," deal" ,[ex3_examples, ex3_finite, ex3_infinite_1, ex3_infinite_2])
23
- ,(4 ," alternate" ,[ex4_examples, ex4])
24
- ,(5 ," lenghtAtLeast" ,[ex5_finite, ex5_infinite])
25
- ,(6 ," chunks" ,[ex6_finite, ex6_infinite])
26
- ,(7 ," IgnoreCase" ,[ex7_type, ex7_works])
27
- ,(8 ," maze" ,[ex8_1, ex8_2])]
23
+ ,(4 ," averages" ,[ex4_simple, ex4_finite, ex4_infinite])
24
+ ,(5 ," alternate" ,[ex5_examples, ex5])
25
+ ,(6 ," lenghtAtLeast" ,[ex6_finite, ex6_infinite])
26
+ ,(7 ," chunks" ,[ex7_finite, ex7_infinite])
27
+ ,(8 ," IgnoreCase" ,[ex8_type, ex8_works])
28
+ ,(9 ," maze" ,[ex9_1, ex9_2])]
28
29
29
30
-- -- -- --
30
31
@@ -88,46 +89,69 @@ ex3_infinite_1 = forAllShrink_ (listOf1 word) $ \names ->
88
89
ex3_infinite_2 =
89
90
$ (testing' [| take 10 (deal (repeat " me" ) (repeat " card" ))| ]) (?== replicate 10 (" card" ," me" ))
90
91
91
- ex4_examples = conjoin [$ (testing' [| take 20 (alternate " abc" " def" ' ,' )| ]) (?== " abc,def,abc,def,abc," )
92
+
93
+
94
+ ex4_simple = conjoin [$ (testing [| averages [1.0 ,2.0 ,3.0 ]| ]) (approximateListEq [1.0 ,(1 + 2 )/ 2 ,(1 + 2 + 3 )/ 3 ])
95
+ ,$ (testing [| averages [7 ,2 ,5 ,8 ]| ]) (approximateListEq [7 ,(7 + 2 )/ 2 ,(7 + 2 + 5 )/ 3 ,(7 + 2 + 5 + 8 )/ 4 ])
96
+ ,let e = [] :: [Double ] in $ (testing [| averages e | ]) (?== e )]
97
+
98
+ ex4_finite = forAllBlind (elements [0 ,1 ,2 ,3 ]) $ \ base ->
99
+ forAllBlind (elements [1 ,2 ,3 ,4 ,5 ,6 ]) $ \ step ->
100
+ forAllBlind (choose (2 ,7 )) $ \ len ->
101
+ let input = [base + i* step | i <- [0 .. len]]
102
+ output = [(j* base + (j- 1 )* j* step/ 2 )/ j | j <- [1 .. len+ 1 ]]
103
+ in $ (testing [| averages input| ]) (approximateListEq output)
104
+
105
+ ex4_infinite = forAllBlind (elements [0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ]) $ \ a ->
106
+ forAllBlind (elements [0 ,1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9 ]) $ \ b ->
107
+ forAllBlind (choose (0 ,1000 )) $ \ i ->
108
+ counterexample (" With a=" ++ show a ++ " , b=" ++ show b) $
109
+ $ (testing' [| averages (cycle [a,b])| ]) $ \ res ->
110
+ counterexample (" element at index " ++ show i) $
111
+ let na = fromIntegral (div i 2 + 1 )
112
+ nb = fromIntegral (div (i+ 1 ) 2 )
113
+ in res !! i ?~= (na* a + nb* b)/ (na+ nb)
114
+
115
+ ex5_examples = conjoin [$ (testing' [| take 20 (alternate " abc" " def" ' ,' )| ]) (?== " abc,def,abc,def,abc," )
92
116
,$ (testing' [| take 10 (alternate [1 ,2 ] [3 ,4 ,5 ] 0 )| ]) (?== [1 ,2 ,0 ,3 ,4 ,5 ,0 ,1 ,2 ,0 ])]
93
117
94
- ex4 = forAllBlind (choose (1 ,3 )) $ \ n ->
118
+ ex5 = forAllBlind (choose (1 ,3 )) $ \ n ->
95
119
forAllBlind (choose (1 ,6 )) $ \ m ->
96
120
forAll_ $ \ (NonNegative i) ->
97
121
$ (testing [| alternate (replicate n 1 ) (replicate m 1 ) 0 | ]) $ \ was ->
98
122
counterexample (" Index " ++ show i) $
99
123
(was !! i ?== if mod i (2 + n+ m) `elem` [n,n+ 1 + m] then 0 else 1 )
100
124
101
- ex5_finite = forAll_ $ \ (is:: [Int ]) ->
125
+ ex6_finite = forAll_ $ \ (is:: [Int ]) ->
102
126
forAll_ $ \ (NonNegative n) ->
103
127
$ (testing [| lengthAtLeast n is| ]) (?== (length is >= n))
104
128
105
- ex5_infinite = forAll_ $ \ (Positive (i:: Int )) ->
129
+ ex6_infinite = forAll_ $ \ (Positive (i:: Int )) ->
106
130
forAll_ $ \ (NonNegative n) ->
107
131
counterexample (" With n = " ++ show n ++ " , i = " ++ show i) $
108
132
$ (testing' [| lengthAtLeast n (repeat i)| ]) (?== True )
109
133
110
- ex6_finite = forAllBlind (choose (1 ,6 )) $ \ n ->
134
+ ex7_finite = forAllBlind (choose (1 ,6 )) $ \ n ->
111
135
forAllShrink_ (listOf word) $ \ ws ->
112
136
$ (testing [| chunks n ws| ]) (?== [[ws!! i | i <- [j.. j+ n- 1 ]] | j <- [0 .. length ws - n]])
113
137
114
- ex6_infinite = forAllBlind (choose (1 ,10 )) $ \ n ->
138
+ ex7_infinite = forAllBlind (choose (1 ,10 )) $ \ n ->
115
139
forAllBlind (choose (1 ,25 )) $ \ k ->
116
140
counterexample (" With k = " ++ show k++ " , n = " ++ show n) $
117
141
$ (testing' [| take k (chunks n [0 .. ])| ]) (?== [[j.. j+ n- 1 ] | j <- [0 .. k- 1 ]])
118
142
119
143
shuffleCase w = (do s <- vectorOf (length w) (elements [toLower,toUpper])
120
144
return (zipWith ($) s w)) `suchThat` (/= w)
121
145
122
- ex7_type = $ (do let s = " IgnoreCase"
146
+ ex8_type = $ (do let s = " IgnoreCase"
123
147
n <- lookupTypeName s
124
148
case n of
125
149
Nothing -> [| counterexample (" Type " ++ s++ " not defined!" ) False | ]
126
150
Just n -> do info <- reify n
127
151
case info of TyConI (NewtypeD _ _ _ _ _ _) -> [| property True | ]
128
152
_ -> [| counterexample (" Definition " ++ s++ " is not a newype declaration!" ) False | ])
129
153
130
- ex7_works =
154
+ ex8_works =
131
155
$ (hasType' " ignorecase" " String -> IgnoreCase" ) $ \ ignorecase ->
132
156
$ (withInstance " Eq" " IgnoreCase" [| (==) | ]) $ \ ((==) ) ->
133
157
property $ do
@@ -156,12 +180,12 @@ play room [] = [describe room]
156
180
play room (d: ds) = case move room d of Nothing -> [describe room]
157
181
Just r -> describe room : play r ds
158
182
159
- ex8_1 = forAllShrink_ (choose (0 ,20 )) $ \ i ->
183
+ ex9_1 = forAllShrink_ (choose (0 ,20 )) $ \ i ->
160
184
counterexample (" with i = " ++ show i) $
161
185
conjoin [$ (testing' [| play maze (replicate i " Left" )| ]) (?== take (i+ 1 ) (cycle [" Maze" ," Deeper in the maze" ," Elsewhere in the maze" ]))
162
186
,$ (testing' [| play maze (replicate i " Right" )| ]) (?== take (i+ 1 ) (cycle [" Maze" ," Elsewhere in the maze" ," Deeper in the maze" ]))]
163
187
164
- ex8_2 = forAllShrinkBlind (listOf (elements [" Left" ," Right" ])) subterms $ \ dirs ->
188
+ ex9_2 = forAllShrinkBlind (listOf (elements [" Left" ," Right" ])) subterms $ \ dirs ->
165
189
let cnt = sum (map (\ d -> case d of " Left" -> 1 ; " Right" -> - 1 ) dirs)
166
190
answer = [" Maze" ," Deeper in the maze" ," Elsewhere in the maze" ] !! mod cnt 3
167
191
in counterexample (" with dirs = " ++ show dirs) $
0 commit comments