Skip to content

DateComponents, URLRequest, URLComponents: Fix hashing #1656

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Foundation.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,8 @@
6EB768281D18C12C00D4B719 /* UUID.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EB768271D18C12C00D4B719 /* UUID.swift */; };
7900433B1CACD33E00ECCBF1 /* TestNSCompoundPredicate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 790043391CACD33E00ECCBF1 /* TestNSCompoundPredicate.swift */; };
7900433C1CACD33E00ECCBF1 /* TestNSPredicate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7900433A1CACD33E00ECCBF1 /* TestNSPredicate.swift */; };
7D0DE86E211883F500540061 /* TestDateComponents.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D0DE86C211883F500540061 /* TestDateComponents.swift */; };
7D0DE86F211883F500540061 /* Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D0DE86D211883F500540061 /* Utilities.swift */; };
90E645DF1E4C89A400D0D47C /* TestNSCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 90E645DE1E4C89A400D0D47C /* TestNSCache.swift */; };
9F0DD3521ECD73D000F68030 /* main.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F0041781ECD5962004138BD /* main.swift */; };
9F0DD3571ECD783500F68030 /* SwiftFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5B5D885D1BBC938800234F36 /* SwiftFoundation.framework */; };
Expand Down Expand Up @@ -815,6 +817,8 @@
790043391CACD33E00ECCBF1 /* TestNSCompoundPredicate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSCompoundPredicate.swift; sourceTree = "<group>"; };
7900433A1CACD33E00ECCBF1 /* TestNSPredicate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSPredicate.swift; sourceTree = "<group>"; };
7A7D6FBA1C16439400957E2E /* TestURLResponse.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestURLResponse.swift; sourceTree = "<group>"; };
7D0DE86C211883F500540061 /* TestDateComponents.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestDateComponents.swift; sourceTree = "<group>"; };
7D0DE86D211883F500540061 /* Utilities.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Utilities.swift; sourceTree = "<group>"; };
83712C8D1C1684900049AD49 /* TestNSURLRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSURLRequest.swift; sourceTree = "<group>"; };
844DC3321C17584F005611F9 /* TestScanner.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestScanner.swift; sourceTree = "<group>"; };
848A30571C137B3500C83206 /* TestHTTPCookie.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = TestHTTPCookie.swift; path = TestFoundation/TestHTTPCookie.swift; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -1489,6 +1493,8 @@
EA66F65A1BF1976100136161 /* Tests */ = {
isa = PBXGroup;
children = (
7D0DE86D211883F500540061 /* Utilities.swift */,
7D0DE86C211883F500540061 /* TestDateComponents.swift */,
6E203B8C1C1303BB003B2576 /* TestBundle.swift */,
A5A34B551C18C85D00FD972B /* TestByteCountFormatter.swift */,
2EBE67A31C77BF05006583D5 /* TestDateFormatter.swift */,
Expand Down Expand Up @@ -2521,6 +2527,7 @@
D5C40F331CDA1D460005690C /* TestOperationQueue.swift in Sources */,
BDBB65901E256BFA001A7286 /* TestEnergyFormatter.swift in Sources */,
5B13B32F1C582D4C00651CE2 /* TestNSGeometry.swift in Sources */,
7D0DE86E211883F500540061 /* TestDateComponents.swift in Sources */,
EA08126C1DA810BE00651B70 /* ProgressFraction.swift in Sources */,
5B13B3351C582D4C00651CE2 /* TestNSKeyedUnarchiver.swift in Sources */,
5B13B33D1C582D4C00651CE2 /* TestPipe.swift in Sources */,
Expand All @@ -2547,6 +2554,7 @@
B951B5EC1F4E2A2000D8B332 /* TestNSLock.swift in Sources */,
5B13B33A1C582D4C00651CE2 /* TestNSNumber.swift in Sources */,
5B13B3521C582D4C00651CE2 /* TestNSValue.swift in Sources */,
7D0DE86F211883F500540061 /* Utilities.swift in Sources */,
5B13B3311C582D4C00651CE2 /* TestIndexPath.swift in Sources */,
5B13B3271C582D4C00651CE2 /* TestNSArray.swift in Sources */,
5B13B3461C582D4C00651CE2 /* TestProcess.swift in Sources */,
Expand Down
123 changes: 84 additions & 39 deletions Foundation/NSCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1276,49 +1276,94 @@ open class NSDateComponents : NSObject, NSCopying, NSSecureCoding {
public override init() {
super.init()
}

open override var hash: Int {
var calHash = 0
if let cal = calendar {
calHash = cal.hashValue
}
if let tz = timeZone {
calHash ^= tz.hashValue
}
var y = year
if NSDateComponentUndefined == y {
y = 0
}
var m = month
if NSDateComponentUndefined == m {
m = 0
}
var d = day
if NSDateComponentUndefined == d {
d = 0
}
var h = hour
if NSDateComponentUndefined == h {
h = 0
}
var mm = minute
if NSDateComponentUndefined == mm {
mm = 0
}
var s = second
if NSDateComponentUndefined == s {
s = 0
}
var yy = yearForWeekOfYear
if NSDateComponentUndefined == yy {
yy = 0
}
return calHash + (32832013 * (y + yy) + 2678437 * m + 86413 * d + 3607 * h + 61 * mm + s) + (41 * weekOfYear + 11 * weekOfMonth + 7 * weekday + 3 * weekdayOrdinal + quarter) * (1 << 5)
var hasher = Hasher()
var mask = 0
// The list of fields fed to the hasher here must be exactly
// the same as the ones compared in isEqual(_:) (modulo
// ordering).
//
// Given that NSDateComponents instances usually only have a
// few fields present, it makes sense to only hash those, as
// an optimization. We keep track of the fields hashed in the
// mask value, which we also feed to the hasher to make sure
// any two unequal values produce different hash encodings.
//
// FIXME: Why not just feed _values, calendar & timeZone to
// the hasher?
if let calendar = calendar {
hasher.combine(calendar)
mask |= 1 << 0
}
if let timeZone = timeZone {
hasher.combine(timeZone)
mask |= 1 << 1
}
if era != NSDateComponentUndefined {
hasher.combine(era)
mask |= 1 << 2
}
if year != NSDateComponentUndefined {
hasher.combine(year)
mask |= 1 << 3
}
if quarter != NSDateComponentUndefined {
hasher.combine(quarter)
mask |= 1 << 4
}
if month != NSDateComponentUndefined {
hasher.combine(month)
mask |= 1 << 5
}
if day != NSDateComponentUndefined {
hasher.combine(day)
mask |= 1 << 6
}
if hour != NSDateComponentUndefined {
hasher.combine(hour)
mask |= 1 << 7
}
if minute != NSDateComponentUndefined {
hasher.combine(minute)
mask |= 1 << 8
}
if second != NSDateComponentUndefined {
hasher.combine(second)
mask |= 1 << 9
}
if nanosecond != NSDateComponentUndefined {
hasher.combine(nanosecond)
mask |= 1 << 10
}
if weekOfYear != NSDateComponentUndefined {
hasher.combine(weekOfYear)
mask |= 1 << 11
}
if weekOfMonth != NSDateComponentUndefined {
hasher.combine(weekOfMonth)
mask |= 1 << 12
}
if yearForWeekOfYear != NSDateComponentUndefined {
hasher.combine(yearForWeekOfYear)
mask |= 1 << 13
}
if weekday != NSDateComponentUndefined {
hasher.combine(weekday)
mask |= 1 << 14
}
if weekdayOrdinal != NSDateComponentUndefined {
hasher.combine(weekdayOrdinal)
mask |= 1 << 15
}
hasher.combine(isLeapMonth)
hasher.combine(mask)
return hasher.finalize()
}

open override func isEqual(_ object: Any?) -> Bool {
guard let other = object as? NSDateComponents else { return false }

// FIXME: Why not just compare _values, calendar & timeZone?
return self === other
|| (era == other.era
&& year == other.year
Expand Down
13 changes: 13 additions & 0 deletions Foundation/NSURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,19 @@ open class NSURLComponents: NSObject, NSCopying {
&& fragment == other.fragment)
}

open override var hash: Int {
var hasher = Hasher()
hasher.combine(scheme)
hasher.combine(user)
hasher.combine(password)
hasher.combine(host)
hasher.combine(port)
hasher.combine(path)
hasher.combine(query)
hasher.combine(fragment)
return hasher.finalize()
}

open func copy(with zone: NSZone? = nil) -> Any {
let copy = NSURLComponents()
copy.scheme = self.scheme
Expand Down
13 changes: 12 additions & 1 deletion Foundation/NSURLRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,18 @@ open class NSURLRequest : NSObject, NSSecureCoding, NSCopying, NSMutableCopying
&& other.allowsCellularAccess == self.allowsCellularAccess
&& other.httpShouldHandleCookies == self.httpShouldHandleCookies)
}


open override var hash: Int {
var hasher = Hasher()
hasher.combine(url)
hasher.combine(mainDocumentURL)
hasher.combine(httpMethod)
hasher.combine(httpBodyStream)
hasher.combine(allowsCellularAccess)
hasher.combine(httpShouldHandleCookies)
return hasher.finalize()
}

/// Indicates that NSURLRequest implements the NSSecureCoding protocol.
open class var supportsSecureCoding: Bool { return true }

Expand Down
93 changes: 93 additions & 0 deletions TestFoundation/TestCalendar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,103 @@ class TestNSDateComponents: XCTestCase {

static var allTests: [(String, (TestNSDateComponents) -> () throws -> Void)] {
return [
("test_hash", test_hash),
("test_copyNSDateComponents", test_copyNSDateComponents),
]
}

func test_hash() {
let c1 = NSDateComponents()
c1.year = 2018
c1.month = 8
c1.day = 1

let c2 = NSDateComponents()
c2.year = 2018
c2.month = 8
c2.day = 1

XCTAssertEqual(c1, c2)
XCTAssertEqual(c1.hash, c2.hash)

checkHashing_NSCopying(
initialValue: NSDateComponents(),
byMutating: \NSDateComponents.calendar,
throughValues: [
Calendar(identifier: .gregorian),
Calendar(identifier: .buddhist),
Calendar(identifier: .chinese),
Calendar(identifier: .coptic),
Calendar(identifier: .hebrew),
Calendar(identifier: .indian),
Calendar(identifier: .islamic),
Calendar(identifier: .iso8601),
Calendar(identifier: .japanese),
Calendar(identifier: .persian)])
checkHashing_NSCopying(
initialValue: NSDateComponents(),
byMutating: \NSDateComponents.timeZone,
throughValues: (-10...10).map { TimeZone(secondsFromGMT: 3600 * $0) })
// Note: These assume components aren't range checked.
checkHashing_NSCopying(
initialValue: NSDateComponents(),
byMutating: \NSDateComponents.era,
throughValues: 0...20)
checkHashing_NSCopying(
initialValue: NSDateComponents(),
byMutating: \NSDateComponents.year,
throughValues: 0...20)
checkHashing_NSCopying(
initialValue: NSDateComponents(),
byMutating: \NSDateComponents.quarter,
throughValues: 0...20)
checkHashing_NSCopying(
initialValue: NSDateComponents(),
byMutating: \NSDateComponents.month,
throughValues: 0...20)
checkHashing_NSCopying(
initialValue: NSDateComponents(),
byMutating: \NSDateComponents.day,
throughValues: 0...20)
checkHashing_NSCopying(
initialValue: NSDateComponents(),
byMutating: \NSDateComponents.hour,
throughValues: 0...20)
checkHashing_NSCopying(
initialValue: NSDateComponents(),
byMutating: \NSDateComponents.minute,
throughValues: 0...20)
checkHashing_NSCopying(
initialValue: NSDateComponents(),
byMutating: \NSDateComponents.second,
throughValues: 0...20)
checkHashing_NSCopying(
initialValue: NSDateComponents(),
byMutating: \NSDateComponents.nanosecond,
throughValues: 0...20)
checkHashing_NSCopying(
initialValue: NSDateComponents(),
byMutating: \NSDateComponents.weekOfYear,
throughValues: 0...20)
checkHashing_NSCopying(
initialValue: NSDateComponents(),
byMutating: \NSDateComponents.weekOfMonth,
throughValues: 0...20)
checkHashing_NSCopying(
initialValue: NSDateComponents(),
byMutating: \NSDateComponents.yearForWeekOfYear,
throughValues: 0...20)
checkHashing_NSCopying(
initialValue: NSDateComponents(),
byMutating: \NSDateComponents.weekday,
throughValues: 0...20)
checkHashing_NSCopying(
initialValue: NSDateComponents(),
byMutating: \NSDateComponents.weekdayOrdinal,
throughValues: 0...20)
// isLeapMonth does not have enough values to test it here.
}

func test_copyNSDateComponents() {
let components = NSDateComponents()
components.year = 1987
Expand Down
Loading