Add test for impl Ord for PreRelease, fix test failure for impl Eq for PreRelease

This commit is contained in:
Nathan Fisher 2024-01-05 12:48:33 -05:00
parent e687e27a5b
commit bb187d4f8c

View File

@ -95,17 +95,17 @@ impl From<PreRelease> for u16 {
match value { match value {
PreRelease::Alpha(Some(v)) => { PreRelease::Alpha(Some(v)) => {
let v = u16::from(v) & mask; let v = u16::from(v) & mask;
v & 0o2_000 v | 0o2_000
} }
PreRelease::Alpha(None) => 0o2_000, PreRelease::Alpha(None) => 0o2_000,
PreRelease::Beta(Some(v)) => { PreRelease::Beta(Some(v)) => {
let v = u16::from(v) & mask; let v = u16::from(v) & mask;
v & 0o4_000 v | 0o4_000
} }
PreRelease::Beta(None) => 0o4_000, PreRelease::Beta(None) => 0o4_000,
PreRelease::RC(Some(v)) => { PreRelease::RC(Some(v)) => {
let v = u16::from(v) & mask; let v = u16::from(v) & mask;
v & 0o10_000 v | 0o10_000
} }
PreRelease::RC(None) => 0o10_000, PreRelease::RC(None) => 0o10_000,
PreRelease::None => 0o20_000, PreRelease::None => 0o20_000,
@ -141,11 +141,22 @@ impl TryFrom<u16> for PreRelease {
impl PartialEq for PreRelease { impl PartialEq for PreRelease {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
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 a: u16 = (*self).into();
let b: u16 = (*other).into(); let b: u16 = (*other).into();
a == b a == b
} }
} }
}
}
impl Eq for PreRelease {} impl Eq for PreRelease {}
@ -159,9 +170,7 @@ impl cmp::Ord for PreRelease {
impl PartialOrd for PreRelease { impl PartialOrd for PreRelease {
fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
let a: u16 = (*self).into(); Some(self.cmp(other))
let b: u16 = (*other).into();
Some(a.cmp(&b))
} }
} }
@ -210,12 +219,42 @@ mod tests {
#[test] #[test]
fn equality() { 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!( assert_eq!(
PreRelease::Alpha(None), PreRelease::Alpha(None),
PreRelease::Alpha(Some(NonZeroU16::new(1).unwrap())), 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));
} }
} }