-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_memory_safety.py
More file actions
76 lines (62 loc) · 2.25 KB
/
test_memory_safety.py
File metadata and controls
76 lines (62 loc) · 2.25 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
"""
Memory safety test for C++ extensions
"""
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
import chunker_cpp
import gc
def test_chunker_memory_safety():
"""Test the chunker with various inputs to ensure memory safety"""
print("Testing chunker memory safety...")
# Test 1: Empty string
try:
result = chunker_cpp.chunk_text_semantically("", 2000, 500, 0.3)
print("✓ Empty string test passed")
except Exception as e:
print(f"✗ Empty string test failed: {e}")
return False
# Test 2: Very large string
try:
large_text = "This is a test sentence. " * 10000 # ~250KB
result = chunker_cpp.chunk_text_semantically(large_text, 1000, 200)
print("✓ Large string test passed")
except Exception as e:
print(f"✗ Large string test failed: {e}")
return False
# Test 3: Malformed text with special characters
try:
malformed_text = "Test with special chars: \x00\x01\x02\n\r\t" + "A" * 5000
result = chunker_cpp.chunk_text_semantically(malformed_text, 1000, 200)
print("✓ Malformed text test passed")
except Exception as e:
print(f"✗ Malformed text test failed: {e}")
return False
# Test 4: Memory leak test - run multiple times
try:
for i in range(100):
test_text = f"This is test sentence number {i}. " * 100
result = chunker_cpp.chunk_text_semantically(test_text, 1000, 200)
if i % 20 == 0:
gc.collect()
print("✓ Memory leak test passed")
except Exception as e:
print(f"✗ Memory leak test failed: {e}")
return False
# Test 5: Invalid parameters
try:
result = chunker_cpp.chunk_text_semantically("test", -1, -1)
print("✓ Invalid parameters test passed")
except Exception as e:
print(f"✗ Invalid parameters test failed: {e}")
return False
print("All memory safety tests passed!")
return True
if __name__ == "__main__":
success = test_chunker_memory_safety()
if success:
print("Memory safety verification completed successfully")
sys.exit(0)
else:
print("Memory safety verification failed")
sys.exit(1)