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