def test_dequeue(self):
        # initialize a buffer with 5 elements
        buf = CircularBuffer(5, [0, 1, 2, 3, 4])
        assert len(buf) == 5
        assert buf.dequeue() == 0
        assert buf.dequeue() == 1
        assert buf.dequeue() == 2
        assert buf.dequeue() == 3
        assert buf.dequeue() == 4
        assert len(buf) == 0

        # initialize a buffer with 5 elements, but add only 2
        buf = CircularBuffer(5, [0, 1, 2])
        assert len(buf) == 3
        assert buf.dequeue() == 0
        assert buf.dequeue() == 1
        assert buf.dequeue() == 2
        assert buf.dequeue() == None
        assert buf.dequeue() == None
        assert len(buf) == 0

        # initialize a buffer with 5 elements, but add more than 5
        buf = CircularBuffer(5, [0, 1, 2, 3, 4, 5, 6, 7])
        assert len(buf) == 5
        assert buf.dequeue() == 3
        assert buf.dequeue() == 4
        assert buf.dequeue() == 5
        assert buf.dequeue() == 6
        assert buf.dequeue() == 7
        assert buf.dequeue() == None
        assert len(buf) == 0
    def test_write_and_read_movement(self):
        cb = CircularBuffer(['A', 'B', 'C'])
        assert cb.write == 3
        assert cb.read == 0  # This better not be moving

        cb.queue('D')
        assert cb.write == 4
        assert cb.read == 0
        cb.dequeue()  # Should not do anything
        assert cb.write == 4
        assert cb.read == 1

        cb = CircularBuffer()  # Clearing
        for i in range(11):
            cb.queue(i)
        assert cb.write == 11
        assert cb.read == 0

        cb.queue(11)
        assert cb.write == 0
        assert cb.read == 0
        assert cb.dequeue() == 0

        # Should BOTH move
        cb.queue(12)
        assert cb.write == 1
        assert cb.read == 1
        assert cb.dequeue() == 1
Exemplo n.º 3
0
def test_create():
    buf = CircularBuffer(BUFFER_SIZE)
    assert isinstance(buf, CircularBuffer)
    assert buf.write_available() == BUFFER_SIZE
    assert len(buf) == 0
    assert str(buf) == ''
    buf = CircularBuffer(size=BUFFER_SIZE)
    assert isinstance(buf, CircularBuffer)
    assert repr(buf) == '<CircularBuffer[0]:>'
 def test_init(self):
     buf = CircularBuffer(5, [0, 1, 2, 3, 4])
     assert len(buf) == 5
     buf = CircularBuffer(5, [0, 1, 2, 3, 4, 5, 6, 7])
     assert len(buf) == 5
     buf = CircularBuffer(5, [0, 1, 2])
     assert len(buf) == 3
     buf = CircularBuffer(5, [0, 'hi', 2, 3, 4, 'why', 6, 7])
     assert len(buf) == 5
Exemplo n.º 5
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'
Exemplo n.º 6
0
 def test_init(self):
     cb = CircularBuffer(2)
     assert cb.size == 0
     assert cb.max == 2
     assert cb.list == [None, None]
     assert cb.head is None
     assert cb.tail is None
Exemplo n.º 7
0
 def test_is_empty(self):
     cb = CircularBuffer(2)
     assert cb.is_empty()
     cb.enqueue('test')
     assert not cb.is_empty()
     cb.dequeue()
     assert cb.is_empty()
    def test_queues(self):
        cb = CircularBuffer()
        for i in range(12):
            cb.queue(i)
        assert cb.read == 0
        assert cb.dequeue() == 0
        assert cb.read == 1
        assert cb.dequeue() == 1

        # Overwriting
        for i in range(12):
            cb.queue(i)
        assert cb.read == 0
        assert cb.dequeue() == 0
        assert cb.read == 1
        assert cb.dequeue() == 1

        # Let's keep dequeueing
        assert cb.dequeue() == 2
        assert cb.dequeue() == 3
        assert cb.dequeue() == 4
        assert cb.dequeue() == 5
        assert cb.dequeue() == 6
        assert cb.dequeue() == 7
        assert cb.dequeue() == 8
        assert cb.dequeue() == 9
        assert cb.dequeue() == 10
        assert cb.dequeue() == 11
        assert cb.dequeue() is None
        assert cb.dequeue() is None
        assert cb.read == 11
        assert cb.write == 0
 def test_init_with_list(self):
     cb = CircularBuffer(['A', 'B', 'C'])
     assert cb.write == 3
     assert cb.read == 0
     assert cb.buffer[0] == 'A'
     assert cb.buffer[1] == 'B'
     assert cb.buffer[2] == 'C'
Exemplo n.º 10
0
def test_count():
    buf = CircularBuffer(32)
    buf.write(b'asdf\r\njkl;\r\n1234\r\n')
    assert buf.count(b'\r\n') == 3
    buf.clear()
    buf.write(b'asdf\r\njkl;\r\n1234\r\na')
    assert buf.count(b'\r\n') == 3
Exemplo n.º 11
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)
Exemplo n.º 12
0
 def test_is_empty(self):
     c = CircularBuffer(3)
     assert c.is_empty() is True
     c.enqueue('A')
     assert c.is_empty() is False
     c.dequeue()
     assert c.is_empty() is True
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'
Exemplo n.º 14
0
 def test_init(self):
     c = CircularBuffer(3)
     assert c.max_size == 3
     assert c.size == 0
     assert c.start == 0
     assert c.end == 0
     assert c.is_empty() is True
     assert c.is_full() is False
