1 use core::convert::TryFrom;
2 use core::num::{IntErrorKind, NonZeroI32, NonZeroI8, NonZeroU32, NonZeroU8};
3 use core::option::Option::{self, None, Some};
7 fn test_create_nonzero_instance() {
8 let _a = unsafe { NonZeroU32::new_unchecked(21) };
12 fn test_size_nonzero_in_option() {
13 assert_eq!(size_of::<NonZeroU32>(), size_of::<Option<NonZeroU32>>());
14 assert_eq!(size_of::<NonZeroI32>(), size_of::<Option<NonZeroI32>>());
18 fn test_match_on_nonzero_option() {
19 let a = Some(unsafe { NonZeroU32::new_unchecked(42) });
21 Some(val) => assert_eq!(val.get(), 42),
22 None => panic!("unexpected None while matching on Some(NonZeroU32(_))"),
25 match unsafe { Some(NonZeroU32::new_unchecked(43)) } {
26 Some(val) => assert_eq!(val.get(), 43),
27 None => panic!("unexpected None while matching on Some(NonZeroU32(_))"),
32 fn test_match_option_empty_vec() {
33 let a: Option<Vec<isize>> = Some(vec![]);
35 None => panic!("unexpected None while matching on Some(vec![])"),
41 fn test_match_option_vec() {
42 let a = Some(vec![1, 2, 3, 4]);
44 Some(v) => assert_eq!(v, [1, 2, 3, 4]),
45 None => panic!("unexpected None while matching on Some(vec![1, 2, 3, 4])"),
50 fn test_match_option_rc() {
53 let five = Rc::new(5);
55 Some(r) => assert_eq!(*r, 5),
56 None => panic!("unexpected None while matching on Some(Rc::new(5))"),
61 fn test_match_option_arc() {
64 let five = Arc::new(5);
66 Some(a) => assert_eq!(*a, 5),
67 None => panic!("unexpected None while matching on Some(Arc::new(5))"),
72 fn test_match_option_empty_string() {
73 let a = Some(String::new());
75 None => panic!("unexpected None while matching on Some(String::new())"),
81 fn test_match_option_string() {
82 let five = "Five".to_string();
84 Some(s) => assert_eq!(s, "Five"),
85 None => panic!("unexpected None while matching on Some(String { ... })"),
90 use core::num::NonZeroU32;
92 #[derive(PartialEq, Eq)]
94 index: NonZeroU32, // private
96 pub const FOO_ATOM: Atom = Atom { index: unsafe { NonZeroU32::new_unchecked(7) } };
106 fn test_match_nonzero_const_pattern() {
108 // Using as a pattern is supported by the compiler:
110 _ => panic!("Expected the const item as a pattern to match."),
115 fn test_from_nonzero() {
116 let nz = NonZeroU32::new(1).unwrap();
117 let num: u32 = nz.into();
118 assert_eq!(num, 1u32);
122 fn test_from_signed_nonzero() {
123 let nz = NonZeroI32::new(1).unwrap();
124 let num: i32 = nz.into();
125 assert_eq!(num, 1i32);
130 assert_eq!("123".parse::<NonZeroU8>(), Ok(NonZeroU8::new(123).unwrap()));
131 assert_eq!("0".parse::<NonZeroU8>().err().map(|e| e.kind().clone()), Some(IntErrorKind::Zero));
133 "-1".parse::<NonZeroU8>().err().map(|e| e.kind().clone()),
134 Some(IntErrorKind::InvalidDigit('-'))
137 "-129".parse::<NonZeroI8>().err().map(|e| e.kind().clone()),
138 Some(IntErrorKind::NegOverflow)
141 "257".parse::<NonZeroU8>().err().map(|e| e.kind().clone()),
142 Some(IntErrorKind::PosOverflow)
147 fn test_nonzero_bitor() {
148 let nz_alt = NonZeroU8::new(0b1010_1010).unwrap();
149 let nz_low = NonZeroU8::new(0b0000_1111).unwrap();
151 let both_nz: NonZeroU8 = nz_alt | nz_low;
152 assert_eq!(both_nz.get(), 0b1010_1111);
154 let rhs_int: NonZeroU8 = nz_low | 0b1100_0000u8;
155 assert_eq!(rhs_int.get(), 0b1100_1111);
157 let rhs_zero: NonZeroU8 = nz_alt | 0u8;
158 assert_eq!(rhs_zero.get(), 0b1010_1010);
160 let lhs_int: NonZeroU8 = 0b0110_0110u8 | nz_alt;
161 assert_eq!(lhs_int.get(), 0b1110_1110);
163 let lhs_zero: NonZeroU8 = 0u8 | nz_low;
164 assert_eq!(lhs_zero.get(), 0b0000_1111);
168 fn test_nonzero_bitor_assign() {
169 let mut target = NonZeroU8::new(0b1010_1010).unwrap();
171 target |= NonZeroU8::new(0b0000_1111).unwrap();
172 assert_eq!(target.get(), 0b1010_1111);
174 target |= 0b0001_0000;
175 assert_eq!(target.get(), 0b1011_1111);
178 assert_eq!(target.get(), 0b1011_1111);
182 fn test_nonzero_from_int_on_success() {
183 assert_eq!(NonZeroU8::try_from(5), Ok(NonZeroU8::new(5).unwrap()));
184 assert_eq!(NonZeroU32::try_from(5), Ok(NonZeroU32::new(5).unwrap()));
186 assert_eq!(NonZeroI8::try_from(-5), Ok(NonZeroI8::new(-5).unwrap()));
187 assert_eq!(NonZeroI32::try_from(-5), Ok(NonZeroI32::new(-5).unwrap()));
191 fn test_nonzero_from_int_on_err() {
192 assert!(NonZeroU8::try_from(0).is_err());
193 assert!(NonZeroU32::try_from(0).is_err());
195 assert!(NonZeroI8::try_from(0).is_err());
196 assert!(NonZeroI32::try_from(0).is_err());
201 // test that the methods of `NonZeroX>` are usable in a const context
202 // Note: only tests NonZero8
204 const NONZERO: NonZeroU8 = unsafe { NonZeroU8::new_unchecked(5) };
206 const GET: u8 = NONZERO.get();
209 const ZERO: Option<NonZeroU8> = NonZeroU8::new(0);
210 assert!(ZERO.is_none());
212 const ONE: Option<NonZeroU8> = NonZeroU8::new(1);
213 assert!(ONE.is_some());