@@ -1,108 +1,12 @@
1−{-# LANGUAGE OverloadedStrings #-}
2−{-# OPTIONS_GHC -fno-warn-orphans #-}
31module Main (main) where
42
5−import Control.Applicative ((<$>))
6−import Prelude hiding (takeWhile)
3+import qualified QC.ByteString as ByteString
4+import qualified QC.Text as Text
75import Test.Framework (defaultMain, testGroup)
8−import Test.Framework.Providers.QuickCheck2 (testProperty)
9−import Test.QuickCheck
10−import qualified Data.Attoparsec as P
11−import qualified Data.ByteString as B
12−import qualified Data.ByteString as S
13−import qualified Data.ByteString.Lazy as L
14−
15−instance Arbitrary S.ByteString where
16− arbitrary = S.pack <$> arbitrary
17−
18−instance Arbitrary L.ByteString where
19− arbitrary = sized $ \n -> resize (round (sqrt (toEnum n :: Double)))
20− ((L.fromChunks . map (S.pack . nonEmpty)) <$> arbitrary)
21− where nonEmpty (NonEmpty a) = a
22−
23−-- Naming.
24−
25−{-
26−label (NonEmpty s) = case parse (anyWord8 <?> s) B.empty of
27− (_, Left err) -> s `isInfixOf` err
28− _ -> False
29−-}
30−
31−-- Basic byte-level combinators.
32−
33−maybeP p s = case P.parse p s `P.feed` B.empty of
34− P.Done _ i -> Just i
35− _ -> Nothing
36−
37−defP p s = P.parse p s `P.feed` B.empty
38−
39−satisfy w s = maybeP (P.satisfy (<=w)) (B.cons w s) == Just w
40−
41−word8 w s = maybeP (P.word8 w) (B.cons w s) == Just w
42−
43−anyWord8 s = maybeP P.anyWord8 s == if B.null s
44− then Nothing
45− else Just (B.head s)
46−
47−notWord8 w (NonEmpty s) = maybeP (P.notWord8 w) bs == if v == w
48− then Nothing
49− else Just v
50− where v = B.head bs
51− bs = B.pack s
52−
53−string s = maybeP (P.string s) s == Just s
54−
55−skipWhile w s =
56− let t = B.dropWhile (<=w) s
57− in case defP (P.skipWhile (<=w)) s of
58− P.Done t' () -> t == t'
59− _ -> False
60−
61−takeCount (Positive k) s =
62− case maybeP (P.take k) s of
63− Nothing -> k > B.length s
64− Just s' -> k <= B.length s
65−
66−takeWhile w s =
67− let (h,t) = B.span (==w) s
68− in case defP (P.takeWhile (==w)) s of
69− P.Done t' h' -> t == t' && h == h'
70− _ -> False
71−
72−takeWhile1 w s =
73− let s' = B.cons w s
74− (h,t) = B.span (<=w) s'
75− in case defP (P.takeWhile1 (<=w)) s' of
76− P.Done t' h' -> t == t' && h == h'
77− _ -> False
78−
79−takeTill w s =
80− let (h,t) = B.break (==w) s
81− in case defP (P.takeTill (==w)) s of
82− P.Done t' h' -> t == t' && h == h'
83− _ -> False
84−
85−takeWhile1_empty = maybeP (P.takeWhile1 undefined) B.empty == Nothing
86−
87−endOfInput s = maybeP P.endOfInput s == if B.null s
88− then Just ()
89− else Nothing
906
917main = defaultMain tests
928
939tests = [
94− testGroup "fnord" [
95− testProperty "satisfy" satisfy,
96− testProperty "word8" word8,
97− testProperty "notWord8" notWord8,
98− testProperty "anyWord8" anyWord8,
99− testProperty "string" string,
100− testProperty "skipWhile" skipWhile,
101− testProperty "takeCount" takeCount,
102− testProperty "takeWhile" takeWhile,
103− testProperty "takeWhile1" takeWhile1,
104− testProperty "takeWhile1_empty" takeWhile1_empty,
105− testProperty "takeTill" takeTill,
106− testProperty "endOfInput" endOfInput
107− ]
10+ testGroup "bs" ByteString.tests
11+ , testGroup "text" Text.tests
10812 ]