]> git.lizzy.rs Git - rust.git/blob - library/std/src/time/tests.rs
Auto merge of #79115 - cuviper:rust-description, r=Mark-Simulacrum
[rust.git] / library / std / src / time / tests.rs
1 use super::{Duration, Instant, SystemTime, UNIX_EPOCH};
2
3 macro_rules! assert_almost_eq {
4     ($a:expr, $b:expr) => {{
5         let (a, b) = ($a, $b);
6         if a != b {
7             let (a, b) = if a > b { (a, b) } else { (b, a) };
8             assert!(a - Duration::from_micros(1) <= b, "{:?} is not almost equal to {:?}", a, b);
9         }
10     }};
11 }
12
13 #[test]
14 fn instant_monotonic() {
15     let a = Instant::now();
16     let b = Instant::now();
17     assert!(b >= a);
18 }
19
20 #[test]
21 fn instant_elapsed() {
22     let a = Instant::now();
23     a.elapsed();
24 }
25
26 #[test]
27 fn instant_math() {
28     let a = Instant::now();
29     let b = Instant::now();
30     println!("a: {:?}", a);
31     println!("b: {:?}", b);
32     let dur = b.duration_since(a);
33     println!("dur: {:?}", dur);
34     assert_almost_eq!(b - dur, a);
35     assert_almost_eq!(a + dur, b);
36
37     let second = Duration::SECOND;
38     assert_almost_eq!(a - second + second, a);
39     assert_almost_eq!(a.checked_sub(second).unwrap().checked_add(second).unwrap(), a);
40
41     // checked_add_duration will not panic on overflow
42     let mut maybe_t = Some(Instant::now());
43     let max_duration = Duration::from_secs(u64::MAX);
44     // in case `Instant` can store `>= now + max_duration`.
45     for _ in 0..2 {
46         maybe_t = maybe_t.and_then(|t| t.checked_add(max_duration));
47     }
48     assert_eq!(maybe_t, None);
49
50     // checked_add_duration calculates the right time and will work for another year
51     let year = Duration::from_secs(60 * 60 * 24 * 365);
52     assert_eq!(a + year, a.checked_add(year).unwrap());
53 }
54
55 #[test]
56 fn instant_math_is_associative() {
57     let now = Instant::now();
58     let offset = Duration::from_millis(5);
59     // Changing the order of instant math shouldn't change the results,
60     // especially when the expression reduces to X + identity.
61     assert_eq!((now + offset) - now, (now - now) + offset);
62 }
63
64 #[test]
65 #[should_panic]
66 fn instant_duration_since_panic() {
67     let a = Instant::now();
68     (a - Duration::SECOND).duration_since(a);
69 }
70
71 #[test]
72 fn instant_checked_duration_since_nopanic() {
73     let now = Instant::now();
74     let earlier = now - Duration::SECOND;
75     let later = now + Duration::SECOND;
76     assert_eq!(earlier.checked_duration_since(now), None);
77     assert_eq!(later.checked_duration_since(now), Some(Duration::SECOND));
78     assert_eq!(now.checked_duration_since(now), Some(Duration::ZERO));
79 }
80
81 #[test]
82 fn instant_saturating_duration_since_nopanic() {
83     let a = Instant::now();
84     let ret = (a - Duration::SECOND).saturating_duration_since(a);
85     assert_eq!(ret, Duration::ZERO);
86 }
87
88 #[test]
89 fn system_time_math() {
90     let a = SystemTime::now();
91     let b = SystemTime::now();
92     match b.duration_since(a) {
93         Ok(Duration::ZERO) => {
94             assert_almost_eq!(a, b);
95         }
96         Ok(dur) => {
97             assert!(b > a);
98             assert_almost_eq!(b - dur, a);
99             assert_almost_eq!(a + dur, b);
100         }
101         Err(dur) => {
102             let dur = dur.duration();
103             assert!(a > b);
104             assert_almost_eq!(b + dur, a);
105             assert_almost_eq!(a - dur, b);
106         }
107     }
108
109     let second = Duration::SECOND;
110     assert_almost_eq!(a.duration_since(a - second).unwrap(), second);
111     assert_almost_eq!(a.duration_since(a + second).unwrap_err().duration(), second);
112
113     assert_almost_eq!(a - second + second, a);
114     assert_almost_eq!(a.checked_sub(second).unwrap().checked_add(second).unwrap(), a);
115
116     let one_second_from_epoch = UNIX_EPOCH + Duration::SECOND;
117     let one_second_from_epoch2 =
118         UNIX_EPOCH + Duration::from_millis(500) + Duration::from_millis(500);
119     assert_eq!(one_second_from_epoch, one_second_from_epoch2);
120
121     // checked_add_duration will not panic on overflow
122     let mut maybe_t = Some(SystemTime::UNIX_EPOCH);
123     let max_duration = Duration::from_secs(u64::MAX);
124     // in case `SystemTime` can store `>= UNIX_EPOCH + max_duration`.
125     for _ in 0..2 {
126         maybe_t = maybe_t.and_then(|t| t.checked_add(max_duration));
127     }
128     assert_eq!(maybe_t, None);
129
130     // checked_add_duration calculates the right time and will work for another year
131     let year = Duration::from_secs(60 * 60 * 24 * 365);
132     assert_eq!(a + year, a.checked_add(year).unwrap());
133 }
134
135 #[test]
136 fn system_time_elapsed() {
137     let a = SystemTime::now();
138     drop(a.elapsed());
139 }
140
141 #[test]
142 fn since_epoch() {
143     let ts = SystemTime::now();
144     let a = ts.duration_since(UNIX_EPOCH + Duration::SECOND).unwrap();
145     let b = ts.duration_since(UNIX_EPOCH).unwrap();
146     assert!(b > a);
147     assert_eq!(b - a, Duration::SECOND);
148
149     let thirty_years = Duration::SECOND * 60 * 60 * 24 * 365 * 30;
150
151     // Right now for CI this test is run in an emulator, and apparently the
152     // aarch64 emulator's sense of time is that we're still living in the
153     // 70s. This is also true for riscv (also qemu)
154     //
155     // Otherwise let's assume that we're all running computers later than
156     // 2000.
157     if !cfg!(target_arch = "aarch64") && !cfg!(target_arch = "riscv64") {
158         assert!(a > thirty_years);
159     }
160
161     // let's assume that we're all running computers earlier than 2090.
162     // Should give us ~70 years to fix this!
163     let hundred_twenty_years = thirty_years * 4;
164     assert!(a < hundred_twenty_years);
165 }