OverfowingBuffer.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // OverflowingBuffer.swift
  3. //
  4. // Copyright © 2015 Rob Napier. All rights reserved.
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a
  7. // copy of this software and associated documentation files (the "Software"),
  8. // to deal in the Software without restriction, including without limitation
  9. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. // and/or sell copies of the Software, and to permit persons to whom the
  11. // Software is furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. // DEALINGS IN THE SOFTWARE.
  23. //
  24. import XCTest
  25. @testable import RNCryptor
  26. class OverflowingBufferTest: XCTestCase {
  27. // When a OverflowingBuffer receives less than its capacity, it outputs nothing and holds everything
  28. func testShort() {
  29. let buffer = OverflowingBuffer(capacity: 4)
  30. let data = Data([1,2,3])
  31. let out = buffer.update(withData: data)
  32. XCTAssert(out.count == 0)
  33. XCTAssertEqual(buffer.finalData(), Data([1,2,3]))
  34. }
  35. // When a OverflowingBuffer receives exactly its capacity, it outputs nothing and holds everything
  36. func testExactly() {
  37. let buffer = OverflowingBuffer(capacity: 4)
  38. let data = Data([1,2,3,4])
  39. let out = buffer.update(withData: data)
  40. XCTAssert(out.count == 0)
  41. XCTAssertEqual(buffer.finalData(), Data([1,2,3,4]))
  42. }
  43. // When a OverflowingBuffer receives more than its capacity, it outputs the earliest bytes and holds the rest
  44. func testOverflow() {
  45. let buffer = OverflowingBuffer(capacity: 4)
  46. let data = Data([1,2,3,4,5])
  47. let out = buffer.update(withData: data)
  48. XCTAssertEqual(out, Data([1]))
  49. XCTAssertEqual(buffer.finalData(), Data([2,3,4,5]))
  50. }
  51. // When a OverflowingBuffer receives less than its capacity in multiple writes, it outputs nothing and holds everything
  52. func testMultiShort() {
  53. let buffer = OverflowingBuffer(capacity: 4)
  54. let out = NSMutableData(data:buffer.update(withData: Data([1])))
  55. out.append(buffer.update(withData: Data([2,3])))
  56. XCTAssert(out.length == 0)
  57. XCTAssertEqual(buffer.finalData(), Data([1,2,3]))
  58. }
  59. // When a OverflowingBuffer receives more than its capacity in multiple writes, it outputs the earliest bytes and holds the rest
  60. func testMultiOverflow() {
  61. let buffer = OverflowingBuffer(capacity: 4)
  62. var out = buffer.update(withData: Data([1,2,3]))
  63. XCTAssertEqual(out.count, 0)
  64. out.append(buffer.update(withData: Data([4,5,6])))
  65. XCTAssertEqual(out, Data([1,2]))
  66. XCTAssertEqual(buffer.finalData(), Data([3,4,5,6]))
  67. }
  68. // When a OverflowingBuffer receives more than its capacity when it already had elements, it outputs the earliest bytes and holds the rest
  69. func testMultiMegaOverflow() {
  70. let buffer = OverflowingBuffer(capacity: 4)
  71. var out = buffer.update(withData: Data([1,2,3]))
  72. XCTAssertEqual(out.count, 0)
  73. out.append(buffer.update(withData: Data([4,5,6,7,8,9])))
  74. XCTAssertEqual(out, Data([1,2,3,4,5]))
  75. XCTAssertEqual(buffer.finalData(), Data([6,7,8,9]))
  76. }
  77. }