-
Notifications
You must be signed in to change notification settings - Fork 456
/
Copy pathSet2bTest.hs
83 lines (66 loc) · 2.36 KB
/
Set2bTest.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
{-# LANGUAGE TemplateHaskell #-}
module Set2bTest where
import Data.List
import Test.QuickCheck
import Mooc.Th
import Mooc.Test
import Set2b
main = score tests
tests = [(1,"binomial",[ex1_binomial])
,(2,"oddFactorial",[ex2_oddFactorial])
,(3,"myGcd",[ex3_myGcd])
,(4,"leftpad",[property ex4_leftpad])
,(5,"countdown",[ex5_countdown])
,(6,"smallestDivisor",[ex6_smallestDivisor_prime, property ex6_smallestDivisor_comp])
,(7,"isPrime",[ex7_isPrime])
,(8,"biggestPrimeAtMost",[ex8_biggestPrimeAtMost])]
ex1_binomial =
forAllBlind (elements [0..10]) $ \n ->
forAllBlind (elements [0..n]) $ \k ->
$(testing [|binomial n k|]) (?== f n `div` (f k * f (n-k)))
where f n = product [1..n]
ex2_oddFactorial =
forAllBlind (elements [1..15]) $ \n ->
$(testing [|oddFactorial n|]) (?== f n)
where f n = product [1,3..n]
ex3_myGcd =
forAllBlind (elements [1..max]) $ \x ->
forAllBlind (elements [1..max]) $ \y ->
$(testing [|myGcd x y|]) (?== gcd x y)
where max = 10000
word = listOf1 (choose ('a','z'))
ex4_leftpad =
do text <- word
ws <- listOf (return ' ')
let res = ws++text
let len = length res
return $ $(testing [|leftpad text len|]) (?==res)
ex5_countdown =
forAllBlind (elements [1..20]) $ \n ->
$(testing [|countdown n|]) . was $ \v ->
counterexample "should start with \"Ready!\"" (take 6 v ?== "Ready!")
.&&.
counterexample "should end with \"Liftoff!\"" (reverse (take 8 (reverse v)) ?== "Liftoff!")
.&&.
let cnt = init $ init $ intercalate "... " (map show [n,n-1..0])
in counterexample ("should contain the string " ++ show cnt) (isInfixOf cnt v)
primes = go [2..]
where go (x:xs) = x : go (filter (notDivBy x) xs)
notDivBy x y = mod y x /= 0
ex6_smallestDivisor_prime = do
forAllBlind (elements $ take 12 primes) $ \p ->
$(testing [|smallestDivisor p|]) (?== p)
ex6_smallestDivisor_comp = do
k <- (elements . take 10 $ primes)
p <- (elements . take 20 . drop 10 $ primes)
let n = k*p
return $ $(testing [|smallestDivisor n|]) (?== k)
ex7_isPrime =
forAllBlind (elements [0..max]) $ \n ->
$(testing [|isPrime n|]) (?== elem n primes')
where max = 20
primes' = takeWhile (<=max) primes
ex8_biggestPrimeAtMost =
forAllBlind (elements [2..max]) $ \n ->
$(testing [|biggestPrimeAtMost n|]) (?== last (takeWhile (<=n) primes))
where max = 100