]>
git.proxmox.com Git - rustc.git/blob - library/core/tests/time.rs
2975c81f8fec90e872ab684df3b39d793cbfa025
1 use core
::time
::Duration
;
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));
8 Duration
::from_millis(10) + Duration
::from_secs(4),
9 Duration
::new(4, 10 * 1_000_000)
11 assert_eq
!(Duration
::from_millis(4000), Duration
::new(4, 0));
17 let _
= Duration
::new(u64::MAX
, 1_000_000_000);
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);
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);
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);
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);
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));
84 assert_eq
!(Duration
::new(0, 0).checked_add(Duration
::new(0, 1)), Some(Duration
::new(0, 1)));
86 Duration
::new(0, 500_000_000).checked_add(Duration
::new(0, 500_000_001)),
87 Some(Duration
::new(1, 1))
89 assert_eq
!(Duration
::new(1, 0).checked_add(Duration
::new(u64::MAX
, 0)), None
);
94 assert_eq
!(Duration
::new(0, 0).saturating_add(Duration
::new(0, 1)), Duration
::new(0, 1));
96 Duration
::new(0, 500_000_000).saturating_add(Duration
::new(0, 500_000_001)),
99 assert_eq
!(Duration
::new(1, 0).saturating_add(Duration
::new(u64::MAX
, 0)), Duration
::MAX
);
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));
111 assert_eq
!(Duration
::NANOSECOND
.checked_sub(Duration
::ZERO
), Some(Duration
::NANOSECOND
));
113 Duration
::SECOND
.checked_sub(Duration
::NANOSECOND
),
114 Some(Duration
::new(0, 999_999_999))
116 assert_eq
!(Duration
::ZERO
.checked_sub(Duration
::NANOSECOND
), None
);
117 assert_eq
!(Duration
::ZERO
.checked_sub(Duration
::SECOND
), None
);
121 fn saturating_sub() {
122 assert_eq
!(Duration
::NANOSECOND
.saturating_sub(Duration
::ZERO
), Duration
::NANOSECOND
);
124 Duration
::SECOND
.saturating_sub(Duration
::NANOSECOND
),
125 Duration
::new(0, 999_999_999)
127 assert_eq
!(Duration
::ZERO
.saturating_sub(Duration
::NANOSECOND
), Duration
::ZERO
);
128 assert_eq
!(Duration
::ZERO
.saturating_sub(Duration
::SECOND
), Duration
::ZERO
);
134 let _
= Duration
::new(0, 0) - Duration
::new(0, 1);
140 let _
= Duration
::new(0, 0) - Duration
::new(1, 0);
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));
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
);
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
);
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));
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);
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);
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
);
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),
219 let sum
= durations
.iter().sum
::<Duration
>();
220 assert_eq
!(sum
, Duration
::new(1 + 2 + 5 + 4, 1_000_000_000 - 5));
224 fn debug_formatting_extreme_values() {
226 format
!("{:?}", Duration
::new(u64::MAX
, 123_456_789)),
227 "18446744073709551615.123456789s"
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");
233 format
!("{:.3?}", Duration
::new(u64::MAX
, 999_500_000)),
234 "18446744073709551616.000s"
237 format
!("{:.3?}", Duration
::new(u64::MAX
, 999_499_999)),
238 "18446744073709551615.999s"
241 format
!("{:.8?}", Duration
::new(u64::MAX
, 999_999_995)),
242 "18446744073709551616.00000000s"
245 format
!("{:.8?}", Duration
::new(u64::MAX
, 999_999_994)),
246 "18446744073709551615.99999999s"
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 ");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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");
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)));
388 // default alignment is left:
389 assert_eq
!("5s ", format
!("{:9?}", Duration
::new(5, 0)));
393 fn debug_formatting_precision_high() {
394 assert_eq
!(format
!("{:.5?}", Duration
::new(0, 23_678)), "23.67800µs");
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");
402 fn duration_const() {
403 // test that the methods of `Duration` are usable in a const context
405 const DURATION
: Duration
= Duration
::new(0, 123_456_789);
407 const SUB_SEC_MILLIS
: u32 = DURATION
.subsec_millis();
408 assert_eq
!(SUB_SEC_MILLIS
, 123);
410 const SUB_SEC_MICROS
: u32 = DURATION
.subsec_micros();
411 assert_eq
!(SUB_SEC_MICROS
, 123_456);
413 const SUB_SEC_NANOS
: u32 = DURATION
.subsec_nanos();
414 assert_eq
!(SUB_SEC_NANOS
, 123_456_789);
416 const IS_ZERO
: bool
= Duration
::ZERO
.is_zero();
419 const SECONDS
: u64 = Duration
::SECOND
.as_secs();
420 assert_eq
!(SECONDS
, 1);
422 const FROM_SECONDS
: Duration
= Duration
::from_secs(1);
423 assert_eq
!(FROM_SECONDS
, Duration
::SECOND
);
425 const SECONDS_F32
: f32 = Duration
::SECOND
.as_secs_f32();
426 assert_eq
!(SECONDS_F32
, 1.0);
428 const FROM_SECONDS_F32
: Duration
= Duration
::from_secs_f32(1.0);
429 assert_eq
!(FROM_SECONDS_F32
, Duration
::SECOND
);
431 const SECONDS_F64
: f64 = Duration
::SECOND
.as_secs_f64();
432 assert_eq
!(SECONDS_F64
, 1.0);
434 const FROM_SECONDS_F64
: Duration
= Duration
::from_secs_f64(1.0);
435 assert_eq
!(FROM_SECONDS_F64
, Duration
::SECOND
);
437 const MILLIS
: u128
= Duration
::SECOND
.as_millis();
438 assert_eq
!(MILLIS
, 1_000);
440 const FROM_MILLIS
: Duration
= Duration
::from_millis(1_000);
441 assert_eq
!(FROM_MILLIS
, Duration
::SECOND
);
443 const MICROS
: u128
= Duration
::SECOND
.as_micros();
444 assert_eq
!(MICROS
, 1_000_000);
446 const FROM_MICROS
: Duration
= Duration
::from_micros(1_000_000);
447 assert_eq
!(FROM_MICROS
, Duration
::SECOND
);
449 const NANOS
: u128
= Duration
::SECOND
.as_nanos();
450 assert_eq
!(NANOS
, 1_000_000_000);
452 const FROM_NANOS
: Duration
= Duration
::from_nanos(1_000_000_000);
453 assert_eq
!(FROM_NANOS
, Duration
::SECOND
);
455 const MAX
: Duration
= Duration
::new(u64::MAX
, 999_999_999);
457 const CHECKED_ADD
: Option
<Duration
> = MAX
.checked_add(Duration
::SECOND
);
458 assert_eq
!(CHECKED_ADD
, None
);
460 const CHECKED_SUB
: Option
<Duration
> = Duration
::ZERO
.checked_sub(Duration
::SECOND
);
461 assert_eq
!(CHECKED_SUB
, None
);
463 const CHECKED_MUL
: Option
<Duration
> = Duration
::SECOND
.checked_mul(1);
464 assert_eq
!(CHECKED_MUL
, Some(Duration
::SECOND
));
466 const MUL_F32
: Duration
= Duration
::SECOND
.mul_f32(1.0);
467 assert_eq
!(MUL_F32
, Duration
::SECOND
);
469 const MUL_F64
: Duration
= Duration
::SECOND
.mul_f64(1.0);
470 assert_eq
!(MUL_F64
, Duration
::SECOND
);
472 const CHECKED_DIV
: Option
<Duration
> = Duration
::SECOND
.checked_div(1);
473 assert_eq
!(CHECKED_DIV
, Some(Duration
::SECOND
));
475 const DIV_F32
: Duration
= Duration
::SECOND
.div_f32(1.0);
476 assert_eq
!(DIV_F32
, Duration
::SECOND
);
478 const DIV_F64
: Duration
= Duration
::SECOND
.div_f64(1.0);
479 assert_eq
!(DIV_F64
, Duration
::SECOND
);
481 const DIV_DURATION_F32
: f32 = Duration
::SECOND
.div_duration_f32(Duration
::SECOND
);
482 assert_eq
!(DIV_DURATION_F32
, 1.0);
484 const DIV_DURATION_F64
: f64 = Duration
::SECOND
.div_duration_f64(Duration
::SECOND
);
485 assert_eq
!(DIV_DURATION_F64
, 1.0);
487 const SATURATING_ADD
: Duration
= MAX
.saturating_add(Duration
::SECOND
);
488 assert_eq
!(SATURATING_ADD
, MAX
);
490 const SATURATING_SUB
: Duration
= Duration
::ZERO
.saturating_sub(Duration
::SECOND
);
491 assert_eq
!(SATURATING_SUB
, Duration
::ZERO
);
493 const SATURATING_MUL
: Duration
= MAX
.saturating_mul(2);
494 assert_eq
!(SATURATING_MUL
, MAX
);
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
);