2019-01-09 18:37:38 +00:00
|
|
|
use core::num::{NonZeroU32, NonZeroI32};
|
2014-12-23 20:52:02 +00:00
|
|
|
use core::option::Option;
|
|
|
|
use core::option::Option::{Some, None};
|
|
|
|
use std::mem::size_of;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_create_nonzero_instance() {
|
|
|
|
let _a = unsafe {
|
2018-02-16 10:02:06 +00:00
|
|
|
NonZeroU32::new_unchecked(21)
|
2014-12-23 20:52:02 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_size_nonzero_in_option() {
|
2018-02-16 10:02:06 +00:00
|
|
|
assert_eq!(size_of::<NonZeroU32>(), size_of::<Option<NonZeroU32>>());
|
2019-01-09 18:37:38 +00:00
|
|
|
assert_eq!(size_of::<NonZeroI32>(), size_of::<Option<NonZeroI32>>());
|
2014-12-23 20:52:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_match_on_nonzero_option() {
|
|
|
|
let a = Some(unsafe {
|
2018-02-16 10:02:06 +00:00
|
|
|
NonZeroU32::new_unchecked(42)
|
2014-12-23 20:52:02 +00:00
|
|
|
});
|
|
|
|
match a {
|
2017-04-04 16:31:38 +00:00
|
|
|
Some(val) => assert_eq!(val.get(), 42),
|
2018-02-16 10:02:06 +00:00
|
|
|
None => panic!("unexpected None while matching on Some(NonZeroU32(_))")
|
2014-12-23 20:52:02 +00:00
|
|
|
}
|
|
|
|
|
2018-02-16 10:02:06 +00:00
|
|
|
match unsafe { Some(NonZeroU32::new_unchecked(43)) } {
|
2017-04-04 16:31:38 +00:00
|
|
|
Some(val) => assert_eq!(val.get(), 43),
|
2018-02-16 10:02:06 +00:00
|
|
|
None => panic!("unexpected None while matching on Some(NonZeroU32(_))")
|
2014-12-23 20:52:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_match_option_empty_vec() {
|
2015-03-26 00:06:52 +00:00
|
|
|
let a: Option<Vec<isize>> = Some(vec![]);
|
2014-12-23 20:52:02 +00:00
|
|
|
match a {
|
|
|
|
None => panic!("unexpected None while matching on Some(vec![])"),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_match_option_vec() {
|
2015-01-25 21:05:03 +00:00
|
|
|
let a = Some(vec![1, 2, 3, 4]);
|
2014-12-23 20:52:02 +00:00
|
|
|
match a {
|
2015-02-24 18:15:45 +00:00
|
|
|
Some(v) => assert_eq!(v, [1, 2, 3, 4]),
|
2014-12-23 20:52:02 +00:00
|
|
|
None => panic!("unexpected None while matching on Some(vec![1, 2, 3, 4])")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_match_option_rc() {
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2015-01-25 21:05:03 +00:00
|
|
|
let five = Rc::new(5);
|
2014-12-23 20:52:02 +00:00
|
|
|
match Some(five) {
|
2015-01-25 21:05:03 +00:00
|
|
|
Some(r) => assert_eq!(*r, 5),
|
2014-12-23 20:52:02 +00:00
|
|
|
None => panic!("unexpected None while matching on Some(Rc::new(5))")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_match_option_arc() {
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2015-01-25 21:05:03 +00:00
|
|
|
let five = Arc::new(5);
|
2014-12-23 20:52:02 +00:00
|
|
|
match Some(five) {
|
2015-01-25 21:05:03 +00:00
|
|
|
Some(a) => assert_eq!(*a, 5),
|
2014-12-23 20:52:02 +00:00
|
|
|
None => panic!("unexpected None while matching on Some(Arc::new(5))")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_match_option_empty_string() {
|
|
|
|
let a = Some(String::new());
|
|
|
|
match a {
|
|
|
|
None => panic!("unexpected None while matching on Some(String::new())"),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_match_option_string() {
|
2015-01-02 07:53:35 +00:00
|
|
|
let five = "Five".to_string();
|
2014-12-23 20:52:02 +00:00
|
|
|
match Some(five) {
|
|
|
|
Some(s) => assert_eq!(s, "Five"),
|
|
|
|
None => panic!("unexpected None while matching on Some(String { ... })")
|
|
|
|
}
|
|
|
|
}
|
2018-02-16 18:28:13 +00:00
|
|
|
|
|
|
|
mod atom {
|
|
|
|
use core::num::NonZeroU32;
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq)]
|
|
|
|
pub struct Atom {
|
|
|
|
index: NonZeroU32, // private
|
|
|
|
}
|
|
|
|
pub const FOO_ATOM: Atom = Atom { index: unsafe { NonZeroU32::new_unchecked(7) } };
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! atom {
|
|
|
|
("foo") => { atom::FOO_ATOM }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_match_nonzero_const_pattern() {
|
|
|
|
match atom!("foo") {
|
|
|
|
// Using as a pattern is supported by the compiler:
|
|
|
|
atom!("foo") => {}
|
|
|
|
_ => panic!("Expected the const item as a pattern to match.")
|
|
|
|
}
|
|
|
|
}
|
2018-09-17 08:35:08 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from_nonzero() {
|
|
|
|
let nz = NonZeroU32::new(1).unwrap();
|
|
|
|
let num: u32 = nz.into();
|
|
|
|
assert_eq!(num, 1u32);
|
|
|
|
}
|
2019-01-09 18:37:38 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from_signed_nonzero() {
|
|
|
|
let nz = NonZeroI32::new(1).unwrap();
|
|
|
|
let num: i32 = nz.into();
|
|
|
|
assert_eq!(num, 1i32);
|
|
|
|
}
|
2019-02-25 06:09:16 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from_str() {
|
2019-02-27 17:37:35 +00:00
|
|
|
assert_eq!("123".parse::<NonZeroU8>(), Ok(NonZeroU8::new(123).unwrap()));
|
2019-02-25 06:09:16 +00:00
|
|
|
assert_eq!(
|
2019-02-27 17:37:35 +00:00
|
|
|
"0".parse::<NonZeroU8>(),
|
|
|
|
Err(ParseIntError {
|
|
|
|
kind: IntErrorKind::Zero
|
2019-02-25 06:09:16 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2019-02-27 17:37:35 +00:00
|
|
|
"-1".parse::<NonZeroU8>(),
|
|
|
|
Err(ParseIntError {
|
|
|
|
kind: IntErrorKind::Underflow
|
|
|
|
})
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
"129".parse::<NonZeroU8>(),
|
|
|
|
Err(ParseIntError {
|
|
|
|
kind: IntErrorKind::Overflow
|
2019-02-25 06:09:16 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|