CompressionTests.swift 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // CompressionTests.swift
  4. //
  5. // Created by Joseph Ross on 7/16/14.
  6. // Copyright © 2017 Joseph Ross.
  7. //
  8. // Licensed under the Apache License, Version 2.0 (the "License");
  9. // you may not use this file except in compliance with the License.
  10. // You may obtain a copy of the License at
  11. //
  12. // http://www.apache.org/licenses/LICENSE-2.0
  13. //
  14. // Unless required by applicable law or agreed to in writing, software
  15. // distributed under the License is distributed on an "AS IS" BASIS,
  16. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. // See the License for the specific language governing permissions and
  18. // limitations under the License.
  19. //
  20. //////////////////////////////////////////////////////////////////////////////////////////////////
  21. import XCTest
  22. @testable import Starscream
  23. class CompressionTests: XCTestCase {
  24. override func setUp() {
  25. super.setUp()
  26. // Put setup code here. This method is called before the invocation of each test method in the class.
  27. }
  28. override func tearDown() {
  29. // Put teardown code here. This method is called after the invocation of each test method in the class.
  30. super.tearDown()
  31. }
  32. func testBasic() {
  33. let compressor = Compressor(windowBits: 15)!
  34. let decompressor = Decompressor(windowBits: 15)!
  35. let rawData = "Hello, World! Hello, World! Hello, World! Hello, World! Hello, World!".data(using: .utf8)!
  36. let compressed = try! compressor.compress(rawData)
  37. let uncompressed = try! decompressor.decompress(compressed, finish: true)
  38. XCTAssert(rawData == uncompressed)
  39. }
  40. func testHugeData() {
  41. let compressor = Compressor(windowBits: 15)!
  42. let decompressor = Decompressor(windowBits: 15)!
  43. // 2 Gigs!
  44. // var rawData = Data(repeating: 0, count: 0x80000000)
  45. var rawData = Data(repeating: 0, count: 0x80000)
  46. let rawDataLen = rawData.count
  47. rawData.withUnsafeMutableBytes { (ptr: UnsafeMutablePointer<UInt8>) -> Void in
  48. arc4random_buf(ptr, rawDataLen)
  49. }
  50. let compressed = try! compressor.compress(rawData)
  51. let uncompressed = try! decompressor.decompress(compressed, finish: true)
  52. XCTAssert(rawData == uncompressed)
  53. }
  54. }