-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet4bTest.hs
72 lines (55 loc) · 2.12 KB
/
Set4bTest.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
{-# LANGUAGE TemplateHaskell, ScopedTypeVariables #-}
module Set4bTest where
import Data.Char
import Data.List
import Test.QuickCheck
import Mooc.Th
import Mooc.Test
import Set4b
main = score tests
tests = [(1,"countNothings",[ex1_countNothings])
,(2,"myMaximum",[ex2_myMaximum])
,(3,"sumAndLength",[ex3_sumAndLength])
,(4,"myConcat",[ex4_myConcat])
,(5,"largest",[ex5_largest])
,(6,"myHead",[ex6_myHead_empty, ex6_myHead_full])
,(7,"myLast",[ex7_myLast_empty, ex7_myLast_full])]
ex1_countNothings = property $ do
justs <- fmap (map Just) $ listOf (choose (0::Int,10))
nothings <- listOf (return Nothing)
input <- shuffle (justs++nothings)
return $
counterexample ("input = "++show input) $
$(testing' [|foldr countHelper 0 input|]) (?==length nothings)
ex2_myMaximum = property $ do
x <- choose (-100::Int,100)
xs <- listOf (choose (-100,100::Int))
return $
counterexample ("x = "++show x) $
counterexample ("xs = "++show xs) $
$(testing' [|foldr maxHelper x xs|]) (?==maximum (x:xs))
niceDouble = fmap ((/2).fromIntegral) (arbitrary :: Gen Int)
ex3_sumAndLength =
forAllBlind (listOf niceDouble) $ \(ds::[Double]) ->
counterexample ("ds = "++show ds) $
$(testing' [|foldr slHelper slStart ds|]) (?==(sum ds, length ds))
ex4_myConcat =
forAll_ $ \(xs :: [[Int]]) ->
counterexample ("xs = "++show xs) $
$(testing' [|foldr concatHelper concatStart xs|]) (?==concat xs)
ex5_largest =
forAll_ $ \(NonEmpty (xs :: [Int])) ->
counterexample ("xs = "++show xs) $
$(testing' [|foldr largestHelper [] xs|]) (?==filter (==maximum xs) xs)
ex6_myHead_empty =
$(testing' [|foldr headHelper Nothing []|]) (?==(Nothing::Maybe Bool))
ex6_myHead_full =
forAll_ $ \(NonEmpty xs::NonEmptyList Int) ->
counterexample ("xs = "++show xs) $
$(testing' [|foldr headHelper Nothing xs|]) (?==Just (head xs))
ex7_myLast_empty =
$(testing' [|foldr lastHelper Nothing []|]) (?==(Nothing::Maybe Bool))
ex7_myLast_full =
forAll_ $ \(NonEmpty xs::NonEmptyList Int) ->
counterexample ("xs = "++show xs) $
$(testing' [|foldr lastHelper Nothing xs|]) (?==Just (last xs))