1 use core::num::{IntErrorKind, NonZeroI32, NonZeroI8, NonZeroU32, NonZeroU8};
2 use core::option::Option::{self, None, Some};
6 fn test_create_nonzero_instance() {
7 let _a = unsafe { NonZeroU32::new_unchecked(21) };
11 fn test_size_nonzero_in_option() {
12 assert_eq!(size_of::<NonZeroU32>(), size_of::<Option<NonZeroU32>>());
13 assert_eq!(size_of::<NonZeroI32>(), size_of::<Option<NonZeroI32>>());
17 fn test_match_on_nonzero_option() {
18 let a = Some(unsafe { NonZeroU32::new_unchecked(42) });
20 Some(val) => assert_eq!(val.get(), 42),
21 None => panic!("unexpected None while matching on Some(NonZeroU32(_))"),
24 match unsafe { Some(NonZeroU32::new_unchecked(43)) } {
25 Some(val) => assert_eq!(val.get(), 43),
26 None => panic!("unexpected None while matching on Some(NonZeroU32(_))"),
31 fn test_match_option_empty_vec() {
32 let a: Option<Vec<isize>> = Some(vec![]);
34 None => panic!("unexpected None while matching on Some(vec![])"),
40 fn test_match_option_vec() {
41 let a = Some(vec![1, 2, 3, 4]);
43 Some(v) => assert_eq!(v, [1, 2, 3, 4]),
44 None => panic!("unexpected None while matching on Some(vec![1, 2, 3, 4])"),
49 fn test_match_option_rc() {
52 let five = Rc::new(5);
54 Some(r) => assert_eq!(*r, 5),
55 None => panic!("unexpected None while matching on Some(Rc::new(5))"),
60 fn test_match_option_arc() {
63 let five = Arc::new(5);
65 Some(a) => assert_eq!(*a, 5),
66 None => panic!("unexpected None while matching on Some(Arc::new(5))"),
71 fn test_match_option_empty_string() {
72 let a = Some(String::new());
74 None => panic!("unexpected None while matching on Some(String::new())"),
80 fn test_match_option_string() {
81 let five = "Five".to_string();
83 Some(s) => assert_eq!(s, "Five"),
84 None => panic!("unexpected None while matching on Some(String { ... })"),
89 use core::num::NonZeroU32;
91 #[derive(PartialEq, Eq)]
93 index: NonZeroU32, // private
95 pub const FOO_ATOM: Atom = Atom { index: unsafe { NonZeroU32::new_unchecked(7) } };
105 fn test_match_nonzero_const_pattern() {
107 // Using as a pattern is supported by the compiler:
109 _ => panic!("Expected the const item as a pattern to match."),
114 fn test_from_nonzero() {
115 let nz = NonZeroU32::new(1).unwrap();
116 let num: u32 = nz.into();
117 assert_eq!(num, 1u32);
121 fn test_from_signed_nonzero() {
122 let nz = NonZeroI32::new(1).unwrap();
123 let num: i32 = nz.into();
124 assert_eq!(num, 1i32);
129 assert_eq!("123".parse::<NonZeroU8>(), Ok(NonZeroU8::new(123).unwrap()));
130 assert_eq!("0".parse::<NonZeroU8>().err().map(|e| e.kind().clone()), Some(IntErrorKind::Zero));
132 "-1".parse::<NonZeroU8>().err().map(|e| e.kind().clone()),
133 Some(IntErrorKind::InvalidDigit)
136 "-129".parse::<NonZeroI8>().err().map(|e| e.kind().clone()),
137 Some(IntErrorKind::Underflow)
140 "257".parse::<NonZeroU8>().err().map(|e| e.kind().clone()),
141 Some(IntErrorKind::Overflow)