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.
12 use core::kinds::marker;
14 use core::clone::Clone;
20 let addr_x: *const int = mem::transmute(&*x);
23 let addr_y: *const int = mem::transmute(&*y);
24 assert_eq!(addr_x, addr_y);
30 let x = "test".to_string();
31 let addr_x = x.as_ptr();
34 let addr_y = y.as_ptr();
35 assert_eq!(addr_x, addr_y);
39 fn test_get_resource() {
41 use core::cell::RefCell;
52 *ii.borrow_mut() = i + 1;
56 fn r(i: Rc<RefCell<int>>) -> R {
62 let i = Rc::new(RefCell::new(0i));
66 let _y = opt.unwrap();
68 assert_eq!(*i.borrow(), 1);
72 fn test_option_dance() {
77 y2 = y.take().unwrap();
83 #[test] #[should_fail]
84 fn test_option_too_much_dance() {
85 let mut y = Some(marker::NoCopy);
86 let _y2 = y.take().unwrap();
87 let _y3 = y.take().unwrap();
92 let x: Option<int> = Some(1i);
93 assert_eq!(x.and(Some(2i)), Some(2));
94 assert_eq!(x.and(None::<int>), None);
96 let x: Option<int> = None;
97 assert_eq!(x.and(Some(2i)), None);
98 assert_eq!(x.and(None::<int>), None);
103 let x: Option<int> = Some(1);
104 assert_eq!(x.and_then(|x| Some(x + 1)), Some(2));
105 assert_eq!(x.and_then(|_| None::<int>), None);
107 let x: Option<int> = None;
108 assert_eq!(x.and_then(|x| Some(x + 1)), None);
109 assert_eq!(x.and_then(|_| None::<int>), None);
114 let x: Option<int> = Some(1);
115 assert_eq!(x.or(Some(2)), Some(1));
116 assert_eq!(x.or(None), Some(1));
118 let x: Option<int> = None;
119 assert_eq!(x.or(Some(2)), Some(2));
120 assert_eq!(x.or(None), None);
125 let x: Option<int> = Some(1);
126 assert_eq!(x.or_else(|| Some(2)), Some(1));
127 assert_eq!(x.or_else(|| None), Some(1));
129 let x: Option<int> = None;
130 assert_eq!(x.or_else(|| Some(2)), Some(2));
131 assert_eq!(x.or_else(|| None), None);
136 assert_eq!(Some(1i).unwrap(), 1);
137 let s = Some("hello".to_string()).unwrap();
138 assert_eq!(s, "hello");
143 fn test_unwrap_panic1() {
144 let x: Option<int> = None;
150 fn test_unwrap_panic2() {
151 let x: Option<String> = None;
156 fn test_unwrap_or() {
157 let x: Option<int> = Some(1);
158 assert_eq!(x.unwrap_or(2), 1);
160 let x: Option<int> = None;
161 assert_eq!(x.unwrap_or(2), 2);
165 fn test_unwrap_or_else() {
166 let x: Option<int> = Some(1);
167 assert_eq!(x.unwrap_or_else(|| 2), 1);
169 let x: Option<int> = None;
170 assert_eq!(x.unwrap_or_else(|| 2), 2);
178 let mut it = x.iter();
180 assert_eq!(it.size_hint(), (1, Some(1)));
181 assert_eq!(it.next(), Some(&val));
182 assert_eq!(it.size_hint(), (0, Some(0)));
183 assert!(it.next().is_none());
191 let mut x = Some(val);
193 let mut it = x.iter_mut();
195 assert_eq!(it.size_hint(), (1, Some(1)));
199 assert_eq!(*interior, val);
202 None => assert!(false),
205 assert_eq!(it.size_hint(), (0, Some(0)));
206 assert!(it.next().is_none());
208 assert_eq!(x, Some(new_val));
213 let small = Some(1.0f64);
214 let big = Some(5.0f64);
215 let nan = Some(0.0f64/0.0);
216 assert!(!(nan < big));
217 assert!(!(nan > big));
218 assert!(small < big);
225 let v: Option<Vec<int>> = range(0i, 0).map(|_| Some(0i)).collect();
226 assert!(v == Some(vec![]));
228 let v: Option<Vec<int>> = range(0i, 3).map(|x| Some(x)).collect();
229 assert!(v == Some(vec![0, 1, 2]));
231 let v: Option<Vec<int>> = range(0i, 3).map(|x| {
232 if x > 1 { None } else { Some(x) }
236 // test that it does not take more elements than it needs
237 let mut functions = [|| Some(()), || None, || panic!()];
239 let v: Option<Vec<()>> = functions.iter_mut().map(|f| (*f)()).collect();
248 let val1_ref = &val1;
249 let opt_none: Option<&'static u32> = None;
250 let opt_ref = Some(&val1);
251 let opt_ref_ref = Some(&val1_ref);
252 let opt_mut_ref = Some(&mut val2);
255 assert_eq!(opt_none.clone(), None);
256 assert_eq!(opt_none.cloned(), None);
259 assert_eq!(opt_mut_ref.cloned(), Some(2u32));
261 // Immutable ref works
262 assert_eq!(opt_ref.clone(), Some(&val1));
263 assert_eq!(opt_ref.cloned(), Some(1u32));
265 // Double Immutable ref works
266 assert_eq!(opt_ref_ref.clone(), Some(&val1_ref));
267 assert_eq!(opt_ref_ref.clone().cloned(), Some(&val1));
268 assert_eq!(opt_ref_ref.cloned().cloned(), Some(1u32));