]> git.lizzy.rs Git - rust.git/blob - src/libcoretest/option.rs
auto merge of #19628 : jbranchaud/rust/add-string-as-string-doctest, r=steveklabnik
[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 use core::clone::Clone;
15
16 #[test]
17 fn test_get_ptr() {
18     unsafe {
19         let x = box 0i;
20         let addr_x: *const int = mem::transmute(&*x);
21         let opt = Some(x);
22         let y = opt.unwrap();
23         let addr_y: *const int = 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<int>>,
45     }
46
47     #[unsafe_destructor]
48     impl Drop for R {
49        fn drop(&mut self) {
50             let ii = &*self.i;
51             let i = *ii.borrow();
52             *ii.borrow_mut() = i + 1;
53         }
54     }
55
56     fn r(i: Rc<RefCell<int>>) -> R {
57         R {
58             i: i
59         }
60     }
61
62     let i = Rc::new(RefCell::new(0i));
63     {
64         let x = r(i.clone());
65         let opt = Some(x);
66         let _y = opt.unwrap();
67     }
68     assert_eq!(*i.borrow(), 1);
69 }
70
71 #[test]
72 fn test_option_dance() {
73     let x = Some(());
74     let mut y = Some(5i);
75     let mut y2 = 0;
76     for _x in x.iter() {
77         y2 = y.take().unwrap();
78     }
79     assert_eq!(y2, 5);
80     assert!(y.is_none());
81 }
82
83 #[test] #[should_fail]
84 fn test_option_too_much_dance() {
85     let mut y = Some(marker::NoCopy);
86     let _y2 = y.take().unwrap();
87     let _y3 = y.take().unwrap();
88 }
89
90 #[test]
91 fn test_and() {
92     let x: Option<int> = Some(1i);
93     assert_eq!(x.and(Some(2i)), Some(2));
94     assert_eq!(x.and(None::<int>), None);
95
96     let x: Option<int> = None;
97     assert_eq!(x.and(Some(2i)), None);
98     assert_eq!(x.and(None::<int>), None);
99 }
100
101 #[test]
102 fn test_and_then() {
103     let x: Option<int> = Some(1);
104     assert_eq!(x.and_then(|x| Some(x + 1)), Some(2));
105     assert_eq!(x.and_then(|_| None::<int>), None);
106
107     let x: Option<int> = None;
108     assert_eq!(x.and_then(|x| Some(x + 1)), None);
109     assert_eq!(x.and_then(|_| None::<int>), None);
110 }
111
112 #[test]
113 fn test_or() {
114     let x: Option<int> = Some(1);
115     assert_eq!(x.or(Some(2)), Some(1));
116     assert_eq!(x.or(None), Some(1));
117
118     let x: Option<int> = None;
119     assert_eq!(x.or(Some(2)), Some(2));
120     assert_eq!(x.or(None), None);
121 }
122
123 #[test]
124 fn test_or_else() {
125     let x: Option<int> = Some(1);
126     assert_eq!(x.or_else(|| Some(2)), Some(1));
127     assert_eq!(x.or_else(|| None), Some(1));
128
129     let x: Option<int> = None;
130     assert_eq!(x.or_else(|| Some(2)), Some(2));
131     assert_eq!(x.or_else(|| None), None);
132 }
133
134 #[test]
135 fn test_unwrap() {
136     assert_eq!(Some(1i).unwrap(), 1);
137     let s = Some("hello".to_string()).unwrap();
138     assert_eq!(s, "hello");
139 }
140
141 #[test]
142 #[should_fail]
143 fn test_unwrap_panic1() {
144     let x: Option<int> = None;
145     x.unwrap();
146 }
147
148 #[test]
149 #[should_fail]
150 fn test_unwrap_panic2() {
151     let x: Option<String> = None;
152     x.unwrap();
153 }
154
155 #[test]
156 fn test_unwrap_or() {
157     let x: Option<int> = Some(1);
158     assert_eq!(x.unwrap_or(2), 1);
159
160     let x: Option<int> = None;
161     assert_eq!(x.unwrap_or(2), 2);
162 }
163
164 #[test]
165 fn test_unwrap_or_else() {
166     let x: Option<int> = Some(1);
167     assert_eq!(x.unwrap_or_else(|| 2), 1);
168
169     let x: Option<int> = None;
170     assert_eq!(x.unwrap_or_else(|| 2), 2);
171 }
172
173 #[test]
174 fn test_iter() {
175     let val = 5i;
176
177     let x = Some(val);
178     let mut it = x.iter();
179
180     assert_eq!(it.size_hint(), (1, Some(1)));
181     assert_eq!(it.next(), Some(&val));
182     assert_eq!(it.size_hint(), (0, Some(0)));
183     assert!(it.next().is_none());
184 }
185
186 #[test]
187 fn test_mut_iter() {
188     let val = 5i;
189     let new_val = 11i;
190
191     let mut x = Some(val);
192     {
193         let mut it = x.iter_mut();
194
195         assert_eq!(it.size_hint(), (1, Some(1)));
196
197         match it.next() {
198             Some(interior) => {
199                 assert_eq!(*interior, val);
200                 *interior = new_val;
201             }
202             None => assert!(false),
203         }
204
205         assert_eq!(it.size_hint(), (0, Some(0)));
206         assert!(it.next().is_none());
207     }
208     assert_eq!(x, Some(new_val));
209 }
210
211 #[test]
212 fn test_ord() {
213     let small = Some(1.0f64);
214     let big = Some(5.0f64);
215     let nan = Some(0.0f64/0.0);
216     assert!(!(nan < big));
217     assert!(!(nan > big));
218     assert!(small < big);
219     assert!(None < big);
220     assert!(big > None);
221 }
222
223 #[test]
224 fn test_collect() {
225     let v: Option<Vec<int>> = range(0i, 0).map(|_| Some(0i)).collect();
226     assert!(v == Some(vec![]));
227
228     let v: Option<Vec<int>> = range(0i, 3).map(|x| Some(x)).collect();
229     assert!(v == Some(vec![0, 1, 2]));
230
231     let v: Option<Vec<int>> = range(0i, 3).map(|x| {
232         if x > 1 { None } else { Some(x) }
233     }).collect();
234     assert!(v == None);
235
236     // test that it does not take more elements than it needs
237     let mut functions = [|| Some(()), || None, || panic!()];
238
239     let v: Option<Vec<()>> = functions.iter_mut().map(|f| (*f)()).collect();
240
241     assert!(v == None);
242 }
243
244 #[test]
245 fn test_cloned() {
246     let val1 = 1u32;
247     let mut val2 = 2u32;
248     let val1_ref = &val1;
249     let opt_none: Option<&'static u32> = None;
250     let opt_ref = Some(&val1);
251     let opt_ref_ref = Some(&val1_ref);
252     let opt_mut_ref = Some(&mut val2);
253
254     // None works
255     assert_eq!(opt_none.clone(), None);
256     assert_eq!(opt_none.cloned(), None);
257
258     // Mutable refs work
259     assert_eq!(opt_mut_ref.cloned(), Some(2u32));
260
261     // Immutable ref works
262     assert_eq!(opt_ref.clone(), Some(&val1));
263     assert_eq!(opt_ref.cloned(), Some(1u32));
264
265     // Double Immutable ref works
266     assert_eq!(opt_ref_ref.clone(), Some(&val1_ref));
267     assert_eq!(opt_ref_ref.clone().cloned(), Some(&val1));
268     assert_eq!(opt_ref_ref.cloned().cloned(), Some(1u32));
269 }