|
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 |
|
@@ -193,6 +194,32 @@ fn since_epoch() {
|
193 | 194 | assert!(a < hundred_twenty_years);
|
194 | 195 | }
|
195 | 196 |
|
| 197 | +#[test] |
| 198 | +fn big_math() { |
| 199 | + // Check that the same result occurs when adding/subtracting each duration one at a time as when |
| 200 | + // adding/subtracting them all at once. |
| 201 | + #[track_caller] |
| 202 | + fn check<T: Eq + Copy + Debug>(start: Option<T>, op: impl Fn(&T, Duration) -> Option<T>) { |
| 203 | + const DURATIONS: [Duration; 2] = |
| 204 | + [Duration::from_secs(i64::MAX as _), Duration::from_secs(50)]; |
| 205 | + if let Some(start) = start { |
| 206 | + assert_eq!( |
| 207 | + op(&start, DURATIONS.into_iter().sum()), |
| 208 | + DURATIONS.into_iter().try_fold(start, |t, d| op(&t, d)) |
| 209 | + ) |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + check(SystemTime::UNIX_EPOCH.checked_sub(Duration::from_secs(100)), SystemTime::checked_add); |
| 214 | + check(SystemTime::UNIX_EPOCH.checked_add(Duration::from_secs(100)), SystemTime::checked_sub); |
| 215 | + |
| 216 | + let instant = Instant::now(); |
| 217 | + check(instant.checked_sub(Duration::from_secs(100)), Instant::checked_add); |
| 218 | + check(instant.checked_sub(Duration::from_secs(i64::MAX as _)), Instant::checked_add); |
| 219 | + check(instant.checked_add(Duration::from_secs(100)), Instant::checked_sub); |
| 220 | + check(instant.checked_add(Duration::from_secs(i64::MAX as _)), Instant::checked_sub); |
| 221 | +} |
| 222 | + |
196 | 223 | macro_rules! bench_instant_threaded {
|
197 | 224 | ($bench_name:ident, $thread_count:expr) => {
|
198 | 225 | #[bench]
|
|
0 commit comments