]>
git.proxmox.com Git - rustc.git/blob - 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.
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.
14 use core
::clone
::Clone
;
19 let x
: Box
<_
> = box 0;
20 let addr_x
: *const isize = mem
::transmute(&*x
);
23 let addr_y
: *const isize = mem
::transmute(&*y
);
24 assert_eq
!(addr_x
, addr_y
);
30 let x
= "test".to_string();
31 let addr_x
= x
.as_ptr();
34 let addr_y
= y
.as_ptr();
35 assert_eq
!(addr_x
, addr_y
);
39 fn test_get_resource() {
41 use core
::cell
::RefCell
;
44 i
: Rc
<RefCell
<isize>>,
51 *ii
.borrow_mut() = i
+ 1;
55 fn r(i
: Rc
<RefCell
<isize>>) -> R
{
61 let i
= Rc
::new(RefCell
::new(0));
65 let _y
= opt
.unwrap();
67 assert_eq
!(*i
.borrow(), 1);
71 fn test_option_dance() {
76 y2
= y
.take().unwrap();
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();
91 let x
: Option
<isize> = Some(1);
92 assert_eq
!(x
.and(Some(2)), Some(2));
93 assert_eq
!(x
.and(None
::<isize>), None
);
95 let x
: Option
<isize> = None
;
96 assert_eq
!(x
.and(Some(2)), None
);
97 assert_eq
!(x
.and(None
::<isize>), None
);
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
);
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
);
113 let x
: Option
<isize> = Some(1);
114 assert_eq
!(x
.or(Some(2)), Some(1));
115 assert_eq
!(x
.or(None
), Some(1));
117 let x
: Option
<isize> = None
;
118 assert_eq
!(x
.or(Some(2)), Some(2));
119 assert_eq
!(x
.or(None
), None
);
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));
128 let x
: Option
<isize> = None
;
129 assert_eq
!(x
.or_else(|| Some(2)), Some(2));
130 assert_eq
!(x
.or_else(|| None
), None
);
135 assert_eq
!(Some(1).unwrap(), 1);
136 let s
= Some("hello".to_string()).unwrap();
137 assert_eq
!(s
, "hello");
142 fn test_unwrap_panic1() {
143 let x
: Option
<isize> = None
;
149 fn test_unwrap_panic2() {
150 let x
: Option
<String
> = None
;
155 fn test_unwrap_or() {
156 let x
: Option
<isize> = Some(1);
157 assert_eq
!(x
.unwrap_or(2), 1);
159 let x
: Option
<isize> = None
;
160 assert_eq
!(x
.unwrap_or(2), 2);
164 fn test_unwrap_or_else() {
165 let x
: Option
<isize> = Some(1);
166 assert_eq
!(x
.unwrap_or_else(|| 2), 1);
168 let x
: Option
<isize> = None
;
169 assert_eq
!(x
.unwrap_or_else(|| 2), 2);
177 let mut it
= x
.iter();
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());
190 let mut x
= Some(val
);
192 let mut it
= x
.iter_mut();
194 assert_eq
!(it
.size_hint(), (1, Some(1)));
198 assert_eq
!(*interior
, val
);
201 None
=> assert
!(false),
204 assert_eq
!(it
.size_hint(), (0, Some(0)));
205 assert
!(it
.next().is_none());
207 assert_eq
!(x
, Some(new_val
));
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
);
224 let v
: Option
<Vec
<isize>> = (0..0).map(|_
| Some(0)).collect();
225 assert
!(v
== Some(vec
![]));
227 let v
: Option
<Vec
<isize>> = (0..3).map(|x
| Some(x
)).collect();
228 assert
!(v
== Some(vec
![0, 1, 2]));
230 let v
: Option
<Vec
<isize>> = (0..3).map(|x
| {
231 if x
> 1 { None }
else { Some(x) }
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
!()];
239 let v
: Option
<Vec
<()>> = functions
.iter_mut().map(|f
| (*f
)()).collect();
249 let opt_none
: Option
<&'
static u32> = None
;
250 let opt_ref
= Some(&val
);
251 let opt_ref_ref
= Some(&val_ref
);
254 assert_eq
!(opt_none
.clone(), None
);
255 assert_eq
!(opt_none
.cloned(), None
);
257 // Immutable ref works
258 assert_eq
!(opt_ref
.clone(), Some(&val
));
259 assert_eq
!(opt_ref
.cloned(), Some(1u32));
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));