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.
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.
11 use core::num::NonZeroU32;
12 use core::option::Option;
13 use core::option::Option::{Some, None};
14 use std::mem::size_of;
17 fn test_create_nonzero_instance() {
19 NonZeroU32::new_unchecked(21)
24 fn test_size_nonzero_in_option() {
25 assert_eq!(size_of::<NonZeroU32>(), size_of::<Option<NonZeroU32>>());
29 fn test_match_on_nonzero_option() {
31 NonZeroU32::new_unchecked(42)
34 Some(val) => assert_eq!(val.get(), 42),
35 None => panic!("unexpected None while matching on Some(NonZeroU32(_))")
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(_))")
45 fn test_match_option_empty_vec() {
46 let a: Option<Vec<isize>> = Some(vec![]);
48 None => panic!("unexpected None while matching on Some(vec![])"),
54 fn test_match_option_vec() {
55 let a = Some(vec![1, 2, 3, 4]);
57 Some(v) => assert_eq!(v, [1, 2, 3, 4]),
58 None => panic!("unexpected None while matching on Some(vec![1, 2, 3, 4])")
63 fn test_match_option_rc() {
66 let five = Rc::new(5);
68 Some(r) => assert_eq!(*r, 5),
69 None => panic!("unexpected None while matching on Some(Rc::new(5))")
74 fn test_match_option_arc() {
77 let five = Arc::new(5);
79 Some(a) => assert_eq!(*a, 5),
80 None => panic!("unexpected None while matching on Some(Arc::new(5))")
85 fn test_match_option_empty_string() {
86 let a = Some(String::new());
88 None => panic!("unexpected None while matching on Some(String::new())"),
94 fn test_match_option_string() {
95 let five = "Five".to_string();
97 Some(s) => assert_eq!(s, "Five"),
98 None => panic!("unexpected None while matching on Some(String { ... })")
103 use core::num::NonZeroU32;
105 #[derive(PartialEq, Eq)]
107 index: NonZeroU32, // private
109 pub const FOO_ATOM: Atom = Atom { index: unsafe { NonZeroU32::new_unchecked(7) } };
113 ("foo") => { atom::FOO_ATOM }
117 fn test_match_nonzero_const_pattern() {
119 // Using as a pattern is supported by the compiler:
121 _ => panic!("Expected the const item as a pattern to match.")
126 fn test_from_nonzero() {
127 let nz = NonZeroU32::new(1).unwrap();
128 let num: u32 = nz.into();
129 assert_eq!(num, 1u32);