| 
1 | 1 | #![warn(rust_2018_idioms, single_use_lifetimes)]  | 
2 | 2 | #![allow(clippy::zero_prefixed_literal)]  | 
 | 3 | +#![allow(clippy::non_ascii_literal)]  | 
3 | 4 | 
 
  | 
4 |  | -// https://github.com/rust-lang/rust/blob/master/src/libcore/tests/time.rs  | 
 | 5 | +// https://github.com/rust-lang/rust/blob/1.49.0/library/core/tests/time.rs  | 
5 | 6 | 
 
  | 
6 | 7 | use core::time;  | 
7 | 8 | use easytime::Duration;  | 
@@ -294,3 +295,95 @@ fn debug_formatting_precision_high() {  | 
294 | 295 | fn debug_formatting_none() {  | 
295 | 296 |     assert_eq!(format!("{:?}", Duration::new(0, 0) - Duration::new(0, 1)), "None");  | 
296 | 297 | }  | 
 | 298 | + | 
 | 299 | +const fn duration_second() -> Duration {  | 
 | 300 | +    Duration::from_secs(1)  | 
 | 301 | +}  | 
 | 302 | + | 
 | 303 | +#[rustversion::since(1.46)]  | 
 | 304 | +#[test]  | 
 | 305 | +fn duration_const() {  | 
 | 306 | +    // test that the methods of `Duration` are usable in a const context  | 
 | 307 | + | 
 | 308 | +    const DURATION: Duration = Duration::from_nanos(123_456_789);  | 
 | 309 | + | 
 | 310 | +    const SUB_SEC_MILLIS: Option<u32> = DURATION.subsec_millis();  | 
 | 311 | +    assert_eq!(SUB_SEC_MILLIS, Some(123));  | 
 | 312 | + | 
 | 313 | +    const SUB_SEC_MICROS: Option<u32> = DURATION.subsec_micros();  | 
 | 314 | +    assert_eq!(SUB_SEC_MICROS, Some(123_456));  | 
 | 315 | + | 
 | 316 | +    const SUB_SEC_NANOS: Option<u32> = DURATION.subsec_nanos();  | 
 | 317 | +    assert_eq!(SUB_SEC_NANOS, Some(123_456_789));  | 
 | 318 | + | 
 | 319 | +    // const IS_ZERO: bool = Duration::ZERO.is_zero();  | 
 | 320 | +    // assert!(IS_ZERO);  | 
 | 321 | + | 
 | 322 | +    const SECONDS: Option<u64> = duration_second().as_secs();  | 
 | 323 | +    assert_eq!(SECONDS, Some(1));  | 
 | 324 | + | 
 | 325 | +    const FROM_SECONDS: Duration = Duration::from_secs(1);  | 
 | 326 | +    assert_eq!(FROM_SECONDS, duration_second());  | 
 | 327 | + | 
 | 328 | +    // const SECONDS_F32: Option<f32> = duration_second().as_secs_f32();  | 
 | 329 | +    // assert_eq!(SECONDS_F32, Some(1.0));  | 
 | 330 | + | 
 | 331 | +    // const FROM_SECONDS_F32: Duration = Duration::from_secs_f32(1.0);  | 
 | 332 | +    // assert_eq!(FROM_SECONDS_F32, duration_second);  | 
 | 333 | + | 
 | 334 | +    // const SECONDS_F64: f64 = duration_second().as_secs_f64();  | 
 | 335 | +    // assert_eq!(SECONDS_F64, 1.0);  | 
 | 336 | + | 
 | 337 | +    // const FROM_SECONDS_F64: Duration = Duration::from_secs_f64(1.0);  | 
 | 338 | +    // assert_eq!(FROM_SECONDS_F64, duration_second());  | 
 | 339 | + | 
 | 340 | +    const MILLIS: Option<u128> = duration_second().as_millis();  | 
 | 341 | +    assert_eq!(MILLIS, Some(1_000));  | 
 | 342 | + | 
 | 343 | +    const FROM_MILLIS: Duration = Duration::from_millis(1_000);  | 
 | 344 | +    assert_eq!(FROM_MILLIS, duration_second());  | 
 | 345 | + | 
 | 346 | +    const MICROS: Option<u128> = duration_second().as_micros();  | 
 | 347 | +    assert_eq!(MICROS, Some(1_000_000));  | 
 | 348 | + | 
 | 349 | +    const FROM_MICROS: Duration = Duration::from_micros(1_000_000);  | 
 | 350 | +    assert_eq!(FROM_MICROS, duration_second());  | 
 | 351 | + | 
 | 352 | +    const NANOS: Option<u128> = duration_second().as_nanos();  | 
 | 353 | +    assert_eq!(NANOS, Some(1_000_000_000));  | 
 | 354 | + | 
 | 355 | +    const FROM_NANOS: Duration = Duration::from_nanos(1_000_000_000);  | 
 | 356 | +    assert_eq!(FROM_NANOS, duration_second());  | 
 | 357 | + | 
 | 358 | +    // const MAX: Duration = Duration::new(u64::MAX, 999_999_999);  | 
 | 359 | + | 
 | 360 | +    // const ADD: Duration = MAX + duration_second();  | 
 | 361 | +    // assert_eq!(ADD.into_inner(), None);  | 
 | 362 | + | 
 | 363 | +    // const SUB: Duration = Duration::ZERO - duration_second();  | 
 | 364 | +    // assert_eq!(SUB.into_inner(), None);  | 
 | 365 | + | 
 | 366 | +    // const MUL: Duration = duration_second() * 1;  | 
 | 367 | +    // assert_eq!(MUL, duration_second());  | 
 | 368 | + | 
 | 369 | +    // const MUL_F32: Duration = duration_second().mul_f32(1.0);  | 
 | 370 | +    // assert_eq!(MUL_F32, duration_second());  | 
 | 371 | + | 
 | 372 | +    // const MUL_F64: Duration = duration_second().mul_f64(1.0);  | 
 | 373 | +    // assert_eq!(MUL_F64, duration_second());  | 
 | 374 | + | 
 | 375 | +    // const DIV: Duration = duration_second() / 1;  | 
 | 376 | +    // assert_eq!(DIV, duration_second());  | 
 | 377 | + | 
 | 378 | +    // const DIV_F32: Duration = duration_second().div_f32(1.0);  | 
 | 379 | +    // assert_eq!(DIV_F32, duration_second());  | 
 | 380 | + | 
 | 381 | +    // const DIV_F64: Duration = duration_second().div_f64(1.0);  | 
 | 382 | +    // assert_eq!(DIV_F64, duration_second());  | 
 | 383 | + | 
 | 384 | +    // const DIV_DURATION_F32: f32 = duration_second().div_duration_f32(duration_second());  | 
 | 385 | +    // assert_eq!(DIV_DURATION_F32, 1.0);  | 
 | 386 | + | 
 | 387 | +    // const DIV_DURATION_F64: f64 = duration_second().div_duration_f64(duration_second());  | 
 | 388 | +    // assert_eq!(DIV_DURATION_F64, 1.0);  | 
 | 389 | +}  | 
0 commit comments