diff options
| author | Alexis Bourget <alexis.bourget@gmail.com> | 2020-05-29 03:05:25 +0200 | 
|---|---|---|
| committer | Alexis Bourget <alexis.bourget@gmail.com> | 2020-05-29 03:05:25 +0200 | 
| commit | d8b51f180a9c6ced4397ce5568fa8ab553a7143e (patch) | |
| tree | 9f9ca2c219468aa006f483641e68516f8798c8f7 | |
| parent | 2031e99d772091854d716df5bdf227f8be9b6397 (diff) | |
| download | rust-d8b51f180a9c6ced4397ce5568fa8ab553a7143e.tar.gz rust-d8b51f180a9c6ced4397ce5568fa8ab553a7143e.zip  | |
Added tests for the implementations
| -rw-r--r-- | src/libcore/tests/nonzero.rs | 20 | 
1 files changed, 19 insertions, 1 deletions
diff --git a/src/libcore/tests/nonzero.rs b/src/libcore/tests/nonzero.rs index 0227a66b863..25000998532 100644 --- a/src/libcore/tests/nonzero.rs +++ b/src/libcore/tests/nonzero.rs @@ -1,4 +1,4 @@ -use core::num::{IntErrorKind, NonZeroI32, NonZeroI8, NonZeroU32, NonZeroU8}; +use core::num::{TryFromIntError, IntErrorKind, NonZeroI32, NonZeroI8, NonZeroU32, NonZeroU8}; use core::option::Option::{self, None, Some}; use std::mem::size_of; @@ -176,3 +176,21 @@ fn test_nonzero_bitor_assign() { target |= 0; assert_eq!(target.get(), 0b1011_1111); } + +#[test] +fn test_nonzero_from_int_on_success() { + assert_eq!(NonZeroU8::try_from(5), Ok(NonZeroU8::new(5))); + assert_eq!(NonZeroU32::try_from(5), Ok(NonZeroU32::new(5))); + + assert_eq!(NonZeroI8::try_from(-5), Ok(NonZeroI8::new(-5))); + assert_eq!(NonZeroI32::try_from(-5), Ok(NonZeroI32::new(-5))); +} + +#[test] +fn test_nonzero_from_int_on_err() { + assert_eq!(NonZeroU8::try_from(0), Err(TryFromIntError(()))); + assert_eq!(NonZeroU32::try_from(0), Err(TryFromIntError(()))); + + assert_eq!(NonZeroI8::try_from(0), Err(TryFromIntError(()))); + assert_eq!(NonZeroI32::try_from(0), Err(TryFromIntError(()))); +}  | 
