]> git.proxmox.com Git - rustc.git/blame - library/core/tests/option.rs
New upstream version 1.48.0~beta.8+dfsg1
[rustc.git] / library / core / tests / option.rs
CommitLineData
1b1a35ee 1use core::cell::Cell;
60c5eb7d
XL
2use core::clone::Clone;
3use core::mem;
416331ca 4use core::ops::DerefMut;
60c5eb7d 5use core::option::*;
1a4d82fc
JJ
6
7#[test]
8fn test_get_ptr() {
9 unsafe {
c34b1796
AL
10 let x: Box<_> = box 0;
11 let addr_x: *const isize = mem::transmute(&*x);
1a4d82fc
JJ
12 let opt = Some(x);
13 let y = opt.unwrap();
c34b1796 14 let addr_y: *const isize = mem::transmute(&*y);
1a4d82fc
JJ
15 assert_eq!(addr_x, addr_y);
16 }
17}
18
19#[test]
20fn test_get_str() {
21 let x = "test".to_string();
22 let addr_x = x.as_ptr();
23 let opt = Some(x);
24 let y = opt.unwrap();
25 let addr_y = y.as_ptr();
26 assert_eq!(addr_x, addr_y);
27}
28
29#[test]
30fn test_get_resource() {
1a4d82fc 31 use core::cell::RefCell;
60c5eb7d 32 use std::rc::Rc;
1a4d82fc
JJ
33
34 struct R {
60c5eb7d 35 i: Rc<RefCell<isize>>,
1a4d82fc
JJ
36 }
37
60c5eb7d
XL
38 impl Drop for R {
39 fn drop(&mut self) {
1a4d82fc
JJ
40 let ii = &*self.i;
41 let i = *ii.borrow();
42 *ii.borrow_mut() = i + 1;
43 }
44 }
45
c34b1796 46 fn r(i: Rc<RefCell<isize>>) -> R {
60c5eb7d 47 R { i }
1a4d82fc
JJ
48 }
49
85aaf69f 50 let i = Rc::new(RefCell::new(0));
1a4d82fc
JJ
51 {
52 let x = r(i.clone());
53 let opt = Some(x);
54 let _y = opt.unwrap();
55 }
56 assert_eq!(*i.borrow(), 1);
57}
58
59#[test]
60fn test_option_dance() {
61 let x = Some(());
85aaf69f 62 let mut y = Some(5);
1a4d82fc 63 let mut y2 = 0;
62682a34 64 for _x in x {
1a4d82fc
JJ
65 y2 = y.take().unwrap();
66 }
67 assert_eq!(y2, 5);
68 assert!(y.is_none());
69}
70
60c5eb7d
XL
71#[test]
72#[should_panic]
1a4d82fc 73fn test_option_too_much_dance() {
62682a34
SL
74 struct A;
75 let mut y = Some(A);
1a4d82fc
JJ
76 let _y2 = y.take().unwrap();
77 let _y3 = y.take().unwrap();
78}
79
80#[test]
81fn test_and() {
c34b1796 82 let x: Option<isize> = Some(1);
85aaf69f 83 assert_eq!(x.and(Some(2)), Some(2));
c34b1796 84 assert_eq!(x.and(None::<isize>), None);
1a4d82fc 85
c34b1796 86 let x: Option<isize> = None;
85aaf69f 87 assert_eq!(x.and(Some(2)), None);
c34b1796 88 assert_eq!(x.and(None::<isize>), None);
1a4d82fc
JJ
89}
90
91#[test]
92fn test_and_then() {
c34b1796 93 let x: Option<isize> = Some(1);
1a4d82fc 94 assert_eq!(x.and_then(|x| Some(x + 1)), Some(2));
c34b1796 95 assert_eq!(x.and_then(|_| None::<isize>), None);
1a4d82fc 96
c34b1796 97 let x: Option<isize> = None;
1a4d82fc 98 assert_eq!(x.and_then(|x| Some(x + 1)), None);
c34b1796 99 assert_eq!(x.and_then(|_| None::<isize>), None);
1a4d82fc
JJ
100}
101
102#[test]
103fn test_or() {
c34b1796 104 let x: Option<isize> = Some(1);
1a4d82fc
JJ
105 assert_eq!(x.or(Some(2)), Some(1));
106 assert_eq!(x.or(None), Some(1));
107
c34b1796 108 let x: Option<isize> = None;
1a4d82fc
JJ
109 assert_eq!(x.or(Some(2)), Some(2));
110 assert_eq!(x.or(None), None);
111}
112
113#[test]
114fn test_or_else() {
c34b1796 115 let x: Option<isize> = Some(1);
1a4d82fc
JJ
116 assert_eq!(x.or_else(|| Some(2)), Some(1));
117 assert_eq!(x.or_else(|| None), Some(1));
118
c34b1796 119 let x: Option<isize> = None;
1a4d82fc
JJ
120 assert_eq!(x.or_else(|| Some(2)), Some(2));
121 assert_eq!(x.or_else(|| None), None);
122}
123
124#[test]
125fn test_unwrap() {
85aaf69f 126 assert_eq!(Some(1).unwrap(), 1);
1a4d82fc
JJ
127 let s = Some("hello".to_string()).unwrap();
128 assert_eq!(s, "hello");
129}
130
131#[test]
c34b1796 132#[should_panic]
1a4d82fc 133fn test_unwrap_panic1() {
c34b1796 134 let x: Option<isize> = None;
1a4d82fc
JJ
135 x.unwrap();
136}
137
138#[test]
c34b1796 139#[should_panic]
1a4d82fc
JJ
140fn test_unwrap_panic2() {
141 let x: Option<String> = None;
142 x.unwrap();
143}
144
145#[test]
146fn test_unwrap_or() {
c34b1796 147 let x: Option<isize> = Some(1);
1a4d82fc
JJ
148 assert_eq!(x.unwrap_or(2), 1);
149
c34b1796 150 let x: Option<isize> = None;
1a4d82fc
JJ
151 assert_eq!(x.unwrap_or(2), 2);
152}
153
154#[test]
155fn test_unwrap_or_else() {
c34b1796 156 let x: Option<isize> = Some(1);
1a4d82fc
JJ
157 assert_eq!(x.unwrap_or_else(|| 2), 1);
158
c34b1796 159 let x: Option<isize> = None;
1a4d82fc
JJ
160 assert_eq!(x.unwrap_or_else(|| 2), 2);
161}
162
163#[test]
164fn test_iter() {
85aaf69f 165 let val = 5;
1a4d82fc
JJ
166
167 let x = Some(val);
168 let mut it = x.iter();
169
170 assert_eq!(it.size_hint(), (1, Some(1)));
171 assert_eq!(it.next(), Some(&val));
172 assert_eq!(it.size_hint(), (0, Some(0)));
173 assert!(it.next().is_none());
e9174d1e
SL
174
175 let mut it = (&x).into_iter();
176 assert_eq!(it.next(), Some(&val));
1a4d82fc
JJ
177}
178
179#[test]
180fn test_mut_iter() {
e9174d1e 181 let mut val = 5;
85aaf69f 182 let new_val = 11;
1a4d82fc
JJ
183
184 let mut x = Some(val);
185 {
186 let mut it = x.iter_mut();
187
188 assert_eq!(it.size_hint(), (1, Some(1)));
189
190 match it.next() {
191 Some(interior) => {
192 assert_eq!(*interior, val);
193 *interior = new_val;
194 }
195 None => assert!(false),
196 }
197
198 assert_eq!(it.size_hint(), (0, Some(0)));
199 assert!(it.next().is_none());
200 }
201 assert_eq!(x, Some(new_val));
e9174d1e
SL
202
203 let mut y = Some(val);
204 let mut it = (&mut y).into_iter();
205 assert_eq!(it.next(), Some(&mut val));
1a4d82fc
JJ
206}
207
208#[test]
209fn test_ord() {
210 let small = Some(1.0f64);
211 let big = Some(5.0f64);
60c5eb7d 212 let nan = Some(0.0f64 / 0.0);
1a4d82fc
JJ
213 assert!(!(nan < big));
214 assert!(!(nan > big));
215 assert!(small < big);
216 assert!(None < big);
217 assert!(big > None);
218}
219
1a4d82fc
JJ
220#[test]
221fn test_collect() {
c34b1796 222 let v: Option<Vec<isize>> = (0..0).map(|_| Some(0)).collect();
1a4d82fc
JJ
223 assert!(v == Some(vec![]));
224
c34b1796 225 let v: Option<Vec<isize>> = (0..3).map(|x| Some(x)).collect();
1a4d82fc
JJ
226 assert!(v == Some(vec![0, 1, 2]));
227
60c5eb7d 228 let v: Option<Vec<isize>> = (0..3).map(|x| if x > 1 { None } else { Some(x) }).collect();
1a4d82fc
JJ
229 assert!(v == None);
230
231 // test that it does not take more elements than it needs
8faf50e0 232 let mut functions: [Box<dyn Fn() -> Option<()>>; 3] =
1a4d82fc
JJ
233 [box || Some(()), box || None, box || panic!()];
234
235 let v: Option<Vec<()>> = functions.iter_mut().map(|f| (*f)()).collect();
236
237 assert!(v == None);
238}
d9579d0f 239
0731742a
XL
240#[test]
241fn test_copied() {
242 let val = 1;
243 let val_ref = &val;
244 let opt_none: Option<&'static u32> = None;
245 let opt_ref = Some(&val);
246 let opt_ref_ref = Some(&val_ref);
247
248 // None works
249 assert_eq!(opt_none.clone(), None);
250 assert_eq!(opt_none.copied(), None);
251
252 // Immutable ref works
253 assert_eq!(opt_ref.clone(), Some(&val));
254 assert_eq!(opt_ref.copied(), Some(1));
255
256 // Double Immutable ref works
257 assert_eq!(opt_ref_ref.clone(), Some(&val_ref));
258 assert_eq!(opt_ref_ref.clone().copied(), Some(&val));
259 assert_eq!(opt_ref_ref.copied().copied(), Some(1));
260}
1a4d82fc
JJ
261
262#[test]
263fn test_cloned() {
54a0048b 264 let val = 1;
d9579d0f 265 let val_ref = &val;
1a4d82fc 266 let opt_none: Option<&'static u32> = None;
d9579d0f
AL
267 let opt_ref = Some(&val);
268 let opt_ref_ref = Some(&val_ref);
1a4d82fc
JJ
269
270 // None works
271 assert_eq!(opt_none.clone(), None);
272 assert_eq!(opt_none.cloned(), None);
273
1a4d82fc 274 // Immutable ref works
d9579d0f 275 assert_eq!(opt_ref.clone(), Some(&val));
54a0048b 276 assert_eq!(opt_ref.cloned(), Some(1));
1a4d82fc
JJ
277
278 // Double Immutable ref works
d9579d0f
AL
279 assert_eq!(opt_ref_ref.clone(), Some(&val_ref));
280 assert_eq!(opt_ref_ref.clone().cloned(), Some(&val));
54a0048b 281 assert_eq!(opt_ref_ref.cloned().cloned(), Some(1));
1a4d82fc 282}
ea8adc8c
XL
283
284#[test]
285fn test_try() {
286 fn try_option_some() -> Option<u8> {
287 let val = Some(1)?;
288 Some(val)
289 }
290 assert_eq!(try_option_some(), Some(1));
291
292 fn try_option_none() -> Option<u8> {
293 let val = None?;
294 Some(val)
295 }
296 assert_eq!(try_option_none(), None);
297
298 fn try_option_ok() -> Result<u8, NoneError> {
299 let val = Some(1)?;
300 Ok(val)
301 }
302 assert_eq!(try_option_ok(), Ok(1));
303
304 fn try_option_err() -> Result<u8, NoneError> {
305 let val = None?;
306 Ok(val)
307 }
308 assert_eq!(try_option_err(), Err(NoneError));
309}
8faf50e0 310
b7449926 311#[test]
416331ca 312fn test_option_as_deref() {
b7449926
XL
313 // Some: &Option<T: Deref>::Some(T) -> Option<&T::Deref::Target>::Some(&*T)
314 let ref_option = &Some(&42);
416331ca 315 assert_eq!(ref_option.as_deref(), Some(&42));
b7449926
XL
316
317 let ref_option = &Some(String::from("a result"));
416331ca 318 assert_eq!(ref_option.as_deref(), Some("a result"));
b7449926
XL
319
320 let ref_option = &Some(vec![1, 2, 3, 4, 5]);
416331ca 321 assert_eq!(ref_option.as_deref(), Some([1, 2, 3, 4, 5].as_slice()));
b7449926
XL
322
323 // None: &Option<T: Deref>>::None -> None
324 let ref_option: &Option<&i32> = &None;
416331ca
XL
325 assert_eq!(ref_option.as_deref(), None);
326}
327
328#[test]
329fn test_option_as_deref_mut() {
330 // Some: &mut Option<T: Deref>::Some(T) -> Option<&mut T::Deref::Target>::Some(&mut *T)
331 let mut val = 42;
332 let ref_option = &mut Some(&mut val);
333 assert_eq!(ref_option.as_deref_mut(), Some(&mut 42));
334
335 let ref_option = &mut Some(String::from("a result"));
336 assert_eq!(ref_option.as_deref_mut(), Some(String::from("a result").deref_mut()));
337
338 let ref_option = &mut Some(vec![1, 2, 3, 4, 5]);
339 assert_eq!(ref_option.as_deref_mut(), Some([1, 2, 3, 4, 5].as_mut_slice()));
340
341 // None: &mut Option<T: Deref>>::None -> None
342 let ref_option: &mut Option<&mut i32> = &mut None;
343 assert_eq!(ref_option.as_deref_mut(), None);
b7449926
XL
344}
345
8faf50e0
XL
346#[test]
347fn test_replace() {
348 let mut x = Some(2);
349 let old = x.replace(5);
350
351 assert_eq!(x, Some(5));
352 assert_eq!(old, Some(2));
353
354 let mut x = None;
355 let old = x.replace(3);
356
357 assert_eq!(x, Some(3));
358 assert_eq!(old, None);
359}
1b1a35ee
XL
360
361#[test]
362fn option_const() {
363 // test that the methods of `Option` are usable in a const context
364
365 const OPTION: Option<usize> = Some(32);
366
367 const REF: Option<&usize> = OPTION.as_ref();
368 assert_eq!(REF, Some(&32));
369
370 const IS_SOME: bool = OPTION.is_some();
371 assert!(IS_SOME);
372
373 const IS_NONE: bool = OPTION.is_none();
374 assert!(!IS_NONE);
375}
376
377#[test]
378fn test_unwrap_drop() {
379 struct Dtor<'a> {
380 x: &'a Cell<isize>,
381 }
382
383 impl<'a> std::ops::Drop for Dtor<'a> {
384 fn drop(&mut self) {
385 self.x.set(self.x.get() - 1);
386 }
387 }
388
389 fn unwrap<T>(o: Option<T>) -> T {
390 match o {
391 Some(v) => v,
392 None => panic!(),
393 }
394 }
395
396 let x = &Cell::new(1);
397
398 {
399 let b = Some(Dtor { x });
400 let _c = unwrap(b);
401 }
402
403 assert_eq!(x.get(), 0);
404}