Skip to content

Commit bceef54

Browse files
committed
Better use of StdlibUnittest in compression tests
1 parent d3f3890 commit bceef54

File tree

1 file changed

+64
-53
lines changed

1 file changed

+64
-53
lines changed

test/stdlib/Compression.swift

Lines changed: 64 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,18 @@ class DataSource {
3030
}
3131

3232
@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 {
3436

3537
var payload = Data()
3638
var decompressed = Data()
3739

3840
// Output filter compress
3941
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+
}
4145
while (true) {
4246
let i = f.readData(ofLength: 900)
4347
try ofilter.write(i) // will finalize when i is empty
@@ -46,92 +50,99 @@ func ofiltercompress_ifilterdecompress(_ contents: Data, using algo: Algorithm)
4650

4751
// Input filter decompress
4852
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)
5458
}
5559

5660
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
6162
}
6263

6364
@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 {
6568

6669
var payload = Data()
6770
var decompressed = Data()
6871

6972
// Input filter compress
7073
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)
7679
}
7780

7881
// Output filter decompress
7982
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+
}
8188
while (true) {
8289
let i = g.readData(ofLength: 900)
8390
try ofilter.write(i) // will finalize when i is empty
8491
if i == nil { break }
8592
}
8693

8794
print("ifilter | ofilter \(algo): \(contents.count) -> \(payload.count) -> \(decompressed.count)")
88-
let pass = (contents == decompressed)
89-
if !pass { print("FAIL") }
9095

91-
return pass
96+
return contents == decompressed
9297
}
9398

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. "
105103
// Change some random chars
106104
for _ in 1...10 {
107105
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...])
109108
}
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: "")
125112
}
126113

127114
let tests = TestSuite("Compression")
128115

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
133144
}
134-
expectEqual(nfailed,0)
145+
135146
}
136147

137148
runAllTests()

0 commit comments

Comments
 (0)