1
Fork 0

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

@ -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