Skip to content

Commit a85dab4

Browse files
committed
tighten Instance sanity check
1 parent fbea3e5 commit a85dab4

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

tests/run-pass/time.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,39 @@
11
// compile-flags: -Zmiri-disable-isolation
22

3-
use std::time::{SystemTime, Instant};
3+
use std::time::{SystemTime, Instant, Duration};
4+
5+
fn duration_sanity(diff: Duration) {
6+
// On my laptop, I observed times around 15-40ms. Add 10x lee-way both ways.
7+
assert!(diff.as_millis() > 1);
8+
assert!(diff.as_millis() < 500);
9+
}
410

511
fn main() {
612
// Check `SystemTime`.
713
let now1 = SystemTime::now();
14+
let seconds_since_epoch = now1.duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
15+
let years_since_epoch = seconds_since_epoch / 3600 / 24 / 365;
16+
let year = 1970 + years_since_epoch;
17+
assert!(2020 <= year && year < 2100);
818
// Do some work to make time pass.
919
for _ in 0..10 { drop(vec![42]); }
1020
let now2 = SystemTime::now();
1121
assert!(now2 > now1);
22+
// Sanity-check the difference we got.
1223
let diff = now2.duration_since(now1).unwrap();
1324
assert_eq!(now1 + diff, now2);
1425
assert_eq!(now2 - diff, now1);
15-
// Sanity-check the time we got.
16-
let seconds_since_epoch = now1.duration_since(SystemTime::UNIX_EPOCH).unwrap().as_secs();
17-
let years_since_epoch = seconds_since_epoch / 3600 / 24 / 365;
18-
let year = 1970 + years_since_epoch;
19-
assert!(2020 <= year && year < 2100);
26+
duration_sanity(diff);
2027

2128
// Check `Instant`.
2229
let now1 = Instant::now();
2330
// Do some work to make time pass.
2431
for _ in 0..10 { drop(vec![42]); }
2532
let now2 = Instant::now();
2633
assert!(now2 > now1);
34+
// Sanity-check the difference we got.
2735
let diff = now2.duration_since(now1);
2836
assert_eq!(now1 + diff, now2);
2937
assert_eq!(now2 - diff, now1);
30-
// Sanity-check the difference we got.
31-
assert!(diff.as_micros() > 1);
32-
assert!(diff.as_micros() < 1_000_000);
38+
duration_sanity(diff);
3339
}

0 commit comments

Comments
 (0)