]> git.lizzy.rs Git - rust.git/blob - library/core/tests/nonzero.rs
949d4ea32f06463f7d6ca1b339b5bb1b0967dccb
[rust.git] / library / core / tests / nonzero.rs
1 use core::convert::TryFrom;
2 use core::num::{IntErrorKind, NonZeroI32, NonZeroI8, NonZeroU32, NonZeroU8};
3 use core::option::Option::{self, None, Some};
4 use std::mem::size_of;
5
6 #[test]
7 fn test_create_nonzero_instance() {
8     let _a = unsafe { NonZeroU32::new_unchecked(21) };
9 }
10
11 #[test]
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>>());
15 }
16
17 #[test]
18 fn test_match_on_nonzero_option() {
19     let a = Some(unsafe { NonZeroU32::new_unchecked(42) });
20     match a {
21         Some(val) => assert_eq!(val.get(), 42),
22         None => panic!("unexpected None while matching on Some(NonZeroU32(_))"),
23     }
24
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(_))"),
28     }
29 }
30
31 #[test]
32 fn test_match_option_empty_vec() {
33     let a: Option<Vec<isize>> = Some(vec![]);
34     match a {
35         None => panic!("unexpected None while matching on Some(vec![])"),
36         _ => {}
37     }
38 }
39
40 #[test]
41 fn test_match_option_vec() {
42     let a = Some(vec![1, 2, 3, 4]);
43     match a {
44         Some(v) => assert_eq!(v, [1, 2, 3, 4]),
45         None => panic!("unexpected None while matching on Some(vec![1, 2, 3, 4])"),
46     }
47 }
48
49 #[test]
50 fn test_match_option_rc() {
51     use std::rc::Rc;
52
53     let five = Rc::new(5);
54     match Some(five) {
55         Some(r) => assert_eq!(*r, 5),
56         None => panic!("unexpected None while matching on Some(Rc::new(5))"),
57     }
58 }
59
60 #[test]
61 fn test_match_option_arc() {
62     use std::sync::Arc;
63
64     let five = Arc::new(5);
65     match Some(five) {
66         Some(a) => assert_eq!(*a, 5),
67         None => panic!("unexpected None while matching on Some(Arc::new(5))"),
68     }
69 }
70
71 #[test]
72 fn test_match_option_empty_string() {
73     let a = Some(String::new());
74     match a {
75         None => panic!("unexpected None while matching on Some(String::new())"),
76         _ => {}
77     }
78 }
79
80 #[test]
81 fn test_match_option_string() {
82     let five = "Five".to_string();
83     match Some(five) {
84         Some(s) => assert_eq!(s, "Five"),
85         None => panic!("unexpected None while matching on Some(String { ... })"),
86     }
87 }
88
89 mod atom {
90     use core::num::NonZeroU32;
91
92     #[derive(PartialEq, Eq)]
93     pub struct Atom {
94         index: NonZeroU32, // private
95     }
96     pub const FOO_ATOM: Atom = Atom { index: unsafe { NonZeroU32::new_unchecked(7) } };
97 }
98
99 macro_rules! atom {
100     ("foo") => {
101         atom::FOO_ATOM
102     };
103 }
104
105 #[test]
106 fn test_match_nonzero_const_pattern() {
107     match atom!("foo") {
108         // Using as a pattern is supported by the compiler:
109         atom!("foo") => {}
110         _ => panic!("Expected the const item as a pattern to match."),
111     }
112 }
113
114 #[test]
115 fn test_from_nonzero() {
116     let nz = NonZeroU32::new(1).unwrap();
117     let num: u32 = nz.into();
118     assert_eq!(num, 1u32);
119 }
120
121 #[test]
122 fn test_from_signed_nonzero() {
123     let nz = NonZeroI32::new(1).unwrap();
124     let num: i32 = nz.into();
125     assert_eq!(num, 1i32);
126 }
127
128 #[test]
129 fn test_from_str() {
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));
132     assert_eq!(
133         "-1".parse::<NonZeroU8>().err().map(|e| e.kind().clone()),
134         Some(IntErrorKind::InvalidDigit('-'))
135     );
136     assert_eq!(
137         "-129".parse::<NonZeroI8>().err().map(|e| e.kind().clone()),
138         Some(IntErrorKind::NegOverflow)
139     );
140     assert_eq!(
141         "257".parse::<NonZeroU8>().err().map(|e| e.kind().clone()),
142         Some(IntErrorKind::PosOverflow)
143     );
144 }
145
146 #[test]
147 fn test_nonzero_bitor() {
148     let nz_alt = NonZeroU8::new(0b1010_1010).unwrap();
149     let nz_low = NonZeroU8::new(0b0000_1111).unwrap();
150
151     let both_nz: NonZeroU8 = nz_alt | nz_low;
152     assert_eq!(both_nz.get(), 0b1010_1111);
153
154     let rhs_int: NonZeroU8 = nz_low | 0b1100_0000u8;
155     assert_eq!(rhs_int.get(), 0b1100_1111);
156
157     let rhs_zero: NonZeroU8 = nz_alt | 0u8;
158     assert_eq!(rhs_zero.get(), 0b1010_1010);
159
160     let lhs_int: NonZeroU8 = 0b0110_0110u8 | nz_alt;
161     assert_eq!(lhs_int.get(), 0b1110_1110);
162
163     let lhs_zero: NonZeroU8 = 0u8 | nz_low;
164     assert_eq!(lhs_zero.get(), 0b0000_1111);
165 }
166
167 #[test]
168 fn test_nonzero_bitor_assign() {
169     let mut target = NonZeroU8::new(0b1010_1010).unwrap();
170
171     target |= NonZeroU8::new(0b0000_1111).unwrap();
172     assert_eq!(target.get(), 0b1010_1111);
173
174     target |= 0b0001_0000;
175     assert_eq!(target.get(), 0b1011_1111);
176
177     target |= 0;
178     assert_eq!(target.get(), 0b1011_1111);
179 }
180
181 #[test]
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()));
185
186     assert_eq!(NonZeroI8::try_from(-5), Ok(NonZeroI8::new(-5).unwrap()));
187     assert_eq!(NonZeroI32::try_from(-5), Ok(NonZeroI32::new(-5).unwrap()));
188 }
189
190 #[test]
191 fn test_nonzero_from_int_on_err() {
192     assert!(NonZeroU8::try_from(0).is_err());
193     assert!(NonZeroU32::try_from(0).is_err());
194
195     assert!(NonZeroI8::try_from(0).is_err());
196     assert!(NonZeroI32::try_from(0).is_err());
197 }
198
199 #[test]
200 fn nonzero_const() {
201     // test that the methods of `NonZeroX>` are usable in a const context
202     // Note: only tests NonZero8
203
204     const NONZERO: NonZeroU8 = unsafe { NonZeroU8::new_unchecked(5) };
205
206     const GET: u8 = NONZERO.get();
207     assert_eq!(GET, 5);
208
209     const ZERO: Option<NonZeroU8> = NonZeroU8::new(0);
210     assert!(ZERO.is_none());
211
212     const ONE: Option<NonZeroU8> = NonZeroU8::new(1);
213     assert!(ONE.is_some());
214 }