1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 use core::clone::Clone;
18 let x: Box<_> = box 0;
19 let addr_x: *const isize = mem::transmute(&*x);
22 let addr_y: *const isize = mem::transmute(&*y);
23 assert_eq!(addr_x, addr_y);
29 let x = "test".to_string();
30 let addr_x = x.as_ptr();
33 let addr_y = y.as_ptr();
34 assert_eq!(addr_x, addr_y);
38 fn test_get_resource() {
40 use core::cell::RefCell;
43 i: Rc<RefCell<isize>>,
50 *ii.borrow_mut() = i + 1;
54 fn r(i: Rc<RefCell<isize>>) -> R {
60 let i = Rc::new(RefCell::new(0));
64 let _y = opt.unwrap();
66 assert_eq!(*i.borrow(), 1);
70 fn test_option_dance() {
75 y2 = y.take().unwrap();
81 #[test] #[should_panic]
82 fn test_option_too_much_dance() {
85 let _y2 = y.take().unwrap();
86 let _y3 = y.take().unwrap();
91 let x: Option<isize> = Some(1);
92 assert_eq!(x.and(Some(2)), Some(2));
93 assert_eq!(x.and(None::<isize>), None);
95 let x: Option<isize> = None;
96 assert_eq!(x.and(Some(2)), None);
97 assert_eq!(x.and(None::<isize>), None);
102 let x: Option<isize> = Some(1);
103 assert_eq!(x.and_then(|x| Some(x + 1)), Some(2));
104 assert_eq!(x.and_then(|_| None::<isize>), None);
106 let x: Option<isize> = None;
107 assert_eq!(x.and_then(|x| Some(x + 1)), None);
108 assert_eq!(x.and_then(|_| None::<isize>), None);
113 let x: Option<isize> = Some(1);
114 assert_eq!(x.or(Some(2)), Some(1));
115 assert_eq!(x.or(None), Some(1));
117 let x: Option<isize> = None;
118 assert_eq!(x.or(Some(2)), Some(2));
119 assert_eq!(x.or(None), None);
124 let x: Option<isize> = Some(1);
125 assert_eq!(x.or_else(|| Some(2)), Some(1));
126 assert_eq!(x.or_else(|| None), Some(1));
128 let x: Option<isize> = None;
129 assert_eq!(x.or_else(|| Some(2)), Some(2));
130 assert_eq!(x.or_else(|| None), None);
135 assert_eq!(Some(1).unwrap(), 1);
136 let s = Some("hello".to_string()).unwrap();
137 assert_eq!(s, "hello");
142 fn test_unwrap_panic1() {
143 let x: Option<isize> = None;
149 fn test_unwrap_panic2() {
150 let x: Option<String> = None;
155 fn test_unwrap_or() {
156 let x: Option<isize> = Some(1);
157 assert_eq!(x.unwrap_or(2), 1);
159 let x: Option<isize> = None;
160 assert_eq!(x.unwrap_or(2), 2);
164 fn test_unwrap_or_else() {
165 let x: Option<isize> = Some(1);
166 assert_eq!(x.unwrap_or_else(|| 2), 1);
168 let x: Option<isize> = None;
169 assert_eq!(x.unwrap_or_else(|| 2), 2);
177 let mut it = x.iter();
179 assert_eq!(it.size_hint(), (1, Some(1)));
180 assert_eq!(it.next(), Some(&val));
181 assert_eq!(it.size_hint(), (0, Some(0)));
182 assert!(it.next().is_none());
184 let mut it = (&x).into_iter();
185 assert_eq!(it.next(), Some(&val));
193 let mut x = Some(val);
195 let mut it = x.iter_mut();
197 assert_eq!(it.size_hint(), (1, Some(1)));
201 assert_eq!(*interior, val);
204 None => assert!(false),
207 assert_eq!(it.size_hint(), (0, Some(0)));
208 assert!(it.next().is_none());
210 assert_eq!(x, Some(new_val));
212 let mut y = Some(val);
213 let mut it = (&mut y).into_iter();
214 assert_eq!(it.next(), Some(&mut val));
219 let small = Some(1.0f64);
220 let big = Some(5.0f64);
221 let nan = Some(0.0f64/0.0);
222 assert!(!(nan < big));
223 assert!(!(nan > big));
224 assert!(small < big);
231 let v: Option<Vec<isize>> = (0..0).map(|_| Some(0)).collect();
232 assert!(v == Some(vec![]));
234 let v: Option<Vec<isize>> = (0..3).map(|x| Some(x)).collect();
235 assert!(v == Some(vec![0, 1, 2]));
237 let v: Option<Vec<isize>> = (0..3).map(|x| {
238 if x > 1 { None } else { Some(x) }
242 // test that it does not take more elements than it needs
243 let mut functions: [Box<Fn() -> Option<()>>; 3] =
244 [box || Some(()), box || None, box || panic!()];
246 let v: Option<Vec<()>> = functions.iter_mut().map(|f| (*f)()).collect();
256 let opt_none: Option<&'static u32> = None;
257 let opt_ref = Some(&val);
258 let opt_ref_ref = Some(&val_ref);
261 assert_eq!(opt_none.clone(), None);
262 assert_eq!(opt_none.cloned(), None);
264 // Immutable ref works
265 assert_eq!(opt_ref.clone(), Some(&val));
266 assert_eq!(opt_ref.cloned(), Some(1u32));
268 // Double Immutable ref works
269 assert_eq!(opt_ref_ref.clone(), Some(&val_ref));
270 assert_eq!(opt_ref_ref.clone().cloned(), Some(&val));
271 assert_eq!(opt_ref_ref.cloned().cloned(), Some(1u32));