]> git.proxmox.com Git - rustc.git/blob - src/libcoretest/option.rs
Imported Upstream version 1.9.0+dfsg1
[rustc.git] / src / libcoretest / option.rs
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.
4 //
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.
10
11 use core::option::*;
12 use core::mem;
13 use core::clone::Clone;
14
15 #[test]
16 fn test_get_ptr() {
17 unsafe {
18 let x: Box<_> = box 0;
19 let addr_x: *const isize = mem::transmute(&*x);
20 let opt = Some(x);
21 let y = opt.unwrap();
22 let addr_y: *const isize = mem::transmute(&*y);
23 assert_eq!(addr_x, addr_y);
24 }
25 }
26
27 #[test]
28 fn test_get_str() {
29 let x = "test".to_string();
30 let addr_x = x.as_ptr();
31 let opt = Some(x);
32 let y = opt.unwrap();
33 let addr_y = y.as_ptr();
34 assert_eq!(addr_x, addr_y);
35 }
36
37 #[test]
38 fn test_get_resource() {
39 use std::rc::Rc;
40 use core::cell::RefCell;
41
42 struct R {
43 i: Rc<RefCell<isize>>,
44 }
45
46 impl Drop for R {
47 fn drop(&mut self) {
48 let ii = &*self.i;
49 let i = *ii.borrow();
50 *ii.borrow_mut() = i + 1;
51 }
52 }
53
54 fn r(i: Rc<RefCell<isize>>) -> R {
55 R {
56 i: i
57 }
58 }
59
60 let i = Rc::new(RefCell::new(0));
61 {
62 let x = r(i.clone());
63 let opt = Some(x);
64 let _y = opt.unwrap();
65 }
66 assert_eq!(*i.borrow(), 1);
67 }
68
69 #[test]
70 fn test_option_dance() {
71 let x = Some(());
72 let mut y = Some(5);
73 let mut y2 = 0;
74 for _x in x {
75 y2 = y.take().unwrap();
76 }
77 assert_eq!(y2, 5);
78 assert!(y.is_none());
79 }
80
81 #[test] #[should_panic]
82 fn test_option_too_much_dance() {
83 struct A;
84 let mut y = Some(A);
85 let _y2 = y.take().unwrap();
86 let _y3 = y.take().unwrap();
87 }
88
89 #[test]
90 fn test_and() {
91 let x: Option<isize> = Some(1);
92 assert_eq!(x.and(Some(2)), Some(2));
93 assert_eq!(x.and(None::<isize>), None);
94
95 let x: Option<isize> = None;
96 assert_eq!(x.and(Some(2)), None);
97 assert_eq!(x.and(None::<isize>), None);
98 }
99
100 #[test]
101 fn test_and_then() {
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);
105
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);
109 }
110
111 #[test]
112 fn test_or() {
113 let x: Option<isize> = Some(1);
114 assert_eq!(x.or(Some(2)), Some(1));
115 assert_eq!(x.or(None), Some(1));
116
117 let x: Option<isize> = None;
118 assert_eq!(x.or(Some(2)), Some(2));
119 assert_eq!(x.or(None), None);
120 }
121
122 #[test]
123 fn test_or_else() {
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));
127
128 let x: Option<isize> = None;
129 assert_eq!(x.or_else(|| Some(2)), Some(2));
130 assert_eq!(x.or_else(|| None), None);
131 }
132
133 #[test]
134 fn test_unwrap() {
135 assert_eq!(Some(1).unwrap(), 1);
136 let s = Some("hello".to_string()).unwrap();
137 assert_eq!(s, "hello");
138 }
139
140 #[test]
141 #[should_panic]
142 fn test_unwrap_panic1() {
143 let x: Option<isize> = None;
144 x.unwrap();
145 }
146
147 #[test]
148 #[should_panic]
149 fn test_unwrap_panic2() {
150 let x: Option<String> = None;
151 x.unwrap();
152 }
153
154 #[test]
155 fn test_unwrap_or() {
156 let x: Option<isize> = Some(1);
157 assert_eq!(x.unwrap_or(2), 1);
158
159 let x: Option<isize> = None;
160 assert_eq!(x.unwrap_or(2), 2);
161 }
162
163 #[test]
164 fn test_unwrap_or_else() {
165 let x: Option<isize> = Some(1);
166 assert_eq!(x.unwrap_or_else(|| 2), 1);
167
168 let x: Option<isize> = None;
169 assert_eq!(x.unwrap_or_else(|| 2), 2);
170 }
171
172 #[test]
173 fn test_iter() {
174 let val = 5;
175
176 let x = Some(val);
177 let mut it = x.iter();
178
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());
183
184 let mut it = (&x).into_iter();
185 assert_eq!(it.next(), Some(&val));
186 }
187
188 #[test]
189 fn test_mut_iter() {
190 let mut val = 5;
191 let new_val = 11;
192
193 let mut x = Some(val);
194 {
195 let mut it = x.iter_mut();
196
197 assert_eq!(it.size_hint(), (1, Some(1)));
198
199 match it.next() {
200 Some(interior) => {
201 assert_eq!(*interior, val);
202 *interior = new_val;
203 }
204 None => assert!(false),
205 }
206
207 assert_eq!(it.size_hint(), (0, Some(0)));
208 assert!(it.next().is_none());
209 }
210 assert_eq!(x, Some(new_val));
211
212 let mut y = Some(val);
213 let mut it = (&mut y).into_iter();
214 assert_eq!(it.next(), Some(&mut val));
215 }
216
217 #[test]
218 fn test_ord() {
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);
225 assert!(None < big);
226 assert!(big > None);
227 }
228
229 #[test]
230 fn test_collect() {
231 let v: Option<Vec<isize>> = (0..0).map(|_| Some(0)).collect();
232 assert!(v == Some(vec![]));
233
234 let v: Option<Vec<isize>> = (0..3).map(|x| Some(x)).collect();
235 assert!(v == Some(vec![0, 1, 2]));
236
237 let v: Option<Vec<isize>> = (0..3).map(|x| {
238 if x > 1 { None } else { Some(x) }
239 }).collect();
240 assert!(v == None);
241
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!()];
245
246 let v: Option<Vec<()>> = functions.iter_mut().map(|f| (*f)()).collect();
247
248 assert!(v == None);
249 }
250
251
252 #[test]
253 fn test_cloned() {
254 let val = 1;
255 let val_ref = &val;
256 let opt_none: Option<&'static u32> = None;
257 let opt_ref = Some(&val);
258 let opt_ref_ref = Some(&val_ref);
259
260 // None works
261 assert_eq!(opt_none.clone(), None);
262 assert_eq!(opt_none.cloned(), None);
263
264 // Immutable ref works
265 assert_eq!(opt_ref.clone(), Some(&val));
266 assert_eq!(opt_ref.cloned(), Some(1));
267
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(1));
272 }