use audio_clock_bsd::ClockAnchor;
use proptest::prelude::*;
proptest! {
#[test]
fn prop_sample_ptp_round_trip(
rate in 8_000u32..=192_000,
ref_sample in -1_000_000i64..1_000_000,
ref_ptp_ns in -1_000_000_000_000i64..1_000_000_000_000,
sample in -1_000_000i64..1_000_000,
) {
let anchor = ClockAnchor::new(rate, ref_sample, ref_ptp_ns)?;
let ptp = anchor.sample_to_ptp_checked(sample)?;
let back = anchor.ptp_to_sample_checked(ptp)?;
prop_assert!((back - sample).abs() <= 1, "round trip {sample} -> {ptp} -> {back}");
}
#[test]
fn prop_ptp_sample_round_trip(
rate in 8_000u32..=192_000,
ref_ptp_ns in -1_000_000_000_000i64..1_000_000_000_000,
ptp_ns in -1_000_000_000_000i64..1_000_000_000_000,
) {
let anchor = ClockAnchor::new(rate, 0, ref_ptp_ns)?;
let s = anchor.ptp_to_sample_checked(ptp_ns)?;
let back = anchor.sample_to_ptp_checked(s)?;
let slack = 1_000_000_000i64 / i64::from(rate) + 1;
prop_assert!(
(back - ptp_ns).abs() <= slack,
"round trip {ptp_ns} -> {s} -> {back} (slack {slack})"
);
}
#[test]
fn prop_conversion_is_affine(
rate in 1_000u32..=192_000,
sample_base in -50_000i64..50_000,
) {
let anchor = ClockAnchor::new(rate, 0, 0)?;
let one = anchor.sample_to_ptp_checked(sample_base)?;
let two = anchor.sample_to_ptp_checked(sample_base * 2)?;
prop_assert!((two - 2 * one).abs() <= 2, "affine: {two} != 2*{one}");
}
}