@@ -1,3 +1,4 @@
1+{-# LANGUAGE FlexibleInstances #-}
12{-# OPTIONS_GHC -fno-enable-rewrite-rules #-}
23
34import Test.QuickCheck
@@ -49,32 +50,51 @@ prop_T_utf16BE = (E.decodeUtf16BE . E.encodeUtf16BE) `eq` id
4950prop_T_utf32LE = (E.decodeUtf32LE . E.encodeUtf32LE) `eq` id
5051prop_T_utf32BE = (E.decodeUtf32BE . E.encodeUtf32BE) `eq` id
5152
53+class Target t where
54+ packT :: String -> t
55+ splitAtT :: Int -> t -> (t,t)
56+
57+instance Target (S.Stream Char) where
58+ packT = S.streamList
59+ splitAtT n s = (S.take n s, S.drop n s)
60+
61+instance Target T.Text where
62+ packT = T.pack
63+ splitAtT = T.splitAt
64+
65+instance Target TL.Text where
66+ packT = TL.pack
67+ splitAtT = TL.splitAt
68+
5269-- Do two functions give the same answer?
5370eq :: (Eq a) => (t -> a) -> (t -> a) -> t -> Bool
5471eq a b s = crashy False $ a s == b s
72+
5573-- What about with the RHS packed?
56−eqP :: (Eq a, Show a) => (String -> a) -> (T.Text -> a) -> String -> Word8 -> Bool
74+eqP :: (Eq a, Show a, Target t) =>
75+ (String -> a) -> (t -> a) -> String -> Word8 -> Bool
5776eqP a b s w = eq "orig" (a s) (b t) &&
5877 eq "head" (a sa) (b ta) &&
5978 eq "tail" (a sb) (b tb)
60− where t = T.pack s
79+ where t = packT s
6180 (sa,sb) = splitAt m s
62− (ta,tb) = T.splitAt m t
81+ (ta,tb) = splitAtT m t
6382 l = length s
6483 m | l == 0 = n
6584 | otherwise = n `mod` l
6685 n = fromIntegral w
6786 eq s a b | crashy False $ a == b = True
6887 | otherwise = trace (s ++ ": " ++ show a ++ " /= " ++ show b) False
88+
6989-- Or with the string non-empty, and the RHS packed?
70−eqEP :: (Eq a) =>
71− (String -> a) -> (T.Text -> a) -> NotEmpty String -> Word8 -> Bool
90+eqEP :: (Eq a, Target t) =>
91+ (String -> a) -> (t -> a) -> NotEmpty String -> Word8 -> Bool
7292eqEP a b e w = a s == b t &&
7393 (null sa || a sa == b ta) &&
7494 (null sb || a sb == b tb)
7595 where (sa,sb) = splitAt m s
76− (ta,tb) = T.splitAt m t
77− t = T.pack s
96+ (ta,tb) = splitAtT m t
97+ t = packT s
7898 l = length s
7999 m | l == 0 = n
80100 | otherwise = n `mod` l