-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Motivation: HPACKHeaders is equatable, and it rarely makes sense to have a type that is equatable but not hashable. The definition of equatability is pretty limited here (see #342) but we can define a definition of hashability consistent with it. Modifications: - Make HPACKHeaders hashable. Result: People can store HPACKHeaders in Sets Co-authored-by: David Nadoba <dnadoba@gmail.com>
- Loading branch information
Showing
4 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// HPACKHeadersTests+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 HPACKHeadersTests { | ||
|
||
@available(*, deprecated, message: "not actually deprecated. Just deprecated to allow deprecated tests (which test deprecated functionality) without warnings") | ||
static var allTests : [(String, (HPACKHeadersTests) -> () throws -> Void)] { | ||
return [ | ||
("testHPACKHeadersAreHashable", testHPACKHeadersAreHashable), | ||
] | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the SwiftNIO open source project | ||
// | ||
// Copyright (c) 2022 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 NIOCore | ||
import NIOHPACK | ||
|
||
final class HPACKHeadersTests: XCTestCase { | ||
func testHPACKHeadersAreHashable() throws { | ||
let firstHeaders = HPACKHeaders([ | ||
(":foo", "bar"), | ||
(":bar", "baz"), | ||
("boz", "box"), | ||
("fox", "fro"), | ||
]) | ||
// The same as firstHeaders but in a different order. | ||
let secondHeaders = HPACKHeaders([ | ||
(":bar", "baz"), | ||
(":foo", "bar"), | ||
("fox", "fro"), | ||
("boz", "box"), | ||
]) | ||
// Differs by one name from firstHeaders | ||
let thirdHeaders = HPACKHeaders([ | ||
(":foo", "bar"), | ||
(":bar", "baz"), | ||
("boz", "box"), | ||
("fax", "fro"), | ||
]) | ||
// Differs by one value from firstHeaders | ||
let fourthHeaders = HPACKHeaders([ | ||
(":foo", "bar"), | ||
(":bar", "baz"), | ||
("boz", "box"), | ||
("fox", "fra"), | ||
]) | ||
|
||
// First, confirm all things hash to themselves. This proves basic hashing correctness. | ||
XCTAssertEqual( | ||
Set([firstHeaders, firstHeaders]), | ||
Set([firstHeaders]) | ||
) | ||
XCTAssertEqual( | ||
Set([secondHeaders, secondHeaders]), | ||
Set([secondHeaders]) | ||
) | ||
XCTAssertEqual( | ||
Set([thirdHeaders, thirdHeaders]), | ||
Set([thirdHeaders]) | ||
) | ||
XCTAssertEqual( | ||
Set([fourthHeaders, fourthHeaders]), | ||
Set([fourthHeaders]) | ||
) | ||
|
||
// Next, prove we can discriminate between different things. Here, importantly, secondHeaders is removed, as | ||
// it hashes equal to firstHeaders. | ||
XCTAssertEqual( | ||
Set([firstHeaders, secondHeaders, thirdHeaders, fourthHeaders]), | ||
Set([firstHeaders, thirdHeaders, fourthHeaders]) | ||
) | ||
} | ||
} |