-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_new_modules.py
More file actions
133 lines (103 loc) · 4.41 KB
/
test_new_modules.py
File metadata and controls
133 lines (103 loc) · 4.41 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""
Quick test script to verify all new modules work correctly.
"""
import sys
import os
# Add package root to path
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
print("=" * 60)
print("TESTING NEW MODULES")
print("=" * 60)
# Test 1: String Search
print("\n1. Testing String Search Module...")
try:
from searching.string_search.algorithms import naive_string_search, trie_search
from searching.string_search.constraints import get_valid_string_algorithms
from searching.string_search.features import StringSearchFeatureExtractor
result = naive_string_search("hello world", "world")
assert result == [6], f"Expected [6], got {result}"
valid = get_valid_string_algorithms(num_patterns=1)
assert 'naive_string_search' in valid
assert 'trie_search' not in valid # Single pattern, not prefix
valid = get_valid_string_algorithms(num_patterns=5)
assert 'trie_search' in valid # Multiple patterns
print(" ✅ String Search algorithms work correctly")
except Exception as e:
print(f" ❌ Error: {e}")
# Test 2: Tree Search
print("\n2. Testing Tree Search Module...")
try:
from searching.tree_search.algorithms import BST, AVLTree, bst_search
from searching.tree_search.constraints import get_valid_tree_algorithms
from searching.tree_search.features import TreeSearchFeatureExtractor
bst = BST()
for k in [5, 3, 7, 1, 9]:
bst.insert(k)
found, comps = bst_search(bst, 7)
assert found == True, "Should find 7"
avl = AVLTree()
for k in range(1, 16):
avl.insert(k)
assert avl.get_balance_factor() < 0.3, "AVL should be balanced"
print(" ✅ Tree Search algorithms work correctly")
except Exception as e:
print(f" ❌ Error: {e}")
# Test 3: Structure Inference
print("\n3. Testing Structure Inference Module...")
try:
from utils.structure_inference import StructureInference, StructureType
si = StructureInference()
result = si.infer([1, 2, 3, 4, 5])
assert result.structure_type == StructureType.LINEAR_ARRAY
result = si.infer_from_problem("sort the array in ascending order")
assert result.structure_type == StructureType.LINEAR_ARRAY
result = si.infer_from_problem("find shortest path in graph")
assert result.structure_type == StructureType.GRAPH
print(" ✅ Structure Inference works correctly")
except Exception as e:
print(f" ❌ Error: {e}")
# Test 4: Web App Task Detection
print("\n4. Testing Web App Task Detection...")
try:
from web.app import detect_task
assert detect_task("sort the array") == "sorting"
assert detect_task("find pattern in string") == "string_search"
assert detect_task("search in binary tree") == "tree_search"
assert detect_task("find shortest path in graph") == "graph_search"
print(" ✅ Task detection works correctly")
except Exception as e:
print(f" ❌ Error: {e}")
# Test 5: Trained Models
print("\n5. Testing Trained Models...")
try:
models_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'models')
# Check string search model
string_model = os.path.join(models_dir, 'string_search_model.pkl')
if os.path.exists(string_model):
from searching.string_search.recommender import StringSearchRecommender
recommender = StringSearchRecommender()
recommender.load_model(models_dir=models_dir)
result = recommender.recommend("hello world", ["hello", "world"])
print(f" String Search: {result['algorithm']} (valid: {result['valid_algorithms']})")
print(" ✅ String Search model loaded and works")
else:
print(" ⚠️ String search model not found (train first)")
# Check tree search model
tree_model = os.path.join(models_dir, 'tree_search_model.pkl')
if os.path.exists(tree_model):
from searching.tree_search.recommender import TreeSearchRecommender
recommender = TreeSearchRecommender()
recommender.load_model(models_dir=models_dir)
avl = AVLTree()
for k in [5, 3, 7, 1, 9]:
avl.insert(k)
result = recommender.recommend(avl)
print(f" Tree Search: {result['algorithm']} (valid: {result['valid_algorithms']})")
print(" ✅ Tree Search model loaded and works")
else:
print(" ⚠️ Tree search model not found (train first)")
except Exception as e:
print(f" ❌ Error: {e}")
print("\n" + "=" * 60)
print("ALL TESTS COMPLETE")
print("=" * 60)