Repository navigation
Expand file tree
/
Copy pathtest_level3.py
More file actions
90 lines (77 loc) · 2.82 KB
/
Copy pathtest_level3.py
File metadata and controls
90 lines (77 loc) · 2.82 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
from level3_refactor 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_level3():
passed = 0
total = 9
def c1():
fs = FileServer()
fs.FILE_UPLOAD_AT(10, "f.txt", 50, ttl=5)
assert fs.FILE_GET_AT(14, "f.txt") == 50
assert fs.FILE_GET_AT(15, "f.txt") is None
passed += run_case("TTL boundary behavior", c1)
def c2():
fs = FileServer()
fs.FILE_UPLOAD_AT(0, "perm.txt", 1)
assert fs.FILE_GET_AT(10000, "perm.txt") == 1
passed += run_case("Upload without ttl lives forever", c2)
def c3():
fs = FileServer()
fs.FILE_UPLOAD_AT(10, "orig.txt", 100, ttl=20)
fs.FILE_COPY_AT(12, "orig.txt", "c.txt")
assert fs.FILE_GET_AT(31, "c.txt") == 100
assert fs.FILE_GET_AT(33, "c.txt") is None
passed += run_case("Copy inherits ttl and uses copy timestamp", c3)
def c4():
fs = FileServer()
fs.FILE_UPLOAD_AT(0, "a.txt", 10, ttl=5)
try:
fs.FILE_COPY_AT(6, "a.txt", "b.txt")
except RuntimeError:
return
raise AssertionError("Expected RuntimeError copying from expired source")
passed += run_case("Copy from expired source raises", c4)
def c5():
fs = FileServer()
fs.FILE_UPLOAD_AT(0, "f1", 10)
fs.FILE_UPLOAD_AT(5, "f2", 20, ttl=10)
fs.FILE_UPLOAD_AT(6, "f3", 20, ttl=2)
res = fs.FILE_SEARCH_AT(9, "f")
assert res == ["f2", "f1"]
passed += run_case("FILE_SEARCH_AT filters expired files", c5)
def c6():
fs = FileServer()
fs.FILE_UPLOAD_AT(0, "dup", 1)
try:
fs.FILE_UPLOAD_AT(1, "dup", 2)
except RuntimeError:
return
raise AssertionError("Expected RuntimeError on duplicate timestamped upload")
passed += run_case("Duplicate timestamped upload raises", c6)
def c7():
fs = FileServer()
assert fs.FILE_GET_AT(10, "missing") is None
passed += run_case("FILE_GET_AT missing returns None", c7)
def c8():
fs = FileServer()
fs.FILE_UPLOAD_AT(0, "s", 5)
fs.FILE_UPLOAD_AT(1, "t", 9)
fs.FILE_COPY_AT(2, "t", "s")
assert fs.FILE_GET_AT(3, "s") == 9
passed += run_case("Copy overwrites dest with correct size", c8)
def c9():
fs = FileServer()
fs.FILE_UPLOAD_AT(0, "temp.txt", 10, ttl=5)
assert fs.FILE_GET_AT(5, "temp.txt") is None
fs.FILE_UPLOAD_AT(6, "temp.txt", 20, ttl=5)
assert fs.FILE_GET_AT(10, "temp.txt") == 20
passed += run_case("Reupload after expiration allowed", c9)
print(f"[RESULT] {passed}/{total} tests passed for Level 3.")
if __name__ == '__main__':
test_level3()