Debian Codegraph @

debian/missing-sources/time/tests/integration/instant.rs

raw
1 #![expect(deprecated)]
2
3 use std::cmp::Ordering;
4 use std::thread;
5 use std::time::Instant as StdInstant;
6
7 use time::ext::{NumericalDuration, NumericalStdDuration};
8 use time::{Duration, Instant};
9
10 #[test]
11 fn elapsed() {
12 let instant = Instant::now();
13 thread::sleep(1.std_milliseconds());
14 assert!(instant.elapsed() >= 1.milliseconds());
15 }
16
17 #[test]
18 fn checked_add() {
19 let now = Instant::now();
20 assert_eq!(now.checked_add(0.seconds()), Some(now));
21 assert_eq!(now.checked_add(5.seconds()), Some(now + 5.seconds()));
22 assert_eq!(now.checked_add((-5).seconds()), Some(now + (-5).seconds()));
23 }
24
25 #[test]
26 fn checked_sub() {
27 let now = Instant::now();
28 assert_eq!(now.checked_sub(0.seconds()), Some(now));
29 assert_eq!(now.checked_sub(5.seconds()), Some(now - 5.seconds()));
30 assert_eq!(now.checked_sub((-5).seconds()), Some(now - (-5).seconds()));
31 }
32
33 #[test]
34 fn into_inner() {
35 let now = Instant::now();
36 assert_eq!(now.into_inner(), now.0);
37 }
38
39 #[test]
40 fn std_from() {
41 let now_time = Instant::now();
42 let now_std = StdInstant::from(now_time);
43 assert_eq!(now_time, now_std);
44 }
45
46 #[test]
47 fn from_std() {
48 let now_std = StdInstant::now();
49 let now_time = Instant::from(now_std);
50 assert_eq!(now_time, now_std);
51 }
52
53 #[test]
54 fn sub() {
55 let start = Instant::now();
56 thread::sleep(1.std_milliseconds());
57 assert!(Instant::now() - start >= 1.milliseconds());
58 assert_eq!(start - start, Duration::ZERO);
59 }
60
61 #[test]
62 fn sub_std() {
63 let start = StdInstant::now();
64 thread::sleep(1.std_milliseconds());
65 assert!(Instant::now() - start >= 1.milliseconds());
66 }
67
68 #[test]
69 fn std_sub() {
70 let start = Instant::now();
71 thread::sleep(1.std_milliseconds());
72 assert!(StdInstant::now() - start >= 1.milliseconds());
73 }
74
75 #[test]
76 fn add_duration() {
77 let start = Instant::now();
78 assert!(start + 0.seconds() <= Instant::now());
79 thread::sleep(1.std_milliseconds());
80 assert!(start + 1.milliseconds() <= Instant::now());
81 }
82
83 #[test]
84 fn std_add_duration() {
85 let start = StdInstant::now();
86 thread::sleep(1.std_milliseconds());
87 assert!(start + 1.milliseconds() <= StdInstant::now());
88 }
89
90 #[test]
91 fn add_std_duration() {
92 let start = Instant::now();
93 thread::sleep(1.std_milliseconds());
94 assert!(start + 1.std_milliseconds() <= Instant::now());
95 }
96
97 #[test]
98 fn add_assign_duration() {
99 let mut start = Instant::now();
100 thread::sleep(1.std_milliseconds());
101 start += 1.milliseconds();
102 assert!(start <= Instant::now());
103 }
104
105 #[test]
106 fn std_add_assign_duration() {
107 let mut start = StdInstant::now();
108 thread::sleep(1.std_milliseconds());
109 start += 1.milliseconds();
110 assert!(start <= StdInstant::now());
111 }
112
113 #[test]
114 fn add_assign_std_duration() {
115 let mut start = Instant::now();
116 thread::sleep(1.std_milliseconds());
117 start += 1.std_milliseconds();
118 assert!(start <= Instant::now());
119 }
120
121 #[test]
122 fn sub_duration() {
123 let instant = Instant::now();
124 assert!(instant - 100.milliseconds() <= Instant::now());
125 assert_eq!(instant - Duration::ZERO, instant);
126 }
127
128 #[test]
129 fn std_sub_duration() {
130 let instant = StdInstant::now();
131 assert!(instant - 100.milliseconds() <= StdInstant::now());
132 }
133
134 #[test]
135 fn sub_std_duration() {
136 let instant = Instant::now();
137 assert!(instant - 100.std_milliseconds() <= Instant::now());
138 }
139
140 #[test]
141 fn sub_assign_duration() {
142 let mut instant = Instant::now();
143 instant -= 100.milliseconds();
144 assert!(instant <= Instant::now());
145 }
146
147 #[test]
148 fn std_sub_assign_duration() {
149 let mut instant = StdInstant::now();
150 instant -= 100.milliseconds();
151 assert!(instant <= StdInstant::now());
152 }
153
154 #[test]
155 fn sub_assign_std_duration() {
156 let mut instant = Instant::now();
157 instant -= 100.std_milliseconds();
158 assert!(instant <= Instant::now());
159 }
160
161 #[test]
162 fn eq_std() {
163 let now_time = Instant::now();
164 let now_std = StdInstant::from(now_time);
165 assert_eq!(now_time, now_std);
166 }
167
168 #[test]
169 fn std_eq() {
170 let now_time = Instant::now();
171 let now_std = StdInstant::from(now_time);
172 assert_eq!(now_std, now_time);
173 }
174
175 #[test]
176 fn ord() {
177 let now_time = Instant::now();
178 let now_std = now_time + 1.seconds();
179 assert_eq!(now_time.cmp(&now_std), Ordering::Less);
180
181 let now_time = Instant::now();
182 let now_std = now_time - 1.seconds();
183 assert_eq!(now_time.cmp(&now_std), Ordering::Greater);
184 }
185
186 #[test]
187 fn partial_ord_std() {
188 let now_time = Instant::now();
189 let now_std = StdInstant::from(now_time) + 1.seconds();
190 assert!(now_time < now_std);
191
192 let now_time = Instant::now();
193 let now_std = StdInstant::from(now_time) - 1.seconds();
194 assert!(now_time > now_std);
195 }
196
197 #[test]
198 fn std_partial_ord() {
199 let now_time = Instant::now();
200 let now_std = StdInstant::from(now_time) + 1.seconds();
201 assert!(now_std > now_time);
202
203 let now_time = Instant::now();
204 let now_std = StdInstant::from(now_time) - 1.seconds();
205 assert!(now_std < now_time);
206 }
207
208 #[test]
209 fn sub_regression() {
210 let now = Instant::now();
211 let future = now + Duration::seconds(5);
212 let past = now - Duration::seconds(5);
213
214 assert_eq!(future - now, Duration::seconds(5));
215 assert_eq!(now - past, Duration::seconds(5));
216 assert_eq!(future - past, Duration::seconds(10));
217
218 assert_eq!(now - future, Duration::seconds(-5));
219 assert_eq!(past - now, Duration::seconds(-5));
220 assert_eq!(past - future, Duration::seconds(-10));
221 }
222
223 #[test]
224 fn as_ref() {
225 let now = Instant::now();
226 assert_eq!(now.as_ref(), now.as_ref());
227 }
228
229 #[test]
230 fn borrow() {
231 use std::borrow::Borrow;
232 let now = Instant::now();
233 assert_eq!(
234 <Instant as Borrow<StdInstant>>::borrow(&now),
235 <Instant as Borrow<StdInstant>>::borrow(&now)
236 );
237 }