use super::*;
use crate::collection::EDGE;
use crate::tree::CHUNK;
fn wide(count: usize) -> i64 {
i64::try_from(count).expect("a small count")
}
fn root(list: &List) -> usize {
list.root
.as_ref()
.expect("a non-empty list has a tree")
.id()
}
#[test]
fn growing_a_held_list_leaves_its_tree_as_it_was_until_an_end_fills() {
let held = ints(0..wide(10 * CHUNK - 1));
let before = root(&held);
let mut grown = held.clone();
for at in 0..wide(EDGE) {
grown = grown.push_back(Value::int(-at)).push_front(Value::int(at));
assert_eq!(root(&grown), before, "the tree is shared whole");
check(&grown);
}
assert_eq!(read(&held), (0..wide(10 * CHUNK - 1)).collect::<Vec<_>>());
assert_eq!(grown.len(), held.len() + 2 * EDGE);
}
#[test]
fn a_full_end_moves_into_the_tree_as_one_leaf_and_copies_none() {
let held = ints(0..wide(4 * CHUNK));
let before = leaves(&held);
let grown = (0..=wide(EDGE)).fold(held.clone(), |list, at| list.push_back(Value::int(-at)));
let after = leaves(&grown);
assert_eq!(
after.len(),
before.len() + 1,
"the full end became one leaf"
);
assert_eq!(
&after[..before.len()],
&before[..],
"every old leaf is shared"
);
assert_eq!(grown.back.len(), 1);
assert_eq!(leaves(&held), before, "the held list is untouched");
check(&grown);
}
#[test]
fn a_list_of_at_most_edge_elements_has_no_tree() {
for count in 0..=EDGE {
let list = ints(0..wide(count));
assert!(list.root.is_none());
let grown = (0..wide(count)).fold(List::new(), |list, at| list.push_front(Value::int(at)));
assert!(grown.root.is_none());
check(&grown);
}
}
#[test]
fn joining_two_long_held_lists_shares_every_leaf_of_both() {
let left = ints(0..wide(5 * CHUNK));
let right = ints(0..wide(2 * CHUNK + 1));
let mut expected = leaves(&left);
expected.extend(leaves(&right));
let joined = left.clone().concat(right.clone());
assert_eq!(leaves(&joined), expected);
assert_eq!(joined.len(), left.len() + right.len());
check(&joined);
}
#[test]
fn joining_a_short_list_at_either_end_fills_the_leaf_at_the_seam() {
let long = ints(0..wide(3 * CHUNK - 2));
let joined = long.clone().concat(ints([7, 8]));
assert_eq!(leaves(&joined).len(), 3, "the two joined the last leaf");
assert_eq!(
read(&joined)[joined.len() - 3..],
[wide(3 * CHUNK - 3), 7, 8]
);
let rest = long.slice(1, long.len()).unwrap();
let joined = ints([7]).concat(rest);
assert_eq!(leaves(&joined).len(), 3, "the one joined the first leaf");
assert_eq!(read(&joined)[..2], [7, 1]);
check(&joined);
}
#[test]
fn growing_a_part_cuts_its_tree_down_to_it() {
let list = ints(0..wide(8 * CHUNK));
let part = list.slice(CHUNK + 3, 4 * CHUNK).unwrap();
assert_eq!(
part.root.as_ref().unwrap().id(),
list.root.as_ref().unwrap().id()
);
let grown = (0..=wide(EDGE)).fold(part, |list, at| list.push_back(Value::int(-at)));
assert_eq!(grown.start, 0);
assert_eq!(grown.root.as_ref().unwrap().len(), grown.middle);
assert_eq!(read(&grown)[0], wide(CHUNK + 3));
check(&grown);
}
#[test]
fn an_empty_part_grows_into_a_list_of_one() {
let list = ints(0..3);
for (start, end) in [(0, 0), (1, 1), (3, 3)] {
let empty = list.slice(start, end).unwrap();
assert!(empty.get(0).is_none());
let one = empty.clone().push_front(Value::int(9));
assert_eq!(read(&one), [9]);
assert_eq!(read(&empty.clone().concat(empty)), Vec::<i64>::new());
check(&one);
}
}
#[test]
fn a_built_list_is_as_low_as_its_leaves_allow() {
for count in [1, 2, 3, 5, 8, 33, 100] {
let list = ints(0..wide(count * CHUNK));
let Some(root) = list.root.as_ref() else {
assert!(count * CHUNK <= EDGE, "only a short list has no tree");
continue;
};
let height = tree::check(root);
assert_eq!(
u32::from(height),
count.next_power_of_two().trailing_zeros()
);
assert_eq!(leaves(&list).len(), count);
}
assert!(List::new().root.is_none());
assert!(ints([]).root.is_none());
}
#[test]
fn dropping_a_list_spares_the_nodes_another_list_holds() {
let inner = ints([1, 2]);
let held = Value::List(inner.clone());
let list: List = (0..4 * CHUNK).map(|_| held.clone()).collect();
let part = list.slice(CHUNK, CHUNK + 1).unwrap();
let grown = list.clone().push_back(Value::int(0));
drop(list);
drop(grown);
assert_eq!(read(&inner), [1, 2]);
let Some(Value::List(kept)) = part.get(0) else {
panic!("the part still holds its element")
};
assert_eq!(read(kept), [1, 2]);
}
#[test]
fn many_lists_grown_from_one_held_list_each_see_only_their_own_growth() {
let mut list = List::new();
let mut held = Vec::new();
for at in 0..300 {
held.push(list.clone());
list = if at % 3 == 0 {
list.push_front(Value::int(at))
} else {
list.concat(ints([at]))
};
check(&list);
}
for (length, earlier) in held.iter().enumerate() {
assert_eq!(earlier.len(), length);
let expected: Model = (0..wide(length)).fold(Model::new(), |mut model, at| {
if at % 3 == 0 {
model.push_front(at);
} else {
model.push_back(at);
}
model
});
assert_eq!(read(earlier), Vec::from(expected));
}
}
type Walk = fn(tree::Iter<'_>) -> usize;
#[test]
fn walking_a_tree_from_either_end_visits_each_node_a_bounded_number_of_times() {
let count = 100 * CHUNK * CHUNK;
let root = tree::build((0..wide(count)).map(Value::int)).expect("a tree");
let mut ids = Vec::new();
tree::leaves(&root, &mut ids);
let nodes = 2 * ids.len() - 1;
let walks: [(&str, Walk); 3] = [
("forward", |walk| walk.count()),
("backward", |walk| walk.rev().count()),
("from both ends", |mut walk| {
let mut seen = 0;
while walk.next().is_some() {
seen += 1;
if walk.next_back().is_some() {
seen += 1;
}
}
seen
}),
];
for (name, walk) in walks {
tree::VISITED.with(|visited| visited.set(0));
assert_eq!(walk(tree::Iter::new(Some(&root), 0, count)), count);
let visited = tree::VISITED.with(std::cell::Cell::get);
assert!(
visited <= 2 * (nodes + root.height()),
"walking {name} visited {visited} nodes of a tree of {} leaves",
ids.len()
);
}
}