]> git.proxmox.com Git - rustc.git/blob - src/libcoretest/option.rs
Imported Upstream version 1.1.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::marker;
13 use core::mem;
14 use core::clone::Clone;
15
16 #[test]
17 fn test_get_ptr() {
18 unsafe {
19 let x: Box<_> = box 0;
20 let addr_x: *const isize = mem::transmute(&*x);
21 let opt = Some(x);
22 let y = opt.unwrap();
23 let addr_y: *const isize = mem::transmute(&*y);
24 assert_eq!(addr_x, addr_y);
25 }
26 }
27
28 #[test]
29 fn test_get_str() {
30 let x = "test".to_string();
31 let addr_x = x.as_ptr();
32 let opt = Some(x);
33 let y = opt.unwrap();
34 let addr_y = y.as_ptr();
35 assert_eq!(addr_x, addr_y);
36 }
37
38 #[test]
39 fn test_get_resource() {
40 use std::rc::Rc;
41 use core::cell::RefCell;
42
43 struct R {
44 i: Rc<RefCell<isize>>,
45 }
46
47 impl Drop for R {
48 fn drop(&mut self) {
49 let ii = &*self.i;
50 let i = *ii.borrow();
51 *ii.borrow_mut() = i + 1;
52 }
53 }
54
55 fn r(i: Rc<RefCell<isize>>) -> R {
56 R {
57 i: i
58 }
59 }
60
61 let i = Rc::new(RefCell::new(0));
62 {
63 let x = r(i.clone());
64 let opt = Some(x);
65 let _y = opt.unwrap();
66 }
67 assert_eq!(*i.borrow(), 1);
68 }
69
70 #[test]
71 fn test_option_dance() {
72 let x = Some(());
73 let mut y = Some(5);
74 let mut y2 = 0;
75 for _x in x.iter() {
76 y2 = y.take().unwrap();
77 }
78 assert_eq!(y2, 5);
79 assert!(y.is_none());
80 }
81
82 #[test] #[should_panic]
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();
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
185 #[test]
186 fn test_mut_iter() {
187 let val = 5;
188 let new_val = 11;
189
190 let mut x = Some(val);
191 {
192 let mut it = x.iter_mut();
193
194 assert_eq!(it.size_hint(), (1, Some(1)));
195
196 match it.next() {
197 Some(interior) => {
198 assert_eq!(*interior, val);
199 *interior = new_val;
200 }
201 None => assert!(false),
202 }
203
204 assert_eq!(it.size_hint(), (0, Some(0)));
205 assert!(it.next().is_none());
206 }
207 assert_eq!(x, Some(new_val));
208 }
209
210 #[test]
211 fn test_ord() {
212 let small = Some(1.0f64);
213 let big = Some(5.0f64);
214 let nan = Some(0.0f64/0.0);
215 assert!(!(nan < big));
216 assert!(!(nan > big));
217 assert!(small < big);
218 assert!(None < big);
219 assert!(big > None);
220 }
221
222 #[test]
223 fn test_collect() {
224 let v: Option<Vec<isize>> = (0..0).map(|_| Some(0)).collect();
225 assert!(v == Some(vec![]));
226
227 let v: Option<Vec<isize>> = (0..3).map(|x| Some(x)).collect();
228 assert!(v == Some(vec![0, 1, 2]));
229
230 let v: Option<Vec<isize>> = (0..3).map(|x| {
231 if x > 1 { None } else { Some(x) }
232 }).collect();
233 assert!(v == None);
234
235 // test that it does not take more elements than it needs
236 let mut functions: [Box<Fn() -> Option<()>>; 3] =
237 [box || Some(()), box || None, box || panic!()];
238
239 let v: Option<Vec<()>> = functions.iter_mut().map(|f| (*f)()).collect();
240
241 assert!(v == None);
242 }
243
244
245 #[test]
246 fn test_cloned() {
247 let val = 1u32;
248 let val_ref = &val;
249 let opt_none: Option<&'static u32> = None;
250 let opt_ref = Some(&val);
251 let opt_ref_ref = Some(&val_ref);
252
253 // None works
254 assert_eq!(opt_none.clone(), None);
255 assert_eq!(opt_none.cloned(), None);
256
257 // Immutable ref works
258 assert_eq!(opt_ref.clone(), Some(&val));
259 assert_eq!(opt_ref.cloned(), Some(1u32));
260
261 // Double Immutable ref works
262 assert_eq!(opt_ref_ref.clone(), Some(&val_ref));
263 assert_eq!(opt_ref_ref.clone().cloned(), Some(&val));
264 assert_eq!(opt_ref_ref.cloned().cloned(), Some(1u32));
265 }