Elffuss BraidHub

utopia-ia/elffuss-braid public

Format: test a root moving down a level and back up

A directory whose root is two levels above its leaves loses every
entry but those of its last subtree: the update keeps that subtree's
root as the new root, one level down, and writes no node. Adding the
entries back makes the old root again. Both are compared with building
from scratch, node for node.
Changesetf2b4da620c53a80091e2054f55ffb5832205e430456dac6f69b720255811b276
AuthorKikoCis <KikoCis@users.noreply.github.com> · 2026-10-16 03:35 +02:00
CommitterKikoCis <KikoCis@users.noreply.github.com> · 2026-10-16 03:35 +02:00
Parents1c278a4f4ef8

Browse the files at this changeset

1 file changed

modified crates/format/tests/update.rs +46 −2
@@ -97,7 +97,7 @@
9797 }
9898 }
9999
100-/// A directory of about `count` distinct valid names.
100+/// A directory of `count` distinct valid names.
101101 fn directory(count: usize, style: u8, seed: u64) -> Entries {
102102 let mut entries = Entries::new();
103103 let mut i = 0u64;
@@ -437,7 +437,51 @@
437437 assert_eq!(gone.entries, 0);
438438 }
439439
440-/// How many levels of nodes a directory of `entries` has: 1 when it is flat.
440+/// The root of a split directory moves down a level when its subtrees but
441+/// one empty, and back up when they fill again.
442+#[test]
443+fn the_root_moves_down_and_up_a_level() {
444+ // A directory whose root is two levels above its leaves, and whose last
445+ // subtree alone is too large for a flat node.
446+ let (old, root, store, children) = (0..)
447+ .find_map(|seed| {
448+ let entries = directory(12_000, 1, seed);
449+ let (root, store) = stored(&entries);
450+ match Node::decode(&store[&root]).unwrap() {
451+ Node::Internal { level: 2, children }
452+ if children.last()?.entry_count > MAX_FLAT_ENTRIES as u64 =>
453+ {
454+ Some((entries, root, store, children))
455+ }
456+ _ => None,
457+ }
458+ })
459+ .unwrap();
460+ // Every entry under the last child of the root stays.
461+ let kept = children.last().unwrap();
462+ let first_kept = children[children.len() - 2].last_name.clone();
463+ let fewer: Entries = old
464+ .iter()
465+ .filter(|(name, _)| **name > first_kept)
466+ .map(|(name, kind)| (name.clone(), kind.clone()))
467+ .collect();
468+ assert_eq!(fewer.len() as u64, kept.entry_count);
469+ assert!(fewer.len() > MAX_FLAT_ENTRIES, "still split");
470+
471+ let (down, _) = update(&store, root, &edits(&old, &fewer, &[]));
472+ check_update(&store, &down, &fewer).unwrap();
473+ let (down_root, down_store) = stored(&fewer);
474+ assert_eq!(down.root, down_root);
475+ assert_eq!(down.root, kept.node, "the last subtree's root is the root");
476+ assert!(down.nodes.is_empty(), "every node it needs is there");
477+ assert!(levels(&down_store, down_root) < 3, "the root moved down");
478+
479+ let (up, _) = update(&down_store, down_root, &edits(&fewer, &old, &[]));
480+ check_update(&down_store, &up, &old).unwrap();
481+ assert_eq!(up.root, root, "and back up, as it was");
482+}
483+
484+/// How many levels of nodes the directory `root` has: 1 when it is flat.
441485 fn levels(store: &Store, root: ObjectId) -> usize {
442486 match Node::decode(&store[&root]).unwrap() {
443487 Node::Internal { level, .. } => usize::from(level) + 1,