]> git.proxmox.com Git - rustc.git/blob - library/core/tests/time.rs
2975c81f8fec90e872ab684df3b39d793cbfa025
[rustc.git] / library / core / tests / time.rs
1 use core::time::Duration;
2
3 #[test]
4 fn creation() {
5 assert_ne!(Duration::from_secs(1), Duration::from_secs(0));
6 assert_eq!(Duration::from_secs(1) + Duration::from_secs(2), Duration::from_secs(3));
7 assert_eq!(
8 Duration::from_millis(10) + Duration::from_secs(4),
9 Duration::new(4, 10 * 1_000_000)
10 );
11 assert_eq!(Duration::from_millis(4000), Duration::new(4, 0));
12 }
13
14 #[test]
15 #[should_panic]
16 fn new_overflow() {
17 let _ = Duration::new(u64::MAX, 1_000_000_000);
18 }
19
20 #[test]
21 fn secs() {
22 assert_eq!(Duration::new(0, 0).as_secs(), 0);
23 assert_eq!(Duration::new(0, 500_000_005).as_secs(), 0);
24 assert_eq!(Duration::new(0, 1_050_000_001).as_secs(), 1);
25 assert_eq!(Duration::from_secs(1).as_secs(), 1);
26 assert_eq!(Duration::from_millis(999).as_secs(), 0);
27 assert_eq!(Duration::from_millis(1001).as_secs(), 1);
28 assert_eq!(Duration::from_micros(999_999).as_secs(), 0);
29 assert_eq!(Duration::from_micros(1_000_001).as_secs(), 1);
30 assert_eq!(Duration::from_nanos(999_999_999).as_secs(), 0);
31 assert_eq!(Duration::from_nanos(1_000_000_001).as_secs(), 1);
32 }
33
34 #[test]
35 fn millis() {
36 assert_eq!(Duration::new(0, 0).subsec_millis(), 0);
37 assert_eq!(Duration::new(0, 500_000_005).subsec_millis(), 500);
38 assert_eq!(Duration::new(0, 1_050_000_001).subsec_millis(), 50);
39 assert_eq!(Duration::from_secs(1).subsec_millis(), 0);
40 assert_eq!(Duration::from_millis(999).subsec_millis(), 999);
41 assert_eq!(Duration::from_millis(1001).subsec_millis(), 1);
42 assert_eq!(Duration::from_micros(999_999).subsec_millis(), 999);
43 assert_eq!(Duration::from_micros(1_001_000).subsec_millis(), 1);
44 assert_eq!(Duration::from_nanos(999_999_999).subsec_millis(), 999);
45 assert_eq!(Duration::from_nanos(1_001_000_000).subsec_millis(), 1);
46 }
47
48 #[test]
49 fn micros() {
50 assert_eq!(Duration::new(0, 0).subsec_micros(), 0);
51 assert_eq!(Duration::new(0, 500_000_005).subsec_micros(), 500_000);
52 assert_eq!(Duration::new(0, 1_050_000_001).subsec_micros(), 50_000);
53 assert_eq!(Duration::from_secs(1).subsec_micros(), 0);
54 assert_eq!(Duration::from_millis(999).subsec_micros(), 999_000);
55 assert_eq!(Duration::from_millis(1001).subsec_micros(), 1_000);
56 assert_eq!(Duration::from_micros(999_999).subsec_micros(), 999_999);
57 assert_eq!(Duration::from_micros(1_000_001).subsec_micros(), 1);
58 assert_eq!(Duration::from_nanos(999_999_999).subsec_micros(), 999_999);
59 assert_eq!(Duration::from_nanos(1_000_001_000).subsec_micros(), 1);
60 }
61
62 #[test]
63 fn nanos() {
64 assert_eq!(Duration::new(0, 0).subsec_nanos(), 0);
65 assert_eq!(Duration::new(0, 5).subsec_nanos(), 5);
66 assert_eq!(Duration::new(0, 1_000_000_001).subsec_nanos(), 1);
67 assert_eq!(Duration::from_secs(1).subsec_nanos(), 0);
68 assert_eq!(Duration::from_millis(999).subsec_nanos(), 999_000_000);
69 assert_eq!(Duration::from_millis(1001).subsec_nanos(), 1_000_000);
70 assert_eq!(Duration::from_micros(999_999).subsec_nanos(), 999_999_000);
71 assert_eq!(Duration::from_micros(1_000_001).subsec_nanos(), 1000);
72 assert_eq!(Duration::from_nanos(999_999_999).subsec_nanos(), 999_999_999);
73 assert_eq!(Duration::from_nanos(1_000_000_001).subsec_nanos(), 1);
74 }
75
76 #[test]
77 fn add() {
78 assert_eq!(Duration::new(0, 0) + Duration::new(0, 1), Duration::new(0, 1));
79 assert_eq!(Duration::new(0, 500_000_000) + Duration::new(0, 500_000_001), Duration::new(1, 1));
80 }
81
82 #[test]
83 fn checked_add() {
84 assert_eq!(Duration::new(0, 0).checked_add(Duration::new(0, 1)), Some(Duration::new(0, 1)));
85 assert_eq!(
86 Duration::new(0, 500_000_000).checked_add(Duration::new(0, 500_000_001)),
87 Some(Duration::new(1, 1))
88 );
89 assert_eq!(Duration::new(1, 0).checked_add(Duration::new(u64::MAX, 0)), None);
90 }
91
92 #[test]
93 fn saturating_add() {
94 assert_eq!(Duration::new(0, 0).saturating_add(Duration::new(0, 1)), Duration::new(0, 1));
95 assert_eq!(
96 Duration::new(0, 500_000_000).saturating_add(Duration::new(0, 500_000_001)),
97 Duration::new(1, 1)
98 );
99 assert_eq!(Duration::new(1, 0).saturating_add(Duration::new(u64::MAX, 0)), Duration::MAX);
100 }
101
102 #[test]
103 fn sub() {
104 assert_eq!(Duration::new(0, 1) - Duration::new(0, 0), Duration::new(0, 1));
105 assert_eq!(Duration::new(0, 500_000_001) - Duration::new(0, 500_000_000), Duration::new(0, 1));
106 assert_eq!(Duration::new(1, 0) - Duration::new(0, 1), Duration::new(0, 999_999_999));
107 }
108
109 #[test]
110 fn checked_sub() {
111 assert_eq!(Duration::NANOSECOND.checked_sub(Duration::ZERO), Some(Duration::NANOSECOND));
112 assert_eq!(
113 Duration::SECOND.checked_sub(Duration::NANOSECOND),
114 Some(Duration::new(0, 999_999_999))
115 );
116 assert_eq!(Duration::ZERO.checked_sub(Duration::NANOSECOND), None);
117 assert_eq!(Duration::ZERO.checked_sub(Duration::SECOND), None);
118 }
119
120 #[test]
121 fn saturating_sub() {
122 assert_eq!(Duration::NANOSECOND.saturating_sub(Duration::ZERO), Duration::NANOSECOND);
123 assert_eq!(
124 Duration::SECOND.saturating_sub(Duration::NANOSECOND),
125 Duration::new(0, 999_999_999)
126 );
127 assert_eq!(Duration::ZERO.saturating_sub(Duration::NANOSECOND), Duration::ZERO);
128 assert_eq!(Duration::ZERO.saturating_sub(Duration::SECOND), Duration::ZERO);
129 }
130
131 #[test]
132 #[should_panic]
133 fn sub_bad1() {
134 let _ = Duration::new(0, 0) - Duration::new(0, 1);
135 }
136
137 #[test]
138 #[should_panic]
139 fn sub_bad2() {
140 let _ = Duration::new(0, 0) - Duration::new(1, 0);
141 }
142
143 #[test]
144 fn mul() {
145 assert_eq!(Duration::new(0, 1) * 2, Duration::new(0, 2));
146 assert_eq!(Duration::new(1, 1) * 3, Duration::new(3, 3));
147 assert_eq!(Duration::new(0, 500_000_001) * 4, Duration::new(2, 4));
148 assert_eq!(Duration::new(0, 500_000_001) * 4000, Duration::new(2000, 4000));
149 }
150
151 #[test]
152 fn checked_mul() {
153 assert_eq!(Duration::new(0, 1).checked_mul(2), Some(Duration::new(0, 2)));
154 assert_eq!(Duration::new(1, 1).checked_mul(3), Some(Duration::new(3, 3)));
155 assert_eq!(Duration::new(0, 500_000_001).checked_mul(4), Some(Duration::new(2, 4)));
156 assert_eq!(Duration::new(0, 500_000_001).checked_mul(4000), Some(Duration::new(2000, 4000)));
157 assert_eq!(Duration::new(u64::MAX - 1, 0).checked_mul(2), None);
158 }
159
160 #[test]
161 fn saturating_mul() {
162 assert_eq!(Duration::new(0, 1).saturating_mul(2), Duration::new(0, 2));
163 assert_eq!(Duration::new(1, 1).saturating_mul(3), Duration::new(3, 3));
164 assert_eq!(Duration::new(0, 500_000_001).saturating_mul(4), Duration::new(2, 4));
165 assert_eq!(Duration::new(0, 500_000_001).saturating_mul(4000), Duration::new(2000, 4000));
166 assert_eq!(Duration::new(u64::MAX - 1, 0).saturating_mul(2), Duration::MAX);
167 }
168
169 #[test]
170 fn div() {
171 assert_eq!(Duration::new(0, 1) / 2, Duration::new(0, 0));
172 assert_eq!(Duration::new(1, 1) / 3, Duration::new(0, 333_333_333));
173 assert_eq!(Duration::new(99, 999_999_000) / 100, Duration::new(0, 999_999_990));
174 }
175
176 #[test]
177 fn div_duration_f32() {
178 assert_eq!(Duration::ZERO.div_duration_f32(Duration::MAX), 0.0);
179 assert_eq!(Duration::MAX.div_duration_f32(Duration::ZERO), f32::INFINITY);
180 assert_eq!((Duration::SECOND * 2).div_duration_f32(Duration::SECOND), 2.0);
181 assert!(Duration::ZERO.div_duration_f32(Duration::ZERO).is_nan());
182 // These tests demonstrate it doesn't panic with extreme values.
183 // Accuracy of the computed value is not a huge concern, we know floats don't work well
184 // at these extremes.
185 assert!((Duration::MAX).div_duration_f32(Duration::NANOSECOND) > 10.0f32.powf(28.0));
186 assert!((Duration::NANOSECOND).div_duration_f32(Duration::MAX) < 0.1);
187 }
188
189 #[test]
190 fn div_duration_f64() {
191 assert_eq!(Duration::ZERO.div_duration_f64(Duration::MAX), 0.0);
192 assert_eq!(Duration::MAX.div_duration_f64(Duration::ZERO), f64::INFINITY);
193 assert_eq!((Duration::SECOND * 2).div_duration_f64(Duration::SECOND), 2.0);
194 assert!(Duration::ZERO.div_duration_f64(Duration::ZERO).is_nan());
195 // These tests demonstrate it doesn't panic with extreme values.
196 // Accuracy of the computed value is not a huge concern, we know floats don't work well
197 // at these extremes.
198 assert!((Duration::MAX).div_duration_f64(Duration::NANOSECOND) > 10.0f64.powf(28.0));
199 assert!((Duration::NANOSECOND).div_duration_f64(Duration::MAX) < 0.1);
200 }
201
202 #[test]
203 fn checked_div() {
204 assert_eq!(Duration::new(2, 0).checked_div(2), Some(Duration::new(1, 0)));
205 assert_eq!(Duration::new(1, 0).checked_div(2), Some(Duration::new(0, 500_000_000)));
206 assert_eq!(Duration::new(2, 0).checked_div(0), None);
207 }
208
209 #[test]
210 fn correct_sum() {
211 let durations = [
212 Duration::new(1, 999_999_999),
213 Duration::new(2, 999_999_999),
214 Duration::new(0, 999_999_999),
215 Duration::new(0, 999_999_999),
216 Duration::new(0, 999_999_999),
217 Duration::new(5, 0),
218 ];
219 let sum = durations.iter().sum::<Duration>();
220 assert_eq!(sum, Duration::new(1 + 2 + 5 + 4, 1_000_000_000 - 5));
221 }
222
223 #[test]
224 fn debug_formatting_extreme_values() {
225 assert_eq!(
226 format!("{:?}", Duration::new(u64::MAX, 123_456_789)),
227 "18446744073709551615.123456789s"
228 );
229 assert_eq!(format!("{:.0?}", Duration::MAX), "18446744073709551616s");
230 assert_eq!(format!("{:.0?}", Duration::new(u64::MAX, 500_000_000)), "18446744073709551616s");
231 assert_eq!(format!("{:.0?}", Duration::new(u64::MAX, 499_999_999)), "18446744073709551615s");
232 assert_eq!(
233 format!("{:.3?}", Duration::new(u64::MAX, 999_500_000)),
234 "18446744073709551616.000s"
235 );
236 assert_eq!(
237 format!("{:.3?}", Duration::new(u64::MAX, 999_499_999)),
238 "18446744073709551615.999s"
239 );
240 assert_eq!(
241 format!("{:.8?}", Duration::new(u64::MAX, 999_999_995)),
242 "18446744073709551616.00000000s"
243 );
244 assert_eq!(
245 format!("{:.8?}", Duration::new(u64::MAX, 999_999_994)),
246 "18446744073709551615.99999999s"
247 );
248 assert_eq!(format!("{:21.0?}", Duration::MAX), "18446744073709551616s");
249 assert_eq!(format!("{:22.0?}", Duration::MAX), "18446744073709551616s ");
250 assert_eq!(format!("{:24.0?}", Duration::MAX), "18446744073709551616s ");
251 }
252
253 #[test]
254 fn debug_formatting_secs() {
255 assert_eq!(format!("{:?}", Duration::new(7, 000_000_000)), "7s");
256 assert_eq!(format!("{:?}", Duration::new(7, 100_000_000)), "7.1s");
257 assert_eq!(format!("{:?}", Duration::new(7, 000_010_000)), "7.00001s");
258 assert_eq!(format!("{:?}", Duration::new(7, 000_000_001)), "7.000000001s");
259 assert_eq!(format!("{:?}", Duration::new(7, 123_456_789)), "7.123456789s");
260
261 assert_eq!(format!("{:?}", Duration::new(88, 000_000_000)), "88s");
262 assert_eq!(format!("{:?}", Duration::new(88, 100_000_000)), "88.1s");
263 assert_eq!(format!("{:?}", Duration::new(88, 000_010_000)), "88.00001s");
264 assert_eq!(format!("{:?}", Duration::new(88, 000_000_001)), "88.000000001s");
265 assert_eq!(format!("{:?}", Duration::new(88, 123_456_789)), "88.123456789s");
266
267 assert_eq!(format!("{:?}", Duration::new(999, 000_000_000)), "999s");
268 assert_eq!(format!("{:?}", Duration::new(999, 100_000_000)), "999.1s");
269 assert_eq!(format!("{:?}", Duration::new(999, 000_010_000)), "999.00001s");
270 assert_eq!(format!("{:?}", Duration::new(999, 000_000_001)), "999.000000001s");
271 assert_eq!(format!("{:?}", Duration::new(999, 123_456_789)), "999.123456789s");
272 }
273
274 #[test]
275 fn debug_formatting_millis() {
276 assert_eq!(format!("{:?}", Duration::new(0, 7_000_000)), "7ms");
277 assert_eq!(format!("{:?}", Duration::new(0, 7_100_000)), "7.1ms");
278 assert_eq!(format!("{:?}", Duration::new(0, 7_000_001)), "7.000001ms");
279 assert_eq!(format!("{:?}", Duration::new(0, 7_123_456)), "7.123456ms");
280
281 assert_eq!(format!("{:?}", Duration::new(0, 88_000_000)), "88ms");
282 assert_eq!(format!("{:?}", Duration::new(0, 88_100_000)), "88.1ms");
283 assert_eq!(format!("{:?}", Duration::new(0, 88_000_001)), "88.000001ms");
284 assert_eq!(format!("{:?}", Duration::new(0, 88_123_456)), "88.123456ms");
285
286 assert_eq!(format!("{:?}", Duration::new(0, 999_000_000)), "999ms");
287 assert_eq!(format!("{:?}", Duration::new(0, 999_100_000)), "999.1ms");
288 assert_eq!(format!("{:?}", Duration::new(0, 999_000_001)), "999.000001ms");
289 assert_eq!(format!("{:?}", Duration::new(0, 999_123_456)), "999.123456ms");
290 }
291
292 #[test]
293 fn debug_formatting_micros() {
294 assert_eq!(format!("{:?}", Duration::new(0, 7_000)), "7µs");
295 assert_eq!(format!("{:?}", Duration::new(0, 7_100)), "7.1µs");
296 assert_eq!(format!("{:?}", Duration::new(0, 7_001)), "7.001µs");
297 assert_eq!(format!("{:?}", Duration::new(0, 7_123)), "7.123µs");
298
299 assert_eq!(format!("{:?}", Duration::new(0, 88_000)), "88µs");
300 assert_eq!(format!("{:?}", Duration::new(0, 88_100)), "88.1µs");
301 assert_eq!(format!("{:?}", Duration::new(0, 88_001)), "88.001µs");
302 assert_eq!(format!("{:?}", Duration::new(0, 88_123)), "88.123µs");
303
304 assert_eq!(format!("{:?}", Duration::new(0, 999_000)), "999µs");
305 assert_eq!(format!("{:?}", Duration::new(0, 999_100)), "999.1µs");
306 assert_eq!(format!("{:?}", Duration::new(0, 999_001)), "999.001µs");
307 assert_eq!(format!("{:?}", Duration::new(0, 999_123)), "999.123µs");
308 }
309
310 #[test]
311 fn debug_formatting_nanos() {
312 assert_eq!(format!("{:?}", Duration::new(0, 0)), "0ns");
313 assert_eq!(format!("{:?}", Duration::new(0, 1)), "1ns");
314 assert_eq!(format!("{:?}", Duration::new(0, 88)), "88ns");
315 assert_eq!(format!("{:?}", Duration::new(0, 999)), "999ns");
316 }
317
318 #[test]
319 fn debug_formatting_precision_zero() {
320 assert_eq!(format!("{:.0?}", Duration::new(0, 0)), "0ns");
321 assert_eq!(format!("{:.0?}", Duration::new(0, 123)), "123ns");
322
323 assert_eq!(format!("{:.0?}", Duration::new(0, 1_001)), "1µs");
324 assert_eq!(format!("{:.0?}", Duration::new(0, 1_499)), "1µs");
325 assert_eq!(format!("{:.0?}", Duration::new(0, 1_500)), "2µs");
326 assert_eq!(format!("{:.0?}", Duration::new(0, 1_999)), "2µs");
327
328 assert_eq!(format!("{:.0?}", Duration::new(0, 1_000_001)), "1ms");
329 assert_eq!(format!("{:.0?}", Duration::new(0, 1_499_999)), "1ms");
330 assert_eq!(format!("{:.0?}", Duration::new(0, 1_500_000)), "2ms");
331 assert_eq!(format!("{:.0?}", Duration::new(0, 1_999_999)), "2ms");
332
333 assert_eq!(format!("{:.0?}", Duration::new(1, 000_000_001)), "1s");
334 assert_eq!(format!("{:.0?}", Duration::new(1, 499_999_999)), "1s");
335 assert_eq!(format!("{:.0?}", Duration::new(1, 500_000_000)), "2s");
336 assert_eq!(format!("{:.0?}", Duration::new(1, 999_999_999)), "2s");
337 }
338
339 #[test]
340 fn debug_formatting_precision_two() {
341 assert_eq!(format!("{:.2?}", Duration::new(0, 0)), "0.00ns");
342 assert_eq!(format!("{:.2?}", Duration::new(0, 123)), "123.00ns");
343
344 assert_eq!(format!("{:.2?}", Duration::new(0, 1_000)), "1.00µs");
345 assert_eq!(format!("{:.2?}", Duration::new(0, 7_001)), "7.00µs");
346 assert_eq!(format!("{:.2?}", Duration::new(0, 7_100)), "7.10µs");
347 assert_eq!(format!("{:.2?}", Duration::new(0, 7_109)), "7.11µs");
348 assert_eq!(format!("{:.2?}", Duration::new(0, 7_199)), "7.20µs");
349 assert_eq!(format!("{:.2?}", Duration::new(0, 1_999)), "2.00µs");
350
351 assert_eq!(format!("{:.2?}", Duration::new(0, 1_000_000)), "1.00ms");
352 assert_eq!(format!("{:.2?}", Duration::new(0, 3_001_000)), "3.00ms");
353 assert_eq!(format!("{:.2?}", Duration::new(0, 3_100_000)), "3.10ms");
354 assert_eq!(format!("{:.2?}", Duration::new(0, 1_999_999)), "2.00ms");
355
356 assert_eq!(format!("{:.2?}", Duration::new(1, 000_000_000)), "1.00s");
357 assert_eq!(format!("{:.2?}", Duration::new(4, 001_000_000)), "4.00s");
358 assert_eq!(format!("{:.2?}", Duration::new(2, 100_000_000)), "2.10s");
359 assert_eq!(format!("{:.2?}", Duration::new(2, 104_990_000)), "2.10s");
360 assert_eq!(format!("{:.2?}", Duration::new(2, 105_000_000)), "2.11s");
361 assert_eq!(format!("{:.2?}", Duration::new(8, 999_999_999)), "9.00s");
362 }
363
364 #[test]
365 fn debug_formatting_padding() {
366 assert_eq!("0ns ", format!("{:<9?}", Duration::new(0, 0)));
367 assert_eq!(" 0ns", format!("{:>9?}", Duration::new(0, 0)));
368 assert_eq!(" 0ns ", format!("{:^9?}", Duration::new(0, 0)));
369 assert_eq!("123ns ", format!("{:<9.0?}", Duration::new(0, 123)));
370 assert_eq!(" 123ns", format!("{:>9.0?}", Duration::new(0, 123)));
371 assert_eq!(" 123ns ", format!("{:^9.0?}", Duration::new(0, 123)));
372 assert_eq!("123.0ns ", format!("{:<9.1?}", Duration::new(0, 123)));
373 assert_eq!(" 123.0ns", format!("{:>9.1?}", Duration::new(0, 123)));
374 assert_eq!(" 123.0ns ", format!("{:^9.1?}", Duration::new(0, 123)));
375 assert_eq!("7.1µs ", format!("{:<9?}", Duration::new(0, 7_100)));
376 assert_eq!(" 7.1µs", format!("{:>9?}", Duration::new(0, 7_100)));
377 assert_eq!(" 7.1µs ", format!("{:^9?}", Duration::new(0, 7_100)));
378 assert_eq!("999.123456ms", format!("{:<9?}", Duration::new(0, 999_123_456)));
379 assert_eq!("999.123456ms", format!("{:>9?}", Duration::new(0, 999_123_456)));
380 assert_eq!("999.123456ms", format!("{:^9?}", Duration::new(0, 999_123_456)));
381 assert_eq!("5s ", format!("{:<9?}", Duration::new(5, 0)));
382 assert_eq!(" 5s", format!("{:>9?}", Duration::new(5, 0)));
383 assert_eq!(" 5s ", format!("{:^9?}", Duration::new(5, 0)));
384 assert_eq!("5.000000000000s", format!("{:<9.12?}", Duration::new(5, 0)));
385 assert_eq!("5.000000000000s", format!("{:>9.12?}", Duration::new(5, 0)));
386 assert_eq!("5.000000000000s", format!("{:^9.12?}", Duration::new(5, 0)));
387
388 // default alignment is left:
389 assert_eq!("5s ", format!("{:9?}", Duration::new(5, 0)));
390 }
391
392 #[test]
393 fn debug_formatting_precision_high() {
394 assert_eq!(format!("{:.5?}", Duration::new(0, 23_678)), "23.67800µs");
395
396 assert_eq!(format!("{:.9?}", Duration::new(1, 000_000_000)), "1.000000000s");
397 assert_eq!(format!("{:.10?}", Duration::new(4, 001_000_000)), "4.0010000000s");
398 assert_eq!(format!("{:.20?}", Duration::new(4, 001_000_000)), "4.00100000000000000000s");
399 }
400
401 #[test]
402 fn duration_const() {
403 // test that the methods of `Duration` are usable in a const context
404
405 const DURATION: Duration = Duration::new(0, 123_456_789);
406
407 const SUB_SEC_MILLIS: u32 = DURATION.subsec_millis();
408 assert_eq!(SUB_SEC_MILLIS, 123);
409
410 const SUB_SEC_MICROS: u32 = DURATION.subsec_micros();
411 assert_eq!(SUB_SEC_MICROS, 123_456);
412
413 const SUB_SEC_NANOS: u32 = DURATION.subsec_nanos();
414 assert_eq!(SUB_SEC_NANOS, 123_456_789);
415
416 const IS_ZERO: bool = Duration::ZERO.is_zero();
417 assert!(IS_ZERO);
418
419 const SECONDS: u64 = Duration::SECOND.as_secs();
420 assert_eq!(SECONDS, 1);
421
422 const FROM_SECONDS: Duration = Duration::from_secs(1);
423 assert_eq!(FROM_SECONDS, Duration::SECOND);
424
425 const SECONDS_F32: f32 = Duration::SECOND.as_secs_f32();
426 assert_eq!(SECONDS_F32, 1.0);
427
428 const FROM_SECONDS_F32: Duration = Duration::from_secs_f32(1.0);
429 assert_eq!(FROM_SECONDS_F32, Duration::SECOND);
430
431 const SECONDS_F64: f64 = Duration::SECOND.as_secs_f64();
432 assert_eq!(SECONDS_F64, 1.0);
433
434 const FROM_SECONDS_F64: Duration = Duration::from_secs_f64(1.0);
435 assert_eq!(FROM_SECONDS_F64, Duration::SECOND);
436
437 const MILLIS: u128 = Duration::SECOND.as_millis();
438 assert_eq!(MILLIS, 1_000);
439
440 const FROM_MILLIS: Duration = Duration::from_millis(1_000);
441 assert_eq!(FROM_MILLIS, Duration::SECOND);
442
443 const MICROS: u128 = Duration::SECOND.as_micros();
444 assert_eq!(MICROS, 1_000_000);
445
446 const FROM_MICROS: Duration = Duration::from_micros(1_000_000);
447 assert_eq!(FROM_MICROS, Duration::SECOND);
448
449 const NANOS: u128 = Duration::SECOND.as_nanos();
450 assert_eq!(NANOS, 1_000_000_000);
451
452 const FROM_NANOS: Duration = Duration::from_nanos(1_000_000_000);
453 assert_eq!(FROM_NANOS, Duration::SECOND);
454
455 const MAX: Duration = Duration::new(u64::MAX, 999_999_999);
456
457 const CHECKED_ADD: Option<Duration> = MAX.checked_add(Duration::SECOND);
458 assert_eq!(CHECKED_ADD, None);
459
460 const CHECKED_SUB: Option<Duration> = Duration::ZERO.checked_sub(Duration::SECOND);
461 assert_eq!(CHECKED_SUB, None);
462
463 const CHECKED_MUL: Option<Duration> = Duration::SECOND.checked_mul(1);
464 assert_eq!(CHECKED_MUL, Some(Duration::SECOND));
465
466 const MUL_F32: Duration = Duration::SECOND.mul_f32(1.0);
467 assert_eq!(MUL_F32, Duration::SECOND);
468
469 const MUL_F64: Duration = Duration::SECOND.mul_f64(1.0);
470 assert_eq!(MUL_F64, Duration::SECOND);
471
472 const CHECKED_DIV: Option<Duration> = Duration::SECOND.checked_div(1);
473 assert_eq!(CHECKED_DIV, Some(Duration::SECOND));
474
475 const DIV_F32: Duration = Duration::SECOND.div_f32(1.0);
476 assert_eq!(DIV_F32, Duration::SECOND);
477
478 const DIV_F64: Duration = Duration::SECOND.div_f64(1.0);
479 assert_eq!(DIV_F64, Duration::SECOND);
480
481 const DIV_DURATION_F32: f32 = Duration::SECOND.div_duration_f32(Duration::SECOND);
482 assert_eq!(DIV_DURATION_F32, 1.0);
483
484 const DIV_DURATION_F64: f64 = Duration::SECOND.div_duration_f64(Duration::SECOND);
485 assert_eq!(DIV_DURATION_F64, 1.0);
486
487 const SATURATING_ADD: Duration = MAX.saturating_add(Duration::SECOND);
488 assert_eq!(SATURATING_ADD, MAX);
489
490 const SATURATING_SUB: Duration = Duration::ZERO.saturating_sub(Duration::SECOND);
491 assert_eq!(SATURATING_SUB, Duration::ZERO);
492
493 const SATURATING_MUL: Duration = MAX.saturating_mul(2);
494 assert_eq!(SATURATING_MUL, MAX);
495 }
496
497 #[test]
498 fn from_neg_zero() {
499 assert_eq!(Duration::try_from_secs_f32(-0.0), Ok(Duration::ZERO));
500 assert_eq!(Duration::try_from_secs_f64(-0.0), Ok(Duration::ZERO));
501 assert_eq!(Duration::from_secs_f32(-0.0), Duration::ZERO);
502 assert_eq!(Duration::from_secs_f64(-0.0), Duration::ZERO);
503 }