Skip to content

Commit 86a038f

Browse files
committed
DateInterval: Modernize hashing
1 parent c81e113 commit 86a038f

File tree

4 files changed

+50
-13
lines changed

4 files changed

+50
-13
lines changed

Foundation.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,7 @@
355355
7D0DE86E211883F500540061 /* TestDateComponents.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D0DE86C211883F500540061 /* TestDateComponents.swift */; };
356356
7D0DE86F211883F500540061 /* Utilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D0DE86D211883F500540061 /* Utilities.swift */; };
357357
7D8BD739225ED1480057CF37 /* TestMeasurement.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D8BD738225ED1480057CF37 /* TestMeasurement.swift */; };
358+
7D8BD737225EADB80057CF37 /* TestDateInterval.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D8BD736225EADB80057CF37 /* TestDateInterval.swift */; };
358359
90E645DF1E4C89A400D0D47C /* TestNSCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = 90E645DE1E4C89A400D0D47C /* TestNSCache.swift */; };
359360
91B668A32252B3C5001487A1 /* FileManager+POSIX.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91B668A22252B3C5001487A1 /* FileManager+POSIX.swift */; };
360361
91B668A52252B3E7001487A1 /* FileManager+Win32.swift in Sources */ = {isa = PBXBuildFile; fileRef = 91B668A42252B3E7001487A1 /* FileManager+Win32.swift */; };
@@ -883,6 +884,7 @@
883884
7D0DE86C211883F500540061 /* TestDateComponents.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestDateComponents.swift; sourceTree = "<group>"; };
884885
7D0DE86D211883F500540061 /* Utilities.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Utilities.swift; sourceTree = "<group>"; };
885886
7D8BD738225ED1480057CF37 /* TestMeasurement.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestMeasurement.swift; sourceTree = "<group>"; };
887+
7D8BD736225EADB80057CF37 /* TestDateInterval.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestDateInterval.swift; sourceTree = "<group>"; };
886888
83712C8D1C1684900049AD49 /* TestNSURLRequest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSURLRequest.swift; sourceTree = "<group>"; };
887889
844DC3321C17584F005611F9 /* TestScanner.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestScanner.swift; sourceTree = "<group>"; };
888890
848A30571C137B3500C83206 /* TestHTTPCookie.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = TestHTTPCookie.swift; path = TestFoundation/TestHTTPCookie.swift; sourceTree = SOURCE_ROOT; };
@@ -1588,6 +1590,7 @@
15881590
7D0DE86D211883F500540061 /* Utilities.swift */,
15891591
155D3BBB22401D1100B0D38E /* FixtureValues.swift */,
15901592
7D0DE86C211883F500540061 /* TestDateComponents.swift */,
1593+
7D8BD736225EADB80057CF37 /* TestDateInterval.swift */,
15911594
6E203B8C1C1303BB003B2576 /* TestBundle.swift */,
15921595
A5A34B551C18C85D00FD972B /* TestByteCountFormatter.swift */,
15931596
2EBE67A31C77BF05006583D5 /* TestDateFormatter.swift */,
@@ -2664,6 +2667,7 @@
26642667
2EBE67A51C77BF0E006583D5 /* TestDateFormatter.swift in Sources */,
26652668
5B13B3291C582D4C00651CE2 /* TestCalendar.swift in Sources */,
26662669
158BCCAA2220A12600750239 /* TestDateIntervalFormatter.swift in Sources */,
2670+
7D8BD737225EADB80057CF37 /* TestDateInterval.swift in Sources */,
26672671
5B13B34F1C582D4C00651CE2 /* TestXMLParser.swift in Sources */,
26682672
BF85E9D81FBDCC2000A79793 /* TestHost.swift in Sources */,
26692673
D5C40F331CDA1D460005690C /* TestOperationQueue.swift in Sources */,

Foundation/DateInterval.swift

+3-8
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,9 @@ public struct DateInterval : ReferenceConvertible, Comparable, Hashable {
150150
return false
151151
}
152152

153-
public var hashValue: Int {
154-
var buf: (UInt, UInt) = (UInt(start.timeIntervalSinceReferenceDate), UInt(end.timeIntervalSinceReferenceDate))
155-
return withUnsafeMutablePointer(to: &buf) { bufferPtr in
156-
let count = MemoryLayout<UInt>.size * 2
157-
return bufferPtr.withMemoryRebound(to: UInt8.self, capacity: count) { bufferBytes in
158-
return Int(bitPattern: CFHashBytes(bufferBytes, CFIndex(count)))
159-
}
160-
}
153+
public func hash(into hasher: inout Hasher) {
154+
hasher.combine(start)
155+
hasher.combine(duration)
161156
}
162157

163158
public static func ==(lhs: DateInterval, rhs: DateInterval) -> Bool {

TestFoundation/TestDateInterval.swift

+42-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,46 @@
1+
// This source file is part of the Swift.org open source project
12
//
2-
// TestDateInterval.swift
3-
// TestFoundation
3+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
4+
// Licensed under Apache License v2.0 with Runtime Library Exception
45
//
5-
// Created by Karoly Lorentey on 2019-04-10.
6-
// Copyright © 2019 Apple. All rights reserved.
6+
// See http://swift.org/LICENSE.txt for license information
7+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
78
//
89

9-
import Foundation
10+
class TestDateInterval: XCTestCase {
11+
static var allTests: [(String, (TestDateInterval) -> () throws -> Void)] {
12+
return [
13+
("test_hashing", test_hashing),
14+
]
15+
}
16+
17+
func test_hashing() {
18+
guard #available(iOS 10.10, OSX 10.12, tvOS 10.0, watchOS 3.0, *) else { return }
19+
20+
let start1a = dateWithString("2019-04-04 17:09:23 -0700")
21+
let start1b = dateWithString("2019-04-04 17:09:23 -0700")
22+
let start2a = Date(timeIntervalSinceReferenceDate: start1a.timeIntervalSinceReferenceDate.nextUp)
23+
let start2b = Date(timeIntervalSinceReferenceDate: start1a.timeIntervalSinceReferenceDate.nextUp)
24+
let duration1 = 1800.0
25+
let duration2 = duration1.nextUp
26+
let intervals: [[DateInterval]] = [
27+
[
28+
DateInterval(start: start1a, duration: duration1),
29+
DateInterval(start: start1b, duration: duration1),
30+
],
31+
[
32+
DateInterval(start: start1a, duration: duration2),
33+
DateInterval(start: start1b, duration: duration2),
34+
],
35+
[
36+
DateInterval(start: start2a, duration: duration1),
37+
DateInterval(start: start2b, duration: duration1),
38+
],
39+
[
40+
DateInterval(start: start2a, duration: duration2),
41+
DateInterval(start: start2b, duration: duration2),
42+
],
43+
]
44+
checkHashableGroups(intervals)
45+
}
46+
}

TestFoundation/main.swift

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ XCTMain([
3434
testCase(TestNSData.allTests),
3535
testCase(TestDate.allTests),
3636
testCase(TestDateComponents.allTests),
37+
testCase(TestDateInterval.allTests),
3738
testCase(TestNSDateComponents.allTests),
3839
testCase(TestDateFormatter.allTests),
3940
testCase(TestDateIntervalFormatter.allTests),

0 commit comments

Comments
 (0)