Skip to content

Commit c681530

Browse files
committed
add test case of decode_variable_length in util/bytes
1 parent 2efc6dd commit c681530

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

pymysqlreplication/tests/test_util_bytes.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,38 @@ def test_empty_data(self):
6666
self.assertEqual(result, 0)
6767

6868

69+
class TestDecodeVariableLength(unittest.TestCase):
70+
def test_single_byte(self):
71+
# Test with a single byte where the high bit is not set (indicating the end)
72+
data = bytearray([0x05]) # 5 with the high bit not set
73+
length, pos = decode_variable_length(data)
74+
self.assertEqual(length, 5)
75+
self.assertEqual(pos, 1)
76+
77+
def test_multiple_bytes(self):
78+
# Test with multiple bytes
79+
# 0x81 -> 1 with the high bit set, indicating more bytes
80+
# 0x01 -> 1 with the high bit not set, indicating the end
81+
# Combined value is 1 + (1 << 7) = 129
82+
data = bytearray([0x81, 0x01])
83+
length, pos = decode_variable_length(data)
84+
self.assertEqual(length, 129)
85+
self.assertEqual(pos, 2)
86+
87+
def test_max_length(self):
88+
# Test with the maximum length (5 bytes)
89+
# This will test the boundary condition of the loop in the function
90+
data = bytearray(
91+
[0x80, 0x80, 0x80, 0x80, 0x01]
92+
) # Each 0x80 has the high bit set, 0x01 does not
93+
# The value is 1 << (7 * 4) = 2**28
94+
length, pos = decode_variable_length(data)
95+
self.assertEqual(length, 2**28)
96+
self.assertEqual(pos, 5)
97+
98+
# You can add more test cases to cover different scenarios and edge cases.
99+
100+
69101
class TestParseUint16(unittest.TestCase):
70102
def test_valid_input(self):
71103
data = bytearray([0x01, 0x00]) # Represents the unsigned integer 1

0 commit comments

Comments
 (0)