Exemplo n.º 15
0
 def test_rollover(self):
     cb = CircularBuffer(5, ['A', 'B', 'C', 'D', 'E'])
     assert cb.front() == 'A'
     assert cb.length() == 5
     assert cb.is_full() is True
     cb.enqueue('X')
     assert cb.front() == 'X'
     assert cb.length() == 5
     assert cb.is_full() is True
Exemplo n.º 16
0
def test_resize():
    buf = CircularBuffer(10)
    assert buf.write_available() == 10
    buf.resize(5)
    assert buf.write_available() == 10
    buf.resize(15)
    assert buf.write_available() == 15
    buf.resize(size=20)
    assert buf.write_available() == 20
Exemplo n.º 17
0
 def test_enqueue_and_dequeue(self):
     abcd = ['a', 'b', 'c', 'd']
     cb = CircularBuffer(4)
     for i in range(4):
         cb.enqueue(abcd[i])
         assert cb.size == i + 1
     for i in range(3, 0):
         cb.dequeue()
         assert cb.size == i + 1
Exemplo n.º 18
0
 def test_overflow(self):
     cb = CircularBuffer(4)
     cb.enqueue(3)
     cb.enqueue(1)
     cb.enqueue(4)
     cb.enqueue(1)
     assert cb.front() == 3
     cb.enqueue(5)
     assert cb.front() == 1
     assert cb.dequeue() == 1
Exemplo n.º 19
0
    def __init__(self, loop, mgr):
        self.mgr = mgr
        self._loop = loop
        self._buf = CircularBuffer(conf['connection']['buffer_size'])

        log_name = type(self).__name__
        if self.name is not None:
            log_name += '(%s)' % self.name

        self._log = getLogger(log_name)
Exemplo n.º 20
0
 def test_dequeue(self):
     cb = CircularBuffer(4)
     cb.enqueue(3)
     cb.enqueue(1)
     cb.enqueue(4)
     cb.enqueue(1)
     assert cb.dequeue() == 3
     assert cb.dequeue() == 1
     assert cb.dequeue() == 4
     assert cb.dequeue() == 1
 def test_enqueue(self):
     buf = CircularBuffer(5)
     assert len(buf) == 0
     buf.enqueue('hi')
     assert len(buf) == 1
     buf.enqueue('boo')
     buf.enqueue('yo')
     buf.enqueue(1)
     buf.enqueue(2)
     buf.enqueue(10)
     assert len(buf) == 5
Exemplo n.º 22
0
 def test_front(self):
     q = CircularBuffer()
     assert q.front() is None
     q.enqueue('A')
     assert q.front() == 'A'
     q.enqueue('B')
     assert q.front() == 'A'
     q.dequeue()
     assert q.front() == 'B'
     q.dequeue()
     assert q.front() is None
Exemplo n.º 23
0
 def test_length(self):
     q = CircularBuffer()
     assert q.length() == 0
     q.enqueue('A')
     assert q.length() == 1
     q.enqueue('B')
     assert q.length() == 2
     q.dequeue()
     assert q.length() == 1
     q.dequeue()
     assert q.length() == 0
Exemplo n.º 24
0
 def test_dequeue(self):
     q = CircularBuffer(['A', 'B', 'C'])
     assert q.dequeue() == 'A'
     assert q.length() == 2
     assert q.dequeue() == 'B'
     assert q.length() == 1
     assert q.dequeue() == 'C'
     assert q.length() == 0
     assert q.is_empty() is True
     with self.assertRaises(ValueError):
         q.dequeue()
Exemplo n.º 25
0
 def test_is_full(self):
     c = CircularBuffer(3)
     assert c.is_full() is False
     c.enqueue('A')
     assert c.is_full() is False
     c.enqueue('B')
     assert c.is_full() is False
     c.enqueue('C')
     assert c.is_full() is True
     c.dequeue()
     assert c.is_full() is False
Exemplo n.º 26
0
 def test_is_full(self):
     cb = CircularBuffer(3)
     assert not cb.is_full()
     cb.enqueue('test')
     assert not cb.is_full()
     cb.enqueue('other test')
     assert not cb.is_full()
     cb.enqueue(12345)
     assert cb.is_full()
     cb.dequeue()
     assert not cb.is_full()
Exemplo n.º 27
0
 def test_length(self):
     cb = CircularBuffer(5)
     assert cb.length() == 0
     cb.enqueue('A')
     assert cb.length() == 1
     cb.enqueue('B')
     assert cb.length() == 2
     cb.dequeue()
     assert cb.length() == 1
     cb.dequeue()
     assert cb.length() == 0
Exemplo n.º 28
0
 def test_dequeue(self):
     cb = CircularBuffer(['A', 'B', 'C'])
     assert cb.dequeue() == 'A'
     assert cb.size == 2
     assert cb.dequeue() == 'B'
     assert cb.size == 1
     assert cb.dequeue() == 'C'
     assert cb.size == 0
     assert cb.is_empty() is True
     with self.assertRaises(ValueError):
         cb.dequeue()
Exemplo n.º 29
0
 def test_front(self):
     cb = CircularBuffer()
     assert cb.peek() is None
     cb.enqueue('A')
     assert cb.peek() == 'A'
     cb.enqueue('B')
     assert cb.peek() == 'A'
     cb.dequeue()
     assert cb.peek() == 'B'
     cb.dequeue()
     assert cb.peek() is None
Exemplo n.º 30
0
 def test_size(self):
     cb = CircularBuffer()
     assert cb.size == 0
     cb.enqueue('A')
     assert cb.size == 1
     cb.enqueue('B')
     assert cb.size == 2
     cb.dequeue()
     assert cb.size == 1
     cb.dequeue()
     assert cb.size == 0