Add Strideable conformance to HTTP2StreamID (#133)

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.
This commit is contained in:
Cory Benfield authored and GitHub committed 2019-05-26 10:24:10 +01:00
1 parent 0d153b56a4
commit dad9eee427
4 files changed
+73

No files matched your search

+14
View File
@@ -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.
+1
View File
@@ -43,5 +43,6 @@ import XCTest
testCase(OutboundFlowControlBufferTests.allTests),
testCase(ReentrancyTests.allTests),
testCase(SimpleClientServerTests.allTests),
testCase(StreamIDTests.allTests),
])
#endif
@@ -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
View File
@@ -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)))
}
}