@@ -8,8 +8,10 @@ module Tests.Properties.Folds
88 ) where
99
1010import Control.Arrow (second )
11+ import Control.Exception (ErrorCall , evaluate , try )
1112import Data.Word (Word8 , Word16 )
1213import Test.Tasty (TestTree , testGroup )
14+ import Test.Tasty.HUnit (testCase , assertFailure )
1315import Test.Tasty.QuickCheck (testProperty , Small (.. ), (===) , applyFun , applyFun2 )
1416import Tests.QuickCheckUtils
1517import qualified Data.List as L
@@ -47,13 +49,32 @@ sf_foldr (applyFun -> p) (applyFun2 -> f) z =
4749 where _types = f :: Char -> Char -> Char
4850t_foldr (applyFun2 -> f) z = L. foldr f z `eqP` T. foldr f z
4951 where _types = f :: Char -> Char -> Char
52+ t_foldr' (applyFun2 -> f) z = L. foldr f z `eqP` T. foldr' f z
53+ where _types = f :: Char -> Char -> Char
5054tl_foldr (applyFun2 -> f) z = L. foldr f z `eqPSqrt` TL. foldr f z
5155 where _types = f :: Char -> Char -> Char
5256sf_foldr1 (applyFun -> p) (applyFun2 -> f) =
5357 (L. foldr1 f . L. filter p) `eqPSqrt` (S. foldr1 f . S. filter p)
5458t_foldr1 (applyFun2 -> f) = L. foldr1 f `eqP` T. foldr1 f
5559tl_foldr1 (applyFun2 -> f) = L. foldr1 f `eqPSqrt` TL. foldr1 f
5660
61+ -- Distinguish foldl/foldr from foldl'/foldr'
62+
63+ fold_apart :: IO ()
64+ fold_apart = do
65+ ok (T. foldr f () (T. pack " az" ))
66+ ko (T. foldr' f () (T. pack " az" ))
67+ ok (T. foldl (flip f) () (T. pack " za" ))
68+ ko (T. foldl' (flip f) () (T. pack " za" ))
69+ where
70+ f c _ = if c == ' z' then error " catchme" else ()
71+ ok = evaluate
72+ ko t = do
73+ x <- try (evaluate t)
74+ case x :: Either ErrorCall () of
75+ Left _ -> pure ()
76+ Right _ -> assertFailure " test should have failed but didn't"
77+
5778-- Special folds
5879
5980s_concat_s = (L. concat . unSqrt) `eq` (unpackS . S. unstream . S. concat . map packS . unSqrt)
@@ -187,10 +208,12 @@ testFolds =
187208 testProperty " tl_foldl1'" tl_foldl1',
188209 testProperty " sf_foldr" sf_foldr,
189210 testProperty " t_foldr" t_foldr,
211+ testProperty " t_foldr'" t_foldr',
190212 testProperty " tl_foldr" tl_foldr,
191213 testProperty " sf_foldr1" sf_foldr1,
192214 testProperty " t_foldr1" t_foldr1,
193215 testProperty " tl_foldr1" tl_foldr1,
216+ testCase " fold_apart" fold_apart,
194217
195218 testGroup " special" [
196219 testProperty " s_concat_s" s_concat_s,
0 commit comments