use super::*;
use crate::primitives::mediatime::tests::timestamp;
#[test]
fn nanos_convert_a_pts_through_its_timebase() {
assert_eq!(timestamp(90_000, 1, 90_000).to_nanos(), Ok(1_000_000_000));
assert_eq!(timestamp(-99, 1, 90_000).to_nanos(), Ok(-1_100_000));
assert_eq!(timestamp(3, 1, 1).to_nanos(), Ok(3_000_000_000));
assert_eq!(timestamp(0, 0, 1).to_nanos(), Ok(0));
}
#[test]
fn equal_instants_agree_on_nanoseconds_across_timebases() {
let milliseconds = timestamp(1_000, 1, 1_000);
let ninety_kilohertz = timestamp(90_000, 1, 90_000);
assert_eq!(milliseconds, ninety_kilohertz);
assert_eq!(milliseconds.to_nanos(), ninety_kilohertz.to_nanos());
assert_eq!(milliseconds.to_nanos(), Ok(1_000_000_000));
}
#[test]
fn nanos_truncate_toward_zero() {
assert_eq!(timestamp(1, 1, 3).to_nanos(), Ok(333_333_333));
assert_eq!(timestamp(-1, 1, 3).to_nanos(), Ok(-333_333_333));
}
#[test]
fn nanos_pin_the_upper_boundary() {
assert_eq!(
timestamp(i64::MAX, 1, 1_000_000_000).to_nanos(),
Ok(i64::MAX),
"one nanosecond per tick puts the ceiling exactly in reach"
);
let overflow = timestamp(1 << 62, 2, 1_000_000_000)
.to_nanos()
.expect_err("i64::MAX + 1 nanoseconds must not fit");
assert_eq!(overflow.nanos(), 9_223_372_036_854_775_808);
assert_eq!(
overflow.to_string(),
"9223372036854775808 nanoseconds does not fit an i64"
);
}
#[test]
fn nanos_pin_the_lower_boundary() {
assert_eq!(
timestamp(i64::MIN, 1, 1_000_000_000).to_nanos(),
Ok(i64::MIN)
);
assert_eq!(
timestamp(-(1 << 62), 2, 1_000_000_000).to_nanos(),
Ok(i64::MIN)
);
let overflow = timestamp(-(1 << 62) - 1, 2, 1_000_000_000)
.to_nanos()
.expect_err("below i64::MIN must not fit");
assert_eq!(overflow.nanos(), -9_223_372_036_854_775_810);
}
#[test]
fn the_widest_product_stays_inside_i128() {
let overflow = timestamp(i64::MIN, i32::MAX, 1)
.to_nanos()
.expect_err("this is far outside i64");
assert_eq!(
overflow.nanos(),
-19_807_040_619_342_712_361_531_211_776_000_000_000
);
assert!(overflow.nanos() > i128::MIN, "the i128 headroom is real");
}
#[test]
fn the_accessor_quartet_agrees_with_itself() {
let instant = timestamp(7, 1, 90_000);
let mut in_place = MediaTimestampFilter::new();
in_place
.set_gt(MediaInstantBound::new(instant))
.set_in_(vec![instant]);
assert_eq!(
in_place,
MediaTimestampFilter::new()
.with_gt(MediaInstantBound::new(instant))
.with_in_(vec![instant])
);
let cleared = in_place
.clone()
.maybe_gt(None)
.maybe_in_(None::<Vec<MediaTimestamp>>);
assert_eq!(cleared, MediaTimestampFilter::new());
assert!(cleared.is_empty());
let mut updated = in_place.clone();
updated
.update_gt(None)
.update_in_(None::<Vec<MediaTimestamp>>);
assert_eq!(updated, cleared);
}
#[test]
fn list_operators_accept_any_into_boxed_slice() {
let instant = timestamp(7, 1, 90_000);
let from_vec = MediaTimestampFilter::new().with_in_(vec![instant]);
let from_array = MediaTimestampFilter::new().with_in_([instant]);
let from_boxed = MediaTimestampFilter::new().with_in_(vec![instant].into_boxed_slice());
assert_eq!(from_vec, from_array);
assert_eq!(from_vec, from_boxed);
}