]> git.immae.eu Git - github/fretlink/text-pipes.git/blame - test/Test.hs
repaired tests, clean encodeUtf8 to return Done r rather than yield B.empty >> Done...
[github/fretlink/text-pipes.git] / test / Test.hs
CommitLineData
8c482809 1import Utils
2
3import Test.QuickCheck
4import Test.QuickCheck.Monadic
5import Test.Framework (Test, testGroup, defaultMain)
6import Test.Framework.Providers.QuickCheck2 (testProperty)
7
8import Control.Exception (catch)
9import Data.Char (chr, isDigit, isHexDigit, isLower, isSpace, isUpper, ord)
10import Data.Monoid (Monoid(..))
c9d1c945 11import Control.Monad
8c482809 12import Data.String (fromString)
13import Data.Text.Encoding.Error
14import qualified Data.List as L
15
16import qualified Data.Bits as Bits (shiftL, shiftR)
17import qualified Data.ByteString as B
18import qualified Data.ByteString.Lazy as BL
19import qualified Data.Text as T
20import qualified Data.Text.Lazy as TL
21import qualified Data.Text.Encoding as E
22import qualified Pipes.Text.Internal as PE
c9d1c945 23import qualified Pipes.Text as TP
24import qualified Pipes.ByteString as BP
25import qualified Pipes as P
1b4f5326 26
8c482809 27main :: IO ()
28main = defaultMain [tests]
29-- >>> :main -a 10000
30
31tests = testGroup "stream_decode" [
c9d1c945 32 -- testProperty "t_utf8_incr_valid" t_utf8_incr_valid,
cd4fd5dd 33 testProperty "t_utf8_incr_mixed" t_utf8_incr_mixed ,
1b4f5326 34 testProperty "t_utf8_incr_pipe" t_utf8_incr_pipe]
8c482809 35
36t_utf8_incr_valid = do
37 Positive n <- arbitrary
38 forAll genUnicode $ recode n `eq` id
39 where recode n = T.concat . feedChunksOf n PE.streamDecodeUtf8 . E.encodeUtf8
40 feedChunksOf :: Int -> (B.ByteString -> PE.Decoding) -> B.ByteString
41 -> [T.Text]
42 feedChunksOf n f bs
43 | B.null bs = []
44 | otherwise = let (a,b) = B.splitAt n bs
45 PE.Some t _ f' = f a
46 in case f a of
47 PE.Some t _ f' -> t : feedChunksOf n f' b
48 _ -> []
49
c9d1c945 50t_utf8_incr_mixed = do
51 Positive n <- arbitrary
8c482809 52 txt <- genUnicode
c9d1c945 53 let chunkSize = mod n 7 + 1
54 forAll (vector 9) $
55 (roundtrip . chunk chunkSize . appendBytes txt) `eq` (appendBytes txt)
8c482809 56 where
57 roundtrip :: [B.ByteString] -> B.ByteString
c9d1c945 58 roundtrip bss = go PE.streamDecodeUtf8 B.empty bss where
59 go dec acc [] = acc
60 go dec acc [bs] = case dec bs of
61 PE.Some t l dec' -> acc <> E.encodeUtf8 t <> l
62 PE.Other t bs' -> acc <> E.encodeUtf8 t <> bs'
63 go dec acc (bs:bss) = case dec bs of
64 PE.Some t l dec' -> go dec' (acc <> E.encodeUtf8 t) bss
65 PE.Other t bs' -> acc <> E.encodeUtf8 t <> bs' <> B.concat bss
66 chunk n bs = let (a,b) = B.splitAt n bs in if B.null a then [] else a : chunk n b
67 appendBytes txt bts = E.encodeUtf8 txt <> B.pack bts ; (<>) = B.append
68
c9d1c945 69t_utf8_incr_pipe = do
70 Positive m <- arbitrary
71 Positive n <- arbitrary
72 txt <- genUnicode
73 let chunkSize = mod n 7 + 1
cd4fd5dd 74 bytesLength = mod 10 m
c9d1c945 75 forAll (vector bytesLength) $
76 (BL.toStrict . BP.toLazy . roundtrip . P.each . chunk chunkSize . appendBytes txt)
77 `eq`
78 appendBytes txt
79 where
80 roundtrip :: Monad m => P.Producer B.ByteString m r -> P.Producer B.ByteString m r
1b4f5326 81 roundtrip p = join (TP.decodeUtf8 p P.>-> TP.encodeUtf8)
8c482809 82 chunk n bs = let (a,b) = B.splitAt n bs in if B.null a then [] else a : chunk n b
83 appendBytes txt bts = E.encodeUtf8 txt <> B.pack bts ; (<>) = B.append
c9d1c945 84
85
86
87
88