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 {
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<u16> 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<cmp::Ordering> {
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));
}
}