-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathtest_node_split_minimal.py
More file actions
91 lines (67 loc) · 2.45 KB
/
test_node_split_minimal.py
File metadata and controls
91 lines (67 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
"""
Minimal test for node split bug - smallest possible failing test.
Following TDD: write the smallest test that replicates the problem.
"""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import pytest
try:
import bplustree_c
HAS_C_EXTENSION = True
except ImportError as e:
pytest.skip(f"C extension not available: {e}", allow_module_level=True)
def test_single_node_split_maintains_order():
"""
SMALLEST POSSIBLE TEST: Single node split must maintain sorted order.
This test MUST fail until the bug is fixed.
"""
if not HAS_C_EXTENSION:
pytest.skip("C extension not available")
# Create tree with capacity 4 - split will happen after 4 items
tree = bplustree_c.BPlusTree(capacity=4)
# Insert exactly enough items to cause ONE split
for i in range(5): # 5 items in capacity-4 tree = 1 split
tree[i] = i * 10
# After split, iteration MUST return keys in sorted order
keys = list(tree.keys())
print(f"Keys after single split: {keys}")
print(f"Expected: [0, 1, 2, 3, 4]")
# THE CRITICAL TEST: keys must be sorted
assert keys == [
0,
1,
2,
3,
4,
], f"Keys not in sorted order after single node split. Got: {keys}"
print("✅ PASSED: Keys in correct order after split")
def test_two_splits_maintains_order():
"""
Second minimal test: Two splits must maintain sorted order.
"""
if not HAS_C_EXTENSION:
pytest.skip("C extension not available")
# Create tree with capacity 4
tree = bplustree_c.BPlusTree(capacity=4)
# Insert enough items to cause TWO splits
for i in range(9): # Should cause 2 splits
tree[i] = i * 10
# Keys must still be sorted
keys = list(tree.keys())
expected = list(range(9))
print(f"Keys after two splits: {keys}")
print(f"Expected: {expected}")
assert keys == expected, f"Keys not in sorted order after two splits. Got: {keys}"
print("✅ PASSED: Keys in correct order after two splits")
if __name__ == "__main__":
print("Running MINIMAL node split tests...")
print("=" * 50)
# Test 1: Single split
result1 = test_single_node_split_maintains_order()
# Test 2: Two splits
result2 = test_two_splits_maintains_order()
if result1 and result2:
print("\n🎉 All minimal tests PASSED")
else:
print("\n🚨 MINIMAL tests FAILED - must fix before proceeding")