diff --git a/.github/workflows/test-step.yml b/.github/workflows/test-step.yml index f39a94d..dac443d 100644 --- a/.github/workflows/test-step.yml +++ b/.github/workflows/test-step.yml @@ -16,4 +16,8 @@ jobs: - uses: actions-rs/cargo@v1 with: command: test - args: --no-default-features --features step_trait \ No newline at end of file + args: --no-default-features --features step_trait + - uses: actions-rs/cargo@v1 + with: + command: test + args: --no-default-features --features step_trait,hint \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index c3fb502..fe75280 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1254,7 +1254,7 @@ where T: Copy + Step, { #[inline] - fn steps_between(start: &Self, end: &Self) -> Option { + fn steps_between(start: &Self, end: &Self) -> (usize, Option) { Step::steps_between(&start.value(), &end.value()) } diff --git a/tests/tests.rs b/tests/tests.rs index e674586..245e135 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1821,21 +1821,39 @@ fn rotate_right() { #[cfg(feature = "step_trait")] #[test] fn range_agrees_with_underlying() { - compare_range(u19::MIN, u19::MAX); - compare_range(u37::new(95_993), u37::new(1_994_910)); - compare_range(u68::new(58_858_348), u68::new(58_860_000)); - compare_range(u122::new(111_222_333_444), u122::new(111_222_444_555)); - compare_range(u5::MIN, u5::MAX); - compare_range(u23::MIN, u23::MAX); - compare_range(u48::new(999_444), u48::new(1_005_000)); - compare_range(u99::new(12345), u99::new(54321)); - - fn compare_range(arb_start: UInt, arb_end: UInt) + compare_range_32(u19::MIN, u19::MAX); + compare_range_64(u37::new(95_993), u37::new(1_994_910)); + compare_range_128(u68::new(58_858_348), u68::new(58_860_000)); + compare_range_128(u122::new(111_222_333_444), u122::new(111_222_444_555)); + compare_range_32(u23::MIN, u23::MAX); + compare_range_64(u48::new(999_444), u48::new(1_005_000)); + compare_range_128(u99::new(12345), u99::new(54321)); + + // with the `hint` feature enabled, ::value only exist with primitive types, not on all + // implementations. This makes some copy & paste necessary here. + fn compare_range_32(arb_start: UInt, arb_end: UInt) where - T: Copy + Step, - UInt: Step, + UInt: Step, { - let arbint_range = (arb_start..=arb_end).map(UInt::value); + let arbint_range = (arb_start..=arb_end).map(UInt::::value); + let underlying_range = arb_start.value()..=arb_end.value(); + + assert!(arbint_range.eq(underlying_range)); + } + fn compare_range_64(arb_start: UInt, arb_end: UInt) + where + UInt: Step, + { + let arbint_range = (arb_start..=arb_end).map(UInt::::value); + let underlying_range = arb_start.value()..=arb_end.value(); + + assert!(arbint_range.eq(underlying_range)); + } + fn compare_range_128(arb_start: UInt, arb_end: UInt) + where + UInt: Step, + { + let arbint_range = (arb_start..=arb_end).map(UInt::::value); let underlying_range = arb_start.value()..=arb_end.value(); assert!(arbint_range.eq(underlying_range)); @@ -1870,18 +1888,24 @@ fn backward_checked() { #[cfg(feature = "step_trait")] #[test] fn steps_between() { - assert_eq!(Some(0), Step::steps_between(&u50::new(50), &u50::new(50))); + assert_eq!( + (0, Some(0)), + Step::steps_between(&u50::new(50), &u50::new(50)) + ); - assert_eq!(Some(4), Step::steps_between(&u24::new(5), &u24::new(9))); - assert_eq!(None, Step::steps_between(&u24::new(9), &u24::new(5))); + assert_eq!( + (4, Some(4)), + Step::steps_between(&u24::new(5), &u24::new(9)) + ); + assert_eq!((0, None), Step::steps_between(&u24::new(9), &u24::new(5))); // this assumes usize is <= 64 bits. a test like this one exists in `core::iter::step`. assert_eq!( - Some(usize::MAX), + (usize::MAX, Some(usize::MAX)), Step::steps_between(&u125::new(0x7), &u125::new(0x1_0000_0000_0000_0006)) ); assert_eq!( - None, + (usize::MAX, None), Step::steps_between(&u125::new(0x7), &u125::new(0x1_0000_0000_0000_0007)) ); }