Commit 9ae2b0c2 authored by Armin Rigo's avatar Armin Rigo
Browse files

Test and fix

parent a7b303390e51
......@@ -99,9 +99,10 @@ class SubBuffer(Buffer):
self.readonly = buffer.readonly
if isinstance(buffer, SubBuffer): # don't nest them
# we want a view (offset, size) over a view
# (buffer.offset, buffer.size) over buffer.buffer
at_most = buffer.size - offset
if size > at_most:
# (buffer.offset, buffer.size) over buffer.buffer.
# Note that either '.size' can be -1 to mean 'up to the end'.
at_most = buffer.getlength() - offset
if size > at_most or size < 0:
if at_most < 0:
at_most = 0
size = at_most
......
......@@ -45,6 +45,19 @@ def test_as_str_and_offset_maybe():
ssbuf = SubBuffer(sbuf, 3, 2)
assert ssbuf.getslice(0, 2, 1, 2) == 'ld'
assert ssbuf.as_str_and_offset_maybe() == ('hello world', 9)
#
ss2buf = SubBuffer(sbuf, 1, -1)
assert ss2buf.as_str() == 'orld'
assert ss2buf.getlength() == 4
ss3buf = SubBuffer(ss2buf, 1, -1)
assert ss3buf.as_str() == 'rld'
assert ss3buf.getlength() == 3
#
ss4buf = SubBuffer(buf, 3, 4)
assert ss4buf.as_str() == 'lo w'
ss5buf = SubBuffer(ss4buf, 1, -1)
assert ss5buf.as_str() == 'o w'
assert ss5buf.getlength() == 3
def test_repeated_subbuffer():
buf = StringBuffer('x' * 10000)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment