-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_explanation_file.py
More file actions
66 lines (56 loc) · 2.36 KB
/
test_explanation_file.py
File metadata and controls
66 lines (56 loc) · 2.36 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
"""Test: verify regime-aware explanation generation - writes to file."""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
output_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'test_results_explanation.txt')
from sorting.recommender import SortingRecommender
from sorting.features import SortingFeatureExtractor
lines = []
r = SortingRecommender()
r.load_model()
lines.append("=" * 70)
lines.append("TEST 1: Small random array (n=10)")
lines.append("=" * 70)
res = r.recommend([5, 2, 8, 1, 9, 3, 7, 4, 6, 10])
lines.append(f"Algorithm: {res['algorithm']}")
lines.append(f"Has justification: {'<strong>' in res['explanation']}")
lines.append(f"Explanation (first 300 chars): {res['explanation'][:300]}")
lines.append("")
lines.append("=" * 70)
lines.append("TEST 2: Small nearly-sorted array (n=30)")
lines.append("=" * 70)
res = r.recommend(list(range(1, 31)))
lines.append(f"Algorithm: {res['algorithm']}")
lines.append(f"Has justification: {'<strong>' in res['explanation']}")
lines.append("")
lines.append("=" * 70)
lines.append("TEST 3: Large random array (n=5000) - should NOT have justification")
lines.append("=" * 70)
import random; random.seed(42)
res = r.recommend([random.random() for _ in range(5000)])
lines.append(f"Algorithm: {res['algorithm']}")
lines.append(f"Has justification: {'<strong>' in res['explanation']}")
lines.append(f"Explanation: {res['explanation']}")
lines.append("")
lines.append("=" * 70)
lines.append("TEST 4: Reverse sorted small (n=30) - merge_sort, no justification")
lines.append("=" * 70)
res = r.recommend(list(range(30, 0, -1)))
lines.append(f"Algorithm: {res['algorithm']}")
lines.append(f"Has justification: {'<strong>' in res['explanation']}")
lines.append(f"Explanation: {res['explanation']}")
lines.append("")
lines.append("=" * 70)
lines.append("TEST 5: _n_from_features round-trip")
lines.append("=" * 70)
ext = SortingFeatureExtractor()
for test_n in [5, 10, 20, 30, 40, 50, 100, 1000]:
arr = list(range(test_n))
feats = ext.extract(arr)
recovered = r._n_from_features(feats)
match = "OK" if recovered == test_n else f"MISMATCH (got {recovered})"
lines.append(f" n={test_n:5d} -> size_log={feats['size_log']:.6f} -> recovered={recovered:5d} {match}")
lines.append("")
lines.append("ALL TESTS COMPLETE")
with open(output_file, 'w', encoding='utf-8') as f:
f.write('\n'.join(lines))