From 0e039ada55bab9b94ceb3fabff409790e37635c6 Mon Sep 17 00:00:00 2001 From: Luqman Aden Date: Tue, 23 Dec 2014 15:52:02 -0500 Subject: [PATCH] libcoretest: Add tests for NonZero. --- src/libcore/nonzero.rs | 38 -------------- src/libcoretest/lib.rs | 1 + src/libcoretest/nonzero.rs | 100 +++++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+), 38 deletions(-) create mode 100644 src/libcoretest/nonzero.rs diff --git a/src/libcore/nonzero.rs b/src/libcore/nonzero.rs index 131627329462e..c429e4b8212bc 100644 --- a/src/libcore/nonzero.rs +++ b/src/libcore/nonzero.rs @@ -51,41 +51,3 @@ impl Deref for NonZero { inner } } - -#[cfg(test)] -mod test { - use super::NonZero; - - #[test] - fn test_create_nonzero_instance() { - let _a = unsafe { - NonZero::new(21) - }; - } - - #[test] - fn test_size_nonzero_in_option() { - use mem::size_of; - use option::Option; - - assert_eq!(size_of::>(), size_of::>>()); - } - - #[test] - fn test_match_on_nonzero_option() { - use option::Some; - - let a = Some(unsafe { - NonZero::new(42) - }); - match a { - Some(val) => assert_eq!(*val, 42), - None => panic!("unexpected None while matching on Some(NonZero(_))") - } - - match unsafe { NonZero::new(43) } { - Some(val) => assert_eq!(*val, 43), - None => panic!("unexpected None while matching on Some(NonZero(_))") - } - } -} diff --git a/src/libcoretest/lib.rs b/src/libcoretest/lib.rs index 44029ebb7fa0f..e6608eee3ddfa 100644 --- a/src/libcoretest/lib.rs +++ b/src/libcoretest/lib.rs @@ -25,6 +25,7 @@ mod fmt; mod hash; mod iter; mod mem; +mod nonzero; mod num; mod ops; mod option; diff --git a/src/libcoretest/nonzero.rs b/src/libcoretest/nonzero.rs new file mode 100644 index 0000000000000..ed66be3d890dc --- /dev/null +++ b/src/libcoretest/nonzero.rs @@ -0,0 +1,100 @@ +// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use core::nonzero::NonZero; +use core::option::Option; +use core::option::Option::{Some, None}; +use std::mem::size_of; + +#[test] +fn test_create_nonzero_instance() { + let _a = unsafe { + NonZero::new(21i) + }; +} + +#[test] +fn test_size_nonzero_in_option() { + assert_eq!(size_of::>(), size_of::>>()); +} + +#[test] +fn test_match_on_nonzero_option() { + let a = Some(unsafe { + NonZero::new(42i) + }); + match a { + Some(val) => assert_eq!(*val, 42), + None => panic!("unexpected None while matching on Some(NonZero(_))") + } + + match unsafe { Some(NonZero::new(43i)) } { + Some(val) => assert_eq!(*val, 43), + None => panic!("unexpected None while matching on Some(NonZero(_))") + } +} + +#[test] +fn test_match_option_empty_vec() { + let a: Option> = Some(vec![]); + match a { + None => panic!("unexpected None while matching on Some(vec![])"), + _ => {} + } +} + +#[test] +fn test_match_option_vec() { + let a = Some(vec![1i, 2, 3, 4]); + match a { + Some(v) => assert_eq!(v, vec![1i, 2, 3, 4]), + None => panic!("unexpected None while matching on Some(vec![1, 2, 3, 4])") + } +} + +#[test] +fn test_match_option_rc() { + use std::rc::Rc; + + let five = Rc::new(5i); + match Some(five) { + Some(r) => assert_eq!(*r, 5i), + None => panic!("unexpected None while matching on Some(Rc::new(5))") + } +} + +#[test] +fn test_match_option_arc() { + use std::sync::Arc; + + let five = Arc::new(5i); + match Some(five) { + Some(a) => assert_eq!(*a, 5i), + None => panic!("unexpected None while matching on Some(Arc::new(5))") + } +} + +#[test] +fn test_match_option_empty_string() { + let a = Some(String::new()); + match a { + None => panic!("unexpected None while matching on Some(String::new())"), + _ => {} + } +} + +#[test] +fn test_match_option_string() { + let five = "Five".into_string(); + match Some(five) { + Some(s) => assert_eq!(s, "Five"), + None => panic!("unexpected None while matching on Some(String { ... })") + } +}