Skip to content

Commit

Permalink
Add Strideable conformance to HTTP2StreamID (#133)
Browse files Browse the repository at this point in the history
Motivation:

While working on other tests I realised that HTTP2StreamID is a type
that can meaningfully be strided, but it does not conform to Strideable.
We may as well add that conformance.

Modifications:

- Added Strideable conformance to HTTP2StreamID.

Result:

Users can stride over HTTP2StreamIDs.
  • Loading branch information
Lukasa authored May 26, 2019
1 parent 0d153b5 commit dad9eee
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Sources/NIOHTTP2/HTTP2StreamID.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,20 @@ extension HTTP2StreamID: ExpressibleByIntegerLiteral {
}


// MARK:- Strideable conformance for HTTP2StreamID
extension HTTP2StreamID: Strideable {
public typealias Stride = Int

public func advanced(by n: Stride) -> HTTP2StreamID {
return HTTP2StreamID(self.networkStreamID + Int32(n))
}

public func distance(to other: HTTP2StreamID) -> Stride {
return Int(other.networkStreamID - self.networkStreamID)
}
}


// MARK:- Helper initializers for integer conversion.
public extension Int {
/// Create an Int holding the integer value of this streamID.
Expand Down
1 change: 1 addition & 0 deletions Tests/LinuxMain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@ import XCTest
testCase(OutboundFlowControlBufferTests.allTests),
testCase(ReentrancyTests.allTests),
testCase(SimpleClientServerTests.allTests),
testCase(StreamIDTests.allTests),
])
#endif
33 changes: 33 additions & 0 deletions Tests/NIOHTTP2Tests/StreamIDTests+XCTest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2018 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
//
// StreamIDTests+XCTest.swift
//
import XCTest

///
/// NOTE: This file was generated by generate_linux_tests.rb
///
/// Do NOT edit this file directly as it will be regenerated automatically when needed.
///

extension StreamIDTests {

static var allTests : [(String, (StreamIDTests) -> () throws -> Void)] {
return [
("testStreamIDsAreStrideable", testStreamIDsAreStrideable),
]
}
}

25 changes: 25 additions & 0 deletions Tests/NIOHTTP2Tests/StreamIDTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2019 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import XCTest
import NIOHTTP2


final class StreamIDTests: XCTestCase {
func testStreamIDsAreStrideable() {
XCTAssertEqual(Array(HTTP2StreamID(1)..<HTTP2StreamID(30)), Array(stride(from: HTTP2StreamID(1), to: HTTP2StreamID(30), by: 1)))
XCTAssertEqual([10, 9, 8, 7, 6, 5, 4, 3, 2, 1], Array(stride(from: HTTP2StreamID(10), through: HTTP2StreamID(1), by: -1)))
XCTAssertEqual([1, 3, 5, 7, 9], Array(stride(from: HTTP2StreamID(1), to: HTTP2StreamID(10), by: 2)))
}
}

0 comments on commit dad9eee

Please sign in to comment.