]> git.lizzy.rs Git - rust.git/commitdiff
tighten Instance sanity check
authorRalf Jung <post@ralfj.de>
Wed, 15 Apr 2020 10:41:54 +0000 (12:41 +0200)
committerRalf Jung <post@ralfj.de>
Wed, 15 Apr 2020 11:13:17 +0000 (13:13 +0200)
tests/run-pass/time.rs

index 2c9b579f7e2945f9bdc3126aa3f765c2adc284be..d430062a153336a2f87ea2380c5a10d5ffdbe74f 100644 (file)
@@ -1,22 +1,29 @@
 // compile-flags: -Zmiri-disable-isolation
 
-use std::time::{SystemTime, Instant};
+use std::time::{SystemTime, Instant, Duration};
+
+fn duration_sanity(diff: Duration) {
+    // On my laptop, I observed times around 15-40ms. Add 10x lee-way both ways.
+    assert!(diff.as_millis() > 1);
+    assert!(diff.as_millis() < 500);
+}
 
 fn main() {
     // Check `SystemTime`.
     let now1 = SystemTime::now();
+    let seconds_since_epoch = now1.duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
+    let years_since_epoch = seconds_since_epoch / 3600 / 24 / 365;
+    let year = 1970 + years_since_epoch;
+    assert!(2020 <= year && year < 2100);
     // Do some work to make time pass.
     for _ in 0..10 { drop(vec![42]); }
     let now2 = SystemTime::now();
     assert!(now2 > now1);
+    // Sanity-check the difference we got.
     let diff = now2.duration_since(now1).unwrap();
     assert_eq!(now1 + diff, now2);
     assert_eq!(now2 - diff, now1);
-    // Sanity-check the time we got.
-    let seconds_since_epoch = now1.duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
-    let years_since_epoch = seconds_since_epoch / 3600 / 24 / 365;
-    let year = 1970 + years_since_epoch;
-    assert!(2020 <= year && year < 2100);
+    duration_sanity(diff);
 
     // Check `Instant`.
     let now1 = Instant::now();
@@ -24,10 +31,9 @@ fn main() {
     for _ in 0..10 { drop(vec![42]); }
     let now2 = Instant::now();
     assert!(now2 > now1);
+    // Sanity-check the difference we got.
     let diff = now2.duration_since(now1);
     assert_eq!(now1 + diff, now2);
     assert_eq!(now2 - diff, now1);
-    // Sanity-check the difference we got.
-    assert!(diff.as_micros() > 1);
-    assert!(diff.as_micros() < 1_000_000);
+    duration_sanity(diff);
 }