Dogfood Duration API in std::time tests

This expands time's test suite to use more and in more places the
range of methods and constants added to Duration in recent
proposals for the sake of testing more API surface area and
improving legibility.
This commit is contained in:
Jubilee Young 2020-10-21 17:56:09 -07:00
parent 22e6b9c689
commit d72d5f48c2
3 changed files with 62 additions and 61 deletions

View File

@ -108,24 +108,26 @@ fn sub() {
#[test]
fn checked_sub() {
let zero = Duration::new(0, 0);
let one_nano = Duration::new(0, 1);
let one_sec = Duration::new(1, 0);
assert_eq!(one_nano.checked_sub(zero), Some(Duration::new(0, 1)));
assert_eq!(one_sec.checked_sub(one_nano), Some(Duration::new(0, 999_999_999)));
assert_eq!(zero.checked_sub(one_nano), None);
assert_eq!(zero.checked_sub(one_sec), None);
let zero = Duration::zero();
assert_eq!(Duration::NANOSECOND.checked_sub(zero), Some(Duration::NANOSECOND));
assert_eq!(
Duration::SECOND.checked_sub(Duration::NANOSECOND),
Some(Duration::new(0, 999_999_999))
);
assert_eq!(zero.checked_sub(Duration::NANOSECOND), None);
assert_eq!(zero.checked_sub(Duration::SECOND), None);
}
#[test]
fn saturating_sub() {
let zero = Duration::new(0, 0);
let one_nano = Duration::new(0, 1);
let one_sec = Duration::new(1, 0);
assert_eq!(one_nano.saturating_sub(zero), Duration::new(0, 1));
assert_eq!(one_sec.saturating_sub(one_nano), Duration::new(0, 999_999_999));
assert_eq!(zero.saturating_sub(one_nano), Duration::MIN);
assert_eq!(zero.saturating_sub(one_sec), Duration::MIN);
assert_eq!(Duration::NANOSECOND.saturating_sub(zero), Duration::NANOSECOND);
assert_eq!(
Duration::SECOND.saturating_sub(Duration::NANOSECOND),
Duration::new(0, 999_999_999)
);
assert_eq!(zero.saturating_sub(Duration::NANOSECOND), Duration::MIN);
assert_eq!(zero.saturating_sub(Duration::SECOND), Duration::MIN);
}
#[test]
@ -343,80 +345,78 @@ fn duration_const() {
const IS_ZERO: bool = ZERO.is_zero();
assert!(IS_ZERO);
const ONE: Duration = Duration::new(1, 0);
const SECONDS: u64 = ONE.as_secs();
const SECONDS: u64 = Duration::SECOND.as_secs();
assert_eq!(SECONDS, 1);
const FROM_SECONDS: Duration = Duration::from_secs(1);
assert_eq!(FROM_SECONDS, ONE);
assert_eq!(FROM_SECONDS, Duration::SECOND);
const SECONDS_F32: f32 = ONE.as_secs_f32();
const SECONDS_F32: f32 = Duration::SECOND.as_secs_f32();
assert_eq!(SECONDS_F32, 1.0);
const FROM_SECONDS_F32: Duration = Duration::from_secs_f32(1.0);
assert_eq!(FROM_SECONDS_F32, ONE);
assert_eq!(FROM_SECONDS_F32, Duration::SECOND);
const SECONDS_F64: f64 = ONE.as_secs_f64();
const SECONDS_F64: f64 = Duration::SECOND.as_secs_f64();
assert_eq!(SECONDS_F64, 1.0);
const FROM_SECONDS_F64: Duration = Duration::from_secs_f64(1.0);
assert_eq!(FROM_SECONDS_F64, ONE);
assert_eq!(FROM_SECONDS_F64, Duration::SECOND);
const MILLIS: u128 = ONE.as_millis();
const MILLIS: u128 = Duration::SECOND.as_millis();
assert_eq!(MILLIS, 1_000);
const FROM_MILLIS: Duration = Duration::from_millis(1_000);
assert_eq!(FROM_MILLIS, ONE);
assert_eq!(FROM_MILLIS, Duration::SECOND);
const MICROS: u128 = ONE.as_micros();
const MICROS: u128 = Duration::SECOND.as_micros();
assert_eq!(MICROS, 1_000_000);
const FROM_MICROS: Duration = Duration::from_micros(1_000_000);
assert_eq!(FROM_MICROS, ONE);
assert_eq!(FROM_MICROS, Duration::SECOND);
const NANOS: u128 = ONE.as_nanos();
const NANOS: u128 = Duration::SECOND.as_nanos();
assert_eq!(NANOS, 1_000_000_000);
const FROM_NANOS: Duration = Duration::from_nanos(1_000_000_000);
assert_eq!(FROM_NANOS, ONE);
assert_eq!(FROM_NANOS, Duration::SECOND);
const MAX: Duration = Duration::new(u64::MAX, 999_999_999);
const CHECKED_ADD: Option<Duration> = MAX.checked_add(ONE);
const CHECKED_ADD: Option<Duration> = MAX.checked_add(Duration::SECOND);
assert_eq!(CHECKED_ADD, None);
const CHECKED_SUB: Option<Duration> = ZERO.checked_sub(ONE);
const CHECKED_SUB: Option<Duration> = ZERO.checked_sub(Duration::SECOND);
assert_eq!(CHECKED_SUB, None);
const CHECKED_MUL: Option<Duration> = ONE.checked_mul(1);
assert_eq!(CHECKED_MUL, Some(ONE));
const CHECKED_MUL: Option<Duration> = Duration::SECOND.checked_mul(1);
assert_eq!(CHECKED_MUL, Some(Duration::SECOND));
const MUL_F32: Duration = ONE.mul_f32(1.0);
assert_eq!(MUL_F32, ONE);
const MUL_F32: Duration = Duration::SECOND.mul_f32(1.0);
assert_eq!(MUL_F32, Duration::SECOND);
const MUL_F64: Duration = ONE.mul_f64(1.0);
assert_eq!(MUL_F64, ONE);
const MUL_F64: Duration = Duration::SECOND.mul_f64(1.0);
assert_eq!(MUL_F64, Duration::SECOND);
const CHECKED_DIV: Option<Duration> = ONE.checked_div(1);
assert_eq!(CHECKED_DIV, Some(ONE));
const CHECKED_DIV: Option<Duration> = Duration::SECOND.checked_div(1);
assert_eq!(CHECKED_DIV, Some(Duration::SECOND));
const DIV_F32: Duration = ONE.div_f32(1.0);
assert_eq!(DIV_F32, ONE);
const DIV_F32: Duration = Duration::SECOND.div_f32(1.0);
assert_eq!(DIV_F32, Duration::SECOND);
const DIV_F64: Duration = ONE.div_f64(1.0);
assert_eq!(DIV_F64, ONE);
const DIV_F64: Duration = Duration::SECOND.div_f64(1.0);
assert_eq!(DIV_F64, Duration::SECOND);
const DIV_DURATION_F32: f32 = ONE.div_duration_f32(ONE);
const DIV_DURATION_F32: f32 = Duration::SECOND.div_duration_f32(Duration::SECOND);
assert_eq!(DIV_DURATION_F32, 1.0);
const DIV_DURATION_F64: f64 = ONE.div_duration_f64(ONE);
const DIV_DURATION_F64: f64 = Duration::SECOND.div_duration_f64(Duration::SECOND);
assert_eq!(DIV_DURATION_F64, 1.0);
const SATURATING_ADD: Duration = MAX.saturating_add(ONE);
const SATURATING_ADD: Duration = MAX.saturating_add(Duration::SECOND);
assert_eq!(SATURATING_ADD, MAX);
const SATURATING_SUB: Duration = ZERO.saturating_sub(ONE);
const SATURATING_SUB: Duration = ZERO.saturating_sub(Duration::SECOND);
assert_eq!(SATURATING_SUB, ZERO);
const SATURATING_MUL: Duration = MAX.saturating_mul(2);

View File

@ -255,6 +255,7 @@
#![feature(doc_spotlight)]
#![feature(dropck_eyepatch)]
#![feature(duration_constants)]
#![feature(duration_zero)]
#![feature(exact_size_is_empty)]
#![feature(exhaustive_patterns)]
#![feature(extend_one)]

View File

@ -5,7 +5,7 @@ macro_rules! assert_almost_eq {
let (a, b) = ($a, $b);
if a != b {
let (a, b) = if a > b { (a, b) } else { (b, a) };
assert!(a - Duration::new(0, 1000) <= b, "{:?} is not almost equal to {:?}", a, b);
assert!(a - Duration::from_micros(1) <= b, "{:?} is not almost equal to {:?}", a, b);
}
}};
}
@ -34,7 +34,7 @@ fn instant_math() {
assert_almost_eq!(b - dur, a);
assert_almost_eq!(a + dur, b);
let second = Duration::new(1, 0);
let second = Duration::SECOND;
assert_almost_eq!(a - second + second, a);
assert_almost_eq!(a.checked_sub(second).unwrap().checked_add(second).unwrap(), a);
@ -65,24 +65,24 @@ fn instant_math_is_associative() {
#[should_panic]
fn instant_duration_since_panic() {
let a = Instant::now();
(a - Duration::new(1, 0)).duration_since(a);
(a - Duration::SECOND).duration_since(a);
}
#[test]
fn instant_checked_duration_since_nopanic() {
let now = Instant::now();
let earlier = now - Duration::new(1, 0);
let later = now + Duration::new(1, 0);
let earlier = now - Duration::SECOND;
let later = now + Duration::SECOND;
assert_eq!(earlier.checked_duration_since(now), None);
assert_eq!(later.checked_duration_since(now), Some(Duration::new(1, 0)));
assert_eq!(now.checked_duration_since(now), Some(Duration::new(0, 0)));
assert_eq!(later.checked_duration_since(now), Some(Duration::SECOND));
assert_eq!(now.checked_duration_since(now), Some(Duration::zero()));
}
#[test]
fn instant_saturating_duration_since_nopanic() {
let a = Instant::now();
let ret = (a - Duration::new(1, 0)).saturating_duration_since(a);
assert_eq!(ret, Duration::new(0, 0));
let ret = (a - Duration::SECOND).saturating_duration_since(a);
assert_eq!(ret, Duration::zero());
}
#[test]
@ -90,7 +90,7 @@ fn system_time_math() {
let a = SystemTime::now();
let b = SystemTime::now();
match b.duration_since(a) {
Ok(dur) if dur == Duration::new(0, 0) => {
Ok(dur) if dur == Duration::zero() => {
assert_almost_eq!(a, b);
}
Ok(dur) => {
@ -106,16 +106,16 @@ fn system_time_math() {
}
}
let second = Duration::new(1, 0);
let second = Duration::SECOND;
assert_almost_eq!(a.duration_since(a - second).unwrap(), second);
assert_almost_eq!(a.duration_since(a + second).unwrap_err().duration(), second);
assert_almost_eq!(a - second + second, a);
assert_almost_eq!(a.checked_sub(second).unwrap().checked_add(second).unwrap(), a);
let one_second_from_epoch = UNIX_EPOCH + Duration::new(1, 0);
let one_second_from_epoch = UNIX_EPOCH + Duration::SECOND;
let one_second_from_epoch2 =
UNIX_EPOCH + Duration::new(0, 500_000_000) + Duration::new(0, 500_000_000);
UNIX_EPOCH + Duration::from_millis(500) + Duration::from_millis(500);
assert_eq!(one_second_from_epoch, one_second_from_epoch2);
// checked_add_duration will not panic on overflow
@ -141,12 +141,12 @@ fn system_time_elapsed() {
#[test]
fn since_epoch() {
let ts = SystemTime::now();
let a = ts.duration_since(UNIX_EPOCH + Duration::new(1, 0)).unwrap();
let a = ts.duration_since(UNIX_EPOCH + Duration::SECOND).unwrap();
let b = ts.duration_since(UNIX_EPOCH).unwrap();
assert!(b > a);
assert_eq!(b - a, Duration::new(1, 0));
assert_eq!(b - a, Duration::SECOND);
let thirty_years = Duration::new(1, 0) * 60 * 60 * 24 * 365 * 30;
let thirty_years = Duration::SECOND * 60 * 60 * 24 * 365 * 30;
// Right now for CI this test is run in an emulator, and apparently the
// aarch64 emulator's sense of time is that we're still living in the