Skip to content

Add TryFrom<{int}> for NonZero{int} #72717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 25, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added tests for the implementations
poliorcetics committed May 29, 2020

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit d8b51f180a9c6ced4397ce5568fa8ab553a7143e
20 changes: 19 additions & 1 deletion src/libcore/tests/nonzero.rs
Original file line number Diff line number Diff line change
@@ -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(())));
}