Skip to content

Commit ec1aa8b

Browse files
committed
Refactor progress
1 parent 7a7ac8b commit ec1aa8b

2 files changed

Lines changed: 49 additions & 3 deletions

File tree

common/fragment.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,12 @@ def allocate(block: Block, desired_size: int) -> Union[None, "Fragment"]:
7474
result = block.allocate_data(desired_size)
7575
if result is None:
7676
return None
77+
(start, size, data) = result
7778
return Fragment(
7879
block=block,
79-
start=result.start,
80-
size=result.size,
81-
data=result.data,
80+
start=start,
81+
size=size,
82+
data=data,
8283
)
8384

8485
def to_mgmt(self) -> dict:

common/tests/test_fragment.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,48 @@ def test_fragment_init():
2929
size=10,
3030
data=bytes.fromhex("00010203040506070809"),
3131
)
32+
33+
34+
def test_fragment_properties():
35+
"""
36+
Test Fragment properties.
37+
"""
38+
pass
39+
40+
41+
def test_fragment_allocate_full():
42+
"""
43+
Test Fragment allocation: requested bytes are fully available.
44+
(This also covers all the @property methods.)
45+
"""
46+
block = _create_test_block(100)
47+
fragment = Fragment.allocate(block, 10)
48+
assert fragment.block == block
49+
assert fragment.start == 0
50+
assert fragment.size == 10
51+
assert fragment.data == bytes.fromhex("00010203040506070809")
52+
53+
54+
def test_fragment_allocate_partial():
55+
"""
56+
Test Fragment allocation: requested bytes are partially available.
57+
"""
58+
block = _create_test_block(5)
59+
fragment = Fragment.allocate(block, 10)
60+
assert fragment.block == block
61+
assert fragment.start == 0
62+
assert fragment.size == 5
63+
assert fragment.data == bytes.fromhex("0001020304")
64+
65+
66+
def test_fragment_allocate_none():
67+
"""
68+
Test Fragment allocation: requested bytes are not available.
69+
"""
70+
block = _create_test_block(5)
71+
fragment = Fragment.allocate(block, 5)
72+
assert fragment.block == block
73+
assert fragment.start == 0
74+
assert fragment.size == 5
75+
assert fragment.data == bytes.fromhex("0001020304")
76+
assert Fragment.allocate(block, 5) is None

0 commit comments

Comments
 (0)