-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSet11aTest.hs
105 lines (82 loc) · 3.24 KB
/
Set11aTest.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
{-# LANGUAGE TemplateHaskell, ScopedTypeVariables #-}
module Set11aTest where
import Mooc.Test
import Mooc.Th
import Control.Monad
import Data.List
import Test.QuickCheck
import Set11a hiding (ask)
main = score tests
tests = precondition imports
[(1,"hello",[ex1_hello])
,(2,"greetIO",[ex2_greet])
,(3,"greetIO2",[ex3_greet2])
,(4,"readWords",[ex4_readWords])
,(5,"readUntil",[ex5_readUntil])
,(6,"countdownPrint",[ex6])
,(7,"isums",[ex7_isums])
,(8,"whenM",[ex8_whenM_True, ex8_whenM_False])
,(9,"whileIO",[ex9_example, ex9_False])
,(10,"debug",[ex10_debug])]
-- -- -- -- --
imports = $(importsOnly "Set11a" ["Control.Monad","Data.List","Data.Functor","Data.Foldable","Data.Traversable","Prelude","Data.OldList","System.IO"
,"Text.Read"
,"GHC.Num", "GHC.Base", "GHC.Classes", "GHC.Show", "GHC.Types", "GHC.Err", "GHC.List"
,"Mooc.Todo"])
ex1_hello =
$(testing [|hello|]) . withNoInput $
checkOutput (?== "HELLO\nWORLD\n")
word = listOf1 (choose ('a','z'))
ex2_greet = forAllBlind word $ \name ->
$(testing [|greet name|]) . withNoInput $
checkOutput (?== ("HELLO "++name++"\n"))
ex3_greet2 = forAllBlind word $ \name ->
$(testing [|greet2|]) . withInput (name++"\n") $
checkOutput (?== ("HELLO "++name++"\n"))
ex4_readWords =
forAllBlind (listOf1 word) $ \words ->
$(testing [|readWords (length words - 1)|]) . withInput (unlines words) $
checkResult (?== sort (init words))
ex5_readUntil =
forAllBlind word $ \end ->
forAllBlind (listOf1 (word `suchThat` (/=end))) $ \words ->
counterexample ("readUntil (=="++show end++")") $
($ readUntil (==end)) $
withInput (unlines $ words ++ [end]) $
checkResult (?==words)
ex6 =
forAllBlind (choose (0,40)) $ \n ->
$(testing [|countdownPrint n|]) . withNoInput $
checkOutput (?== unlines (map show [n,n-1..0]))
ex7_isums =
forAllBlind (listOf1 (choose (-10,10))) $ \nums ->
$(testing [|isums (length nums)|]) . withInput (unlines $ map show nums) $
check (?==unlines (map show $ scanl1 (+) nums)) (?==sum nums)
ex8_whenM_True =
forAllBlind (choose (0::Int,10)) $ \i ->
counterexample ("with i = " ++ show i) $
$(testing' [|whenM (return True) (print i)|]) . withNoInput $
checkOutput (?== show i++"\n")
ex8_whenM_False =
forAllBlind (choose (0::Int,10)) $ \i ->
counterexample ("with i = " ++ show i) $
$(testing' [|whenM (return False) (print i)|]) . withNoInput $
checkOutput (?== "")
ask :: IO Bool
ask = do putStrLn "Y/N?"
line <- getLine
return $ line == "Y"
ex9_example =
forAllBlind (choose (0,10)) $ \i ->
$(testing' [|while ask (putStrLn "YAY!")|]) . withInput (unlines $ replicate i "Y" ++ ["N"]) $
checkOutput (?== unlines (concat (replicate i ["Y/N?","YAY!"]) ++ ["Y/N?"]))
ex9_False =
$(testing' [|while (return False) (putStrLn "IMPOSSIBLE")|]) . withNoInput $
checkOutput (?== "")
ex10_debug =
forAllBlind word $ \message ->
forAllBlind word $ \output ->
forAllBlind word $ \result ->
counterexample ("debug "++show message++" (do putStrLn "++show output++"; return "++show result++")") $
($ (debug message (putStrLn output >> return result))) . withNoInput $
check (?== unlines [message,output,message]) (?== result)