1 use core::num::{IntErrorKind, NonZeroI32, NonZeroI8, NonZeroU32, NonZeroU8};
2 use core::option::Option::{self, None, Some};
6 fn test_create_nonzero_instance() {
8 NonZeroU32::new_unchecked(21)
13 fn test_size_nonzero_in_option() {
14 assert_eq!(size_of::<NonZeroU32>(), size_of::<Option<NonZeroU32>>());
15 assert_eq!(size_of::<NonZeroI32>(), size_of::<Option<NonZeroI32>>());
19 fn test_match_on_nonzero_option() {
21 NonZeroU32::new_unchecked(42)
24 Some(val) => assert_eq!(val.get(), 42),
25 None => panic!("unexpected None while matching on Some(NonZeroU32(_))")
28 match unsafe { Some(NonZeroU32::new_unchecked(43)) } {
29 Some(val) => assert_eq!(val.get(), 43),
30 None => panic!("unexpected None while matching on Some(NonZeroU32(_))")
35 fn test_match_option_empty_vec() {
36 let a: Option<Vec<isize>> = Some(vec![]);
38 None => panic!("unexpected None while matching on Some(vec![])"),
44 fn test_match_option_vec() {
45 let a = Some(vec![1, 2, 3, 4]);
47 Some(v) => assert_eq!(v, [1, 2, 3, 4]),
48 None => panic!("unexpected None while matching on Some(vec![1, 2, 3, 4])")
53 fn test_match_option_rc() {
56 let five = Rc::new(5);
58 Some(r) => assert_eq!(*r, 5),
59 None => panic!("unexpected None while matching on Some(Rc::new(5))")
64 fn test_match_option_arc() {
67 let five = Arc::new(5);
69 Some(a) => assert_eq!(*a, 5),
70 None => panic!("unexpected None while matching on Some(Arc::new(5))")
75 fn test_match_option_empty_string() {
76 let a = Some(String::new());
78 None => panic!("unexpected None while matching on Some(String::new())"),
84 fn test_match_option_string() {
85 let five = "Five".to_string();
87 Some(s) => assert_eq!(s, "Five"),
88 None => panic!("unexpected None while matching on Some(String { ... })")
93 use core::num::NonZeroU32;
95 #[derive(PartialEq, Eq)]
97 index: NonZeroU32, // private
99 pub const FOO_ATOM: Atom = Atom { index: unsafe { NonZeroU32::new_unchecked(7) } };
103 ("foo") => { atom::FOO_ATOM }
107 fn test_match_nonzero_const_pattern() {
109 // Using as a pattern is supported by the compiler:
111 _ => panic!("Expected the const item as a pattern to match.")
116 fn test_from_nonzero() {
117 let nz = NonZeroU32::new(1).unwrap();
118 let num: u32 = nz.into();
119 assert_eq!(num, 1u32);
123 fn test_from_signed_nonzero() {
124 let nz = NonZeroI32::new(1).unwrap();
125 let num: i32 = nz.into();
126 assert_eq!(num, 1i32);
131 assert_eq!("123".parse::<NonZeroU8>(), Ok(NonZeroU8::new(123).unwrap()));
133 "0".parse::<NonZeroU8>().err().map(|e| e.kind().clone()),
134 Some(IntErrorKind::Zero)
137 "-1".parse::<NonZeroU8>().err().map(|e| e.kind().clone()),
138 Some(IntErrorKind::InvalidDigit)
141 "-129".parse::<NonZeroI8>().err().map(|e| e.kind().clone()),
142 Some(IntErrorKind::Underflow)
145 "257".parse::<NonZeroU8>().err().map(|e| e.kind().clone()),
146 Some(IntErrorKind::Overflow)