]> git.proxmox.com Git - rustc.git/blob - library/core/tests/option.rs
New upstream version 1.47.0~beta.2+dfsg1
[rustc.git] / library / core / tests / option.rs
1 use core::array::FixedSizeArray;
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_iter() {
165 let val = 5;
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());
174
175 let mut it = (&x).into_iter();
176 assert_eq!(it.next(), Some(&val));
177 }
178
179 #[test]
180 fn test_mut_iter() {
181 let mut val = 5;
182 let new_val = 11;
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));
202
203 let mut y = Some(val);
204 let mut it = (&mut y).into_iter();
205 assert_eq!(it.next(), Some(&mut val));
206 }
207
208 #[test]
209 fn test_ord() {
210 let small = Some(1.0f64);
211 let big = Some(5.0f64);
212 let nan = Some(0.0f64 / 0.0);
213 assert!(!(nan < big));
214 assert!(!(nan > big));
215 assert!(small < big);
216 assert!(None < big);
217 assert!(big > None);
218 }
219
220 #[test]
221 fn test_collect() {
222 let v: Option<Vec<isize>> = (0..0).map(|_| Some(0)).collect();
223 assert!(v == Some(vec![]));
224
225 let v: Option<Vec<isize>> = (0..3).map(|x| Some(x)).collect();
226 assert!(v == Some(vec![0, 1, 2]));
227
228 let v: Option<Vec<isize>> = (0..3).map(|x| if x > 1 { None } else { Some(x) }).collect();
229 assert!(v == None);
230
231 // test that it does not take more elements than it needs
232 let mut functions: [Box<dyn Fn() -> Option<()>>; 3] =
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 }
239
240 #[test]
241 fn 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 }
261
262 #[test]
263 fn test_cloned() {
264 let val = 1;
265 let val_ref = &val;
266 let opt_none: Option<&'static u32> = None;
267 let opt_ref = Some(&val);
268 let opt_ref_ref = Some(&val_ref);
269
270 // None works
271 assert_eq!(opt_none.clone(), None);
272 assert_eq!(opt_none.cloned(), None);
273
274 // Immutable ref works
275 assert_eq!(opt_ref.clone(), Some(&val));
276 assert_eq!(opt_ref.cloned(), Some(1));
277
278 // Double Immutable ref works
279 assert_eq!(opt_ref_ref.clone(), Some(&val_ref));
280 assert_eq!(opt_ref_ref.clone().cloned(), Some(&val));
281 assert_eq!(opt_ref_ref.cloned().cloned(), Some(1));
282 }
283
284 #[test]
285 fn 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 }
310
311 #[test]
312 fn test_option_as_deref() {
313 // Some: &Option<T: Deref>::Some(T) -> Option<&T::Deref::Target>::Some(&*T)
314 let ref_option = &Some(&42);
315 assert_eq!(ref_option.as_deref(), Some(&42));
316
317 let ref_option = &Some(String::from("a result"));
318 assert_eq!(ref_option.as_deref(), Some("a result"));
319
320 let ref_option = &Some(vec![1, 2, 3, 4, 5]);
321 assert_eq!(ref_option.as_deref(), Some([1, 2, 3, 4, 5].as_slice()));
322
323 // None: &Option<T: Deref>>::None -> None
324 let ref_option: &Option<&i32> = &None;
325 assert_eq!(ref_option.as_deref(), None);
326 }
327
328 #[test]
329 fn 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);
344 }
345
346 #[test]
347 fn 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 }