aboutsummaryrefslogtreecommitdiffhomepage
path: root/test
diff options
context:
space:
mode:
authormichaelt <what_is_it_to_do_anything@yahoo.com>2013-12-25 22:25:07 -0500
committermichaelt <what_is_it_to_do_anything@yahoo.com>2013-12-25 22:25:07 -0500
commitc9d1c945a4343d756533b85060c35c04be0c8b02 (patch)
tree0c31ae2e13002c1311ae3cc1e15608750c5a0a9c /test
parent8c48280926efffc0ca52a5d9ca796d639d053379 (diff)
downloadtext-pipes-c9d1c945a4343d756533b85060c35c04be0c8b02.tar.gz
text-pipes-c9d1c945a4343d756533b85060c35c04be0c8b02.tar.zst
text-pipes-c9d1c945a4343d756533b85060c35c04be0c8b02.zip
scrap character replacement; simplify
Diffstat (limited to 'test')
-rw-r--r--test/Test.hs60
1 files changed, 47 insertions, 13 deletions
diff --git a/test/Test.hs b/test/Test.hs
index 1579f2b..66351d1 100644
--- a/test/Test.hs
+++ b/test/Test.hs
@@ -8,6 +8,7 @@ import Test.Framework.Providers.QuickCheck2 (testProperty)
8import Control.Exception (catch) 8import Control.Exception (catch)
9import Data.Char (chr, isDigit, isHexDigit, isLower, isSpace, isUpper, ord) 9import Data.Char (chr, isDigit, isHexDigit, isLower, isSpace, isUpper, ord)
10import Data.Monoid (Monoid(..)) 10import Data.Monoid (Monoid(..))
11import Control.Monad
11import Data.String (fromString) 12import Data.String (fromString)
12import Data.Text.Encoding.Error 13import Data.Text.Encoding.Error
13import qualified Data.List as L 14import qualified Data.List as L
@@ -19,15 +20,20 @@ import qualified Data.Text as T
19import qualified Data.Text.Lazy as TL 20import qualified Data.Text.Lazy as TL
20import qualified Data.Text.Encoding as E 21import qualified Data.Text.Encoding as E
21import qualified Pipes.Text.Internal as PE 22import qualified Pipes.Text.Internal as PE
23import qualified Pipes.Text as TP
24import qualified Pipes.ByteString as BP
25import qualified Pipes as P
22 26
27
28import Debug.Trace
23main :: IO () 29main :: IO ()
24main = defaultMain [tests] 30main = defaultMain [tests]
25-- >>> :main -a 10000 31-- >>> :main -a 10000
26 32
27tests = testGroup "stream_decode" [ 33tests = testGroup "stream_decode" [
28 34 -- testProperty "t_utf8_incr_valid" t_utf8_incr_valid,
29 testProperty "t_utf8_incr_valid" t_utf8_incr_valid, 35 testProperty "t_utf8_incr_mixed" t_utf8_incr_mixed,
30 testProperty "t_utf8_incr_mixed" t_utf8_incr_mixed] 36 testProperty "t_utf8_incr_pipe" t_utf8_incr_pipe]
31 37
32t_utf8_incr_valid = do 38t_utf8_incr_valid = do
33 Positive n <- arbitrary 39 Positive n <- arbitrary
@@ -43,18 +49,46 @@ t_utf8_incr_valid = do
43 PE.Some t _ f' -> t : feedChunksOf n f' b 49 PE.Some t _ f' -> t : feedChunksOf n f' b
44 _ -> [] 50 _ -> []
45 51
46t_utf8_incr_mixed = do 52t_utf8_incr_mixed = do
47 Positive n <- arbitrary 53 Positive n <- arbitrary
48 txt <- genUnicode 54 txt <- genUnicode
49 forAll (vector 9) $ (roundtrip . chunk (mod n 7 + 1) . appendBytes txt) `eq` appendBytes txt 55 let chunkSize = mod n 7 + 1
56 forAll (vector 9) $
57 (roundtrip . chunk chunkSize . appendBytes txt) `eq` (appendBytes txt)
50 where 58 where
51 roundtrip :: [B.ByteString] -> B.ByteString 59 roundtrip :: [B.ByteString] -> B.ByteString
52 roundtrip bss = go (PE.streamDecodeUtf8With Nothing) B.empty B.empty bss where 60 roundtrip bss = go PE.streamDecodeUtf8 B.empty bss where
53 go dec acc old [] = acc <> old 61 go dec acc [] = acc
54 go dec acc old (bs:bss) = case dec bs of 62 go dec acc [bs] = case dec bs of
55 PE.Some t new dec' -> if T.null t then go dec' (acc <> E.encodeUtf8 t) (old <> new) bss 63 PE.Some t l dec' -> acc <> E.encodeUtf8 t <> l
56 else go dec' (acc <> E.encodeUtf8 t) new bss 64 PE.Other t bs' -> acc <> E.encodeUtf8 t <> bs'
57 PE.Other t bs' -> if T.null t then acc <> old <> bs <> B.concat bss 65 go dec acc (bs:bss) = case dec bs of
58 else acc <> E.encodeUtf8 t <> bs' <> B.concat bss 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
59 chunk n bs = let (a,b) = B.splitAt n bs in if B.null a then [] else a : chunk n b 88 chunk n bs = let (a,b) = B.splitAt n bs in if B.null a then [] else a : chunk n b
60 appendBytes txt bts = E.encodeUtf8 txt <> B.pack bts ; (<>) = B.append 89 appendBytes txt bts = E.encodeUtf8 txt <> B.pack bts ; (<>) = B.append
90
91
92
93
94