示例#1
0
def test_sequence_two_sections():
    buf = CircularBuffer(10)
    assert buf.write(b'1234567890') == 10
    assert buf.write_available() == 0
    assert len(buf) == 10
    assert str(buf) == '1234567890'

    # where the first section is longer than the second
    assert buf.read(3) == b'123'
    assert buf.write(b'123') == 3
    assert buf.write_available() == 0
    assert len(buf) == 10
    assert str(buf) == '4567890123'

    # where the first section is equal length with the second
    assert buf.read(2) == b'45'
    assert buf.write(b'12') == 2
    assert buf.write_available() == 0
    assert len(buf) == 10
    assert str(buf) == '6789012312'

    # where the first section is shorter than the second
    assert buf.read(3) == b'678'
    assert buf.write(b'123') == 3
    assert buf.write_available() == 0
    assert len(buf) == 10
    assert str(buf) == '9012312123'

    # where the first section is only one byte
    assert buf.read(1) == b'9'
    assert buf.write_available() == 1
    assert len(buf) == 9
    assert str(buf) == '012312123'
示例#2
0
def test_sequence_two_sections():
    buf = CircularBuffer(10)
    assert buf.write(b'1234567890') == 10
    assert buf.write_available() == 0
    assert len(buf) == 10
    assert str(buf) == '1234567890'

    # where the first section is longer than the second
    assert buf.read(3) == b'123'
    assert buf.write(b'123') == 3
    assert buf.write_available() == 0
    assert len(buf) == 10
    assert str(buf) == '4567890123'

    # where the first section is equal length with the second
    assert buf.read(2) == b'45'
    assert buf.write(b'12') == 2
    assert buf.write_available() == 0
    assert len(buf) == 10
    assert str(buf) == '6789012312'

    # where the first section is shorter than the second
    assert buf.read(3) == b'678'
    assert buf.write(b'123') == 3
    assert buf.write_available() == 0
    assert len(buf) == 10
    assert str(buf) == '9012312123'

    # where the first section is only one byte
    assert buf.read(1) == b'9'
    assert buf.write_available() == 1
    assert len(buf) == 9
    assert str(buf) == '012312123'
def test_make_contiguous():
    buf = CircularBuffer(10)
    #'#          '#
    assert buf.write(b'1234') == 4
    #'1234#      '#
    buf.make_contiguous()
    assert str(buf) == '1234'

    assert buf.write(b'5678') == 4
    #'12345678#  '#
    buf.make_contiguous()
    assert str(buf) == '12345678'

    assert buf.read(2) == b'12'
    #'  345678#  '#
    assert buf.write(b'9012') == 4
    #'2#345678901'#
    assert str(buf) == '3456789012'
    buf.make_contiguous()
    #'3456789012#'#
    assert str(buf) == '3456789012'

    assert buf.read(7) == b'3456789'
    #'       012#'#
    assert str(buf) == '012'
    assert buf.write(b'1234567') == 7
    #'234567#0121'#
    assert str(buf) == '0121234567'
    buf.make_contiguous()
    #'0121234567#'#
    assert str(buf) == '0121234567'
示例#4
0
def test_repr():
    buf = CircularBuffer(70)
    buf.write(b'a' * 32)
    assert len(buf) == 32
    assert repr(buf) == '<CircularBuffer[32]:' + ('a' * 32) + '>'
    assert str(buf) == 'a' * 32
    buf.read(32)
    assert len(buf) == 0
    buf.write(b'b' * 64)
    assert len(buf) == 64
    assert repr(buf) == '<CircularBuffer[64]:' + ('b' * 41) + '..>'
    assert str(buf) == 'b' * 64
示例#5
0
def test_repr():
    buf = CircularBuffer(70)
    buf.write(b'a' * 32)
    assert len(buf) == 32
    assert repr(buf) == '<CircularBuffer[32]:' + ('a' * 32) + '>'
    assert str(buf) == 'a' * 32
    buf.read(32)
    assert len(buf) == 0
    buf.write(b'b' * 64)
    assert len(buf) == 64
    assert repr(buf) == '<CircularBuffer[64]:' + ('b' * 41) + '..>'
    assert str(buf) == 'b' * 64
