|
1 | 1 | use super::{Duration, Instant, SystemTime, UNIX_EPOCH};
|
| 2 | +use core::fmt::Debug; |
2 | 3 | #[cfg(not(target_arch = "wasm32"))]
|
3 | 4 | use test::{black_box, Bencher};
|
4 | 5 |
|
@@ -201,6 +202,32 @@ fn since_epoch() {
|
201 | 202 | assert!(a < hundred_twenty_years);
|
202 | 203 | }
|
203 | 204 |
|
| 205 | +#[test] |
| 206 | +fn big_math() { |
| 207 | + // Check that the same result occurs when adding/subtracting each duration one at a time as when |
| 208 | + // adding/subtracting them all at once. |
| 209 | + #[track_caller] |
| 210 | + fn check<T: Eq + Copy + Debug>(start: Option<T>, op: impl Fn(&T, Duration) -> Option<T>) { |
| 211 | + const DURATIONS: [Duration; 2] = |
| 212 | + [Duration::from_secs(i64::MAX as _), Duration::from_secs(50)]; |
| 213 | + if let Some(start) = start { |
| 214 | + assert_eq!( |
| 215 | + op(&start, DURATIONS.into_iter().sum()), |
| 216 | + DURATIONS.into_iter().try_fold(start, |t, d| op(&t, d)) |
| 217 | + ) |
| 218 | + } |
| 219 | + } |
| 220 | + |
| 221 | + check(SystemTime::UNIX_EPOCH.checked_sub(Duration::from_secs(100)), SystemTime::checked_add); |
| 222 | + check(SystemTime::UNIX_EPOCH.checked_add(Duration::from_secs(100)), SystemTime::checked_sub); |
| 223 | + |
| 224 | + let instant = Instant::now(); |
| 225 | + check(instant.checked_sub(Duration::from_secs(100)), Instant::checked_add); |
| 226 | + check(instant.checked_sub(Duration::from_secs(i64::MAX as _)), Instant::checked_add); |
| 227 | + check(instant.checked_add(Duration::from_secs(100)), Instant::checked_sub); |
| 228 | + check(instant.checked_add(Duration::from_secs(i64::MAX as _)), Instant::checked_sub); |
| 229 | +} |
| 230 | + |
204 | 231 | macro_rules! bench_instant_threaded {
|
205 | 232 | ($bench_name:ident, $thread_count:expr) => {
|
206 | 233 | #[bench]
|
|
0 commit comments