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;
19 let addr_x: *const int = mem::transmute(&*x);
22 let addr_y: *const int = mem::transmute(&*y);
23 assert_eq!(addr_x, addr_y);
29 let x = "test".to_string();
30 let addr_x = x.as_slice().as_ptr();
33 let addr_y = y.as_slice().as_ptr();
34 assert_eq!(addr_x, addr_y);
38 fn test_get_resource() {
40 use core::cell::RefCell;
51 *ii.borrow_mut() = i + 1;
55 fn r(i: Rc<RefCell<int>>) -> R {
61 let i = Rc::new(RefCell::new(0i));
65 let _y = opt.unwrap();
67 assert_eq!(*i.borrow(), 1);
71 fn test_option_dance() {
76 y2 = y.take().unwrap();
82 #[test] #[should_fail]
83 fn test_option_too_much_dance() {
84 let mut y = Some(marker::NoCopy);
85 let _y2 = y.take().unwrap();
86 let _y3 = y.take().unwrap();
91 let x: Option<int> = Some(1i);
92 assert_eq!(x.and(Some(2i)), Some(2));
93 assert_eq!(x.and(None::<int>), None);
95 let x: Option<int> = None;
96 assert_eq!(x.and(Some(2i)), None);
97 assert_eq!(x.and(None::<int>), None);
102 let x: Option<int> = Some(1);
103 assert_eq!(x.and_then(|x| Some(x + 1)), Some(2));
104 assert_eq!(x.and_then(|_| None::<int>), None);
106 let x: Option<int> = None;
107 assert_eq!(x.and_then(|x| Some(x + 1)), None);
108 assert_eq!(x.and_then(|_| None::<int>), None);
113 let x: Option<int> = Some(1);
114 assert_eq!(x.or(Some(2)), Some(1));
115 assert_eq!(x.or(None), Some(1));
117 let x: Option<int> = None;
118 assert_eq!(x.or(Some(2)), Some(2));
119 assert_eq!(x.or(None), None);
124 let x: Option<int> = 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<int> = None;
129 assert_eq!(x.or_else(|| Some(2)), Some(2));
130 assert_eq!(x.or_else(|| None), None);
134 fn test_option_while_some() {
136 Some(10i).while_some(|j| {
149 assert_eq!(Some(1i).unwrap(), 1);
150 let s = Some("hello".to_string()).unwrap();
151 assert_eq!(s.as_slice(), "hello");
156 fn test_unwrap_fail1() {
157 let x: Option<int> = None;
163 fn test_unwrap_fail2() {
164 let x: Option<String> = None;
169 fn test_unwrap_or() {
170 let x: Option<int> = Some(1);
171 assert_eq!(x.unwrap_or(2), 1);
173 let x: Option<int> = None;
174 assert_eq!(x.unwrap_or(2), 2);
178 fn test_unwrap_or_else() {
179 let x: Option<int> = Some(1);
180 assert_eq!(x.unwrap_or_else(|| 2), 1);
182 let x: Option<int> = None;
183 assert_eq!(x.unwrap_or_else(|| 2), 2);
188 let some_stuff = Some(42i);
189 let modified_stuff = some_stuff.filtered(|&x| {x < 10});
190 assert_eq!(some_stuff.unwrap(), 42);
191 assert!(modified_stuff.is_none());
199 let mut it = x.iter();
201 assert_eq!(it.size_hint(), (1, Some(1)));
202 assert_eq!(it.next(), Some(&val));
203 assert_eq!(it.size_hint(), (0, Some(0)));
204 assert!(it.next().is_none());
212 let mut x = Some(val);
214 let mut it = x.mut_iter();
216 assert_eq!(it.size_hint(), (1, Some(1)));
220 assert_eq!(*interior, val);
223 None => assert!(false),
226 assert_eq!(it.size_hint(), (0, Some(0)));
227 assert!(it.next().is_none());
229 assert_eq!(x, Some(new_val));
234 let small = Some(1.0f64);
235 let big = Some(5.0f64);
236 let nan = Some(0.0f64/0.0);
237 assert!(!(nan < big));
238 assert!(!(nan > big));
239 assert!(small < big);
246 let mut x = Some(3i);
247 assert!(x.mutate(|i| i+1));
248 assert_eq!(x, Some(4i));
249 assert!(x.mutate_or_set(0, |i| i+1));
250 assert_eq!(x, Some(5i));
252 assert!(!x.mutate(|i| i+1));
254 assert!(!x.mutate_or_set(0i, |i| i+1));
255 assert_eq!(x, Some(0i));
260 let v: Option<Vec<int>> = collect(range(0i, 0)
262 assert!(v == Some(vec![]));
264 let v: Option<Vec<int>> = collect(range(0i, 3)
266 assert!(v == Some(vec![0, 1, 2]));
268 let v: Option<Vec<int>> = collect(range(0i, 3)
269 .map(|x| if x > 1 { None } else { Some(x) }));
272 // test that it does not take more elements than it needs
273 let mut functions = [|| Some(()), || None, || fail!()];
275 let v: Option<Vec<()>> = collect(functions.mut_iter().map(|f| (*f)()));