-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_level2.py
More file actions
85 lines (74 loc) · 2.57 KB
/
test_level2.py
File metadata and controls
85 lines (74 loc) · 2.57 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
from level2_processing import FileServer
def run_case(name, fn):
try:
fn()
print(f"[PASS] {name}")
return True
except Exception as e:
print(f"[FAIL] {name} -> Exception: {e}")
return False
def test_level2():
passed = 0
total = 7
# Case 1: Basic search ordering by size desc
def c1():
fs = FileServer()
fs.FILE_UPLOAD("a.txt", 100)
fs.FILE_UPLOAD("b.txt", 200)
fs.FILE_UPLOAD("ax.txt", 150)
res = fs.FILE_SEARCH("a")
assert res == ["ax.txt", "a.txt"]
passed += run_case("Search sorts by size desc", c1)
# Case 2: Tie on size -> sort by name asc
def c2():
fs = FileServer()
fs.FILE_UPLOAD("apple.txt", 100)
fs.FILE_UPLOAD("apricot.txt", 100)
fs.FILE_UPLOAD("ape.txt", 100)
res = fs.FILE_SEARCH("ap")
assert res == ["ape.txt", "apple.txt", "apricot.txt"]
passed += run_case("Search tie-break by filename asc", c2)
# Case 3: Limit to top 10
def c3():
fs = FileServer()
for i in range(12):
fs.FILE_UPLOAD(f"pref{i}.txt", i)
res = fs.FILE_SEARCH("pref")
assert len(res) == 10
assert res[0] == "pref11.txt"
assert res[-1] == "pref2.txt"
passed += run_case("Search limits results to 10", c3)
# Case 4: Non-matching prefix returns empty list
def c4():
fs = FileServer()
fs.FILE_UPLOAD("x.txt", 1)
assert fs.FILE_SEARCH("nope") == []
passed += run_case("Non-matching prefix -> empty", c4)
# Case 5: Prefix exact file name match included
def c5():
fs = FileServer()
fs.FILE_UPLOAD("prefix", 10)
fs.FILE_UPLOAD("prefix_long", 5)
res = fs.FILE_SEARCH("prefix")
assert res == ["prefix", "prefix_long"]
passed += run_case("Exact name match is included", c5)
# Case 6: Overwrites don't break search ordering
def c6():
fs = FileServer()
fs.FILE_UPLOAD("a1", 10)
fs.FILE_UPLOAD("a2", 20)
fs.FILE_COPY("a2", "a1")
res = fs.FILE_SEARCH("a")
assert res[0] == "a1" and res[1] == "a2"
passed += run_case("Overwrite affects search ordering", c6)
# Case 7: Mixed names (case sensitivity)
def c7():
fs = FileServer()
fs.FILE_UPLOAD("Ab.txt", 5)
fs.FILE_UPLOAD("ab.txt", 6)
res = fs.FILE_SEARCH("ab")
assert res == ["ab.txt"]
passed += run_case("Case sensitivity of prefix matching", c7)
print(f"[RESULT] {passed}/{total} tests passed for Level 2.")
if __name__ == '__main__':
test_level2()