git.iko.soy/typeable/attoparsec
on master help sign in
Test the Buffer types
commit848e12eb01f93c5c72bca2ba23ac4e0e8d85498e authorBryan O'Sullivan <bos@serpentine.com> dateMon 26 May 2014, 22:53 (12 years ago) parent22c483a
3 files changed+94−3
attoparsec.cabal+6−3 tests/QC.hs+2 tests/QC/Buffer.hs+86
attoparsec.cabal+6−3
@@ -78,9 +78,10 @@ library
7878
7979test-suite tests
8080 type: exitcode-stdio-1.0
81− hs-source-dirs: tests
81+ hs-source-dirs: tests .
8282 main-is: QC.hs
83− other-modules: QC.ByteString
83+ other-modules: QC.Buffer
84+ QC.ByteString
8485 QC.Combinator
8586 QC.Common
8687 QC.Text
@@ -92,10 +93,12 @@ test-suite tests
9293 ghc-options: -Werror
9394
9495 build-depends:
95− attoparsec,
96+ array,
9697 base >= 4 && < 5,
9798 bytestring,
99+ deepseq >= 1.1,
98100 QuickCheck >= 2.7,
101+ scientific,
99102 test-framework >= 0.8.0.2,
100103 test-framework-quickcheck2 >= 0.3.0.3,
101104 text
tests/QC.hs+2
@@ -1,6 +1,7 @@
11{-# OPTIONS_GHC -fno-warn-missing-signatures #-}
22module Main (main) where
33
4+import qualified QC.Buffer as Buffer
45import qualified QC.ByteString as ByteString
56import qualified QC.Combinator as Combinator
67import qualified QC.Text as Text
@@ -10,6 +11,7 @@ main = defaultMain tests
1011
1112tests = [
1213 testGroup "bs" ByteString.tests
14+ , testGroup "buf" Buffer.tests
1315 , testGroup "combinator" Combinator.tests
1416 , testGroup "text" Text.tests
1517 ]
tests/QC/Buffer.hsnew file+86
@@ -0,0 +1,86 @@
1+{-# LANGUAGE BangPatterns, FlexibleInstances, OverloadedStrings,
2+ TypeSynonymInstances #-}
3+
4+module QC.Buffer (tests) where
5+
6+import Data.Monoid (mconcat)
7+import QC.Common ()
8+import Test.Framework (Test)
9+import Test.Framework.Providers.QuickCheck2 (testProperty)
10+import Test.QuickCheck
11+import qualified Data.Attoparsec.ByteString.Buffer as BB
12+import qualified Data.Attoparsec.Text.Buffer as BT
13+import qualified Data.ByteString as B
14+import qualified Data.ByteString.Unsafe as B
15+import qualified Data.Text as T
16+import qualified Data.Text.Unsafe as T
17+
18+data BP t b = BP [t] !t !b
19+ deriving (Eq, Show)
20+
21+type BPB = BP B.ByteString BB.Buffer
22+type BPT = BP T.Text BT.Buffer
23+
24+instance Arbitrary BPB where
25+ arbitrary = do
26+ bss <- arbitrary
27+ return $! BP bss (mconcat bss) (mconcat (map BB.buffer bss))
28+
29+instance Arbitrary BPT where
30+ arbitrary = do
31+ bss <- arbitrary
32+ return $! BP bss (mconcat bss) (mconcat (map BT.buffer bss))
33+
34+b_unbuffer :: BPB -> Property
35+b_unbuffer (BP _ts t buf) = t === BB.unbuffer buf
36+
37+t_unbuffer :: BPT -> Property
38+t_unbuffer (BP _ts t buf) = t === BT.unbuffer buf
39+
40+b_length :: BPB -> Property
41+b_length (BP _ts t buf) = B.length t === BB.length buf
42+
43+t_length :: BPT -> Property
44+t_length (BP _ts t buf) = T.lengthWord16 t === BT.length buf
45+
46+b_unsafeIndex :: BPB -> Gen Property
47+b_unsafeIndex (BP _ts t buf) = do
48+ let l = B.length t
49+ i <- choose (0,l-1)
50+ return $ l === 0 .||. B.unsafeIndex t i === BB.unsafeIndex buf i
51+
52+t_iter :: BPT -> Gen Property
53+t_iter (BP _ts t buf) = do
54+ let l = T.lengthWord16 t
55+ i <- choose (0,l-1)
56+ let it (T.Iter c q) = (c,q)
57+ return $ l === 0 .||. it (T.iter t i) === it (BT.iter buf i)
58+
59+t_iter_ :: BPT -> Gen Property
60+t_iter_ (BP _ts t buf) = do
61+ let l = T.lengthWord16 t
62+ i <- choose (0,l-1)
63+ return $ l === 0 .||. T.iter_ t i === BT.iter_ buf i
64+
65+b_unsafeDrop :: BPB -> Gen Property
66+b_unsafeDrop (BP _ts t buf) = do
67+ i <- choose (0, B.length t)
68+ return $ B.unsafeDrop i t === BB.unsafeDrop i buf
69+
70+t_dropWord16 :: BPT -> Gen Property
71+t_dropWord16 (BP _ts t buf) = do
72+ i <- choose (0, T.lengthWord16 t)
73+ return $ T.dropWord16 i t === BT.dropWord16 i buf
74+
75+tests :: [Test]
76+tests = [
77+ testProperty "b_unbuffer" b_unbuffer
78+ , testProperty "t_unbuffer" t_unbuffer
79+ , testProperty "b_length" b_length
80+ , testProperty "t_length" t_length
81+ , testProperty "b_unsafeIndex" b_unsafeIndex
82+ , testProperty "t_iter" t_iter
83+ , testProperty "t_iter_" t_iter_
84+ , testProperty "b_unsafeDrop" b_unsafeDrop
85+ , testProperty "t_dropWord16" t_dropWord16
86+ ]