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