2014-06-28 15:57:36 -05:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use core::option::*;
|
|
|
|
use core::mem;
|
2014-11-07 13:05:50 -06:00
|
|
|
use core::clone::Clone;
|
2014-06-28 15:57:36 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_ptr() {
|
|
|
|
unsafe {
|
2015-02-17 14:41:32 -06:00
|
|
|
let x: Box<_> = box 0;
|
2015-03-25 19:06:52 -05:00
|
|
|
let addr_x: *const isize = mem::transmute(&*x);
|
2014-06-28 15:57:36 -05:00
|
|
|
let opt = Some(x);
|
|
|
|
let y = opt.unwrap();
|
2015-03-25 19:06:52 -05:00
|
|
|
let addr_y: *const isize = mem::transmute(&*y);
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(addr_x, addr_y);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_str() {
|
|
|
|
let x = "test".to_string();
|
2014-11-27 12:12:30 -06:00
|
|
|
let addr_x = x.as_ptr();
|
2014-06-28 15:57:36 -05:00
|
|
|
let opt = Some(x);
|
|
|
|
let y = opt.unwrap();
|
2014-11-27 12:12:30 -06:00
|
|
|
let addr_y = y.as_ptr();
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(addr_x, addr_y);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_get_resource() {
|
|
|
|
use std::rc::Rc;
|
|
|
|
use core::cell::RefCell;
|
|
|
|
|
|
|
|
struct R {
|
2015-03-25 19:06:52 -05:00
|
|
|
i: Rc<RefCell<isize>>,
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
2015-04-27 16:10:49 -05:00
|
|
|
impl Drop for R {
|
2014-06-28 15:57:36 -05:00
|
|
|
fn drop(&mut self) {
|
|
|
|
let ii = &*self.i;
|
|
|
|
let i = *ii.borrow();
|
|
|
|
*ii.borrow_mut() = i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
fn r(i: Rc<RefCell<isize>>) -> R {
|
2014-06-28 15:57:36 -05:00
|
|
|
R {
|
2017-08-07 00:54:09 -05:00
|
|
|
i,
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
let i = Rc::new(RefCell::new(0));
|
2014-06-28 15:57:36 -05:00
|
|
|
{
|
|
|
|
let x = r(i.clone());
|
|
|
|
let opt = Some(x);
|
|
|
|
let _y = opt.unwrap();
|
|
|
|
}
|
|
|
|
assert_eq!(*i.borrow(), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_option_dance() {
|
|
|
|
let x = Some(());
|
2015-01-25 15:05:03 -06:00
|
|
|
let mut y = Some(5);
|
2014-06-28 15:57:36 -05:00
|
|
|
let mut y2 = 0;
|
2015-06-10 11:22:20 -05:00
|
|
|
for _x in x {
|
2014-08-18 19:52:38 -05:00
|
|
|
y2 = y.take().unwrap();
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
assert_eq!(y2, 5);
|
|
|
|
assert!(y.is_none());
|
|
|
|
}
|
|
|
|
|
2015-01-31 17:08:25 -06:00
|
|
|
#[test] #[should_panic]
|
2014-06-28 15:57:36 -05:00
|
|
|
fn test_option_too_much_dance() {
|
2015-06-10 15:33:52 -05:00
|
|
|
struct A;
|
|
|
|
let mut y = Some(A);
|
2014-08-18 19:52:38 -05:00
|
|
|
let _y2 = y.take().unwrap();
|
|
|
|
let _y3 = y.take().unwrap();
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_and() {
|
2015-03-25 19:06:52 -05:00
|
|
|
let x: Option<isize> = Some(1);
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(x.and(Some(2)), Some(2));
|
2015-03-25 19:06:52 -05:00
|
|
|
assert_eq!(x.and(None::<isize>), None);
|
2014-06-28 15:57:36 -05:00
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
let x: Option<isize> = None;
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(x.and(Some(2)), None);
|
2015-03-25 19:06:52 -05:00
|
|
|
assert_eq!(x.and(None::<isize>), None);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_and_then() {
|
2015-03-25 19:06:52 -05:00
|
|
|
let x: Option<isize> = Some(1);
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(x.and_then(|x| Some(x + 1)), Some(2));
|
2015-03-25 19:06:52 -05:00
|
|
|
assert_eq!(x.and_then(|_| None::<isize>), None);
|
2014-06-28 15:57:36 -05:00
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
let x: Option<isize> = None;
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(x.and_then(|x| Some(x + 1)), None);
|
2015-03-25 19:06:52 -05:00
|
|
|
assert_eq!(x.and_then(|_| None::<isize>), None);
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_or() {
|
2015-03-25 19:06:52 -05:00
|
|
|
let x: Option<isize> = Some(1);
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(x.or(Some(2)), Some(1));
|
|
|
|
assert_eq!(x.or(None), Some(1));
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
let x: Option<isize> = None;
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(x.or(Some(2)), Some(2));
|
|
|
|
assert_eq!(x.or(None), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_or_else() {
|
2015-03-25 19:06:52 -05:00
|
|
|
let x: Option<isize> = Some(1);
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(x.or_else(|| Some(2)), Some(1));
|
|
|
|
assert_eq!(x.or_else(|| None), Some(1));
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
let x: Option<isize> = None;
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(x.or_else(|| Some(2)), Some(2));
|
|
|
|
assert_eq!(x.or_else(|| None), None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_unwrap() {
|
2015-01-25 15:05:03 -06:00
|
|
|
assert_eq!(Some(1).unwrap(), 1);
|
2014-06-28 15:57:36 -05:00
|
|
|
let s = Some("hello".to_string()).unwrap();
|
2014-11-27 12:12:30 -06:00
|
|
|
assert_eq!(s, "hello");
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-01-31 17:08:25 -06:00
|
|
|
#[should_panic]
|
2014-10-09 14:17:22 -05:00
|
|
|
fn test_unwrap_panic1() {
|
2015-03-25 19:06:52 -05:00
|
|
|
let x: Option<isize> = None;
|
2014-06-28 15:57:36 -05:00
|
|
|
x.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-01-31 17:08:25 -06:00
|
|
|
#[should_panic]
|
2014-10-09 14:17:22 -05:00
|
|
|
fn test_unwrap_panic2() {
|
2014-06-28 15:57:36 -05:00
|
|
|
let x: Option<String> = None;
|
|
|
|
x.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_unwrap_or() {
|
2015-03-25 19:06:52 -05:00
|
|
|
let x: Option<isize> = Some(1);
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(x.unwrap_or(2), 1);
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
let x: Option<isize> = None;
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(x.unwrap_or(2), 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_unwrap_or_else() {
|
2015-03-25 19:06:52 -05:00
|
|
|
let x: Option<isize> = Some(1);
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(x.unwrap_or_else(|| 2), 1);
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
let x: Option<isize> = None;
|
2014-06-28 15:57:36 -05:00
|
|
|
assert_eq!(x.unwrap_or_else(|| 2), 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_iter() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let val = 5;
|
2014-06-28 15:57:36 -05:00
|
|
|
|
|
|
|
let x = Some(val);
|
|
|
|
let mut it = x.iter();
|
|
|
|
|
|
|
|
assert_eq!(it.size_hint(), (1, Some(1)));
|
|
|
|
assert_eq!(it.next(), Some(&val));
|
|
|
|
assert_eq!(it.size_hint(), (0, Some(0)));
|
|
|
|
assert!(it.next().is_none());
|
2015-08-27 01:37:40 -05:00
|
|
|
|
|
|
|
let mut it = (&x).into_iter();
|
|
|
|
assert_eq!(it.next(), Some(&val));
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_mut_iter() {
|
2015-08-27 01:37:40 -05:00
|
|
|
let mut val = 5;
|
2015-01-25 15:05:03 -06:00
|
|
|
let new_val = 11;
|
2014-06-28 15:57:36 -05:00
|
|
|
|
|
|
|
let mut x = Some(val);
|
|
|
|
{
|
2014-09-14 22:27:36 -05:00
|
|
|
let mut it = x.iter_mut();
|
2014-06-28 15:57:36 -05:00
|
|
|
|
|
|
|
assert_eq!(it.size_hint(), (1, Some(1)));
|
|
|
|
|
|
|
|
match it.next() {
|
|
|
|
Some(interior) => {
|
|
|
|
assert_eq!(*interior, val);
|
|
|
|
*interior = new_val;
|
|
|
|
}
|
|
|
|
None => assert!(false),
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(it.size_hint(), (0, Some(0)));
|
|
|
|
assert!(it.next().is_none());
|
|
|
|
}
|
|
|
|
assert_eq!(x, Some(new_val));
|
2015-08-27 01:37:40 -05:00
|
|
|
|
|
|
|
let mut y = Some(val);
|
|
|
|
let mut it = (&mut y).into_iter();
|
|
|
|
assert_eq!(it.next(), Some(&mut val));
|
2014-06-28 15:57:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_ord() {
|
|
|
|
let small = Some(1.0f64);
|
|
|
|
let big = Some(5.0f64);
|
|
|
|
let nan = Some(0.0f64/0.0);
|
|
|
|
assert!(!(nan < big));
|
|
|
|
assert!(!(nan > big));
|
|
|
|
assert!(small < big);
|
|
|
|
assert!(None < big);
|
|
|
|
assert!(big > None);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_collect() {
|
2015-03-25 19:06:52 -05:00
|
|
|
let v: Option<Vec<isize>> = (0..0).map(|_| Some(0)).collect();
|
2014-06-28 15:57:36 -05:00
|
|
|
assert!(v == Some(vec![]));
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
let v: Option<Vec<isize>> = (0..3).map(|x| Some(x)).collect();
|
2014-06-28 15:57:36 -05:00
|
|
|
assert!(v == Some(vec![0, 1, 2]));
|
|
|
|
|
2015-03-25 19:06:52 -05:00
|
|
|
let v: Option<Vec<isize>> = (0..3).map(|x| {
|
2014-10-15 01:05:01 -05:00
|
|
|
if x > 1 { None } else { Some(x) }
|
|
|
|
}).collect();
|
2014-06-28 15:57:36 -05:00
|
|
|
assert!(v == None);
|
|
|
|
|
|
|
|
// test that it does not take more elements than it needs
|
2015-01-04 22:34:23 -06:00
|
|
|
let mut functions: [Box<Fn() -> Option<()>>; 3] =
|
|
|
|
[box || Some(()), box || None, box || panic!()];
|
2014-06-28 15:57:36 -05:00
|
|
|
|
2014-10-15 01:05:01 -05:00
|
|
|
let v: Option<Vec<()>> = functions.iter_mut().map(|f| (*f)()).collect();
|
2014-06-28 15:57:36 -05:00
|
|
|
|
|
|
|
assert!(v == None);
|
|
|
|
}
|
2015-04-28 23:23:28 -05:00
|
|
|
|
2014-11-07 13:05:50 -06:00
|
|
|
|
2014-11-17 19:59:46 -06:00
|
|
|
#[test]
|
2014-11-07 13:05:50 -06:00
|
|
|
fn test_cloned() {
|
2016-03-10 21:06:36 -06:00
|
|
|
let val = 1;
|
2015-04-28 23:22:06 -05:00
|
|
|
let val_ref = &val;
|
2014-11-17 19:59:46 -06:00
|
|
|
let opt_none: Option<&'static u32> = None;
|
2015-04-28 23:22:06 -05:00
|
|
|
let opt_ref = Some(&val);
|
|
|
|
let opt_ref_ref = Some(&val_ref);
|
2014-11-17 19:59:46 -06:00
|
|
|
|
|
|
|
// None works
|
|
|
|
assert_eq!(opt_none.clone(), None);
|
|
|
|
assert_eq!(opt_none.cloned(), None);
|
|
|
|
|
|
|
|
// Immutable ref works
|
2015-04-28 23:22:06 -05:00
|
|
|
assert_eq!(opt_ref.clone(), Some(&val));
|
2016-03-10 21:06:36 -06:00
|
|
|
assert_eq!(opt_ref.cloned(), Some(1));
|
2014-11-17 19:59:46 -06:00
|
|
|
|
|
|
|
// Double Immutable ref works
|
2015-04-28 23:22:06 -05:00
|
|
|
assert_eq!(opt_ref_ref.clone(), Some(&val_ref));
|
|
|
|
assert_eq!(opt_ref_ref.clone().cloned(), Some(&val));
|
2016-03-10 21:06:36 -06:00
|
|
|
assert_eq!(opt_ref_ref.cloned().cloned(), Some(1));
|
2014-11-27 12:12:30 -06:00
|
|
|
}
|