From bb187d4f8c38ed22eaa889a062256e114e5e2bce Mon Sep 17 00:00:00 2001 From: Nathan Fisher Date: Fri, 5 Jan 2024 12:48:33 -0500 Subject: [PATCH] Add test for `impl Ord for PreRelease`, fix test failure for `impl Eq for PreRelease` --- src/prerelease.rs | 63 ++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 12 deletions(-) diff --git a/src/prerelease.rs b/src/prerelease.rs index 9848b1b..39687ff 100644 --- a/src/prerelease.rs +++ b/src/prerelease.rs @@ -95,17 +95,17 @@ impl From for u16 { match value { PreRelease::Alpha(Some(v)) => { let v = u16::from(v) & mask; - v & 0o2_000 + v | 0o2_000 } PreRelease::Alpha(None) => 0o2_000, PreRelease::Beta(Some(v)) => { let v = u16::from(v) & mask; - v & 0o4_000 + v | 0o4_000 } PreRelease::Beta(None) => 0o4_000, PreRelease::RC(Some(v)) => { let v = u16::from(v) & mask; - v & 0o10_000 + v | 0o10_000 } PreRelease::RC(None) => 0o10_000, PreRelease::None => 0o20_000, @@ -141,9 +141,20 @@ impl TryFrom for PreRelease { impl PartialEq for PreRelease { fn eq(&self, other: &Self) -> bool { - let a: u16 = (*self).into(); - let b: u16 = (*other).into(); - a == b + let _one = NonZeroU16::new(1).unwrap(); + match (self, other) { + (PreRelease::Alpha(Some(_one)), PreRelease::Alpha(None)) + | (PreRelease::Alpha(None), PreRelease::Alpha(Some(_one))) + | (PreRelease::Beta(Some(_one)), PreRelease::Beta(None)) + | (PreRelease::Beta(None), PreRelease::Beta(Some(_one))) + | (PreRelease::RC(Some(_one)), PreRelease::RC(None)) + | (PreRelease::RC(None), PreRelease::RC(Some(_one))) => true, + _ => { + let a: u16 = (*self).into(); + let b: u16 = (*other).into(); + a == b + } + } } } @@ -159,9 +170,7 @@ impl cmp::Ord for PreRelease { impl PartialOrd for PreRelease { fn partial_cmp(&self, other: &Self) -> Option { - let a: u16 = (*self).into(); - let b: u16 = (*other).into(); - Some(a.cmp(&b)) + Some(self.cmp(other)) } } @@ -210,12 +219,42 @@ mod tests { #[test] fn equality() { - let a: u16 = PreRelease::Alpha(None).into(); - let b: u16 = PreRelease::Alpha(Some(NonZeroU16::new(1).unwrap())).into(); - assert_eq!(a, b); assert_eq!( PreRelease::Alpha(None), PreRelease::Alpha(Some(NonZeroU16::new(1).unwrap())), ); + assert_eq!( + PreRelease::Beta(None), + PreRelease::Beta(Some(NonZeroU16::new(1).unwrap())) + ); + assert_eq!( + PreRelease::RC(None), + PreRelease::RC(Some(NonZeroU16::new(1).unwrap())) + ); + } + + #[test] + fn ordering() { + assert!( + PreRelease::Alpha(Some(NonZeroU16::new(2).unwrap())) + > PreRelease::Alpha(Some(NonZeroU16::new(1).unwrap())) + ); + assert!(PreRelease::Alpha(Some(NonZeroU16::new(2).unwrap())) > PreRelease::Alpha(None)); + assert!( + PreRelease::Beta(Some(NonZeroU16::new(2).unwrap())) + > PreRelease::Beta(Some(NonZeroU16::new(1).unwrap())) + ); + assert!(PreRelease::RC(Some(NonZeroU16::new(2).unwrap())) > PreRelease::RC(None)); + assert!( + PreRelease::RC(Some(NonZeroU16::new(2).unwrap())) + > PreRelease::RC(Some(NonZeroU16::new(1).unwrap())) + ); + assert!(PreRelease::RC(Some(NonZeroU16::new(2).unwrap())) > PreRelease::RC(None)); + assert!(PreRelease::None > PreRelease::RC(None)); + assert!(PreRelease::None > PreRelease::Beta(None)); + assert!(PreRelease::None > PreRelease::Alpha(None)); + assert!(PreRelease::RC(None) > PreRelease::Beta(None)); + assert!(PreRelease::RC(None) > PreRelease::Alpha(None)); + assert!(PreRelease::Beta(None) > PreRelease::Alpha(None)); } }