MCPcopy Create free account
hub / github.com/KentBeck/BPlusTree3 / TestNodeMerging

Class TestNodeMerging

python/tests/test_bplus_tree.py:778–903  ·  view source on GitHub ↗

Test node merging during deletion

Source from the content-addressed store, hash-verified

776
777
778class TestNodeMerging:
779 """Test node merging during deletion"""
780
781 def test_leaf_merge_with_right(self):
782 """Test merging a leaf with its right sibling"""
783 left = LeafNode(capacity=4)
784 right = LeafNode(capacity=4)
785
786 # Set up left leaf with underfull keys
787 left.keys = [1]
788 left.values = ["one"]
789
790 # Set up right leaf
791 right.keys = [5, 6]
792 right.values = ["five", "six"]
793
794 # Set up linked list
795 left.next = right
796
797 # Merge left with right
798 left.merge_with_right(right)
799
800 # Verify merge results
801 assert left.keys == [1, 5, 6]
802 assert left.values == ["one", "five", "six"]
803 assert left.next == right.next # Should skip merged node
804
805 def test_branch_merge_with_right(self):
806 """Test merging a branch with its right sibling"""
807 left = BranchNode(capacity=4)
808 right = BranchNode(capacity=4)
809
810 # Set up left branch with underfull keys
811 left.keys = [5]
812 left.children = [LeafNode(4), LeafNode(4)]
813
814 # Set up right branch
815 right.keys = [15, 20]
816 right.children = [LeafNode(4), LeafNode(4), LeafNode(4)]
817
818 # Merge with separator key 10
819 left.merge_with_right(right, 10)
820
821 # Verify merge results
822 assert left.keys == [5, 10, 15, 20]
823 assert len(left.children) == 5 # 2 + 3
824
825 def test_merging_during_deletion_creates_balanced_tree(self):
826 """Test that merging during deletion maintains tree balance"""
827 tree = BPlusTreeMap(capacity=5) # Small capacity to force merging
828
829 # Insert keys to create a tree structure
830 for i in range(1, 10):
831 tree[i] = f"value_{i}"
832
833 # Verify initial state
834 assert check_invariants(tree)
835 initial_leaf_count = tree.leaf_count()

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected