forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestDate.swift
177 lines (154 loc) · 6.49 KB
/
TestDate.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
func dateWithString(_ str: String) -> Date {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .gregorian)
formatter.locale = Locale(identifier: "en_US")
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
return formatter.date(from: str)! as Date
}
class TestDate : XCTestCase {
static var allTests: [(String, (TestDate) -> () throws -> Void)] {
return [
("test_BasicConstruction", test_BasicConstruction),
("test_InitTimeIntervalSince1970", test_InitTimeIntervalSince1970),
("test_InitTimeIntervalSinceSinceDate", test_InitTimeIntervalSinceSinceDate),
("test_TimeIntervalSinceSinceDate", test_TimeIntervalSinceSinceDate),
("test_descriptionWithLocale", test_descriptionWithLocale),
("test_DistantFuture", test_DistantFuture),
("test_DistantPast", test_DistantPast),
("test_DateByAddingTimeInterval", test_DateByAddingTimeInterval),
("test_EarlierDate", test_EarlierDate),
("test_LaterDate", test_LaterDate),
("test_Compare", test_Compare),
("test_IsEqualToDate", test_IsEqualToDate),
("test_timeIntervalSinceReferenceDate", test_timeIntervalSinceReferenceDate),
("test_recreateDateComponentsFromDate", test_recreateDateComponentsFromDate),
("test_Hashing", test_Hashing),
]
}
func test_BasicConstruction() {
let d = Date()
XCTAssert(d.timeIntervalSince1970 != 0)
XCTAssert(d.timeIntervalSinceReferenceDate != 0)
}
func test_descriptionWithLocale() {
let d = NSDate(timeIntervalSince1970: 0)
XCTAssertEqual(d.description(with: nil), "1970-01-01 00:00:00 +0000")
XCTAssertFalse(d.description(with: Locale(identifier: "ja_JP")).isEmpty)
}
func test_InitTimeIntervalSince1970() {
let ti: TimeInterval = 1
let d = Date(timeIntervalSince1970: ti)
XCTAssert(d.timeIntervalSince1970 == ti)
}
func test_InitTimeIntervalSinceSinceDate() {
let ti: TimeInterval = 1
let d1 = Date()
let d2 = Date(timeInterval: ti, since: d1)
XCTAssertNotNil(d2.timeIntervalSince1970 == d1.timeIntervalSince1970 + ti)
}
func test_TimeIntervalSinceSinceDate() {
let ti: TimeInterval = 1
let d1 = Date()
let d2 = Date(timeInterval: ti, since: d1)
XCTAssertEqual(d2.timeIntervalSince(d1), ti)
}
func test_DistantFuture() {
let d = Date.distantFuture
let now = Date()
XCTAssertGreaterThan(d, now)
}
func test_DistantPast() {
let now = Date()
let d = Date.distantPast
XCTAssertLessThan(d, now)
}
func test_DateByAddingTimeInterval() {
let ti: TimeInterval = 1
let d1 = Date()
let d2 = d1 + ti
XCTAssertNotNil(d2.timeIntervalSince1970 == d1.timeIntervalSince1970 + ti)
}
func test_EarlierDate() {
let ti: TimeInterval = 1
let d1 = Date()
let d2 = d1 + ti
XCTAssertLessThan(d1, d2)
}
func test_LaterDate() {
let ti: TimeInterval = 1
let d1 = Date()
let d2 = d1 + ti
XCTAssertGreaterThan(d2, d1)
}
func test_Compare() {
let ti: TimeInterval = 1
let d1 = Date()
let d2 = d1 + ti
XCTAssertEqual(d1.compare(d2), .orderedAscending)
}
func test_IsEqualToDate() {
let ti: TimeInterval = 1
let d1 = Date()
let d2 = d1 + ti
let d3 = d1 + ti
XCTAssertEqual(d2, d3)
}
func test_timeIntervalSinceReferenceDate() {
let d1 = Date().timeIntervalSinceReferenceDate
let sinceReferenceDate = Date.timeIntervalSinceReferenceDate
let d2 = Date().timeIntervalSinceReferenceDate
XCTAssertTrue(d1 <= sinceReferenceDate)
XCTAssertTrue(d2 >= sinceReferenceDate)
}
func test_recreateDateComponentsFromDate() {
let components = DateComponents(calendar: Calendar(identifier: .gregorian),
timeZone: .current,
era: 1,
year: 2017,
month: 11,
day: 5,
hour: 20,
minute: 38,
second: 11,
nanosecond: 40)
guard let date = Calendar(identifier: .gregorian).date(from: components) else {
XCTFail()
return
}
let recreatedComponents = Calendar(identifier: .gregorian).dateComponents(in: .current, from: date)
XCTAssertEqual(recreatedComponents.era, 1)
XCTAssertEqual(recreatedComponents.year, 2017)
XCTAssertEqual(recreatedComponents.month, 11)
XCTAssertEqual(recreatedComponents.hour, 20)
XCTAssertEqual(recreatedComponents.minute, 38)
// Nanoseconds are currently not supported by UCalendar C API, returns nil
// XCTAssertEqual(recreatedComponents.nanosecond, 40)
XCTAssertEqual(recreatedComponents.weekday, 1)
XCTAssertEqual(recreatedComponents.weekdayOrdinal, 1)
// Quarter is currently not supported by UCalendar C API, returns nil
// XCTAssertEqual(recreatedComponents.quarter, 3)
XCTAssertEqual(recreatedComponents.weekOfMonth, 2)
XCTAssertEqual(recreatedComponents.weekOfYear, 45)
XCTAssertEqual(recreatedComponents.yearForWeekOfYear, 2017)
}
func test_Hashing() {
let values: [Date] = [
dateWithString("2010-05-17 14:49:47 -0700"),
dateWithString("2011-05-17 14:49:47 -0700"),
dateWithString("2010-06-17 14:49:47 -0700"),
dateWithString("2010-05-18 14:49:47 -0700"),
dateWithString("2010-05-17 15:49:47 -0700"),
dateWithString("2010-05-17 14:50:47 -0700"),
dateWithString("2010-05-17 14:49:48 -0700"),
]
checkHashable(values, equalityOracle: { $0 == $1 })
}
}