diff options
author | michaelt <what_is_it_to_do_anything@yahoo.com> | 2013-12-25 22:25:07 -0500 |
---|---|---|
committer | michaelt <what_is_it_to_do_anything@yahoo.com> | 2013-12-25 22:25:07 -0500 |
commit | c9d1c945a4343d756533b85060c35c04be0c8b02 (patch) | |
tree | 0c31ae2e13002c1311ae3cc1e15608750c5a0a9c /test | |
parent | 8c48280926efffc0ca52a5d9ca796d639d053379 (diff) | |
download | text-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.hs | 60 |
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) | |||
8 | import Control.Exception (catch) | 8 | import Control.Exception (catch) |
9 | import Data.Char (chr, isDigit, isHexDigit, isLower, isSpace, isUpper, ord) | 9 | import Data.Char (chr, isDigit, isHexDigit, isLower, isSpace, isUpper, ord) |
10 | import Data.Monoid (Monoid(..)) | 10 | import Data.Monoid (Monoid(..)) |
11 | import Control.Monad | ||
11 | import Data.String (fromString) | 12 | import Data.String (fromString) |
12 | import Data.Text.Encoding.Error | 13 | import Data.Text.Encoding.Error |
13 | import qualified Data.List as L | 14 | import qualified Data.List as L |
@@ -19,15 +20,20 @@ import qualified Data.Text as T | |||
19 | import qualified Data.Text.Lazy as TL | 20 | import qualified Data.Text.Lazy as TL |
20 | import qualified Data.Text.Encoding as E | 21 | import qualified Data.Text.Encoding as E |
21 | import qualified Pipes.Text.Internal as PE | 22 | import qualified Pipes.Text.Internal as PE |
23 | import qualified Pipes.Text as TP | ||
24 | import qualified Pipes.ByteString as BP | ||
25 | import qualified Pipes as P | ||
22 | 26 | ||
27 | |||
28 | import Debug.Trace | ||
23 | main :: IO () | 29 | main :: IO () |
24 | main = defaultMain [tests] | 30 | main = defaultMain [tests] |
25 | -- >>> :main -a 10000 | 31 | -- >>> :main -a 10000 |
26 | 32 | ||
27 | tests = testGroup "stream_decode" [ | 33 | tests = 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 | ||
32 | t_utf8_incr_valid = do | 38 | t_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 | ||
46 | t_utf8_incr_mixed = do | 52 | t_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 | |||
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 | ||
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 | |||