]> git.proxmox.com Git - rustc.git/blob - src/libcore/tests/nonzero.rs
New upstream version 1.34.2+dfsg1
[rustc.git] / src / libcore / tests / nonzero.rs
1 use core::num::{NonZeroU32, NonZeroI32};
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 assert_eq!(size_of::<NonZeroI32>(), size_of::<Option<NonZeroI32>>());
17 }
18
19 #[test]
20 fn test_match_on_nonzero_option() {
21 let a = Some(unsafe {
22 NonZeroU32::new_unchecked(42)
23 });
24 match a {
25 Some(val) => assert_eq!(val.get(), 42),
26 None => panic!("unexpected None while matching on Some(NonZeroU32(_))")
27 }
28
29 match unsafe { Some(NonZeroU32::new_unchecked(43)) } {
30 Some(val) => assert_eq!(val.get(), 43),
31 None => panic!("unexpected None while matching on Some(NonZeroU32(_))")
32 }
33 }
34
35 #[test]
36 fn test_match_option_empty_vec() {
37 let a: Option<Vec<isize>> = Some(vec![]);
38 match a {
39 None => panic!("unexpected None while matching on Some(vec![])"),
40 _ => {}
41 }
42 }
43
44 #[test]
45 fn test_match_option_vec() {
46 let a = Some(vec![1, 2, 3, 4]);
47 match a {
48 Some(v) => assert_eq!(v, [1, 2, 3, 4]),
49 None => panic!("unexpected None while matching on Some(vec![1, 2, 3, 4])")
50 }
51 }
52
53 #[test]
54 fn test_match_option_rc() {
55 use std::rc::Rc;
56
57 let five = Rc::new(5);
58 match Some(five) {
59 Some(r) => assert_eq!(*r, 5),
60 None => panic!("unexpected None while matching on Some(Rc::new(5))")
61 }
62 }
63
64 #[test]
65 fn test_match_option_arc() {
66 use std::sync::Arc;
67
68 let five = Arc::new(5);
69 match Some(five) {
70 Some(a) => assert_eq!(*a, 5),
71 None => panic!("unexpected None while matching on Some(Arc::new(5))")
72 }
73 }
74
75 #[test]
76 fn test_match_option_empty_string() {
77 let a = Some(String::new());
78 match a {
79 None => panic!("unexpected None while matching on Some(String::new())"),
80 _ => {}
81 }
82 }
83
84 #[test]
85 fn test_match_option_string() {
86 let five = "Five".to_string();
87 match Some(five) {
88 Some(s) => assert_eq!(s, "Five"),
89 None => panic!("unexpected None while matching on Some(String { ... })")
90 }
91 }
92
93 mod atom {
94 use core::num::NonZeroU32;
95
96 #[derive(PartialEq, Eq)]
97 pub struct Atom {
98 index: NonZeroU32, // private
99 }
100 pub const FOO_ATOM: Atom = Atom { index: unsafe { NonZeroU32::new_unchecked(7) } };
101 }
102
103 macro_rules! atom {
104 ("foo") => { atom::FOO_ATOM }
105 }
106
107 #[test]
108 fn test_match_nonzero_const_pattern() {
109 match atom!("foo") {
110 // Using as a pattern is supported by the compiler:
111 atom!("foo") => {}
112 _ => panic!("Expected the const item as a pattern to match.")
113 }
114 }
115
116 #[test]
117 fn test_from_nonzero() {
118 let nz = NonZeroU32::new(1).unwrap();
119 let num: u32 = nz.into();
120 assert_eq!(num, 1u32);
121 }
122
123 #[test]
124 fn test_from_signed_nonzero() {
125 let nz = NonZeroI32::new(1).unwrap();
126 let num: i32 = nz.into();
127 assert_eq!(num, 1i32);
128 }