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
//! A tree rebuilt node by node keeps every invariant the tree module keeps,
//! whatever shape it was given: balance, recorded lengths and measures, and
//! the first key of each dictionary branch.

use std::collections::BTreeMap;

use super::*;
use crate::collection::Dict;

/// A tree no writer here would make: each branch has one leaf on its right
/// and everything before on its left, given to `Node::branch` in that order.
fn leaning<T: tree::Element>(leaves: Vec<Vec<T>>) -> Node<T> {
    let mut leaves = leaves.into_iter().map(|items| Node::leaf(items).unwrap());
    let first = leaves.next().expect("one leaf at least");
    leaves.fold(first, Node::branch)
}

#[test]
fn a_tree_built_leaning_is_balanced_with_every_measure_recorded() {
    let leaves: Vec<Vec<Value>> = (0..50)
        .map(|leaf| {
            (0..=leaf % 8)
                .map(|at| Value::int(leaf * 10 + at))
                .collect()
        })
        .collect();
    let tree = leaning(leaves.clone());
    tree::check(&tree);
    let list = List::from_parts(vec![], Some((tree.clone(), 0, tree.len())), vec![]).unwrap();
    let expected: Vec<i64> = leaves.iter().flatten().map(number).collect();
    assert_eq!(read(&list), expected);
    check(&list);
}

#[test]
fn a_dictionary_on_a_tree_built_leaning_agrees_with_an_ordered_map() {
    let leaves: Vec<Vec<(Value, Value)>> = (0..40_i64)
        .map(|leaf| {
            (0..=leaf % 8)
                .map(|at| (Value::int(leaf * 100 + at * 2), Value::int(at)))
                .collect()
        })
        .collect();
    let mut model: BTreeMap<i64, i64> = leaves
        .iter()
        .flatten()
        .map(|(key, value)| (number(key), number(value)))
        .collect();
    let tree = leaning(leaves);
    tree::check(&tree);
    tree::check_keys(&tree);
    let mut dict = Dict::from_tree(Some(tree));
    for step in 0..2000_i64 {
        let key = (step * 7919) % 4100 - 50;
        if step % 3 == 0 {
            dict = dict.remove(&Value::int(key)).unwrap();
            model.remove(&key);
        } else {
            dict = dict.insert(Value::int(key), Value::int(step)).unwrap();
            model.insert(key, step);
        }
        let probe = (step * 31) % 4100 - 50;
        assert_eq!(
            dict.get(&Value::int(probe)).unwrap().map(number),
            model.get(&probe).copied()
        );
    }
    let root = dict.root().unwrap();
    tree::check(root);
    tree::check_keys(root);
    let entries: Vec<(i64, i64)> = dict
        .iter()
        .map(|(key, value)| (number(key), number(value)))
        .collect();
    assert_eq!(entries, model.into_iter().collect::<Vec<_>>());
}

#[test]
fn replacing_the_first_entry_keeps_the_first_key_of_every_branch() {
    let dict = Dict::from_entries((0..100).map(|at| (Value::int(at), Value::int(at)))).unwrap();
    let held = dict.clone();
    let dict = dict.insert(Value::int(0), Value::int(-1)).unwrap();
    tree::check_keys(dict.root().unwrap());
    tree::check(dict.root().unwrap());
    assert_eq!(dict.get(&Value::int(0)).unwrap().map(number), Some(-1));
    assert_eq!(held.get(&Value::int(0)).unwrap().map(number), Some(0));
}