Stress tests for edge cases that could break B+ tree invariants
| 17 | |
| 18 | |
| 19 | class TestStressEdgeCases: |
| 20 | """Stress tests for edge cases that could break B+ tree invariants""" |
| 21 | |
| 22 | def test_minimum_capacity_heavy_deletion(self): |
| 23 | """Test minimum capacity (4) with heavy deletion patterns""" |
| 24 | tree = BPlusTreeMap(capacity=4) |
| 25 | |
| 26 | # Build a substantial tree |
| 27 | keys = list(range(100)) |
| 28 | for key in keys: |
| 29 | tree[key] = f"value_{key}" |
| 30 | |
| 31 | assert check_invariants(tree), "Tree should be valid after insertions" |
| 32 | |
| 33 | # Delete in patterns that stress rebalancing |
| 34 | # Pattern 1: Delete every 3rd key |
| 35 | for i in range(0, 100, 3): |
| 36 | if i in tree: |
| 37 | del tree[i] |
| 38 | assert check_invariants(tree), f"Invariants broken after deleting {i}" |
| 39 | |
| 40 | # Pattern 2: Delete consecutive ranges |
| 41 | for start in range(10, 90, 20): |
| 42 | for i in range(start, min(start + 5, 100)): |
| 43 | if i in tree: |
| 44 | del tree[i] |
| 45 | assert check_invariants( |
| 46 | tree |
| 47 | ), f"Invariants broken after deleting {i}" |
| 48 | |
| 49 | def test_alternating_insert_delete_stress(self): |
| 50 | """Test alternating insert/delete operations that could cause instability""" |
| 51 | tree = BPlusTreeMap(capacity=8) |
| 52 | |
| 53 | # Start with some data |
| 54 | for i in range(50): |
| 55 | tree[i] = f"initial_{i}" |
| 56 | |
| 57 | assert check_invariants(tree), "Initial tree should be valid" |
| 58 | |
| 59 | # Alternating pattern that stresses the tree |
| 60 | for round_num in range(10): |
| 61 | # Insert a batch |
| 62 | for i in range(100 + round_num * 20, 120 + round_num * 20): |
| 63 | tree[i] = f"round_{round_num}_{i}" |
| 64 | assert check_invariants(tree), f"Insert {i} broke invariants" |
| 65 | |
| 66 | # Delete a batch from different area |
| 67 | for i in range(round_num * 5, round_num * 5 + 10): |
| 68 | if i in tree: |
| 69 | del tree[i] |
| 70 | assert check_invariants(tree), f"Delete {i} broke invariants" |
| 71 | |
| 72 | def test_large_capacity_edge_cases(self): |
| 73 | """Test very large capacity to stress single-level tree edge cases""" |
| 74 | tree = BPlusTreeMap(capacity=1024) |
| 75 | |
| 76 | # Fill up close to capacity |
no outgoing calls
no test coverage detected