File: Duration.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (237 lines) | stat: -rw-r--r-- 9,303 bytes parent folder | download
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// RUN: %target-run-simple-swift
// REQUIRES: executable_test

// These test a codepath that was fixed in the Swift 5.9 stdlib, so it will
// fail if run against earlier standard library versions.
// UNSUPPORTED: use_os_stdlib
// UNSUPPORTED: back_deployment_runtime

import StdlibUnittest

var suite = TestSuite("DurationTests")
defer { runAllTests() }

if #available(SwiftStdlib 5.7, *) {
  suite.test("seconds from Double") {
    for _ in 0 ..< 100 {
      let integerValue = Double(Int64.random(in: 0 ... 0x7fff_ffff_ffff_fc00))
      let (sec, attosec) = Duration.seconds(integerValue).components
      expectEqual(sec, Int64(integerValue))
      expectEqual(attosec, 0)
    }
    let quarterSecond = Duration.seconds(0.25)
    expectEqual(quarterSecond.components, (0, 250_000_000_000_000_000))
    // Value that overflows conversion from Double -> Int64, but should be
    // representable as a number of seconds:
    let huge: Double = 1.7e20
    let duration = Duration.seconds(huge)
    // Divide by 1000 to get back to a duration with representable components:
    let smallerDuration = duration / 1000
    expectEqual(smallerDuration.components, (170_000_000_000_000_000, 0))
    #if !os(WASI)
    // Now check that the components of the original value trap:
    expectCrashLater()
    let _ = duration.components
    #endif
  }
  
  suite.test("milliseconds from Double") {
    for _ in 0 ..< 100 {
      let integerValue = Double(Int64.random(in: 0 ... 0x7fff_ffff_ffff_fc00))
      let (sec, attosec) = Duration.milliseconds(integerValue).components
      expectEqual(sec, Int64(integerValue) / 1000)
      expectEqual(attosec, Int64(integerValue) % 1000 * 1_000_000_000_000_000)
    }
  }
  
  suite.test("microseconds from Double") {
    for _ in 0 ..< 100 {
      let integerValue = Double(Int64.random(in: 0 ... 0x7fff_ffff_ffff_fc00))
      let (sec, attosec) = Duration.microseconds(integerValue).components
      expectEqual(sec, Int64(integerValue) / 1_000_000)
      expectEqual(attosec, Int64(integerValue) % 1_000_000 * 1_000_000_000_000)
    }
  }
  
  suite.test("seconds from Int64") {
    let one = Duration.seconds(1 as Int64)
    expectEqual(one._high, 0)
    expectEqual(one._low, 1_000_000_000_000_000_000)
    let mone = Duration.seconds(-1 as Int64)
    expectEqual(mone._high, -1)
    expectEqual(mone._low, .max - 999_999_999_999_999_999)
    let max64 = Duration.seconds(Int64.max)
    expectEqual(max64._high, 499_999_999_999_999_999)
    expectEqual(max64._low, .max - 999_999_999_999_999_999)
    let min64 = Duration.seconds(Int64.min)
    expectEqual(min64._high,-500_000_000_000_000_000)
    expectEqual(min64._low, 0)
  }
  
  suite.test("seconds from UInt64") {
    let one = Duration.seconds(1 as UInt64)
    expectEqual(one._high, 0)
    expectEqual(one._low, 1_000_000_000_000_000_000)
    let max64 = Duration.seconds(UInt64.max)
    expectEqual(max64._high, 999_999_999_999_999_999)
    expectEqual(max64._low, .max - 999_999_999_999_999_999)
  }
  
  suite.test("milliseconds from Int64") {
    let one = Duration.milliseconds(1 as Int64)
    expectEqual(one._high, 0)
    expectEqual(one._low, 1_000_000_000_000_000)
    let mone = Duration.milliseconds(-1 as Int64)
    expectEqual(mone._high, -1)
    expectEqual(mone._low, .max - 999_999_999_999_999)
    let max64 = Duration.milliseconds(Int64.max)
    expectEqual(max64._high, 499_999_999_999_999)
    expectEqual(max64._low, .max - 999_999_999_999_999)
    let min64 = Duration.milliseconds(Int64.min)
    expectEqual(min64._high,-500_000_000_000_000)
    expectEqual(min64._low, 0)
  }
  
  suite.test("milliseconds from UInt64") {
    let one = Duration.milliseconds(1 as UInt64)
    expectEqual(one._high, 0)
    expectEqual(one._low, 1_000_000_000_000_000)
    let max64 = Duration.milliseconds(UInt64.max)
    expectEqual(max64._high, 999_999_999_999_999)
    expectEqual(max64._low, .max - 999_999_999_999_999)
  }
  
  suite.test("microseconds from Int64") {
    let one = Duration.microseconds(1 as Int64)
    expectEqual(one._high, 0)
    expectEqual(one._low, 1_000_000_000_000)
    let mone = Duration.microseconds(-1 as Int64)
    expectEqual(mone._high, -1)
    expectEqual(mone._low, .max - 999_999_999_999)
    let max64 = Duration.microseconds(Int64.max)
    expectEqual(max64._high, 499_999_999_999)
    expectEqual(max64._low, .max - 999_999_999_999)
    let min64 = Duration.microseconds(Int64.min)
    expectEqual(min64._high,-500_000_000_000)
    expectEqual(min64._low, 0)
  }
  
  suite.test("microseconds from UInt64") {
    let one = Duration.microseconds(1 as UInt64)
    expectEqual(one._high, 0)
    expectEqual(one._low, 1_000_000_000_000)
    let max64 = Duration.microseconds(UInt64.max)
    expectEqual(max64._high, 999_999_999_999)
    expectEqual(max64._low, .max - 999_999_999_999)
  }
  
  suite.test("nanoseconds from Int64") {
    let one = Duration.nanoseconds(1 as Int64)
    expectEqual(one._high, 0)
    expectEqual(one._low, 1_000_000_000)
    let mone = Duration.nanoseconds(-1 as Int64)
    expectEqual(mone._high, -1)
    expectEqual(mone._low, .max - 999_999_999)
    let max64 = Duration.nanoseconds(Int64.max)
    expectEqual(max64._high, 499_999_999)
    expectEqual(max64._low, .max - 999_999_999)
    let min64 = Duration.nanoseconds(Int64.min)
    expectEqual(min64._high,-500_000_000)
    expectEqual(min64._low, 0)
  }
  
  suite.test("nanoseconds from UInt64") {
    let one = Duration.nanoseconds(1 as UInt64)
    expectEqual(one._high, 0)
    expectEqual(one._low, 1_000_000_000)
    let max64 = Duration.nanoseconds(UInt64.max)
    expectEqual(max64._high, 999_999_999)
    expectEqual(max64._low, .max - 999_999_999)
  }
}
 
