]> git.immae.eu Git - github/fretlink/text-pipes.git/blame - test/Test.hs
scrap character replacement; simplify
[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
8c482809 26
c9d1c945 27
28import Debug.Trace
8c482809 29main :: IO ()
30main = defaultMain [tests]
31-- >>> :main -a 10000
32
33tests = 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
38t_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 52t_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
74t_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