]> git.lizzy.rs Git - rust.git/blob - src/libcore/tests/nonzero.rs
c813bf20cb61ae5712b5326002f4b70ede260389
[rust.git] / src / libcore / tests / nonzero.rs
1 use core::num::NonZeroU32;
2 use core::option::Option;
3 use core::option::Option::{Some, None};
4 use std::mem::size_of;
5
6 #[test]
7 fn test_create_nonzero_instance() {
8     let _a = unsafe {
9         NonZeroU32::new_unchecked(21)
10     };
11 }
12
13 #[test]
14 fn test_size_nonzero_in_option() {
15     assert_eq!(size_of::<NonZeroU32>(), size_of::<Option<NonZeroU32>>());
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 }