Skip to content

Commit 8578a0e

Browse files
Add some additional test coverage for duration static methods (#73578)
The numerics of these operations is fairly subtle, and the overflow boundaries are somewhat surprising, so additional test coverage isn't a bad thing (also gives us an excuse to exercise Int128 more)
1 parent 54a2007 commit 8578a0e

File tree

1 file changed

+175
-5
lines changed

1 file changed

+175
-5
lines changed

test/stdlib/Duration.swift

Lines changed: 175 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
// RUN: %target-run-simple-swift
22
// REQUIRES: executable_test
33

4-
// Int128 operations are not supported on 32bit platforms, 128-bit types are not
5-
// provided by the 32-bit LLVM. See `dividingFullWidth` in IntegerTypes.swift.gyb
6-
// UNSUPPORTED: PTRSIZE=32
7-
84
// These test a codepath that was fixed in the Swift 5.9 stdlib, so it will
95
// fail if run against earlier standard library versions.
106
// UNSUPPORTED: use_os_stdlib
117
// UNSUPPORTED: back_deployment_runtime
128

139
import StdlibUnittest
1410

15-
var suite = TestSuite("StringIndexTests")
11+
var suite = TestSuite("DurationTests")
1612
defer { runAllTests() }
1713

1814
if #available(SwiftStdlib 5.7, *) {
@@ -54,4 +50,178 @@ if #available(SwiftStdlib 5.7, *) {
5450
expectEqual(attosec, Int64(integerValue) % 1_000_000 * 1_000_000_000_000)
5551
}
5652
}
53+
54+
suite.test("seconds from Int64") {
55+
let one = Duration.seconds(1 as Int64)
56+
expectEqual(one._high, 0)
57+
expectEqual(one._low, 1_000_000_000_000_000_000)
58+
let mone = Duration.seconds(-1 as Int64)
59+
expectEqual(mone._high, -1)
60+
expectEqual(mone._low, .max - 999_999_999_999_999_999)
61+
let max64 = Duration.seconds(Int64.max)
62+
expectEqual(max64._high, 499_999_999_999_999_999)
63+
expectEqual(max64._low, .max - 999_999_999_999_999_999)
64+
let min64 = Duration.seconds(Int64.min)
65+
expectEqual(min64._high,-500_000_000_000_000_000)
66+
expectEqual(min64._low, 0)
67+
}
68+
69+
suite.test("seconds from UInt64") {
70+
let one = Duration.seconds(1 as UInt64)
71+
expectEqual(one._high, 0)
72+
expectEqual(one._low, 1_000_000_000_000_000_000)
73+
let max64 = Duration.seconds(UInt64.max)
74+
expectEqual(max64._high, 999_999_999_999_999_999)
75+
expectEqual(max64._low, .max - 999_999_999_999_999_999)
76+
}
77+
78+
suite.test("milliseconds from Int64") {
79+
let one = Duration.milliseconds(1 as Int64)
80+
expectEqual(one._high, 0)
81+
expectEqual(one._low, 1_000_000_000_000_000)
82+
let mone = Duration.milliseconds(-1 as Int64)
83+
expectEqual(mone._high, -1)
84+
expectEqual(mone._low, .max - 999_999_999_999_999)
85+
let max64 = Duration.milliseconds(Int64.max)
86+
expectEqual(max64._high, 499_999_999_999_999)
87+
expectEqual(max64._low, .max - 999_999_999_999_999)
88+
let min64 = Duration.milliseconds(Int64.min)
89+
expectEqual(min64._high,-500_000_000_000_000)
90+
expectEqual(min64._low, 0)
91+
}
92+
93+
suite.test("milliseconds from UInt64") {
94+
let one = Duration.milliseconds(1 as UInt64)
95+
expectEqual(one._high, 0)
96+
expectEqual(one._low, 1_000_000_000_000_000)
97+
let max64 = Duration.milliseconds(UInt64.max)
98+
expectEqual(max64._high, 999_999_999_999_999)
99+
expectEqual(max64._low, .max - 999_999_999_999_999)
100+
}
101+
102+
suite.test("microseconds from Int64") {
103+
let one = Duration.microseconds(1 as Int64)
104+
expectEqual(one._high, 0)
105+
expectEqual(one._low, 1_000_000_000_000)
106+
let mone = Duration.microseconds(-1 as Int64)
107+
expectEqual(mone._high, -1)
108+
expectEqual(mone._low, .max - 999_999_999_999)
109+
let max64 = Duration.microseconds(Int64.max)
110+
expectEqual(max64._high, 499_999_999_999)
111+
expectEqual(max64._low, .max - 999_999_999_999)
112+
let min64 = Duration.microseconds(Int64.min)
113+
expectEqual(min64._high,-500_000_000_000)
114+
expectEqual(min64._low, 0)
115+
}
116+
117+
suite.test("microseconds from UInt64") {
118+
let one = Duration.microseconds(1 as UInt64)
119+
expectEqual(one._high, 0)
120+
expectEqual(one._low, 1_000_000_000_000)
121+
let max64 = Duration.microseconds(UInt64.max)
122+
expectEqual(max64._high, 999_999_999_999)
123+
expectEqual(max64._low, .max - 999_999_999_999)
124+
}
125+
126+
suite.test("nanoseconds from Int64") {
127+
let one = Duration.nanoseconds(1 as Int64)
128+
expectEqual(one._high, 0)
129+
expectEqual(one._low, 1_000_000_000)
130+
let mone = Duration.nanoseconds(-1 as Int64)
131+
expectEqual(mone._high, -1)
132+
expectEqual(mone._low, .max - 999_999_999)
133+
let max64 = Duration.nanoseconds(Int64.max)
134+
expectEqual(max64._high, 499_999_999)
135+
expectEqual(max64._low, .max - 999_999_999)
136+
let min64 = Duration.nanoseconds(Int64.min)
137+
expectEqual(min64._high,-500_000_000)
138+
expectEqual(min64._low, 0)
139+
}
140+
141+
suite.test("nanoseconds from UInt64") {
142+
let one = Duration.nanoseconds(1 as UInt64)
143+
expectEqual(one._high, 0)
144+
expectEqual(one._low, 1_000_000_000)
145+
let max64 = Duration.nanoseconds(UInt64.max)
146+
expectEqual(max64._high, 999_999_999)
147+
expectEqual(max64._low, .max - 999_999_999)
148+
}
149+
}
150+
151+
if #available(SwiftStdlib 6.0, *) {
152+
suite.test("seconds from Int128") {
153+
let one = Duration.seconds(1 as Int128)
154+
expectEqual(one._high, 0)
155+
expectEqual(one._low, 1_000_000_000_000_000_000)
156+
let mone = Duration.seconds(-1 as Int128)
157+
expectEqual(mone._high, -1)
158+
expectEqual(mone._low, .max - 999_999_999_999_999_999)
159+
let maxRep = Duration.seconds( 170141183460469231731 as Int128)
160+
expectEqual(maxRep._high, 9_223_372_036_854_775_807)
161+
expectEqual(maxRep._low, 17_759_440_357_825_445_888)
162+
// negative overflow boundary is _smaller_ than positive for seconds;
163+
// this could be avoided by reworking how highScaled is computed, but
164+
// it's already so large (5 trillion years) that this probably isn't
165+
// necessary.
166+
let minRep = Duration.seconds(-166020696663385964544 as Int128)
167+
expectEqual(minRep._high,-9_000_000_000_000_000_000)
168+
expectEqual(minRep._low, 0)
169+
// Check just above the overflow boundary
170+
expectCrashLater()
171+
let _ = Duration.seconds( 170141183460469231732 as Int128)
172+
}
173+
174+
suite.test("milliseconds from Int128") {
175+
let one = Duration.milliseconds(1 as Int128)
176+
expectEqual(one._high, 0)
177+
expectEqual(one._low, 1_000_000_000_000_000)
178+
let mone = Duration.milliseconds(-1 as Int128)
179+
expectEqual(mone._high, -1)
180+
expectEqual(mone._low, .max - 999_999_999_999_999)
181+
let maxRep = Duration.milliseconds( 170141183460469231731687 as Int128)
182+
expectEqual(maxRep._high, 9_223_372_036_854_775_807)
183+
expectEqual(maxRep._low, 18_446_440_357_825_445_888)
184+
let minRep = Duration.milliseconds(-170134320591823194554368 as Int128)
185+
expectEqual(minRep._high,-9_223_000_000_000_000_000)
186+
expectEqual(minRep._low, 0)
187+
// Check just above the overflow boundary
188+
expectCrashLater()
189+
let _ = Duration.milliseconds( 170141183460469231731689 as Int128)
190+
}
191+
192+
suite.test("microseconds from Int128") {
193+
let one = Duration.microseconds(1 as Int128)
194+
expectEqual(one._high, 0)
195+
expectEqual(one._low, 1_000_000_000_000)
196+
let mone = Duration.microseconds(-1 as Int128)
197+
expectEqual(mone._high, -1)
198+
expectEqual(mone._low, .max - 999_999_999_999)
199+
let maxRep = Duration.microseconds( 170141183460469231731687303 as Int128)
200+
expectEqual(maxRep._high, 9_223_372_036_854_775_807)
201+
expectEqual(maxRep._low, 18_446_743_357_825_445_888)
202+
let minRep = Duration.microseconds(-170141182780618614507569152 as Int128)
203+
expectEqual(minRep._high,-9_223_372_000_000_000_000)
204+
expectEqual(minRep._low, 0)
205+
// Check just above the overflow boundary
206+
expectCrashLater()
207+
let _ = Duration.microseconds( 170141183460469231731687304 as Int128)
208+
}
209+
210+
suite.test("nanoseconds from Int128") {
211+
let one = Duration.nanoseconds(1 as Int128)
212+
expectEqual(one._high, 0)
213+
expectEqual(one._low, 1_000_000_000)
214+
let mone = Duration.nanoseconds(-1 as Int128)
215+
expectEqual(mone._high, -1)
216+
expectEqual(mone._low, .max - 999_999_999)
217+
let maxRep = Duration.nanoseconds( 170141183460469231731687303715 as Int128)
218+
expectEqual(maxRep._high, 9_223_372_036_854_775_807)
219+
expectEqual(maxRep._low, 18_446_744_072_825_445_888)
220+
let minRep = Duration.nanoseconds(-170141183444701401161113010176 as Int128)
221+
expectEqual(minRep._high,-9_223_372_036_000_000_000)
222+
expectEqual(minRep._low, 0)
223+
// Check just above the overflow boundary
224+
expectCrashLater()
225+
let _ = Duration.nanoseconds( 170141183460469231731687303716 as Int128)
226+
}
57227
}

0 commit comments

Comments
 (0)