flow-lang-value 0.1.0-rc.5

The Flow runtime value model: data values, their equality, order and text, and the operations std's built-in types declare.
Documentation
//! Unit tests that reach representation: what a list's tree shares and
//! reuses, and the invariants it keeps.

mod dict_tree;
mod list_model;
mod list_tree;
mod rebuild;
mod text_tree;

use std::collections::VecDeque;

use crate::collection::List;
use crate::tree::{self, Node};
use crate::value::Value;

/// A seeded generator, `SplitMix64`, so a failure reproduces exactly.
struct Draw(u64);

impl Draw {
    fn next(&mut self) -> u64 {
        self.0 = self.0.wrapping_add(0x9E37_79B9_7F4A_7C15);
        let mut z = self.0;
        z = (z ^ (z >> 30)).wrapping_mul(0xBF58_476D_1CE4_E5B9);
        z = (z ^ (z >> 27)).wrapping_mul(0x94D0_49BB_1331_11EB);
        z ^ (z >> 31)
    }

    /// A number in `0..bound`, which must be positive.
    fn below(&mut self, bound: usize) -> usize {
        usize::try_from(self.next() % u64::try_from(bound).expect("a small bound"))
            .expect("less than a usize bound")
    }
}

/// The list of `values`, in order.
fn ints(values: impl IntoIterator<Item = i64>) -> List {
    values.into_iter().map(Value::int).collect()
}

/// The elements of `list`, read back as `i64`s.
fn read(list: &List) -> Vec<i64> {
    list.iter().map(number).collect()
}

/// The `i64` an integer value holds.
fn number(value: &Value) -> i64 {
    let Value::Int(int) = value else {
        panic!("an integer")
    };
    i64::try_from(int.as_bigint()).expect("a small integer")
}

/// Checks the invariants of `list`'s tree: balanced, with every length and
/// height it records right, and holding the list's window.
fn check(list: &List) {
    use crate::collection::EDGE;
    assert!(
        list.front.len() <= EDGE && list.back.len() <= EDGE,
        "ends hold at most EDGE"
    );
    if let Some(root) = &list.root {
        tree::check(root);
        assert!(list.middle > 0, "a tree is kept only for a middle");
        assert!(
            list.start + list.middle <= root.len(),
            "the window lies in the tree"
        );
        assert!(list.len() > EDGE, "a short list has no tree");
    } else {
        assert_eq!(list.middle, 0, "no tree, no middle");
        assert!(
            list.front.is_empty() || list.len() > EDGE,
            "a short list keeps its elements at the back"
        );
    }
}

/// The leaves of `list`'s tree, in order, as ids.
fn leaves(list: &List) -> Vec<usize> {
    let mut out = Vec::new();
    if let Some(root) = &list.root {
        tree::leaves(root, &mut out);
    }
    out
}

/// The model the list is checked against: the double-ended buffer `List` was
/// before it was a tree.
type Model = VecDeque<i64>;