about summary refs log tree commit diff
path: root/library/std/src/time
diff options
context:
space:
mode:
authorJubilee Young <workingjubilee@gmail.com>2020-10-21 17:56:09 -0700
committerJubilee Young <workingjubilee@gmail.com>2020-10-21 20:03:56 -0700
commitd72d5f48c2c534de7382a601e4619de923b7f6a9 (patch)
treef12106f173e97d683684bce98fe6db92722ab6ed /library/std/src/time
parent22e6b9c68941996daa45786b4145e6196e51f0f4 (diff)
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.
Diffstat (limited to 'library/std/src/time')
-rw-r--r--library/std/src/time/tests.rs32
1 files changed, 16 insertions, 16 deletions
diff --git a/library/std/src/time/tests.rs b/library/std/src/time/tests.rs
index 783bf49f315..44c06bac950 100644
--- a/library/std/src/time/tests.rs
+++ b/library/std/src/time/tests.rs
@@ -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