2020-06-08 10:48:36 -05:00
|
|
|
use core::convert::TryFrom;
|
2020-06-08 12:17:05 -05:00
|
|
|
use core::num::{IntErrorKind, NonZeroI32, NonZeroI8, NonZeroU32, NonZeroU8};
|
2019-02-28 13:48:08 -06:00
|
|
|
use core::option::Option::{self, None, Some};
|
2014-12-23 14:52:02 -06:00
|
|
|
use std::mem::size_of;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_create_nonzero_instance() {
|
2019-12-06 22:18:12 -06:00
|
|
|
let _a = unsafe { NonZeroU32::new_unchecked(21) };
|
2014-12-23 14:52:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_size_nonzero_in_option() {
|
2018-02-16 04:02:06 -06:00
|
|
|
assert_eq!(size_of::<NonZeroU32>(), size_of::<Option<NonZeroU32>>());
|
2019-01-09 12:37:38 -06:00
|
|
|
assert_eq!(size_of::<NonZeroI32>(), size_of::<Option<NonZeroI32>>());
|
2014-12-23 14:52:02 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_match_on_nonzero_option() {
|
2019-12-06 22:18:12 -06:00
|
|
|
let a = Some(unsafe { NonZeroU32::new_unchecked(42) });
|
2014-12-23 14:52:02 -06:00
|
|
|
match a {
|
2017-04-04 11:31:38 -05:00
|
|
|
Some(val) => assert_eq!(val.get(), 42),
|
2019-12-06 22:18:12 -06:00
|
|
|
None => panic!("unexpected None while matching on Some(NonZeroU32(_))"),
|
2014-12-23 14:52:02 -06:00
|
|
|
}
|
|
|
|
|
2018-02-16 04:02:06 -06:00
|
|
|
match unsafe { Some(NonZeroU32::new_unchecked(43)) } {
|
2017-04-04 11:31:38 -05:00
|
|
|
Some(val) => assert_eq!(val.get(), 43),
|
2019-12-06 22:18:12 -06:00
|
|
|
None => panic!("unexpected None while matching on Some(NonZeroU32(_))"),
|
2014-12-23 14:52:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_match_option_empty_vec() {
|
2015-03-25 19:06:52 -05:00
|
|
|
let a: Option<Vec<isize>> = Some(vec![]);
|
2014-12-23 14:52:02 -06:00
|
|
|
match a {
|
|
|
|
None => panic!("unexpected None while matching on Some(vec![])"),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_match_option_vec() {
|
2015-01-25 15:05:03 -06:00
|
|
|
let a = Some(vec![1, 2, 3, 4]);
|
2014-12-23 14:52:02 -06:00
|
|
|
match a {
|
2015-02-24 12:15:45 -06:00
|
|
|
Some(v) => assert_eq!(v, [1, 2, 3, 4]),
|
2019-12-06 22:18:12 -06:00
|
|
|
None => panic!("unexpected None while matching on Some(vec![1, 2, 3, 4])"),
|
2014-12-23 14:52:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_match_option_rc() {
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
let five = Rc::new(5);
|
2014-12-23 14:52:02 -06:00
|
|
|
match Some(five) {
|
2015-01-25 15:05:03 -06:00
|
|
|
Some(r) => assert_eq!(*r, 5),
|
2019-12-06 22:18:12 -06:00
|
|
|
None => panic!("unexpected None while matching on Some(Rc::new(5))"),
|
2014-12-23 14:52:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_match_option_arc() {
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
2015-01-25 15:05:03 -06:00
|
|
|
let five = Arc::new(5);
|
2014-12-23 14:52:02 -06:00
|
|
|
match Some(five) {
|
2015-01-25 15:05:03 -06:00
|
|
|
Some(a) => assert_eq!(*a, 5),
|
2019-12-06 22:18:12 -06:00
|
|
|
None => panic!("unexpected None while matching on Some(Arc::new(5))"),
|
2014-12-23 14:52:02 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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 01:53:35 -06:00
|
|
|
let five = "Five".to_string();
|
2014-12-23 14:52:02 -06:00
|
|
|
match Some(five) {
|
|
|
|
Some(s) => assert_eq!(s, "Five"),
|
2019-12-06 22:18:12 -06:00
|
|
|
None => panic!("unexpected None while matching on Some(String { ... })"),
|
2014-12-23 14:52:02 -06:00
|
|
|
}
|
|
|
|
}
|
2018-02-16 12:28:13 -06: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 {
|
2019-12-06 22:18:12 -06:00
|
|
|
("foo") => {
|
|
|
|
atom::FOO_ATOM
|
|
|
|
};
|
2018-02-16 12:28:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_match_nonzero_const_pattern() {
|
|
|
|
match atom!("foo") {
|
|
|
|
// Using as a pattern is supported by the compiler:
|
|
|
|
atom!("foo") => {}
|
2019-12-06 22:18:12 -06:00
|
|
|
_ => panic!("Expected the const item as a pattern to match."),
|
2018-02-16 12:28:13 -06:00
|
|
|
}
|
|
|
|
}
|
2018-09-17 03:35:08 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from_nonzero() {
|
|
|
|
let nz = NonZeroU32::new(1).unwrap();
|
|
|
|
let num: u32 = nz.into();
|
|
|
|
assert_eq!(num, 1u32);
|
|
|
|
}
|
2019-01-09 12:37:38 -06: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 00:09:16 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_from_str() {
|
2019-02-27 11:37:35 -06:00
|
|
|
assert_eq!("123".parse::<NonZeroU8>(), Ok(NonZeroU8::new(123).unwrap()));
|
2019-12-06 22:18:12 -06:00
|
|
|
assert_eq!("0".parse::<NonZeroU8>().err().map(|e| e.kind().clone()), Some(IntErrorKind::Zero));
|
2019-02-25 00:09:16 -06:00
|
|
|
assert_eq!(
|
2019-02-28 13:48:08 -06:00
|
|
|
"-1".parse::<NonZeroU8>().err().map(|e| e.kind().clone()),
|
|
|
|
Some(IntErrorKind::InvalidDigit)
|
2019-02-27 11:37:35 -06:00
|
|
|
);
|
|
|
|
assert_eq!(
|
2019-02-28 13:48:08 -06:00
|
|
|
"-129".parse::<NonZeroI8>().err().map(|e| e.kind().clone()),
|
|
|
|
Some(IntErrorKind::Underflow)
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
"257".parse::<NonZeroU8>().err().map(|e| e.kind().clone()),
|
|
|
|
Some(IntErrorKind::Overflow)
|
2019-02-25 00:09:16 -06:00
|
|
|
);
|
|
|
|
}
|
2020-03-07 18:14:10 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nonzero_bitor() {
|
|
|
|
let nz_alt = NonZeroU8::new(0b1010_1010).unwrap();
|
|
|
|
let nz_low = NonZeroU8::new(0b0000_1111).unwrap();
|
|
|
|
|
|
|
|
let both_nz: NonZeroU8 = nz_alt | nz_low;
|
|
|
|
assert_eq!(both_nz.get(), 0b1010_1111);
|
|
|
|
|
|
|
|
let rhs_int: NonZeroU8 = nz_low | 0b1100_0000u8;
|
|
|
|
assert_eq!(rhs_int.get(), 0b1100_1111);
|
|
|
|
|
|
|
|
let rhs_zero: NonZeroU8 = nz_alt | 0u8;
|
|
|
|
assert_eq!(rhs_zero.get(), 0b1010_1010);
|
|
|
|
|
|
|
|
let lhs_int: NonZeroU8 = 0b0110_0110u8 | nz_alt;
|
|
|
|
assert_eq!(lhs_int.get(), 0b1110_1110);
|
|
|
|
|
|
|
|
let lhs_zero: NonZeroU8 = 0u8 | nz_low;
|
|
|
|
assert_eq!(lhs_zero.get(), 0b0000_1111);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nonzero_bitor_assign() {
|
|
|
|
let mut target = NonZeroU8::new(0b1010_1010).unwrap();
|
|
|
|
|
|
|
|
target |= NonZeroU8::new(0b0000_1111).unwrap();
|
|
|
|
assert_eq!(target.get(), 0b1010_1111);
|
|
|
|
|
|
|
|
target |= 0b0001_0000;
|
|
|
|
assert_eq!(target.get(), 0b1011_1111);
|
|
|
|
|
|
|
|
target |= 0;
|
|
|
|
assert_eq!(target.get(), 0b1011_1111);
|
|
|
|
}
|
2020-05-28 20:05:25 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nonzero_from_int_on_success() {
|
2020-06-08 12:17:05 -05:00
|
|
|
assert_eq!(NonZeroU8::try_from(5), Ok(NonZeroU8::new(5).unwrap()));
|
|
|
|
assert_eq!(NonZeroU32::try_from(5), Ok(NonZeroU32::new(5).unwrap()));
|
2020-05-28 20:05:25 -05:00
|
|
|
|
2020-06-08 12:17:05 -05:00
|
|
|
assert_eq!(NonZeroI8::try_from(-5), Ok(NonZeroI8::new(-5).unwrap()));
|
|
|
|
assert_eq!(NonZeroI32::try_from(-5), Ok(NonZeroI32::new(-5).unwrap()));
|
2020-05-28 20:05:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_nonzero_from_int_on_err() {
|
2020-06-08 12:17:05 -05:00
|
|
|
assert!(NonZeroU8::try_from(0).is_err());
|
|
|
|
assert!(NonZeroU32::try_from(0).is_err());
|
2020-05-28 20:05:25 -05:00
|
|
|
|
2020-06-08 12:17:05 -05:00
|
|
|
assert!(NonZeroI8::try_from(0).is_err());
|
|
|
|
assert!(NonZeroI32::try_from(0).is_err());
|
2020-05-28 20:05:25 -05:00
|
|
|
}
|
2020-09-03 19:35:27 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn nonzero_const() {
|
|
|
|
// test that the methods of `NonZeroX>` are usable in a const context
|
|
|
|
// Note: only tests NonZero8
|
|
|
|
|
|
|
|
const NONZERO: NonZeroU8 = unsafe { NonZeroU8::new_unchecked(5) };
|
|
|
|
|
|
|
|
const GET: u8 = NONZERO.get();
|
|
|
|
assert_eq!(GET, 5);
|
|
|
|
|
|
|
|
const ZERO: Option<NonZeroU8> = NonZeroU8::new(0);
|
|
|
|
assert!(ZERO.is_none());
|
|
|
|
|
|
|
|
const ONE: Option<NonZeroU8> = NonZeroU8::new(1);
|
|
|
|
assert!(ONE.is_some());
|
|
|
|
}
|