- Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathTestDateInterval.swift
148 lines (133 loc) · 6.82 KB
/
TestDateInterval.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
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2018 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
//
classTestDateInterval:XCTestCase{
func test_defaultInitializer(){
letdateInterval=DateInterval()
XCTAssertEqual(dateInterval.duration,0)
}
func test_startEndInitializer(){
letdate1=dateWithString("2019-04-04 17:09:23 -0700")
letdate2=dateWithString("2019-04-04 18:09:23 -0700")
letdateInterval=DateInterval(start: date1, end: date2)
XCTAssertEqual(dateInterval.duration,60*60)
}
func test_startDurationInitializer(){
letdate=dateWithString("2019-04-04 17:09:23 -0700")
letdateInterval=DateInterval(start: date, duration:60)
XCTAssertEqual(dateInterval.duration,60)
}
func test_compareDifferentStarts(){
letdate1=dateWithString("2019-04-04 17:09:23 -0700")
letdate2=dateWithString("2019-04-04 18:09:23 -0700")
letdateInterval1=DateInterval(start: date1, duration:100)
letdateInterval2=DateInterval(start: date2, duration:100)
XCTAssertEqual(dateInterval1.compare(dateInterval2),.orderedAscending)
XCTAssertEqual(dateInterval2.compare(dateInterval1),.orderedDescending)
}
func test_compareDifferentDurations(){
letdate=dateWithString("2019-04-04 17:09:23 -0700")
letdateInterval1=DateInterval(start: date, duration:60)
letdateInterval2=DateInterval(start: date, duration:90)
XCTAssertEqual(dateInterval1.compare(dateInterval2),.orderedAscending)
XCTAssertEqual(dateInterval2.compare(dateInterval1),.orderedDescending)
}
func test_compareSame(){
letdate=dateWithString("2019-04-04 17:09:23 -0700")
letdateInterval1=DateInterval(start: date, duration:60)
letdateInterval2=DateInterval(start: date, duration:60)
XCTAssertEqual(dateInterval1.compare(dateInterval2),.orderedSame)
XCTAssertEqual(dateInterval2.compare(dateInterval1),.orderedSame)
}
func test_comparisonOperators(){
letdate1=dateWithString("2019-04-04 17:00:00 -0700")
letdate2=dateWithString("2019-04-04 17:30:00 -0700")
letdateInterval1=DateInterval(start: date1, duration:60)
letdateInterval2=DateInterval(start: date2, duration:60)
letdateInterval3=DateInterval(start: date1, duration:90)
letdateInterval4=DateInterval(start: date1, duration:60)
XCTAssertTrue(dateInterval1 < dateInterval2)
XCTAssertTrue(dateInterval1 < dateInterval3)
XCTAssertTrue(dateInterval1 == dateInterval4)
}
func test_intersects(){
letdate1=dateWithString("2019-04-04 17:09:23 -0700")
letdate2=dateWithString("2019-04-04 17:10:20 -0700")
letdateInterval1=DateInterval(start: date1, duration:60)
letdateInterval2=DateInterval(start: date2, duration:15)
XCTAssertTrue(dateInterval1.intersects(dateInterval2))
}
func test_intersection(){
letdate1=dateWithString("2019-04-04 17:00:00 -0700")
letdate2=dateWithString("2019-04-04 17:15:00 -0700")
letdateInterval1=DateInterval(start: date1, duration:60*30)
letdateInterval2=DateInterval(start: date2, duration:60*30)
letintersection= dateInterval1.intersection(with: dateInterval2)
XCTAssertNotNil(intersection)
XCTAssertEqual(intersection!.duration,60*15)
}
func test_intersectionZeroDuration(){
letdate1=dateWithString("2019-04-04 17:00:00 -0700")
letdate2=dateWithString("2019-04-04 17:30:00 -0700")
letdateInterval1=DateInterval(start: date1, duration:60*30)
letdateInterval2=DateInterval(start: date2, duration:60*30)
letintersection= dateInterval1.intersection(with: dateInterval2)
XCTAssertNotNil(intersection)
XCTAssertEqual(intersection!.duration,0)
}
func test_intersectionNil(){
letdate1=dateWithString("2019-04-04 17:00:00 -0700")
letdate2=dateWithString("2019-04-04 17:30:01 -0700")
letdateInterval1=DateInterval(start: date1, duration:60*30)
letdateInterval2=DateInterval(start: date2, duration:60*30)
XCTAssertNil(dateInterval1.intersection(with: dateInterval2))
}
func test_contains(){
letdate0=dateWithString("1964-10-01 06:00:00 +0900")
letdate1=dateWithString("2019-04-04 17:00:00 -0700")
letdate2=dateWithString("2019-04-04 17:30:00 -0700")
letdate3=dateWithString("2019-04-04 17:45:00 -0700")
letdate4=dateWithString("2019-04-04 17:50:00 -0700")
XCTAssertTrue(DateInterval(start:.distantPast, end:.distantFuture).contains(date0))
XCTAssertTrue(DateInterval(start:.distantPast, end: date1).contains(date0))
XCTAssertFalse(DateInterval(start:.distantPast, end: date1).contains(date2))
XCTAssertTrue(DateInterval(start: date0, end: date4).contains(date2))
XCTAssertTrue(DateInterval(start: date1, duration:60*45).contains(date3))
XCTAssertFalse(DateInterval(start: date1, duration:60*45).contains(date4))
XCTAssertTrue(DateInterval(start: date2, end:.distantFuture).contains(date2))
XCTAssertFalse(DateInterval(start: date2, end:.distantFuture).contains(date0))
}
func test_hashing(){
guard #available(iOS 10.10, macOS 10.12, tvOS 10.0, watchOS 3.0,*)else{return}
letstart1a=dateWithString("2019-04-04 17:09:23 -0700")
letstart1b=dateWithString("2019-04-04 17:09:23 -0700")
letstart2a=Date(timeIntervalSinceReferenceDate: start1a.timeIntervalSinceReferenceDate.nextUp)
letstart2b=Date(timeIntervalSinceReferenceDate: start1a.timeIntervalSinceReferenceDate.nextUp)
letduration1=1800.0
letduration2= duration1.nextUp
letintervals:[[DateInterval]]=[
[
DateInterval(start: start1a, duration: duration1),
DateInterval(start: start1b, duration: duration1),
],
[
DateInterval(start: start1a, duration: duration2),
DateInterval(start: start1b, duration: duration2),
],
[
DateInterval(start: start2a, duration: duration1),
DateInterval(start: start2b, duration: duration1),
],
[
DateInterval(start: start2a, duration: duration2),
DateInterval(start: start2b, duration: duration2),
],
]
checkHashableGroups(intervals)
}
}