Skip to content

Commit 27ee3fe

Browse files
committed
std: Tweak tests of std::time
Typical algebra currently doesn't work on the types in std::time currently (see [this comment][comment]), so tweak the tests to account for this property. [comment]: rust-lang#29866 (comment) Closes rust-lang#29970
1 parent e24fffe commit 27ee3fe

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

src/libstd/time/mod.rs

+24-14
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,16 @@ impl fmt::Display for SystemTimeError {
231231
mod tests {
232232
use super::{Instant, SystemTime, Duration, UNIX_EPOCH};
233233

234+
macro_rules! assert_almost_eq {
235+
($a:expr, $b:expr) => ({
236+
let (a, b) = ($a, $b);
237+
if a != b {
238+
let (a, b) = if a > b {(a, b)} else {(b, a)};
239+
assert!(a - Duration::new(0, 1) <= b);
240+
}
241+
})
242+
}
243+
234244
#[test]
235245
fn instant_monotonic() {
236246
let a = Instant::now();
@@ -249,11 +259,11 @@ mod tests {
249259
let a = Instant::now();
250260
let b = Instant::now();
251261
let dur = b.duration_from_earlier(a);
252-
assert_eq!(b - dur, a);
253-
assert_eq!(a + dur, b);
262+
assert_almost_eq!(b - dur, a);
263+
assert_almost_eq!(a + dur, b);
254264

255265
let second = Duration::new(1, 0);
256-
assert_eq!(a - second + second, a);
266+
assert_almost_eq!(a - second + second, a);
257267
}
258268

259269
#[test]
@@ -269,31 +279,31 @@ mod tests {
269279
let b = SystemTime::now();
270280
match b.duration_from_earlier(a) {
271281
Ok(dur) if dur == Duration::new(0, 0) => {
272-
assert_eq!(a, b);
282+
assert_almost_eq!(a, b);
273283
}
274284
Ok(dur) => {
275285
assert!(b > a);
276-
assert_eq!(b - dur, a);
277-
assert_eq!(a + dur, b);
286+
assert_almost_eq!(b - dur, a);
287+
assert_almost_eq!(a + dur, b);
278288
}
279289
Err(dur) => {
280290
let dur = dur.duration();
281291
assert!(a > b);
282-
assert_eq!(b + dur, a);
283-
assert_eq!(b - dur, a);
292+
assert_almost_eq!(b + dur, a);
293+
assert_almost_eq!(b - dur, a);
284294
}
285295
}
286296

287297
let second = Duration::new(1, 0);
288-
assert_eq!(a.duration_from_earlier(a - second).unwrap(), second);
289-
assert_eq!(a.duration_from_earlier(a + second).unwrap_err().duration(),
290-
second);
298+
assert_almost_eq!(a.duration_from_earlier(a - second).unwrap(), second);
299+
assert_almost_eq!(a.duration_from_earlier(a + second).unwrap_err()
300+
.duration(), second);
291301

292-
assert_eq!(a - second + second, a);
302+
assert_almost_eq!(a - second + second, a);
293303

294304
let eighty_years = second * 60 * 60 * 24 * 365 * 80;
295-
assert_eq!(a - eighty_years + eighty_years, a);
296-
assert_eq!(a - (eighty_years * 10) + (eighty_years * 10), a);
305+
assert_almost_eq!(a - eighty_years + eighty_years, a);
306+
assert_almost_eq!(a - (eighty_years * 10) + (eighty_years * 10), a);
297307
}
298308

299309
#[test]

0 commit comments

Comments
 (0)