if #available(SwiftStdlib 6.0, *) {
  suite.test("seconds from Int128") {
    let one = Duration.seconds(1 as Int128)
    expectEqual(one._high, 0)
    expectEqual(one._low, 1_000_000_000_000_000_000)
    let mone = Duration.seconds(-1 as Int128)
    expectEqual(mone._high, -1)
    expectEqual(mone._low, .max - 999_999_999_999_999_999)
    let maxRep = Duration.seconds( 170141183460469231731 as Int128)
    expectEqual(maxRep._high, 9_223_372_036_854_775_807)
    expectEqual(maxRep._low, 17_759_440_357_825_445_888)
    //  negative overflow boundary is _smaller_ than positive for seconds;
    //  this could be avoided by reworking how highScaled is computed, but
    //  it's already so large (5 trillion years) that this probably isn't
    //  necessary.
    let minRep = Duration.seconds(-166020696663385964544 as Int128)
    expectEqual(minRep._high,-9_000_000_000_000_000_000)
    expectEqual(minRep._low, 0)
    #if !os(WASI)
    //  Check just above the overflow boundary
    expectCrashLater()
    let _ = Duration.seconds( 170141183460469231732 as Int128)
    #endif
  }
  
  suite.test("milliseconds from Int128") {
    let one = Duration.milliseconds(1 as Int128)
    expectEqual(one._high, 0)
    expectEqual(one._low, 1_000_000_000_000_000)
    let mone = Duration.milliseconds(-1 as Int128)
    expectEqual(mone._high, -1)
    expectEqual(mone._low, .max - 999_999_999_999_999)
    let maxRep = Duration.milliseconds( 170141183460469231731687 as Int128)
    expectEqual(maxRep._high, 9_223_372_036_854_775_807)
    expectEqual(maxRep._low, 18_446_440_357_825_445_888)
    let minRep = Duration.milliseconds(-170134320591823194554368 as Int128)
    expectEqual(minRep._high,-9_223_000_000_000_000_000)
    expectEqual(minRep._low, 0)
    #if !os(WASI)
    //  Check just above the overflow boundary
    expectCrashLater()
    let _ = Duration.milliseconds( 170141183460469231731689 as Int128)
    #endif
  }
  
  suite.test("microseconds from Int128") {
    let one = Duration.microseconds(1 as Int128)
    expectEqual(one._high, 0)
    expectEqual(one._low, 1_000_000_000_000)
    let mone = Duration.microseconds(-1 as Int128)
    expectEqual(mone._high, -1)
    expectEqual(mone._low, .max - 999_999_999_999)
    let maxRep = Duration.microseconds( 170141183460469231731687303 as Int128)
    expectEqual(maxRep._high, 9_223_372_036_854_775_807)
    expectEqual(maxRep._low, 18_446_743_357_825_445_888)
    let minRep = Duration.microseconds(-170141182780618614507569152 as Int128)
    expectEqual(minRep._high,-9_223_372_000_000_000_000)
    expectEqual(minRep._low, 0)
    #if !os(WASI)
    //  Check just above the overflow boundary
    expectCrashLater()
    let _ = Duration.microseconds( 170141183460469231731687304 as Int128)
    #endif
  }
  
  suite.test("nanoseconds from Int128") {
    let one = Duration.nanoseconds(1 as Int128)
    expectEqual(one._high, 0)
    expectEqual(one._low, 1_000_000_000)
    let mone = Duration.nanoseconds(-1 as Int128)
    expectEqual(mone._high, -1)
    expectEqual(mone._low, .max - 999_999_999)
    let maxRep = Duration.nanoseconds( 170141183460469231731687303715 as Int128)
    expectEqual(maxRep._high, 9_223_372_036_854_775_807)
    expectEqual(maxRep._low, 18_446_744_072_825_445_888)
    let minRep = Duration.nanoseconds(-170141183444701401161113010176 as Int128)
    expectEqual(minRep._high,-9_223_372_036_000_000_000)
    expectEqual(minRep._low, 0)
    #if !os(WASI)
    //  Check just above the overflow boundary
    expectCrashLater()
    let _ = Duration.nanoseconds( 170141183460469231731687303716 as Int128)
    #endif
  }
}