]> git.lizzy.rs Git - rust.git/blob - library/core/tests/option.rs
Rollup merge of #100520 - jakubdabek:patch-1, r=thomcc
[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::new(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     const FOO: Option<isize> = Some(1);
91     const A: Option<isize> = FOO.and(Some(2));
92     const B: Option<isize> = FOO.and(None);
93     assert_eq!(A, Some(2));
94     assert_eq!(B, None);
95
96     const BAR: Option<isize> = None;
97     const C: Option<isize> = BAR.and(Some(2));
98     const D: Option<isize> = BAR.and(None);
99     assert_eq!(C, None);
100     assert_eq!(D, None);
101 }
102
103 #[test]
104 fn test_and_then() {
105     const fn plus_one(x: isize) -> Option<isize> {
106         Some(x + 1)
107     }
108
109     const fn none(_: isize) -> Option<isize> {
110         None
111     }
112
113     let x: Option<isize> = Some(1);
114     assert_eq!(x.and_then(plus_one), Some(2));
115     assert_eq!(x.and_then(none), None);
116
117     let x: Option<isize> = None;
118     assert_eq!(x.and_then(plus_one), None);
119     assert_eq!(x.and_then(none), None);
120
121     const FOO: Option<isize> = Some(1);
122     const A: Option<isize> = FOO.and_then(plus_one);
123     const B: Option<isize> = FOO.and_then(none);
124     assert_eq!(A, Some(2));
125     assert_eq!(B, None);
126
127     const BAR: Option<isize> = None;
128     const C: Option<isize> = BAR.and_then(plus_one);
129     const D: Option<isize> = BAR.and_then(none);
130     assert_eq!(C, None);
131     assert_eq!(D, None);
132 }
133
134 #[test]
135 fn test_or() {
136     let x: Option<isize> = Some(1);
137     assert_eq!(x.or(Some(2)), Some(1));
138     assert_eq!(x.or(None), Some(1));
139
140     let x: Option<isize> = None;
141     assert_eq!(x.or(Some(2)), Some(2));
142     assert_eq!(x.or(None), None);
143
144     const FOO: Option<isize> = Some(1);
145     const A: Option<isize> = FOO.or(Some(2));
146     const B: Option<isize> = FOO.or(None);
147     assert_eq!(A, Some(1));
148     assert_eq!(B, Some(1));
149
150     const BAR: Option<isize> = None;
151     const C: Option<isize> = BAR.or(Some(2));
152     const D: Option<isize> = BAR.or(None);
153     assert_eq!(C, Some(2));
154     assert_eq!(D, None);
155 }
156
157 #[test]
158 fn test_or_else() {
159     const fn two() -> Option<isize> {
160         Some(2)
161     }
162
163     const fn none() -> Option<isize> {
164         None
165     }
166
167     let x: Option<isize> = Some(1);
168     assert_eq!(x.or_else(two), Some(1));
169     assert_eq!(x.or_else(none), Some(1));
170
171     let x: Option<isize> = None;
172     assert_eq!(x.or_else(two), Some(2));
173     assert_eq!(x.or_else(none), None);
174
175     const FOO: Option<isize> = Some(1);
176     const A: Option<isize> = FOO.or_else(two);
177     const B: Option<isize> = FOO.or_else(none);
178     assert_eq!(A, Some(1));
179     assert_eq!(B, Some(1));
180
181     const BAR: Option<isize> = None;
182     const C: Option<isize> = BAR.or_else(two);
183     const D: Option<isize> = BAR.or_else(none);
184     assert_eq!(C, Some(2));
185     assert_eq!(D, None);
186 }
187
188 #[test]
189 fn test_unwrap() {
190     assert_eq!(Some(1).unwrap(), 1);
191     let s = Some("hello".to_string()).unwrap();
192     assert_eq!(s, "hello");
193 }
194
195 #[test]
196 #[should_panic]
197 fn test_unwrap_panic1() {
198     let x: Option<isize> = None;
199     x.unwrap();
200 }
201
202 #[test]
203 #[should_panic]
204 fn test_unwrap_panic2() {
205     let x: Option<String> = None;
206     x.unwrap();
207 }
208
209 #[test]
210 fn test_unwrap_or() {
211     let x: Option<isize> = Some(1);
212     assert_eq!(x.unwrap_or(2), 1);
213
214     let x: Option<isize> = None;
215     assert_eq!(x.unwrap_or(2), 2);
216
217     const A: isize = Some(1).unwrap_or(2);
218     const B: isize = None.unwrap_or(2);
219     assert_eq!(A, 1);
220     assert_eq!(B, 2);
221 }
222
223 #[test]
224 fn test_unwrap_or_else() {
225     const fn two() -> isize {
226         2
227     }
228
229     let x: Option<isize> = Some(1);
230     assert_eq!(x.unwrap_or_else(two), 1);
231
232     let x: Option<isize> = None;
233     assert_eq!(x.unwrap_or_else(two), 2);
234
235     const A: isize = Some(1).unwrap_or_else(two);
236     const B: isize = None.unwrap_or_else(two);
237     assert_eq!(A, 1);
238     assert_eq!(B, 2);
239 }
240
241 #[test]
242 fn test_unwrap_unchecked() {
243     assert_eq!(unsafe { Some(1).unwrap_unchecked() }, 1);
244     let s = unsafe { Some("hello".to_string()).unwrap_unchecked() };
245     assert_eq!(s, "hello");
246 }
247
248 #[test]
249 fn test_iter() {
250     let val = 5;
251
252     let x = Some(val);
253     let mut it = x.iter();
254
255     assert_eq!(it.size_hint(), (1, Some(1)));
256     assert_eq!(it.next(), Some(&val));
257     assert_eq!(it.size_hint(), (0, Some(0)));
258     assert!(it.next().is_none());
259
260     let mut it = (&x).into_iter();
261     assert_eq!(it.next(), Some(&val));
262 }
263
264 #[test]
265 fn test_mut_iter() {
266     let mut val = 5;
267     let new_val = 11;
268
269     let mut x = Some(val);
270     {
271         let mut it = x.iter_mut();
272
273         assert_eq!(it.size_hint(), (1, Some(1)));
274
275         match it.next() {
276             Some(interior) => {
277                 assert_eq!(*interior, val);
278                 *interior = new_val;
279             }
280             None => assert!(false),
281         }
282
283         assert_eq!(it.size_hint(), (0, Some(0)));
284         assert!(it.next().is_none());
285     }
286     assert_eq!(x, Some(new_val));
287
288     let mut y = Some(val);
289     let mut it = (&mut y).into_iter();
290     assert_eq!(it.next(), Some(&mut val));
291 }
292
293 #[test]
294 fn test_ord() {
295     let small = Some(1.0f64);
296     let big = Some(5.0f64);
297     let nan = Some(0.0f64 / 0.0);
298     assert!(!(nan < big));
299     assert!(!(nan > big));
300     assert!(small < big);
301     assert!(None < big);
302     assert!(big > None);
303 }
304
305 #[test]
306 fn test_collect() {
307     let v: Option<Vec<isize>> = (0..0).map(|_| Some(0)).collect();
308     assert!(v == Some(vec![]));
309
310     let v: Option<Vec<isize>> = (0..3).map(|x| Some(x)).collect();
311     assert!(v == Some(vec![0, 1, 2]));
312
313     let v: Option<Vec<isize>> = (0..3).map(|x| if x > 1 { None } else { Some(x) }).collect();
314     assert!(v == None);
315
316     // test that it does not take more elements than it needs
317     let mut functions: [Box<dyn Fn() -> Option<()>>; 3] =
318         [Box::new(|| Some(())), Box::new(|| None), Box::new(|| panic!())];
319
320     let v: Option<Vec<()>> = functions.iter_mut().map(|f| (*f)()).collect();
321
322     assert!(v == None);
323 }
324
325 #[test]
326 fn test_copied() {
327     let val = 1;
328     let val_ref = &val;
329     let opt_none: Option<&'static u32> = None;
330     let opt_ref = Some(&val);
331     let opt_ref_ref = Some(&val_ref);
332
333     // None works
334     assert_eq!(opt_none.clone(), None);
335     assert_eq!(opt_none.copied(), None);
336
337     // Immutable ref works
338     assert_eq!(opt_ref.clone(), Some(&val));
339     assert_eq!(opt_ref.copied(), Some(1));
340
341     // Double Immutable ref works
342     assert_eq!(opt_ref_ref.clone(), Some(&val_ref));
343     assert_eq!(opt_ref_ref.clone().copied(), Some(&val));
344     assert_eq!(opt_ref_ref.copied().copied(), Some(1));
345 }
346
347 #[test]
348 fn test_cloned() {
349     let val = 1;
350     let val_ref = &val;
351     let opt_none: Option<&'static u32> = None;
352     let opt_ref = Some(&val);
353     let opt_ref_ref = Some(&val_ref);
354
355     // None works
356     assert_eq!(opt_none.clone(), None);
357     assert_eq!(opt_none.cloned(), None);
358
359     // Immutable ref works
360     assert_eq!(opt_ref.clone(), Some(&val));
361     assert_eq!(opt_ref.cloned(), Some(1));
362
363     // Double Immutable ref works
364     assert_eq!(opt_ref_ref.clone(), Some(&val_ref));
365     assert_eq!(opt_ref_ref.clone().cloned(), Some(&val));
366     assert_eq!(opt_ref_ref.cloned().cloned(), Some(1));
367 }
368
369 #[test]
370 fn test_try() {
371     fn try_option_some() -> Option<u8> {
372         let val = Some(1)?;
373         Some(val)
374     }
375     assert_eq!(try_option_some(), Some(1));
376
377     fn try_option_none() -> Option<u8> {
378         let val = None?;
379         Some(val)
380     }
381     assert_eq!(try_option_none(), None);
382 }
383
384 #[test]
385 fn test_option_as_deref() {
386     // Some: &Option<T: Deref>::Some(T) -> Option<&T::Deref::Target>::Some(&*T)
387     let ref_option = &Some(&42);
388     assert_eq!(ref_option.as_deref(), Some(&42));
389
390     let ref_option = &Some(String::from("a result"));
391     assert_eq!(ref_option.as_deref(), Some("a result"));
392
393     let ref_option = &Some(vec![1, 2, 3, 4, 5]);
394     assert_eq!(ref_option.as_deref(), Some([1, 2, 3, 4, 5].as_slice()));
395
396     // None: &Option<T: Deref>>::None -> None
397     let ref_option: &Option<&i32> = &None;
398     assert_eq!(ref_option.as_deref(), None);
399 }
400
401 #[test]
402 fn test_option_as_deref_mut() {
403     // Some: &mut Option<T: Deref>::Some(T) -> Option<&mut T::Deref::Target>::Some(&mut *T)
404     let mut val = 42;
405     let ref_option = &mut Some(&mut val);
406     assert_eq!(ref_option.as_deref_mut(), Some(&mut 42));
407
408     let ref_option = &mut Some(String::from("a result"));
409     assert_eq!(ref_option.as_deref_mut(), Some(String::from("a result").deref_mut()));
410
411     let ref_option = &mut Some(vec![1, 2, 3, 4, 5]);
412     assert_eq!(ref_option.as_deref_mut(), Some([1, 2, 3, 4, 5].as_mut_slice()));
413
414     // None: &mut Option<T: Deref>>::None -> None
415     let ref_option: &mut Option<&mut i32> = &mut None;
416     assert_eq!(ref_option.as_deref_mut(), None);
417 }
418
419 #[test]
420 fn test_replace() {
421     let mut x = Some(2);
422     let old = x.replace(5);
423
424     assert_eq!(x, Some(5));
425     assert_eq!(old, Some(2));
426
427     let mut x = None;
428     let old = x.replace(3);
429
430     assert_eq!(x, Some(3));
431     assert_eq!(old, None);
432 }
433
434 #[test]
435 fn option_const() {
436     // test that the methods of `Option` are usable in a const context
437
438     const OPTION: Option<usize> = Some(32);
439     assert_eq!(OPTION, Some(32));
440
441     const OPTION_FROM: Option<usize> = Option::from(32);
442     assert_eq!(OPTION_FROM, Some(32));
443
444     const REF: Option<&usize> = OPTION.as_ref();
445     assert_eq!(REF, Some(&32));
446
447     const REF_FROM: Option<&usize> = Option::from(&OPTION);
448     assert_eq!(REF_FROM, Some(&32));
449
450     const IS_SOME: bool = OPTION.is_some();
451     assert!(IS_SOME);
452
453     const IS_NONE: bool = OPTION.is_none();
454     assert!(!IS_NONE);
455
456     const COPIED: Option<usize> = OPTION.as_ref().copied();
457     assert_eq!(COPIED, OPTION);
458 }
459
460 #[test]
461 const fn option_const_mut() {
462     // test that the methods of `Option` that take mutable references are usable in a const context
463
464     let mut option: Option<usize> = Some(32);
465
466     let _take = option.take();
467     let _replace = option.replace(42);
468
469     {
470         let as_mut = option.as_mut();
471         match as_mut {
472             Some(v) => *v = 32,
473             None => unreachable!(),
474         }
475     }
476
477     {
478         let as_mut: Option<&mut usize> = Option::from(&mut option);
479         match as_mut {
480             Some(v) => *v = 42,
481             None => unreachable!(),
482         }
483     }
484 }
485
486 #[test]
487 fn test_unwrap_drop() {
488     struct Dtor<'a> {
489         x: &'a Cell<isize>,
490     }
491
492     impl<'a> std::ops::Drop for Dtor<'a> {
493         fn drop(&mut self) {
494             self.x.set(self.x.get() - 1);
495         }
496     }
497
498     fn unwrap<T>(o: Option<T>) -> T {
499         match o {
500             Some(v) => v,
501             None => panic!(),
502         }
503     }
504
505     let x = &Cell::new(1);
506
507     {
508         let b = Some(Dtor { x });
509         let _c = unwrap(b);
510     }
511
512     assert_eq!(x.get(), 0);
513 }
514
515 #[test]
516 fn option_ext() {
517     let thing = "{{ f }}";
518     let f = thing.find("{{");
519
520     if f.is_none() {
521         println!("None!");
522     }
523 }
524
525 #[test]
526 fn zip_options() {
527     let x = Some(10);
528     let y = Some("foo");
529     let z: Option<usize> = None;
530
531     assert_eq!(x.zip(y), Some((10, "foo")));
532     assert_eq!(x.zip(z), None);
533     assert_eq!(z.zip(x), None);
534 }
535
536 #[test]
537 fn unzip_options() {
538     let x = Some((10, "foo"));
539     let y = None::<(bool, i32)>;
540
541     assert_eq!(x.unzip(), (Some(10), Some("foo")));
542     assert_eq!(y.unzip(), (None, None));
543 }
544
545 #[test]
546 fn zip_unzip_roundtrip() {
547     let x = Some(10);
548     let y = Some("foo");
549
550     let z = x.zip(y);
551     assert_eq!(z, Some((10, "foo")));
552
553     let a = z.unzip();
554     assert_eq!(a, (x, y));
555 }