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