|
| 1 | +// Copyright 2012-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::nonzero::NonZero; |
| 12 | +use core::option::Option; |
| 13 | +use core::option::Option::{Some, None}; |
| 14 | +use std::mem::size_of; |
| 15 | + |
| 16 | +#[test] |
| 17 | +fn test_create_nonzero_instance() { |
| 18 | + let _a = unsafe { |
| 19 | + NonZero::new(21i) |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +#[test] |
| 24 | +fn test_size_nonzero_in_option() { |
| 25 | + assert_eq!(size_of::<NonZero<u32>>(), size_of::<Option<NonZero<u32>>>()); |
| 26 | +} |
| 27 | + |
| 28 | +#[test] |
| 29 | +fn test_match_on_nonzero_option() { |
| 30 | + let a = Some(unsafe { |
| 31 | + NonZero::new(42i) |
| 32 | + }); |
| 33 | + match a { |
| 34 | + Some(val) => assert_eq!(*val, 42), |
| 35 | + None => panic!("unexpected None while matching on Some(NonZero(_))") |
| 36 | + } |
| 37 | + |
| 38 | + match unsafe { Some(NonZero::new(43i)) } { |
| 39 | + Some(val) => assert_eq!(*val, 43), |
| 40 | + None => panic!("unexpected None while matching on Some(NonZero(_))") |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +#[test] |
| 45 | +fn test_match_option_empty_vec() { |
| 46 | + let a: Option<Vec<int>> = Some(vec![]); |
| 47 | + match a { |
| 48 | + None => panic!("unexpected None while matching on Some(vec![])"), |
| 49 | + _ => {} |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +#[test] |
| 54 | +fn test_match_option_vec() { |
| 55 | + let a = Some(vec![1i, 2, 3, 4]); |
| 56 | + match a { |
| 57 | + Some(v) => assert_eq!(v, vec![1i, 2, 3, 4]), |
| 58 | + None => panic!("unexpected None while matching on Some(vec![1, 2, 3, 4])") |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +#[test] |
| 63 | +fn test_match_option_rc() { |
| 64 | + use std::rc::Rc; |
| 65 | + |
| 66 | + let five = Rc::new(5i); |
| 67 | + match Some(five) { |
| 68 | + Some(r) => assert_eq!(*r, 5i), |
| 69 | + None => panic!("unexpected None while matching on Some(Rc::new(5))") |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +#[test] |
| 74 | +fn test_match_option_arc() { |
| 75 | + use std::sync::Arc; |
| 76 | + |
| 77 | + let five = Arc::new(5i); |
| 78 | + match Some(five) { |
| 79 | + Some(a) => assert_eq!(*a, 5i), |
| 80 | + None => panic!("unexpected None while matching on Some(Arc::new(5))") |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +#[test] |
| 85 | +fn test_match_option_empty_string() { |
| 86 | + let a = Some(String::new()); |
| 87 | + match a { |
| 88 | + None => panic!("unexpected None while matching on Some(String::new())"), |
| 89 | + _ => {} |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +#[test] |
| 94 | +fn test_match_option_string() { |
| 95 | + let five = "Five".into_string(); |
| 96 | + match Some(five) { |
| 97 | + Some(s) => assert_eq!(s, "Five"), |
| 98 | + None => panic!("unexpected None while matching on Some(String { ... })") |
| 99 | + } |
| 100 | +} |
0 commit comments