Test sibling key redistribution during deletion
| 557 | |
| 558 | |
| 559 | class TestSiblingRedistribution: |
| 560 | """Test sibling key redistribution during deletion""" |
| 561 | |
| 562 | def test_leaf_can_donate(self): |
| 563 | """Test that leaf nodes correctly detect when they can donate keys""" |
| 564 | leaf = LeafNode(capacity=4) # min_keys = (4-1)//2 = 1 |
| 565 | |
| 566 | # Empty leaf cannot donate |
| 567 | assert not leaf.can_donate() |
| 568 | |
| 569 | # Leaf with 1 key (minimum) cannot donate |
| 570 | leaf.keys = [1] |
| 571 | leaf.values = ["one"] |
| 572 | assert not leaf.can_donate() |
| 573 | |
| 574 | # Leaf with 2 keys can donate |
| 575 | leaf.keys = [1, 2] |
| 576 | leaf.values = ["one", "two"] |
| 577 | assert leaf.can_donate() |
| 578 | |
| 579 | # Leaf with 3 keys can donate |
| 580 | leaf.keys = [1, 2, 3] |
| 581 | leaf.values = ["one", "two", "three"] |
| 582 | assert leaf.can_donate() |
| 583 | |
| 584 | def test_branch_can_donate(self): |
| 585 | """Test that branch nodes correctly detect when they can donate keys""" |
| 586 | branch = BranchNode(capacity=4) # min_keys = (4-1)//2 = 1 |
| 587 | |
| 588 | # Empty branch cannot donate |
| 589 | assert not branch.can_donate() |
| 590 | |
| 591 | # Branch with 1 key (minimum) cannot donate |
| 592 | branch.keys = [5] |
| 593 | branch.children = [LeafNode(4), LeafNode(4)] |
| 594 | assert not branch.can_donate() |
| 595 | |
| 596 | # Branch with 2 keys can donate |
| 597 | branch.keys = [5, 10] |
| 598 | branch.children = [LeafNode(4), LeafNode(4), LeafNode(4)] |
| 599 | assert branch.can_donate() |
| 600 | |
| 601 | # Branch with 3 keys can donate |
| 602 | branch.keys = [5, 10, 15] |
| 603 | branch.children = [LeafNode(4), LeafNode(4), LeafNode(4), LeafNode(4)] |
| 604 | assert branch.can_donate() |
| 605 | |
| 606 | def test_leaf_borrow_from_left(self): |
| 607 | """Test leaf borrowing keys from left sibling""" |
| 608 | left = LeafNode(capacity=4) |
| 609 | right = LeafNode(capacity=4) |
| 610 | |
| 611 | # Set up left sibling with excess keys |
| 612 | left.keys = [1, 2, 3] |
| 613 | left.values = ["one", "two", "three"] |
| 614 | |
| 615 | # Set up right sibling with too few keys |
| 616 | right.keys = [5] |
nothing calls this directly
no outgoing calls
no test coverage detected