def test_read_write():
    buf = CircularBuffer(15)
    #'#               #'
    assert buf.write_available() == 15

    assert buf.write(b'12345') == 5
    #'12345#          #'
    assert buf.write_available() == 10
    assert str(buf) == '12345'

    assert buf.read(2) == b'12'
    #'  345#          #'
    assert buf.write_available() == 12
    assert str(buf) == '345'

    assert buf.read(size=3) == b'345'
    #'     #          #'
    assert buf.write_available() == 15
    assert str(buf) == ''

    assert buf.write(b'1234567890') == 10
    #'     1234567890##'
    assert buf.write_available() == 5
    assert str(buf) == '1234567890'

    assert buf.write(b'12') == 2
    #'2#   12345678901#'
    assert buf.write_available() == 3
    assert str(buf) == '123456789012'

    assert buf.write(b'123456') == 3
    #'2123#12345678901#'
    assert buf.write_available() == 0
    assert str(buf) == '123456789012123'

    assert buf.resize(20) == 20
    #'2123#12345678901     #'
    assert buf.write_available() == 0
    assert str(buf) == '123456789012123'

    assert buf.read(10) == b'1234567890'
    #'2123#          1     #'
    assert buf.write_available() == 10
    assert str(buf) == '12123'

    assert buf.read(2) == b'12'
    #' 123#                #'
    assert buf.write_available() == 17
    assert str(buf) == '123'
def test_read_write():
    buf = CircularBuffer(15)
    #'#               #'
    assert buf.write_available() == 15

    assert buf.write(b'12345') == 5
    #'12345#          #'
    assert buf.write_available() == 10
    assert str(buf) == '12345'

    assert buf.read(2) == b'12'
    #'  345#          #'
    assert buf.write_available() == 12
    assert str(buf) == '345'

    assert buf.read(size=3) == b'345'
    #'     #          #'
    assert buf.write_available() == 15
    assert str(buf) == ''

    assert buf.write(b'1234567890') == 10
    #'     1234567890##'
    assert buf.write_available() == 5
    assert str(buf) == '1234567890'

    assert buf.write(b'12') == 2
    #'2#   12345678901#'
    assert buf.write_available() == 3
    assert str(buf) == '123456789012'

    assert buf.write(b'123456') == 3
    #'2123#12345678901#'
    assert buf.write_available() == 0
    assert str(buf) == '123456789012123'

    assert buf.resize(20) == 20
    #'2123#12345678901     #'
    assert buf.write_available() == 0
    assert str(buf) == '123456789012123'

    assert buf.read(10) == b'1234567890'
    #'2123#          1     #'
    assert buf.write_available() == 10
    assert str(buf) == '12123'

    assert buf.read(2) == b'12'
    #' 123#                #'
    assert buf.write_available() == 17
    assert str(buf) == '123'
示例#8
0
def test_buffer():
    buf = CircularBuffer(15)
    regex = re.compile(br'(5)')

    # one segment
    assert buf.write(b'12345') == 5
    with buf as buf_:
        match = regex.search(buf_)
        assert match is not None

    # floated one segment
    assert buf.read(5) == b'12345'
    assert buf.write(b'67890') == 5
    with buf as buf_:
        match = regex.search(buf_)
        assert match is None

    # two segments
    assert buf.write(b'1234567') == 7
    with buf:
        match = regex.search(buf)
        assert match is not None

    # python2 forced to use context manager
    if sys.version_info[0] < 3:
        with raises(TypeError):
            match = regex.search(buf)
    else:
        match = regex.search(buf)
示例#9
0
import time

from circularbuffer import CircularBuffer

ba = bytearray(10)

b = CircularBuffer(48)
print(b)
print(b.write(b"Hello"))
print(b)
print(b.peek(3))
print(b.drop(2))
print(b)
print(b.peek(4))
print(b)
print(b.read(4))
print(b)
print(b.read(4))
print(b)
print(b.write(b"Hello"))
print(b)
print(b.clear())
print(b)
print("writemsg", b.writemsg(b"Hello"))
print(b)
print(b.peek(10))
print("peekmsg", b.peekmsg())
print(b)
print("readmsg", b.readmsg())
print(b)
print("readmsg", b.readmsg())