gst/Signed: add tests and fix some ops impl

This commit is contained in:
François Laignel 2022-09-18 23:05:37 +02:00 committed by François Laignel
parent 8e6afe147e
commit 432cb36611
2 changed files with 66 additions and 4 deletions

View file

@ -435,6 +435,8 @@ impl std::iter::Sum for ClockTime {
#[cfg(test)]
mod tests {
use super::*;
use crate::format::FormattedValue;
use crate::Signed;
const CT_1: ClockTime = ClockTime::from_nseconds(1);
const CT_2: ClockTime = ClockTime::from_nseconds(2);
@ -443,6 +445,13 @@ mod tests {
const CT_20: ClockTime = ClockTime::from_nseconds(20);
const CT_30: ClockTime = ClockTime::from_nseconds(30);
const P_CT_1: Signed<ClockTime> = Signed::Positive(ClockTime::from_nseconds(1));
const P_CT_2: Signed<ClockTime> = Signed::Positive(ClockTime::from_nseconds(2));
const P_CT_3: Signed<ClockTime> = Signed::Positive(ClockTime::from_nseconds(3));
const N_CT_1: Signed<ClockTime> = Signed::Negative(ClockTime::from_nseconds(1));
const N_CT_2: Signed<ClockTime> = Signed::Negative(ClockTime::from_nseconds(2));
const N_CT_3: Signed<ClockTime> = Signed::Negative(ClockTime::from_nseconds(3));
#[test]
fn opt_time_clock() {
assert_eq!(CT_1.into_glib(), 1);
@ -471,11 +480,31 @@ mod tests {
assert_eq!(3 * CT_10, CT_30);
assert_eq!(3 * &CT_10, CT_30);
assert_eq!(CT_30.nseconds(), 30);
assert_eq!(P_CT_1 + P_CT_2, P_CT_3);
assert_eq!(P_CT_3 + N_CT_2, P_CT_1);
assert_eq!(P_CT_2 + N_CT_3, N_CT_1);
assert_eq!(N_CT_3 + P_CT_1, N_CT_2);
assert_eq!(N_CT_2 + P_CT_3, P_CT_1);
assert_eq!(N_CT_2 + N_CT_1, N_CT_3);
assert_eq!(P_CT_3 - P_CT_2, P_CT_1);
assert_eq!(P_CT_2 - P_CT_3, N_CT_1);
assert_eq!(P_CT_2 - N_CT_1, P_CT_3);
assert_eq!(N_CT_2 - P_CT_1, N_CT_3);
assert_eq!(N_CT_3 - N_CT_1, N_CT_2);
assert_eq!(N_CT_2 - N_CT_3, P_CT_1);
}
#[test]
fn checked_ops() {
assert_eq!(CT_1.checked_add(CT_1), Some(CT_2));
assert_eq!(P_CT_1.checked_add(P_CT_2), Some(P_CT_3));
assert_eq!(P_CT_3.checked_add(N_CT_2), Some(P_CT_1));
assert_eq!(P_CT_2.checked_add(N_CT_3), Some(N_CT_1));
assert_eq!(N_CT_3.checked_add(P_CT_1), Some(N_CT_2));
assert_eq!(N_CT_2.checked_add(P_CT_3), Some(P_CT_1));
assert_eq!(N_CT_2.checked_add(N_CT_1), Some(N_CT_3));
assert_eq!(CT_1.opt_checked_add(CT_1), Ok(Some(CT_2)));
assert_eq!(CT_1.opt_checked_add(Some(CT_1)), Ok(Some(CT_2)));
@ -490,6 +519,12 @@ mod tests {
);
assert_eq!(CT_2.checked_sub(CT_1), Some(CT_1));
assert_eq!(P_CT_3.checked_sub(P_CT_2), Some(P_CT_1));
assert_eq!(P_CT_2.checked_sub(P_CT_3), Some(N_CT_1));
assert_eq!(P_CT_2.checked_sub(N_CT_1), Some(P_CT_3));
assert_eq!(N_CT_2.checked_sub(P_CT_1), Some(N_CT_3));
assert_eq!(N_CT_3.checked_sub(N_CT_1), Some(N_CT_2));
assert_eq!(N_CT_2.checked_sub(N_CT_3), Some(P_CT_1));
assert_eq!(CT_2.opt_checked_sub(CT_1), Ok(Some(CT_1)));
assert_eq!(CT_2.opt_checked_sub(Some(CT_1)), Ok(Some(CT_1)));
@ -549,7 +584,15 @@ mod tests {
#[test]
fn saturating_ops() {
let p_ct_max: Signed<ClockTime> = ClockTime::MAX.into_positive();
assert_eq!(CT_1.saturating_add(CT_2), CT_3);
assert_eq!(P_CT_1.saturating_add(P_CT_2), P_CT_3);
assert_eq!(P_CT_2.saturating_add(N_CT_3), N_CT_1);
assert_eq!(P_CT_3.saturating_add(N_CT_2), P_CT_1);
assert_eq!(N_CT_3.saturating_add(P_CT_1), N_CT_2);
assert_eq!(N_CT_2.saturating_add(P_CT_3), P_CT_1);
assert_eq!(N_CT_2.saturating_add(N_CT_1), N_CT_3);
assert_eq!(CT_1.opt_saturating_add(Some(CT_2)), Some(CT_3));
assert_eq!(Some(CT_1).opt_saturating_add(Some(CT_2)), Some(CT_3));
@ -560,17 +603,29 @@ mod tests {
Some(ClockTime::MAX).opt_saturating_add(Some(CT_1)),
Some(ClockTime::MAX)
);
assert_eq!(p_ct_max.saturating_add(P_CT_1), p_ct_max);
assert_eq!(CT_3.saturating_sub(CT_2), CT_1);
assert_eq!(P_CT_3.saturating_sub(P_CT_2), P_CT_1);
assert_eq!(P_CT_2.saturating_sub(P_CT_3), N_CT_1);
assert_eq!(P_CT_2.saturating_sub(N_CT_1), P_CT_3);
assert_eq!(N_CT_2.saturating_sub(P_CT_1), N_CT_3);
assert_eq!(N_CT_3.saturating_sub(N_CT_1), N_CT_2);
assert_eq!(N_CT_2.saturating_sub(N_CT_3), P_CT_1);
assert_eq!(CT_3.opt_saturating_sub(Some(CT_2)), Some(CT_1));
assert_eq!(Some(CT_3).opt_saturating_sub(Some(CT_2)), Some(CT_1));
assert_eq!(Some(CT_3).opt_saturating_sub(None), None);
assert!(CT_1.saturating_sub(CT_2).is_zero());
assert_eq!(P_CT_1.saturating_sub(P_CT_2), N_CT_1);
assert_eq!(
Some(CT_1).opt_saturating_sub(Some(CT_2)),
Some(ClockTime::ZERO)
);
assert_eq!(CT_1.saturating_mul(2), CT_2);
assert_eq!(ClockTime::MAX.saturating_mul(2), ClockTime::MAX);
}
#[test]
@ -610,6 +665,13 @@ mod tests {
assert!(ClockTime::ZERO < CT_3);
assert!(Some(ClockTime::ZERO) < Some(CT_3));
assert!(ClockTime::ZERO.into_positive() < P_CT_1);
assert!(ClockTime::ZERO.into_positive() > N_CT_1);
assert!(P_CT_1 < P_CT_2);
assert!(P_CT_1 > N_CT_2);
assert!(N_CT_1 < P_CT_2);
assert!(N_CT_3 < N_CT_2);
assert_eq!(Some(CT_2).opt_lt(Some(CT_3)), Some(true));
assert_eq!(Some(CT_3).opt_lt(CT_2), Some(false));
assert_eq!(Some(CT_2).opt_le(Some(CT_3)), Some(true));

View file

@ -317,7 +317,7 @@ macro_rules! impl_signed_ops(
(Signed::Negative(a), Signed::Negative(b)) if a >= b => Some(Signed::Negative(a - b)),
(Signed::Negative(a), Signed::Negative(b)) => Some(Signed::Positive(b - a)),
(Signed::Positive(a), Signed::Negative(b)) => a.checked_add(b).map(Signed::Positive),
(Signed::Negative(a), Signed::Positive(b)) => a.checked_sub(b).map(Signed::Negative),
(Signed::Negative(a), Signed::Positive(b)) => a.checked_add(b).map(Signed::Negative),
}
}
@ -357,7 +357,7 @@ macro_rules! impl_signed_ops(
(Signed::Negative(a), Signed::Negative(b)) if a >= b => Signed::Negative(a - b),
(Signed::Negative(a), Signed::Negative(b)) => Signed::Positive(b - a),
(Signed::Positive(a), Signed::Negative(b)) => Signed::Positive(a.saturating_add(b)),
(Signed::Negative(a), Signed::Positive(b)) => Signed::Negative(a.saturating_sub(b)),
(Signed::Negative(a), Signed::Positive(b)) => Signed::Negative(a.saturating_add(b)),
}
}
@ -376,7 +376,7 @@ macro_rules! impl_signed_ops(
(Signed::Positive(a), Signed::Positive(b)) => Signed::Positive(a.saturating_add(b)),
(Signed::Negative(a), Signed::Negative(b)) => Signed::Negative(a.saturating_add(b)),
(Signed::Positive(_), Signed::Negative(_)) => self.saturating_sub(-other),
(Signed::Negative(_), Signed::Positive(_)) => -((-other).saturating_sub(other)),
(Signed::Negative(_), Signed::Positive(_)) => -((-self).saturating_sub(other)),
}
}
@ -473,7 +473,7 @@ macro_rules! impl_signed_ops(
fn cmp(&self, other: &Signed<$type>) -> std::cmp::Ordering {
match (self, other) {
(Signed::Positive(a), Signed::Positive(b)) => a.cmp(b),
(Signed::Negative(a), Signed::Negative(b)) => a.cmp(b),
(Signed::Negative(a), Signed::Negative(b)) => b.cmp(a),
(Signed::Positive(_), Signed::Negative(_)) => std::cmp::Ordering::Greater,
(Signed::Negative(_), Signed::Positive(_)) => std::cmp::Ordering::Less,
}