ds-ring 0.2.0

Fixed-capacity ring buffers: sample/latency ring and SPSC, loom-tested
Documentation
//! Trait-surface and oracle-branch coverage: the trait impls (`Default`,
//! `Debug`, `IntoIterator`) and the oracle paths the differential properties
//! cannot reach (zero-capacity rings, `OracleSpsc::new(0)`, oracle
//! iteration). Behavior here is pinned by hand, not by the properties.

#![forbid(unsafe_code)]
#![allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]

use ds_ring::oracle::{OracleSampleRing, OracleSpsc};
use ds_ring::{SampleRing, SpscRing};

/// `Default` builds an empty ring; `Debug` prints the live values only.
#[test]
fn sample_ring_default_and_debug() {
    let ring = SampleRing::<u32, 3>::default();
    assert_eq!(ring.len(), 0);
    assert!(ring.is_empty());

    let mut filled = SampleRing::<u32, 3>::new();
    filled.push(1);
    filled.push(2);
    let rendered = format!("{filled:?}");
    assert!(
        rendered.contains('1') && rendered.contains('2'),
        "{rendered}"
    );
    assert!(!rendered.contains("None"), "{rendered}");
}

/// `&SampleRing` iterates oldest-first via `IntoIterator`.
#[test]
fn sample_ring_into_iterator_is_oldest_first() {
    let mut ring = SampleRing::<u32, 3>::new();
    ring.push(1);
    ring.push(2);
    ring.push(3);
    ring.push(4); // evicts 1
    let collected: Vec<u32> = (&ring).into_iter().copied().collect();
    assert_eq!(collected, vec![2, 3, 4]);
}

/// The oracle mirrors the zero-capacity ring: every push bounces the value
/// straight back and nothing is retained.
#[test]
fn zero_capacity_oracle_bounces_values() {
    let mut ring = OracleSampleRing::<u32, 0>::new();
    assert_eq!(ring.push(7), Some(7));
    assert_eq!(ring.len(), 0);
}

/// `&OracleSampleRing` iterates oldest-first over its buffer.
#[test]
fn oracle_into_iterator_matches_buffer_order() {
    let mut ring = OracleSampleRing::<u32, 3>::new();
    ring.push(1);
    ring.push(2);
    let collected: Vec<u32> = (&ring).into_iter().copied().collect();
    assert_eq!(collected, vec![1, 2]);
}

/// The oracle's `is_empty` tracks the live count (mirrors the real ring's
/// approximate snapshot when the thread is quiescent).
#[test]
fn oracle_spsc_is_empty() {
    let mut q = OracleSpsc::<u32>::new(2).expect("capacity 2");
    assert!(q.is_empty());
    q.push(1).expect("fits");
    assert!(!q.is_empty());
}

/// `SpscRing::Debug` reports the live length without touching the slots
/// (it must not dereference the ring's raw storage).
#[test]
fn spsc_ring_debug_reports_len() {
    let ring = SpscRing::<u32, 4>::new();
    assert_eq!(ring.try_push(1), None, "not full");
    assert_eq!(ring.try_push(2), None, "not full");
    let rendered = format!("{ring:?}");
    assert!(rendered.contains("SpscRing"), "{rendered}");
    assert!(rendered.contains("len: 2"), "{rendered}");
}