@@ -30,14 +30,18 @@ class DataSource {
30
30
}
31
31
32
32
@available ( macOS 10 . 12 , iOS 10 . 0 , watchOS 3 . 0 , tvOS 10 . 0 , * )
33
- func ofiltercompress_ifilterdecompress( _ contents: Data , using algo: Algorithm ) throws -> Bool {
33
+ func ofiltercompress_ifilterdecompress(
34
+ _ contents: Data , using algo: Algorithm
35
+ ) throws -> Bool {
34
36
35
37
var payload = Data ( )
36
38
var decompressed = Data ( )
37
39
38
40
// Output filter compress
39
41
let f = DataSource ( contents)
40
- let ofilter = try Compression . OutputFilter ( . compress, using: algo) { ( x: Data ? ) -> ( ) in if x != nil { payload. append ( x!) } }
42
+ let ofilter = try OutputFilter ( . compress, using: algo) { ( x: Data ? ) -> ( ) in
43
+ if let x = x { payload. append ( x) }
44
+ }
41
45
while ( true ) {
42
46
let i = f. readData ( ofLength: 900 )
43
47
try ofilter. write ( i) // will finalize when i is empty
@@ -46,92 +50,99 @@ func ofiltercompress_ifilterdecompress(_ contents: Data, using algo: Algorithm)
46
50
47
51
// Input filter decompress
48
52
let g = DataSource ( payload)
49
- let ifilter = try InputFilter ( . decompress, using: algo, readingFrom : { ( x: Int ) -> Data ? in return g . readData ( ofLength : x ) } )
50
- while ( true ) {
51
- let i = try ifilter . readData ( ofLength : 400 )
52
- if i == nil { break }
53
- decompressed. append ( i! )
53
+ let ifilter = try InputFilter ( . decompress, using: algo) { ( x: Int ) -> Data ? in
54
+ return g . readData ( ofLength : x )
55
+ }
56
+ while let i = try ifilter . readData ( ofLength : 400 ) {
57
+ decompressed. append ( i)
54
58
}
55
59
56
60
print ( " ofilter | ifilter \( algo) : \( contents. count) -> \( payload. count) -> \( decompressed. count) " )
57
- let pass = ( contents == decompressed)
58
- if !pass { print ( " FAIL " ) }
59
-
60
- return pass
61
+ return contents == decompressed
61
62
}
62
63
63
64
@available ( macOS 10 . 12 , iOS 10 . 0 , watchOS 3 . 0 , tvOS 10 . 0 , * )
64
- func ifiltercompress_ofilterdecompress( _ contents: Data , using algo: Algorithm ) throws -> Bool {
65
+ func ifiltercompress_ofilterdecompress(
66
+ _ contents: Data , using algo: Algorithm
67
+ ) throws -> Bool {
65
68
66
69
var payload = Data ( )
67
70
var decompressed = Data ( )
68
71
69
72
// Input filter compress
70
73
let f = DataSource ( contents)
71
- let ifilter = try InputFilter ( . compress, using: algo, readingFrom : { ( x: Int ) -> Data ? in return f . readData ( ofLength : x ) } )
72
- while ( true ) {
73
- let i = try ifilter . readData ( ofLength : 400 )
74
- if i == nil { break }
75
- payload. append ( i! )
74
+ let ifilter = try InputFilter ( . compress, using: algo) { ( x: Int ) -> Data ? in
75
+ return f . readData ( ofLength : x )
76
+ }
77
+ while let i = try ifilter . readData ( ofLength : 400 ) {
78
+ payload. append ( i)
76
79
}
77
80
78
81
// Output filter decompress
79
82
let g = DataSource ( payload)
80
- let ofilter = try OutputFilter ( . decompress, using: algo) { ( x: Data ? ) -> ( ) in if x != nil { decompressed. append ( x!) } }
83
+ let ofilter = try OutputFilter ( . decompress, using: algo) { ( x: Data ? ) -> ( ) in
84
+ if let x = x {
85
+ decompressed. append ( x)
86
+ }
87
+ }
81
88
while ( true ) {
82
89
let i = g. readData ( ofLength: 900 )
83
90
try ofilter. write ( i) // will finalize when i is empty
84
91
if i == nil { break }
85
92
}
86
93
87
94
print ( " ifilter | ofilter \( algo) : \( contents. count) -> \( payload. count) -> \( decompressed. count) " )
88
- let pass = ( contents == decompressed)
89
- if !pass { print ( " FAIL " ) }
90
95
91
- return pass
96
+ return contents == decompressed
92
97
}
93
98
94
- @available ( macOS 10 . 12 , iOS 10 . 0 , watchOS 3 . 0 , tvOS 10 . 0 , * )
95
- func test_all( ) throws -> Int { // return number of failed cases
96
-
97
- var nfailed = 0
98
- let allAlgos : [ Algorithm ] = [ . lz4, . zlib, . lzfse, . lzma ]
99
-
100
- for len_blocks in [ 0 , 1 , 2 , 5 , 10 , 100 ] {
101
-
102
- var testString = " " ;
103
- for _ in 0 ..< len_blocks {
104
- var s = " Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
99
+ func randomString( withBlockLength n: Int ) -> String {
100
+ var strings = [ String] ( )
101
+ for _ in 0 ..< n {
102
+ var s = " Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
105
103
// Change some random chars
106
104
for _ in 1 ... 10 {
107
105
let pos = Int . random ( in: 0 ..< s. count)
108
- s = s. prefix ( pos) + " # " + s. dropFirst ( pos + 1 )
106
+ let idx = s. index ( s. startIndex, offsetBy: pos)
107
+ s = String ( s [ ..< idx] + " # " + s[ idx... ] )
109
108
}
110
- testString += s
111
- }
112
- let contents = testString. data ( using: . utf8) !
113
-
114
- for algo in allAlgos {
115
-
116
- if !( try ofiltercompress_ifilterdecompress ( contents, using: algo) ) { nfailed += 1 }
117
- if !( try ifiltercompress_ofilterdecompress ( contents, using: algo) ) { nfailed += 1 }
118
-
119
- }
120
-
121
- } // len_blocks
122
-
123
- if nfailed > 0 { print ( " FAIL \( nfailed) tests " ) } else { print ( " PASS " ) }
124
- return nfailed
109
+ strings. append ( s)
110
+ }
111
+ return strings. joined ( separator: " " )
125
112
}
126
113
127
114
let tests = TestSuite ( " Compression " )
128
115
129
- tests. test ( " Compression filters " ) {
130
- var nfailed = 0
131
- if #available( macOS 10 . 12 , iOS 10 . 0 , watchOS 3 . 0 , tvOS 10 . 0 , * ) {
132
- do { nfailed = try test_all ( ) } catch { print ( " exception caught " ) ; nfailed = 1 }
116
+ if #available( macOS 10 . 12 , iOS 10 . 0 , watchOS 3 . 0 , tvOS 10 . 0 , * ) {
117
+
118
+ do {
119
+ for blockLength in [ 0 , 1 , 2 , 5 , 10 , 100 ] {
120
+ let testString = randomString ( withBlockLength: blockLength)
121
+ let contents = testString. data ( using: . utf8) !
122
+
123
+ for algo in Algorithm . allCases {
124
+ tests. test ( " OutputFilter/Compress/InputFilter/Decompress/ \( algo) / \( blockLength) " ) {
125
+ expectDoesNotThrow ( {
126
+ expectTrue (
127
+ try ofiltercompress_ifilterdecompress ( contents, using: algo) ,
128
+ " Failing input: \( testString) "
129
+ )
130
+ } )
131
+ }
132
+
133
+ tests. test ( " InputFilter/Compress/OutputFilter/Decompress/ \( algo) / \( blockLength) " ) {
134
+ expectDoesNotThrow ( {
135
+ expectTrue (
136
+ try ifiltercompress_ofilterdecompress ( contents, using: algo) ,
137
+ " Failing input: \( testString) "
138
+ )
139
+ } )
140
+ }
141
+ }
142
+
143
+ } // blockLength
133
144
}
134
- expectEqual ( nfailed , 0 )
145
+
135
146
}
136
147
137
148
runAllTests ( )
0 commit comments