GzipTests.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // GzipTests.swift
  3. // GzipTests
  4. //
  5. // Created by 1024jp on 2015-05-11.
  6. /*
  7. The MIT License (MIT)
  8. © 2015-2019 1024jp
  9. Permission is hereby granted, free of charge, to any person obtaining a copy
  10. of this software and associated documentation files (the "Software"), to deal
  11. in the Software without restriction, including without limitation the rights
  12. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. copies of the Software, and to permit persons to whom the Software is
  14. furnished to do so, subject to the following conditions:
  15. The above copyright notice and this permission notice shall be included in
  16. all copies or substantial portions of the Software.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. THE SOFTWARE.
  24. */
  25. import Binary
  26. import Gzip
  27. import XCTest
  28. final class GzipTests: XCTestCase {
  29. func testGZip() throws {
  30. for _ in 0 ..< 10 {
  31. let testSentence = String.lorem(length: Int.random(in: 1 ..< 100_000))
  32. let bytes = testSentence.bytes(using: .utf8)!
  33. let gzipped = try bytes.gzipped()
  34. let uncompressed = try gzipped.gunzipped()
  35. let uncompressedSentence = String(bytes: uncompressed, encoding: .utf8)
  36. XCTAssertNotEqual(gzipped, bytes)
  37. XCTAssertEqual(uncompressedSentence, testSentence)
  38. XCTAssertTrue(gzipped.isGzipped)
  39. XCTAssertFalse(bytes.isGzipped)
  40. XCTAssertFalse(uncompressed.isGzipped)
  41. }
  42. }
  43. func testZeroLength() throws {
  44. let zeroLengthBytes = Bytes()
  45. XCTAssertEqual(try zeroLengthBytes.gzipped(), zeroLengthBytes)
  46. XCTAssertEqual(try zeroLengthBytes.gunzipped(), zeroLengthBytes)
  47. XCTAssertFalse(zeroLengthBytes.isGzipped)
  48. }
  49. func testWrongUngzip() {
  50. // data not compressed
  51. let bytes = "testString".bytes(using: .utf8)!
  52. var uncompressed: Bytes?
  53. do {
  54. uncompressed = try bytes.gunzipped()
  55. } catch let error as GzipError where error.kind == .data {
  56. XCTAssertEqual(error.message, "incorrect header check")
  57. XCTAssertEqual(error.message, error.localizedDescription)
  58. } catch _ {
  59. XCTFail("Caught incorrect error.")
  60. }
  61. XCTAssertNil(uncompressed)
  62. }
  63. func testCompressionLevel() throws {
  64. let bytes = String.lorem(length: 100_000).bytes(using: .utf8)!
  65. XCTAssertGreaterThan(try bytes.gzipped(level: .bestSpeed).count,
  66. try bytes.gzipped(level: .bestCompression).count)
  67. }
  68. func testFileDecompression() throws {
  69. let url = self.bundleFile(name: "test.txt.gz")
  70. let bytes = try Bytes(contentsOf: url)
  71. let uncompressed = try bytes.gunzipped()
  72. XCTAssertTrue(bytes.isGzipped)
  73. XCTAssertEqual(String(bytes: uncompressed, encoding: .utf8), "test")
  74. }
  75. }
  76. extension XCTestCase {
  77. /// Create URL for bundled test file considering platform.
  78. ///
  79. /// - Parameter name: The file name to load in resources.
  80. fileprivate func bundleFile(name: String) -> URL {
  81. guard let url = Bundle.module.url(forResource: name, withExtension: nil) else {
  82. fatalError("can't find resource named: '\(name)'")
  83. }
  84. return url
  85. }
  86. }
  87. extension String {
  88. /// Generate random letters string for test.
  89. fileprivate static func lorem(length: Int) -> String {
  90. let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 "
  91. return (0 ..< length).reduce(into: "") { string, _ in
  92. string.append(letters.randomElement()!)
  93. }
  94. }
  95. }