Skip to content

Commit 0e039ad

Browse files
committed
libcoretest: Add tests for NonZero.
1 parent 94f1261 commit 0e039ad

File tree

3 files changed

+101
-38
lines changed

3 files changed

+101
-38
lines changed

src/libcore/nonzero.rs

-38
Original file line numberDiff line numberDiff line change
@@ -51,41 +51,3 @@ impl<T: Zeroable> Deref<T> for NonZero<T> {
5151
inner
5252
}
5353
}
54-
55-
#[cfg(test)]
56-
mod test {
57-
use super::NonZero;
58-
59-
#[test]
60-
fn test_create_nonzero_instance() {
61-
let _a = unsafe {
62-
NonZero::new(21)
63-
};
64-
}
65-
66-
#[test]
67-
fn test_size_nonzero_in_option() {
68-
use mem::size_of;
69-
use option::Option;
70-
71-
assert_eq!(size_of::<NonZero<u32>>(), size_of::<Option<NonZero<u32>>>());
72-
}
73-
74-
#[test]
75-
fn test_match_on_nonzero_option() {
76-
use option::Some;
77-
78-
let a = Some(unsafe {
79-
NonZero::new(42)
80-
});
81-
match a {
82-
Some(val) => assert_eq!(*val, 42),
83-
None => panic!("unexpected None while matching on Some(NonZero(_))")
84-
}
85-
86-
match unsafe { NonZero::new(43) } {
87-
Some(val) => assert_eq!(*val, 43),
88-
None => panic!("unexpected None while matching on Some(NonZero(_))")
89-
}
90-
}
91-
}

src/libcoretest/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ mod fmt;
2525
mod hash;
2626
mod iter;
2727
mod mem;
28+
mod nonzero;
2829
mod num;
2930
mod ops;
3031
mod option;

src/libcoretest/nonzero.rs

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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::nonzero::NonZero;
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+
NonZero::new(21i)
20+
};
21+
}
22+
23+
#[test]
24+
fn test_size_nonzero_in_option() {
25+
assert_eq!(size_of::<NonZero<u32>>(), size_of::<Option<NonZero<u32>>>());
26+
}
27+
28+
#[test]
29+
fn test_match_on_nonzero_option() {
30+
let a = Some(unsafe {
31+
NonZero::new(42i)
32+
});
33+
match a {
34+
Some(val) => assert_eq!(*val, 42),
35+
None => panic!("unexpected None while matching on Some(NonZero(_))")
36+
}
37+
38+
match unsafe { Some(NonZero::new(43i)) } {
39+
Some(val) => assert_eq!(*val, 43),
40+
None => panic!("unexpected None while matching on Some(NonZero(_))")
41+
}
42+
}
43+
44+
#[test]
45+
fn test_match_option_empty_vec() {
46+
let a: Option<Vec<int>> = 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![1i, 2, 3, 4]);
56+
match a {
57+
Some(v) => assert_eq!(v, vec![1i, 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(5i);
67+
match Some(five) {
68+
Some(r) => assert_eq!(*r, 5i),
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(5i);
78+
match Some(five) {
79+
Some(a) => assert_eq!(*a, 5i),
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".into_string();
96+
match Some(five) {
97+
Some(s) => assert_eq!(s, "Five"),
98+
None => panic!("unexpected None while matching on Some(String { ... })")
99+
}
100+
}

0 commit comments

Comments
 (0)