]> git.lizzy.rs Git - rust.git/commitdiff
Added tests for the implementations
authorAlexis Bourget <alexis.bourget@gmail.com>
Fri, 29 May 2020 01:05:25 +0000 (03:05 +0200)
committerAlexis Bourget <alexis.bourget@gmail.com>
Fri, 29 May 2020 01:05:25 +0000 (03:05 +0200)
src/libcore/tests/nonzero.rs

index 0227a66b8633a704137287e33ca8486b01dcade7..2500099853237d1c044a772e84f65ba93f56b421 100644 (file)
@@ -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(())));
+}