-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_test.py
More file actions
212 lines (167 loc) · 6.3 KB
/
quick_test.py
File metadata and controls
212 lines (167 loc) · 6.3 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
"""
Quick Test Script for Algorithm Recommender System
===================================================
This script runs a quick test of the system without requiring
the full training process. Run this to verify installation.
Usage:
python quick_test.py
"""
import sys
import os
# Ensure we can import from the package
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
def test_sorting():
"""Test sorting algorithms and features."""
print("=" * 60)
print("TESTING SORTING MODULE")
print("=" * 60)
from sorting.algorithms import (
insertion_sort, selection_sort, merge_sort,
quick_sort, heap_sort
)
from sorting.features import SortingFeatureExtractor
# Test arrays
test_arr = [5, 2, 8, 1, 9, 3, 7]
expected = sorted(test_arr)
print(f"\nTest array: {test_arr}")
print(f"Expected: {expected}")
# Test each algorithm
algorithms = {
'insertion_sort': insertion_sort,
'selection_sort': selection_sort,
'merge_sort': merge_sort,
'quick_sort': quick_sort,
'heap_sort': heap_sort
}
all_passed = True
for name, algo in algorithms.items():
result = algo(test_arr)
passed = result == expected
status = "✓" if passed else "✗"
print(f" {status} {name}: {result}")
if not passed:
all_passed = False
# Test feature extraction
print("\nFeature extraction:")
extractor = SortingFeatureExtractor()
features = extractor.extract(test_arr)
print(f" Size: small={features['size_small']}")
print(f" Sortedness: {features['sortedness']:.2f}")
print(f" Duplicates: {features['duplicate_ratio']:.2f}")
print(f" Summary: {extractor.explain_features(features)}")
return all_passed
def test_array_search():
"""Test array search algorithms and features."""
print("\n" + "=" * 60)
print("TESTING ARRAY SEARCH MODULE")
print("=" * 60)
from searching.array_search.algorithms import (
linear_search, binary_search, jump_search,
exponential_search, get_valid_algorithms
)
from searching.array_search.features import ArraySearchFeatureExtractor
# Sorted array
sorted_arr = list(range(10))
target = 7
print(f"\nSorted array: {sorted_arr}")
print(f"Looking for: {target}")
# Test each algorithm
print("\nAlgorithm results:")
print(f" linear_search: {linear_search(sorted_arr, target)}")
print(f" binary_search: {binary_search(sorted_arr, target)}")
print(f" jump_search: {jump_search(sorted_arr, target)}")
print(f" exponential_search: {exponential_search(sorted_arr, target)}")
# Test feature extraction
extractor = ArraySearchFeatureExtractor()
features = extractor.extract(sorted_arr)
print("\nFeatures:")
print(f" is_sorted: {features['is_sorted']}")
print(f" is_uniform: {features['is_uniform']}")
print(f" Valid algorithms: {get_valid_algorithms(True, True)}")
# Test unsorted array
unsorted_arr = [5, 2, 8, 1, 9]
features_unsorted = extractor.extract(unsorted_arr)
print(f"\nUnsorted array {unsorted_arr}:")
print(f" is_sorted: {features_unsorted['is_sorted']}")
print(f" Valid algorithms: {get_valid_algorithms(False, False)}")
return True
def test_graph_search():
"""Test graph search algorithms and features."""
print("\n" + "=" * 60)
print("TESTING GRAPH SEARCH MODULE")
print("=" * 60)
from searching.graph_search.algorithms import (
bfs_search, dijkstra_search, bellman_ford_search,
get_valid_algorithms
)
from searching.graph_search.features import GraphSearchFeatureExtractor
# Simple graph
graph = {
0: [(1, 1.0), (2, 4.0)],
1: [(2, 2.0), (3, 5.0)],
2: [(3, 1.0)],
3: []
}
print("\nGraph (adjacency list):")
for node, edges in graph.items():
print(f" {node} -> {edges}")
print("\nShortest paths from 0 to 3:")
# BFS (treats all weights as 1)
bfs_path, bfs_cost = bfs_search(graph, 0, 3)
print(f" BFS: path={bfs_path}, cost={bfs_cost}")
# Dijkstra
dijk_path, dijk_cost = dijkstra_search(graph, 0, 3)
print(f" Dijkstra: path={dijk_path}, cost={dijk_cost}")
# Bellman-Ford
bf_path, bf_cost = bellman_ford_search(graph, 0, 3)
print(f" Bellman-Ford: path={bf_path}, cost={bf_cost}")
# Feature extraction
extractor = GraphSearchFeatureExtractor()
features = extractor.extract(graph)
print("\nGraph features:")
print(f" is_weighted: {features['is_weighted']}")
print(f" has_negative_weights: {features['has_negative_weights']}")
print(f" edge_density: {features['edge_density']:.2f}")
# Valid algorithms
print(f"\nValid algorithms (weighted, non-negative, no heuristic):")
print(f" {get_valid_algorithms(True, False, False)}")
# Test with negative weights
neg_graph = {
0: [(1, 5.0)],
1: [(2, -3.0)], # Negative weight
2: []
}
neg_features = extractor.extract(neg_graph)
print(f"\nGraph with negative edge:")
print(f" has_negative_weights: {neg_features['has_negative_weights']}")
print(f" Valid algorithms: {get_valid_algorithms(True, True, False)}")
return True
def main():
"""Run all tests."""
print("ALGORITHM RECOMMENDER SYSTEM - QUICK TEST")
print("=" * 60)
try:
sorting_ok = test_sorting()
array_ok = test_array_search()
graph_ok = test_graph_search()
print("\n" + "=" * 60)
print("TEST SUMMARY")
print("=" * 60)
print(f" Sorting: {'✓ PASS' if sorting_ok else '✗ FAIL'}")
print(f" Array Search: {'✓ PASS' if array_ok else '✗ FAIL'}")
print(f" Graph Search: {'✓ PASS' if graph_ok else '✗ FAIL'}")
if sorting_ok and array_ok and graph_ok:
print("\n✓ All tests passed! The system is ready to use.")
print("\nNext steps:")
print(" 1. Train models: python unified_interface.py")
print(" 2. Or use individual modules directly")
else:
print("\n✗ Some tests failed. Check the output above.")
except Exception as e:
print(f"\n✗ Error during testing: {e}")
import traceback
traceback.print_exc()
return 1
return 0
if __name__ == '__main__':
sys.exit(main())