audio-clock-bsd 0.1.1

PTP/NTP clock synchronization and sample-index to timestamp conversion for real-time audio
Documentation
//! Property tests for [`ClockAnchor`] round-trip identities.

use audio_clock_bsd::ClockAnchor;
use proptest::prelude::*;

proptest! {
    /// sample_to_ptp then ptp_to_sample recovers the original sample index
    /// (within ±1 truncation) for any anchor and sample.
    #[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}");
    }

    /// ptp_to_sample then sample_to_ptp recovers the original timestamp
    /// (within ±1 sample period, i.e. within the rate's resolution).
    #[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)?;
        // One sample period in ns = 1e9 / rate; allow one period of slack.
        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})"
        );
    }

    /// The linear map is affine: doubling the sample delta doubles the ptp delta.
    #[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)?;
        // two ≈ 2 * one (exact up to integer truncation per step).
        prop_assert!((two - 2 * one).abs() <= 2, "affine: {two} != 2*{one}");
    }
}