]> git.lizzy.rs Git - rust.git/blob - src/libcore/tests/nonzero.rs
Auto merge of #54734 - pawroman:fix_range_borrowing_suggestion, r=varkor
[rust.git] / src / libcore / tests / nonzero.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use core::num::NonZeroU32;
12 use core::option::Option;
13 use core::option::Option::{Some, None};
14 use std::mem::size_of;
15
16 #[test]
17 fn test_create_nonzero_instance() {
18     let _a = unsafe {
19         NonZeroU32::new_unchecked(21)
20     };
21 }
22
23 #[test]
24 fn test_size_nonzero_in_option() {
25     assert_eq!(size_of::<NonZeroU32>(), size_of::<Option<NonZeroU32>>());
26 }
27
28 #[test]
29 fn test_match_on_nonzero_option() {
30     let a = Some(unsafe {
31         NonZeroU32::new_unchecked(42)
32     });
33     match a {
34         Some(val) => assert_eq!(val.get(), 42),
35         None => panic!("unexpected None while matching on Some(NonZeroU32(_))")
36     }
37
38     match unsafe { Some(NonZeroU32::new_unchecked(43)) } {
39         Some(val) => assert_eq!(val.get(), 43),
40         None => panic!("unexpected None while matching on Some(NonZeroU32(_))")
41     }
42 }
43
44 #[test]
45 fn test_match_option_empty_vec() {
46     let a: Option<Vec<isize>> = Some(vec![]);
47     match a {
48         None => panic!("unexpected None while matching on Some(vec![])"),
49         _ => {}
50     }
51 }
52
53 #[test]
54 fn test_match_option_vec() {
55     let a = Some(vec![1, 2, 3, 4]);
56     match a {
57         Some(v) => assert_eq!(v, [1, 2, 3, 4]),
58         None => panic!("unexpected None while matching on Some(vec![1, 2, 3, 4])")
59     }
60 }
61
62 #[test]
63 fn test_match_option_rc() {
64     use std::rc::Rc;
65
66     let five = Rc::new(5);
67     match Some(five) {
68         Some(r) => assert_eq!(*r, 5),
69         None => panic!("unexpected None while matching on Some(Rc::new(5))")
70     }
71 }
72
73 #[test]
74 fn test_match_option_arc() {
75     use std::sync::Arc;
76
77     let five = Arc::new(5);
78     match Some(five) {
79         Some(a) => assert_eq!(*a, 5),
80         None => panic!("unexpected None while matching on Some(Arc::new(5))")
81     }
82 }
83
84 #[test]
85 fn test_match_option_empty_string() {
86     let a = Some(String::new());
87     match a {
88         None => panic!("unexpected None while matching on Some(String::new())"),
89         _ => {}
90     }
91 }
92
93 #[test]
94 fn test_match_option_string() {
95     let five = "Five".to_string();
96     match Some(five) {
97         Some(s) => assert_eq!(s, "Five"),
98         None => panic!("unexpected None while matching on Some(String { ... })")
99     }
100 }
101
102 mod atom {
103     use core::num::NonZeroU32;
104
105     #[derive(PartialEq, Eq)]
106     pub struct Atom {
107         index: NonZeroU32, // private
108     }
109     pub const FOO_ATOM: Atom = Atom { index: unsafe { NonZeroU32::new_unchecked(7) } };
110 }
111
112 macro_rules! atom {
113     ("foo") => { atom::FOO_ATOM }
114 }
115
116 #[test]
117 fn test_match_nonzero_const_pattern() {
118     match atom!("foo") {
119         // Using as a pattern is supported by the compiler:
120         atom!("foo") => {}
121         _ => panic!("Expected the const item as a pattern to match.")
122     }
123 }
124
125 #[test]
126 fn test_from_nonzero() {
127     let nz = NonZeroU32::new(1).unwrap();
128     let num: u32 = nz.into();
129     assert_eq!(num, 1u32);
130 }