]> git.lizzy.rs Git - rust.git/blob - src/libcoretest/option.rs
auto merge of #16811 : nick29581/rust/dst-bug-2, r=nikomatsakis
[rust.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::kinds::marker;
13 use core::mem;
14
15 #[test]
16 fn test_get_ptr() {
17     unsafe {
18         let x = box 0i;
19         let addr_x: *const int = mem::transmute(&*x);
20         let opt = Some(x);
21         let y = opt.unwrap();
22         let addr_y: *const int = 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_slice().as_ptr();
31     let opt = Some(x);
32     let y = opt.unwrap();
33     let addr_y = y.as_slice().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<int>>,
44     }
45
46     #[unsafe_destructor]
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<int>>) -> R {
56         R {
57             i: i
58         }
59     }
60
61     let i = Rc::new(RefCell::new(0i));
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(5i);
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_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();
87 }
88
89 #[test]
90 fn test_and() {
91     let x: Option<int> = Some(1i);
92     assert_eq!(x.and(Some(2i)), Some(2));
93     assert_eq!(x.and(None::<int>), None);
94
95     let x: Option<int> = None;
96     assert_eq!(x.and(Some(2i)), None);
97     assert_eq!(x.and(None::<int>), None);
98 }
99
100 #[test]
101 fn test_and_then() {
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);
105
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);
109 }
110
111 #[test]
112 fn test_or() {
113     let x: Option<int> = Some(1);
114     assert_eq!(x.or(Some(2)), Some(1));
115     assert_eq!(x.or(None), Some(1));
116
117     let x: Option<int> = 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<int> = 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<int> = 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_option_while_some() {
135     let mut i = 0i;
136     Some(10i).while_some(|j| {
137         i += 1;
138         if j > 0 {
139             Some(j-1)
140         } else {
141             None
142         }
143     });
144     assert_eq!(i, 11);
145 }
146
147 #[test]
148 fn test_unwrap() {
149     assert_eq!(Some(1i).unwrap(), 1);
150     let s = Some("hello".to_string()).unwrap();
151     assert_eq!(s.as_slice(), "hello");
152 }
153
154 #[test]
155 #[should_fail]
156 fn test_unwrap_fail1() {
157     let x: Option<int> = None;
158     x.unwrap();
159 }
160
161 #[test]
162 #[should_fail]
163 fn test_unwrap_fail2() {
164     let x: Option<String> = None;
165     x.unwrap();
166 }
167
168 #[test]
169 fn test_unwrap_or() {
170     let x: Option<int> = Some(1);
171     assert_eq!(x.unwrap_or(2), 1);
172
173     let x: Option<int> = None;
174     assert_eq!(x.unwrap_or(2), 2);
175 }
176
177 #[test]
178 fn test_unwrap_or_else() {
179     let x: Option<int> = Some(1);
180     assert_eq!(x.unwrap_or_else(|| 2), 1);
181
182     let x: Option<int> = None;
183     assert_eq!(x.unwrap_or_else(|| 2), 2);
184 }
185
186 #[test]
187 fn test_filtered() {
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());
192 }
193
194 #[test]
195 fn test_iter() {
196     let val = 5i;
197
198     let x = Some(val);
199     let mut it = x.iter();
200
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());
205 }
206
207 #[test]
208 fn test_mut_iter() {
209     let val = 5i;
210     let new_val = 11i;
211
212     let mut x = Some(val);
213     {
214         let mut it = x.mut_iter();
215
216         assert_eq!(it.size_hint(), (1, Some(1)));
217
218         match it.next() {
219             Some(interior) => {
220                 assert_eq!(*interior, val);
221                 *interior = new_val;
222             }
223             None => assert!(false),
224         }
225
226         assert_eq!(it.size_hint(), (0, Some(0)));
227         assert!(it.next().is_none());
228     }
229     assert_eq!(x, Some(new_val));
230 }
231
232 #[test]
233 fn test_ord() {
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);
240     assert!(None < big);
241     assert!(big > None);
242 }
243
244 #[test]
245 fn test_mutate() {
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));
251     x = None;
252     assert!(!x.mutate(|i| i+1));
253     assert_eq!(x, None);
254     assert!(!x.mutate_or_set(0i, |i| i+1));
255     assert_eq!(x, Some(0i));
256 }
257
258 #[test]
259 fn test_collect() {
260     let v: Option<Vec<int>> = collect(range(0i, 0)
261                                       .map(|_| Some(0i)));
262     assert!(v == Some(vec![]));
263
264     let v: Option<Vec<int>> = collect(range(0i, 3)
265                                       .map(|x| Some(x)));
266     assert!(v == Some(vec![0, 1, 2]));
267
268     let v: Option<Vec<int>> = collect(range(0i, 3)
269                                       .map(|x| if x > 1 { None } else { Some(x) }));
270     assert!(v == None);
271
272     // test that it does not take more elements than it needs
273     let mut functions = [|| Some(()), || None, || fail!()];
274
275     let v: Option<Vec<()>> = collect(functions.mut_iter().map(|f| (*f)()));
276
277     assert!(v == None);
278 }