forked from snoyberg/zlib-bindings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.hs
190 lines (170 loc) · 6.27 KB
/
test.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
{-# OPTIONS_GHC -F -pgmF htfpp #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
import System.Environment ( getArgs )
import Test.Framework
import Codec.Zlib
import Codec.Compression.Zlib
import qualified Codec.Compression.GZip as Gzip
import qualified Data.ByteString as S
import qualified Data.ByteString.Char8 as S8
import qualified Data.ByteString.UTF8 as SU8
import qualified Data.ByteString.Lazy as L
import Control.Monad (foldM)
import System.IO.Unsafe (unsafePerformIO)
decompress' :: L.ByteString -> L.ByteString
decompress' gziped = unsafePerformIO $ do
inf <- initInflate defaultWindowBits
ungziped <- foldM (go' inf) id $ L.toChunks gziped
final <- finishInflate inf
return $ L.fromChunks $ ungziped [final]
where
go' inf front bs = withInflateInput inf bs $ go front
go front x = do
y <- x
case y of
Nothing -> return front
Just z -> go (front . (:) z) x
prop_decompress' :: L.ByteString -> Bool
prop_decompress' lbs = lbs == decompress' (compress lbs)
compress' :: L.ByteString -> L.ByteString
compress' raw = unsafePerformIO $ do
def <- initDeflate 7 defaultWindowBits
gziped <- foldM (go' def) id $ L.toChunks raw
gziped' <- finishDeflate def $ go gziped
return $ L.fromChunks $ gziped' []
where
go' def front bs = withDeflateInput def bs $ go front
go front x = do
y <- x
case y of
Nothing -> return front
Just z -> go (front . (:) z) x
prop_compress' :: L.ByteString -> Bool
prop_compress' lbs = lbs == decompress (compress' lbs)
license :: S.ByteString
license = S8.filter (/= '\r') $ unsafePerformIO $ S.readFile "LICENSE"
exampleDict = SU8.fromString "INITIALDICTIONARY"
deflateWithDict :: SU8.ByteString -> L.ByteString -> L.ByteString
deflateWithDict dict raw = unsafePerformIO $ do
def <- initDeflateWithDictionary 7 dict $ WindowBits 15
compressed <- foldM (go' def) id $ L.toChunks raw
compressed' <- finishDeflate def $ go compressed
return $ L.fromChunks $ compressed' []
where
go' def front bs = withDeflateInput def bs $ go front
go front x = do
y <- x
case y of
Nothing -> return front
Just z -> go (front . (:) z) x
inflateWithDict :: SU8.ByteString -> L.ByteString -> L.ByteString
inflateWithDict dict compressed = unsafePerformIO $ do
inf <- initInflateWithDictionary (WindowBits 15) dict
decompressed <- foldM (go' inf) id $ L.toChunks compressed
final <- finishInflate inf
return $ L.fromChunks $ decompressed [final]
where
go' inf front bs = withInflateInput inf bs $ go front
go front x = do
y <- x
case y of
Nothing -> return front
Just z -> go (front . (:) z) x
prop_inflate_deflate_with_dictionary :: L.ByteString -> Bool
prop_inflate_deflate_with_dictionary bs =
bs == (inflateWithDict exampleDict . deflateWithDict exampleDict) bs
test_license_single_deflate :: Assertion
test_license_single_deflate = do
def <- initDeflate 8 $ WindowBits 31
gziped <- withDeflateInput def license $ go id
gziped' <- finishDeflate def $ go gziped
let raw' = L.fromChunks [license]
assertEqual raw' $ Gzip.decompress $ L.fromChunks $ gziped' []
where
go front x = do
y <- x
case y of
Nothing -> return front
Just z -> go (front . (:) z) x
test_fail_deflate_inflate_different_dict :: Assertion
test_fail_deflate_inflate_different_dict = do
raw <- L.readFile "LICENSE"
deflated <- return $ deflateWithDict exampleDict raw
inflated <- return $ inflateWithDict (SU8.drop 1 exampleDict) deflated
assertBool $ L.null inflated
test_license_single_inflate :: Assertion
test_license_single_inflate = do
gziped <- S.readFile "LICENSE.gz"
inf <- initInflate $ WindowBits 31
ungziped <- withInflateInput inf gziped $ go id
final <- finishInflate inf
assertEqual license $ S.concat $ ungziped [final]
where
go front x = do
y <- x
case y of
Nothing -> return front
Just z -> go (front . (:) z) x
test_license_multi_deflate :: Assertion
test_license_multi_deflate = do
def <- initDeflate 5 $ WindowBits 31
gziped <- foldM (go' def) id $ map S.singleton $ S.unpack license
gziped' <- finishDeflate def $ go gziped
let raw' = L.fromChunks [license]
assertEqual raw' $ Gzip.decompress $ L.fromChunks $ gziped' []
where
go' inf front bs = withDeflateInput inf bs $ go front
go front x = do
y <- x
case y of
Nothing -> return front
Just z -> go (front . (:) z) x
test_license_multi_inflate :: Assertion
test_license_multi_inflate = do
gziped <- S.readFile "LICENSE.gz"
let gziped' = map S.singleton $ S.unpack gziped
inf <- initInflate $ WindowBits 31
ungziped' <- foldM (go' inf) id gziped'
final <- finishInflate inf
assertEqual license $ S.concat $ ungziped' [final]
where
go' inf front bs = withInflateInput inf bs $ go front
go front x = do
y <- x
case y of
Nothing -> return front
Just z -> go (front . (:) z) x
instance Arbitrary L.ByteString where
arbitrary = L.fromChunks `fmap` arbitrary
instance Arbitrary S.ByteString where
arbitrary = S.pack `fmap` arbitrary
prop_lbs_zlib_inflate :: L.ByteString -> Bool
prop_lbs_zlib_inflate lbs = unsafePerformIO $ do
let glbs = compress lbs
inf <- initInflate defaultWindowBits
inflated <- foldM (go' inf) id $ L.toChunks glbs
final <- finishInflate inf
return $ lbs == L.fromChunks (inflated [final])
where
go' inf front bs = withInflateInput inf bs $ go front
go front x = do
y <- x
case y of
Nothing -> return front
Just z -> go (front . (:) z) x
prop_lbs_zlib_deflate :: L.ByteString -> Bool
prop_lbs_zlib_deflate lbs = unsafePerformIO $ do
def <- initDeflate 7 defaultWindowBits
deflated <- foldM (go' def) id $ L.toChunks lbs
deflated' <- finishDeflate def $ go deflated
return $ lbs == decompress (L.fromChunks (deflated' []))
where
go' inf front bs = withDeflateInput inf bs $ go front
go front x = do
y <- x
case y of
Nothing -> return front
Just z -> go (front . (:) z) x
main = do
args <- getArgs
runTestWithArgs args allHTFTests