Skip to content

Commit d8b51f1

Browse files
committed
Added tests for the implementations
1 parent 2031e99 commit d8b51f1

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/libcore/tests/nonzero.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::num::{IntErrorKind, NonZeroI32, NonZeroI8, NonZeroU32, NonZeroU8};
1+
use core::num::{TryFromIntError, IntErrorKind, NonZeroI32, NonZeroI8, NonZeroU32, NonZeroU8};
22
use core::option::Option::{self, None, Some};
33
use std::mem::size_of;
44

@@ -176,3 +176,21 @@ fn test_nonzero_bitor_assign() {
176176
target |= 0;
177177
assert_eq!(target.get(), 0b1011_1111);
178178
}
179+
180+
#[test]
181+
fn test_nonzero_from_int_on_success() {
182+
assert_eq!(NonZeroU8::try_from(5), Ok(NonZeroU8::new(5)));
183+
assert_eq!(NonZeroU32::try_from(5), Ok(NonZeroU32::new(5)));
184+
185+
assert_eq!(NonZeroI8::try_from(-5), Ok(NonZeroI8::new(-5)));
186+
assert_eq!(NonZeroI32::try_from(-5), Ok(NonZeroI32::new(-5)));
187+
}
188+
189+
#[test]
190+
fn test_nonzero_from_int_on_err() {
191+
assert_eq!(NonZeroU8::try_from(0), Err(TryFromIntError(())));
192+
assert_eq!(NonZeroU32::try_from(0), Err(TryFromIntError(())));
193+
194+
assert_eq!(NonZeroI8::try_from(0), Err(TryFromIntError(())));
195+
assert_eq!(NonZeroI32::try_from(0), Err(TryFromIntError(())));
196+
}

0 commit comments

Comments
 (0)