on master help sign in
Fix a bug in unfoldrN, add sensible tests

--HG-- extra : convert_revision : ba115606d8468702747f577b7aba18b3d7376dd8

commitd1508c3a72ff6dd007b33720137df7aad8fc47b5 authorBryan O'Sullivan <bos@serpentine.com> dateTue 3 Feb 2009, 22:16 (18 years ago) parente8675b0
3 files changed+26−8
Data/Text/Fusion.hs+2−1 tests/Properties.hs+9−6 tests/QuickCheckUtils.hs+15−1
Data/Text/Fusion.hs+2−1
@@ -592,7 +592,8 @@ unfoldr f s0 = Stream next s0 1
592592-- first argument to 'unfoldrN'. This function is more efficient than
593593-- 'unfoldr' when the length of the result is known.
594594unfoldrN :: Int -> (a -> Maybe (Char,a)) -> a -> Stream Char
595−unfoldrN n f s0 = Stream next (0 :!: s0) (n*2)
595+unfoldrN n f s0 | n < 0 = empty
596+ | otherwise = Stream next (0 :!: s0) (n*2)
596597 where
597598 {-# INLINE next #-}
598599 next (z :!: s) = case f s of
tests/Properties.hs+9−6
@@ -74,10 +74,13 @@ prop_minimum = L.minimum `eqEP` T.minimum
7474prop_scanl f z = L.scanl f z `eqP` (unpack . T.scanl f z)
7575
7676prop_replicate n = L.replicate n `eq` (unpack . T.replicate n)
77−prop_unfoldr f = L.unfoldr f `eq` (unpack . T.unfoldr f)
78− where types = f :: Int -> Maybe (Char,Int)
79−prop_unfoldrN n f = (L.take n . L.unfoldr f) `eq` (unpack . T.unfoldrN n f)
80− where types = f :: Int -> Maybe (Char,Int)
77+prop_unfoldr n = L.unfoldr f `eq` (unpack . T.unfoldr f)
78+ where f c | fromEnum c * 100 > n = Nothing
79+ | otherwise = Just (c, succ c)
80+
81+prop_unfoldrN n m = (L.take n . L.unfoldr f) `eq` (unpack . T.unfoldrN n f)
82+ where f c | fromEnum c * 100 > m = Nothing
83+ | otherwise = Just (c, succ c)
8184
8285prop_take n = L.take n `eqP` (unpack . T.take n)
8386prop_drop n = L.drop n `eqP` (unpack . T.drop n)
@@ -158,8 +161,8 @@ tests = [
158161 ("prop_scanl", mytest prop_scanl),
159162
160163 ("prop_replicate", mytest prop_replicate),
161−--("prop_unfoldr", mytest prop_unfoldr),
162−--("prop_unfoldrN", mytest prop_unfoldrN),
164+ ("prop_unfoldr", mytest prop_unfoldr),
165+ ("prop_unfoldrN", mytest prop_unfoldrN),
163166
164167 ("prop_take", mytest prop_take),
165168 ("prop_drop", mytest prop_drop),
tests/QuickCheckUtils.hs+15−1
@@ -3,13 +3,27 @@
33module QuickCheckUtils where
44
55import Data.List
6+import Data.Word
67import qualified Data.Text as T
78import System.IO
89import System.Random
910import Test.QuickCheck
1011
12+integralRandomR :: (Integral a, RandomGen g) => (a,a) -> g -> (a,g)
13+integralRandomR (a,b) g = case randomR (fromIntegral a :: Integer,
14+ fromIntegral b :: Integer) g of
15+ (x,g) -> (fromIntegral x, g)
16+
17+instance Random Word16 where
18+ randomR = integralRandomR
19+ random = randomR (minBound,maxBound)
20+
21+instance Arbitrary Word16 where
22+ arbitrary = choose (minBound,maxBound)
23+ coarbitrary c = variant (fromEnum c `rem` 4)
24+
1125instance Arbitrary Char where
12− arbitrary = oneof [choose ('\0','\55295'), choose ('\57334','\1114111')]
26+ arbitrary = oneof [choose ('\0','\55295'), choose ('\57334','\1114111')]
1327 coarbitrary c = variant (fromEnum c `rem` 4)
1428
1529instance Arbitrary T.Text where