]> git.lizzy.rs Git - rust.git/blob - src/libcoretest/option.rs
rollup merge of #18407 : thestinger/arena
[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_unwrap() {
135     assert_eq!(Some(1i).unwrap(), 1);
136     let s = Some("hello".to_string()).unwrap();
137     assert_eq!(s.as_slice(), "hello");
138 }
139
140 #[test]
141 #[should_fail]
142 fn test_unwrap_panic1() {
143     let x: Option<int> = None;
144     x.unwrap();
145 }
146
147 #[test]
148 #[should_fail]
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<int> = Some(1);
157     assert_eq!(x.unwrap_or(2), 1);
158
159     let x: Option<int> = None;
160     assert_eq!(x.unwrap_or(2), 2);
161 }
162
163 #[test]
164 fn test_unwrap_or_else() {
165     let x: Option<int> = Some(1);
166     assert_eq!(x.unwrap_or_else(|| 2), 1);
167
168     let x: Option<int> = None;
169     assert_eq!(x.unwrap_or_else(|| 2), 2);
170 }
171
172 #[test]
173 fn test_iter() {
174     let val = 5i;
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 = 5i;
188     let new_val = 11i;
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<int>> = range(0i, 0).map(|_| Some(0i)).collect();
225     assert!(v == Some(vec![]));
226
227     let v: Option<Vec<int>> = range(0i, 3).map(|x| Some(x)).collect();
228     assert!(v == Some(vec![0, 1, 2]));
229
230     let v: Option<Vec<int>> = range(0i, 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 = [|| Some(()), || None, || panic!()];
237
238     let v: Option<Vec<()>> = functions.iter_mut().map(|f| (*f)()).collect();
239
240     assert!(v == None);
241 }