Cotton code
57 lines · 2.2 KB · RustRaw
//! Every case came across verbatim — the exhaustion and midpoint ones//! especially, because they are what stops a reorder from writing a duplicate//! position.use super::*;#[test]fn seeds_an_empty_list_at_one_step() {    assert_eq!(position_between(None, None), POSITION_STEP);}#[test]fn drops_before_the_head_by_stepping_down_and_after_the_tail_by_stepping_up() {    assert_eq!(position_between(None, Some(1024.0)), 1024.0 - POSITION_STEP);    assert_eq!(position_between(Some(3072.0), None), 3072.0 + POSITION_STEP);}#[test]fn lands_on_the_midpoint_between_two_neighbours() {    assert_eq!(position_between(Some(1024.0), Some(2048.0)), 1536.0);    // Midpoints stay strictly between even after many splits.    assert_eq!(position_between(Some(1024.0), Some(1025.0)), 1024.5);}/// The realistic worst case: a user repeatedly drops a row into the same slot./// A double has ~52 bits of mantissa, so one STEP-wide gap takes about that/// many halvings before it closes — far past any human's patience with one/// list.#[test]fn survives_fifty_consecutive_splits_into_the_same_gap() {    let mut before = 1024.0_f64;    let after = before + POSITION_STEP;    for _ in 0..50 {        let mid = position_between(Some(before), Some(after));        assert!(mid > before);        assert!(mid < after);        before = mid;    }}/// Documented, not defended. Once two neighbours are adjacent doubles the/// midpoint IS one of them, and the caller gets a duplicate position rather/// than a new slot — the rows then sort stably instead of moving. The fix when/// it ever bites is to re-spread the list (`index * POSITION_STEP`), the same/// materialise-the-whole-order write the first arrange already does; no caller/// pre-empts it because reaching here needs ~52 splits of one gap.////// 1024 = 2^10, so the next representable double above it is one ulp away:/// 2^(10-52). Their true midpoint is not representable at all.#[test]fn known_limit_an_exhausted_gap_collapses_onto_its_neighbour() {    let before = 1024.0_f64;    let after = before + 2.0_f64.powi(-42);    assert!(after > before, "genuinely adjacent, not equal");    let mid = position_between(Some(before), Some(after));    assert!(mid == before || mid == after);}