Set 13b Ex 7: tests for fmap

fixes #75
This commit is contained in:
Joel Kaasinen
2023-01-18 07:47:45 +02:00
parent 62ce34962f
commit f94c9eaf78
+14 -2
View File
@@ -29,7 +29,7 @@ tests = [(1,"ifM",[ex1_maybe, ex1_state])
,(4,"findSum2", [ex4_simple, ex4]) ,(4,"findSum2", [ex4_simple, ex4])
,(5,"allSums", [ex5_bits]) ,(5,"allSums", [ex5_bits])
,(6,"foldM", [ex6_sumBounded_ok, ex6_sumBounded_fail, ex6_sumNotTwice]) ,(6,"foldM", [ex6_sumBounded_ok, ex6_sumBounded_fail, ex6_sumNotTwice])
,(7,"Monad Result", [ex7]) ,(7,"Monad Result", [ex7_fmap, ex7_fmap_empties, ex7_monad])
,(8,"Monad SL", [ex8_fmap_1, ex8_fmap_2, ex8_1, ex8_2, ex8_stress]) ,(8,"Monad SL", [ex8_fmap_1, ex8_fmap_2, ex8_1, ex8_2, ex8_stress])
,(9,"mkCounter",[ex9_mkCounter])] ,(9,"mkCounter",[ex9_mkCounter])]
@@ -213,7 +213,19 @@ ex6_sumNotTwice =
forAllShrink_ (listOf1 (choose (-5,5))) $ \is -> forAllShrink_ (listOf1 (choose (-5,5))) $ \is ->
$(testing [|sumNotTwice is|]) (?==sum (nub is)) $(testing [|sumNotTwice is|]) (?==sum (nub is))
ex7 = ex7_fmap =
forAll_ $ \(k::Int) ->
counterexample ("fmap (+1) (MkResult "++show k++")") $
fmap (+(1::Int)) (MkResult k) ?== MkResult (k+1)
ex7_fmap_empties =
$(testing' [|fmap not NoResult|]) (?== NoResult)
.&.
$(testing' [|fmap not (Failure "oh no.")|]) (?== Failure "oh no.")
.&.
$(testing' [|fmap not (Failure "Mein Gott!")|]) (?== Failure "Mein Gott!")
ex7_monad =
let op :: Int -> Result Int let op :: Int -> Result Int
op i = if i>3 then Failure "big" else return (i+1) op i = if i>3 then Failure "big" else return (i+1)
s = "let op i = if (i>3) then Failure \"big\" else return (i+1) in " s = "let op i = if (i>3) then Failure \"big\" else return (i+1) in "