on master help sign in
Add tests for shifts, and fix a bug exposed along the way.

--HG-- extra : convert_revision : 91346438c89ca067c82d1910726628fbdd90b26f

commit793e0b343e88c284c6c216f731e0e26609391eee authorBryan O'Sullivan <bos@serpentine.com> dateSat 15 Aug 2009, 07:27 (17 years ago) parent532149b
3 files changed+38−4
Data/Text/UnsafeShift.hs+2−2 tests/Properties.hs+27−1 tests/QuickCheckUtils.hs+9−1
Data/Text/UnsafeShift.hs+2−2
@@ -33,14 +33,14 @@ class UnsafeShift a where
3333
3434instance UnsafeShift Word16 where
3535 {-# INLINE shiftL #-}
36− shiftL (W16# x#) (I# i#) = W16# (x# `uncheckedShiftL#` i#)
36+ shiftL (W16# x#) (I# i#) = W16# (narrow16Word# (x# `uncheckedShiftL#` i#))
3737
3838 {-# INLINE shiftR #-}
3939 shiftR (W16# x#) (I# i#) = W16# (x# `uncheckedShiftRL#` i#)
4040
4141instance UnsafeShift Word32 where
4242 {-# INLINE shiftL #-}
43− shiftL (W32# x#) (I# i#) = W32# (x# `uncheckedShiftL#` i#)
43+ shiftL (W32# x#) (I# i#) = W32# (narrow32Word# (x# `uncheckedShiftL#` i#))
4444
4545 {-# INLINE shiftR #-}
4646 shiftR (W32# x#) (I# i#) = W32# (x# `uncheckedShiftRL#` i#)
tests/Properties.hs+27−1
@@ -5,12 +5,13 @@
55import Test.QuickCheck
66import Text.Show.Functions ()
77
8+import qualified Data.Bits as Bits (shiftL, shiftR)
89import Data.Char (chr, isLower, isSpace, isUpper, ord)
910import Data.Monoid (Monoid(..))
1011import Data.String (fromString)
1112import Debug.Trace (trace)
1213import Control.Arrow ((***), second)
13−import Data.Word (Word8)
14+import Data.Word (Word8, Word16, Word32)
1415import qualified Data.Text as T
1516import qualified Data.Text.Compat as C
1617import qualified Data.Text.Lazy as TL
@@ -20,6 +21,7 @@ import qualified Data.Text.Fusion as S
2021import qualified Data.Text.Fusion.Common as S
2122import qualified Data.Text.Lazy.Encoding as EL
2223import qualified Data.Text.Lazy.Fusion as SL
24+import qualified Data.Text.UnsafeShift as U
2325import qualified Data.List as L
2426import System.IO.Unsafe (unsafePerformIO)
2527import Test.Framework (defaultMain, testGroup)
@@ -465,6 +467,21 @@ sf_zipWith p c s = (L.zipWith c (L.filter p s) . L.filter p) `eqP` (unpackS . S
465467t_zipWith c s = L.zipWith c s `eqP` (unpackS . T.zipWith c (packS s))
466468tl_zipWith c s = L.zipWith c s `eqP` (unpackS . TL.zipWith c (packS s))
467469
470+-- Bit shifts.
471+shiftL w = forAll (choose (0,width-1)) $ \k -> Bits.shiftL w k == U.shiftL w k
472+ where width = round (log (fromIntegral m) / log 2)
473+ (m,_) = (maxBound, m == w)
474+shiftR w = forAll (choose (0,width-1)) $ \k -> Bits.shiftR w k == U.shiftR w k
475+ where width = round (log (fromIntegral m) / log 2)
476+ (m,_) = (maxBound, m == w)
477+
478+shiftL_Int = shiftL :: Int -> Property
479+shiftL_Word16 = shiftL :: Word16 -> Property
480+shiftL_Word32 = shiftL :: Word32 -> Property
481+shiftR_Int = shiftR :: Int -> Property
482+shiftR_Word16 = shiftR :: Word16 -> Property
483+shiftR_Word32 = shiftR :: Word32 -> Property
484+
468485-- Regression tests.
469486s_filter_eq s = S.filter p t == S.streamList (filter p s)
470487 where p = (/= S.last t)
@@ -799,5 +816,14 @@ tests = [
799816
800817 testGroup "regressions" [
801818 testProperty "s_filter_eq" s_filter_eq
819+ ],
820+
821+ testGroup "shifts" [
822+ testProperty "shiftL_Int" shiftL_Int,
823+ testProperty "shiftL_Word16" shiftL_Word16,
824+ testProperty "shiftL_Word32" shiftL_Word32,
825+ testProperty "shiftR_Int" shiftR_Int,
826+ testProperty "shiftR_Word16" shiftR_Word16,
827+ testProperty "shiftR_Word32" shiftR_Word32
802828 ]
803829 ]
tests/QuickCheckUtils.hs+9−1
@@ -2,7 +2,7 @@
22
33module QuickCheckUtils where
44
5−import Data.Word (Word8, Word16)
5+import Data.Word (Word8, Word16, Word32)
66import qualified Data.Text as T
77import qualified Data.Text.Lazy as TL
88import System.Random (Random(..), RandomGen)
@@ -34,6 +34,14 @@ instance Arbitrary Word16 where
3434 arbitrary = choose (minBound,maxBound)
3535 coarbitrary c = variant (fromEnum c `rem` 4)
3636
37+instance Random Word32 where
38+ randomR = integralRandomR
39+ random = randomR (minBound,maxBound)
40+
41+instance Arbitrary Word32 where
42+ arbitrary = choose (minBound,maxBound)
43+ coarbitrary c = variant (fromEnum c `rem` 4)
44+
3745instance Arbitrary Char where
3846 arbitrary = oneof [choose ('\0','\55295'), choose ('\57344','\1114111')]
3947 coarbitrary c = variant (fromEnum c `rem` 4)