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

Class TestBranchNode

python/tests/test_bplus_tree.py:512–556  ·  view source on GitHub ↗

Test BranchNode operations

Source from the content-addressed store, hash-verified

510
511
512class TestBranchNode:
513 """Test BranchNode operations"""
514
515 def test_branch_node_creation(self):
516 """Test creating a branch node"""
517 branch = BranchNode(capacity=4)
518 assert not branch.is_leaf()
519 assert not branch.is_full()
520 assert len(branch) == 0
521
522 def test_find_child_index(self):
523 """Test finding correct child index"""
524 branch = BranchNode(capacity=4)
525 branch.keys = [10, 20, 30]
526
527 # Create dummy leaf nodes as children
528 for i in range(4):
529 branch.children.append(LeafNode(capacity=4))
530
531 # Test finding child indices
532 assert branch.find_child_index(5) == 0 # < 10
533 assert branch.find_child_index(10) == 1 # >= 10, < 20
534 assert branch.find_child_index(15) == 1 # >= 10, < 20
535 assert branch.find_child_index(20) == 2 # >= 20, < 30
536 assert branch.find_child_index(25) == 2 # >= 20, < 30
537 assert branch.find_child_index(30) == 3 # >= 30
538 assert branch.find_child_index(35) == 3 # >= 30
539
540 def test_branch_node_split(self):
541 """Test splitting a branch node"""
542 branch = BranchNode(capacity=4)
543 branch.keys = [10, 20, 30, 40]
544
545 # Create dummy children (one more than keys)
546 branch.children = [LeafNode(4) for _ in range(5)]
547
548 # Split the branch
549 new_branch, separator = branch.split()
550
551 # Check the split results
552 assert separator == 30 # Middle key should be promoted (keys[2])
553 assert branch.keys == [10, 20] # Left half
554 assert new_branch.keys == [40] # Right half (excluding promoted key)
555 assert len(branch.children) == 3 # mid + 1 = 3
556 assert len(new_branch.children) == 2 # 5 - 3 = 2
557
558
559class TestSiblingRedistribution:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected