88from common .utils import bytes_to_str
99
1010
11- # pylint: disable=missing-function-docstring
12-
13-
1411def _bytes_test_pattern (size ):
1512 return bytes ([i % 255 for i in range (size )])
1613
@@ -23,6 +20,9 @@ def _create_test_block(size):
2320
2421
2522def test_fragment_init ():
23+ """
24+ Initialize a fragment.
25+ """
2626 block = _create_test_block (100 )
2727 _fragment = Fragment (
2828 block = block ,
@@ -32,7 +32,10 @@ def test_fragment_init():
3232 )
3333
3434
35- def test_fragment_allocate_full ():
35+ def test_allocate_full ():
36+ """
37+ Allocate a fragment. Requested allocation is fully available.
38+ """
3639 block = _create_test_block (100 )
3740 fragment = Fragment .allocate (block , 10 )
3841 assert fragment .block == block
@@ -41,7 +44,10 @@ def test_fragment_allocate_full():
4144 assert fragment .data == bytes .fromhex ("00010203040506070809" )
4245
4346
44- def test_fragment_allocate_partial ():
47+ def test_allocate_partial ():
48+ """
49+ Allocate a fragment. Requested allocation is partially available.
50+ """
4551 block = _create_test_block (5 )
4652 fragment = Fragment .allocate (block , 10 )
4753 assert fragment .block == block
@@ -50,7 +56,10 @@ def test_fragment_allocate_partial():
5056 assert fragment .data == bytes .fromhex ("0001020304" )
5157
5258
53- def test_fragment_allocate_none ():
59+ def test_allocate_none ():
60+ """
61+ Allocate a fragment. No allocation is available.
62+ """
5463 block = _create_test_block (5 )
5564 fragment = Fragment .allocate (block , 5 )
5665 assert fragment .block == block
@@ -60,7 +69,41 @@ def test_fragment_allocate_none():
6069 assert Fragment .allocate (block , 5 ) is None
6170
6271
72+ def test_give_back_success ():
73+ """
74+ Give a fragment back to the block.
75+ """
76+ # pylint: disable=protected-access
77+ block = _create_test_block (16 )
78+ assert block .nr_used_bytes == 0
79+ # Allocate 5 bytes for fragment A
80+ assert block ._data == bytes .fromhex ("000102030405060708090a0b0c0d0e0f" )
81+ fragment_a = Fragment .allocate (block , 5 )
82+ assert fragment_a .block == block
83+ assert fragment_a .start == 0
84+ assert fragment_a .size == 5
85+ assert fragment_a .data == bytes .fromhex ("0001020304" )
86+ assert block .nr_used_bytes == 5
87+ assert block ._data == bytes .fromhex ("000000000005060708090a0b0c0d0e0f" )
88+ # Allocate 3 bytes for fragment B
89+ fragment_b = Fragment .allocate (block , 3 )
90+ assert fragment_b .block == block
91+ assert fragment_b .start == 5
92+ assert fragment_b .size == 3
93+ assert fragment_b .data == bytes .fromhex ("050607" )
94+ assert block .nr_used_bytes == 8
95+ assert block ._data == bytes .fromhex ("000000000000000008090a0b0c0d0e0f" )
96+ # Give fragment A back
97+ fragment_a .give_back ()
98+ assert fragment_a .data is None
99+ assert block .nr_used_bytes == 3
100+ assert block ._data == bytes .fromhex ("000102030400000008090a0b0c0d0e0f" )
101+
102+
63103def test_fragment_to_mgmt ():
104+ """
105+ Get the management status of a fragment.
106+ """
64107 block = _create_test_block (5 )
65108 fragment = Fragment .allocate (block , 5 )
66109 fragment_mgmt = fragment .to_mgmt ()
0 commit comments