use super::*;
use crate::primitives::mediatime::tests::{timebase, timestamp};
#[test]
fn a_timestamp_spans_its_value_columns_and_its_derived_one() {
let shapes = <MediaTimestamp as ColumnKind>::SEGMENTS;
assert_eq!(shapes.len(), 3);
assert!(matches!(shapes[1], ColumnShape::Composite { segments, .. }
if core::ptr::eq(segments, <Timebase as ColumnKind>::SEGMENTS)));
let names: Vec<_> = shapes
.iter()
.map(|shape| {
shape
.field()
.expect("every segment names a field")
.declared()
.as_str()
})
.collect();
assert_eq!(names, ["pts", "timebase", "nanos"]);
}
#[test]
fn the_cursor_carries_the_value_columns() {
let mut bytes = Vec::new();
timestamp(12_345, 1, 90_000).write_cursor(&mut bytes);
assert_eq!(
bytes.len(),
3 * size_of::<i32>() + size_of::<i64>() - size_of::<i32>()
);
assert_eq!(
bytes.len(),
16,
"eight bytes of count, eight of reduced timebase"
);
let read = <MediaTimestamp as CursorValue>::read_cursor(&bytes).expect("its own bytes");
assert_eq!(read.get().pts(), 12_345);
assert_eq!(read.get().timebase(), timebase(1, 90_000).get());
}
#[test]
fn a_written_cursor_reads_back_as_the_value_it_came_from() {
for (pts, num, den) in [
(0, 1, 1),
(-99, 1, 90_000),
(i64::MAX, 1001, 30_000),
(7, 2, 180_000),
] {
let written = timestamp(pts, num, den);
let mut bytes = Vec::new();
written.write_cursor(&mut bytes);
let read = <MediaTimestamp as CursorValue>::read_cursor(&bytes).expect("its own bytes");
assert_eq!(read.get().pts(), pts, "{pts} @ {num}/{den}");
assert_eq!(read, written, "{pts} @ {num}/{den}");
}
}
#[test]
fn a_forged_cursor_is_refused_rather_than_read() {
for forged in [[0u8; 0].as_slice(), &[0; 8], &[0; 15], &[0; 17]] {
assert_eq!(
<MediaTimestamp as CursorValue>::read_cursor(forged),
None,
"{forged:?}"
);
}
let zero_denominator = [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0];
assert_eq!(
<MediaTimestamp as CursorValue>::read_cursor(&zero_denominator),
None
);
}
#[test]
fn an_unrepresentable_instant_keeps_its_value_and_loses_its_key() {
let extreme = timestamp(i64::MAX, 1, 1);
assert!(extreme.to_nanos().is_err(), "the key has no i64 to land in");
assert_eq!(
extreme.get().pts(),
i64::MAX,
"the count is exact regardless"
);
let mut bytes = Vec::new();
extreme.write_cursor(&mut bytes);
assert_eq!(
<MediaTimestamp as CursorValue>::read_cursor(&bytes),
Some(extreme),
"and so is the cursor, which carries the value"
);
}