]> git.lizzy.rs Git - rust.git/blob - src/libcore/tests/nonzero.rs
Rollup merge of #68301 - tmandry:dont-propagate-test-invoke, r=alexcrichton
[rust.git] / src / libcore / tests / nonzero.rs
1 use core::num::{IntErrorKind, NonZeroI32, NonZeroI8, NonZeroU32, NonZeroU8};
2 use core::option::Option::{self, None, Some};
3 use std::mem::size_of;
4
5 #[test]
6 fn test_create_nonzero_instance() {
7     let _a = unsafe { NonZeroU32::new_unchecked(21) };
8 }
9
10 #[test]
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>>());
14 }
15
16 #[test]
17 fn test_match_on_nonzero_option() {
18     let a = Some(unsafe { NonZeroU32::new_unchecked(42) });
19     match a {
20         Some(val) => assert_eq!(val.get(), 42),
21         None => panic!("unexpected None while matching on Some(NonZeroU32(_))"),
22     }
23
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(_))"),
27     }
28 }
29
30 #[test]
31 fn test_match_option_empty_vec() {
32     let a: Option<Vec<isize>> = Some(vec![]);
33     match a {
34         None => panic!("unexpected None while matching on Some(vec![])"),
35         _ => {}
36     }
37 }
38
39 #[test]
40 fn test_match_option_vec() {
41     let a = Some(vec![1, 2, 3, 4]);
42     match a {
43         Some(v) => assert_eq!(v, [1, 2, 3, 4]),
44         None => panic!("unexpected None while matching on Some(vec![1, 2, 3, 4])"),
45     }
46 }
47
48 #[test]
49 fn test_match_option_rc() {
50     use std::rc::Rc;
51
52     let five = Rc::new(5);
53     match Some(five) {
54         Some(r) => assert_eq!(*r, 5),
55         None => panic!("unexpected None while matching on Some(Rc::new(5))"),
56     }
57 }
58
59 #[test]
60 fn test_match_option_arc() {
61     use std::sync::Arc;
62
63     let five = Arc::new(5);
64     match Some(five) {
65         Some(a) => assert_eq!(*a, 5),
66         None => panic!("unexpected None while matching on Some(Arc::new(5))"),
67     }
68 }
69
70 #[test]
71 fn test_match_option_empty_string() {
72     let a = Some(String::new());
73     match a {
74         None => panic!("unexpected None while matching on Some(String::new())"),
75         _ => {}
76     }
77 }
78
79 #[test]
80 fn test_match_option_string() {
81     let five = "Five".to_string();
82     match Some(five) {
83         Some(s) => assert_eq!(s, "Five"),
84         None => panic!("unexpected None while matching on Some(String { ... })"),
85     }
86 }
87
88 mod atom {
89     use core::num::NonZeroU32;
90
91     #[derive(PartialEq, Eq)]
92     pub struct Atom {
93         index: NonZeroU32, // private
94     }
95     pub const FOO_ATOM: Atom = Atom { index: unsafe { NonZeroU32::new_unchecked(7) } };
96 }
97
98 macro_rules! atom {
99     ("foo") => {
100         atom::FOO_ATOM
101     };
102 }
103
104 #[test]
105 fn test_match_nonzero_const_pattern() {
106     match atom!("foo") {
107         // Using as a pattern is supported by the compiler:
108         atom!("foo") => {}
109         _ => panic!("Expected the const item as a pattern to match."),
110     }
111 }
112
113 #[test]
114 fn test_from_nonzero() {
115     let nz = NonZeroU32::new(1).unwrap();
116     let num: u32 = nz.into();
117     assert_eq!(num, 1u32);
118 }
119
120 #[test]
121 fn test_from_signed_nonzero() {
122     let nz = NonZeroI32::new(1).unwrap();
123     let num: i32 = nz.into();
124     assert_eq!(num, 1i32);
125 }
126
127 #[test]
128 fn test_from_str() {
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));
131     assert_eq!(
132         "-1".parse::<NonZeroU8>().err().map(|e| e.kind().clone()),
133         Some(IntErrorKind::InvalidDigit)
134     );
135     assert_eq!(
136         "-129".parse::<NonZeroI8>().err().map(|e| e.kind().clone()),
137         Some(IntErrorKind::Underflow)
138     );
139     assert_eq!(
140         "257".parse::<NonZeroU8>().err().map(|e| e.kind().clone()),
141         Some(IntErrorKind::Overflow)
142     );
143 }