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
Ejemplo n.º 2
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()
Ejemplo n.º 3
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_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
Ejemplo n.º 5
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
Ejemplo n.º 6
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_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
Ejemplo n.º 8
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
Ejemplo n.º 9
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
Ejemplo n.º 10
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()
Ejemplo n.º 11
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
Ejemplo n.º 12
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
Ejemplo n.º 13
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()
Ejemplo n.º 14
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()
Ejemplo n.º 15
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
Ejemplo n.º 16
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
Ejemplo n.º 17
0
 def test_front(self):
     c = CircularBuffer(3)
     assert c.front() is None
     c.enqueue('A')
     assert c.front() == 'A'
     c.enqueue('B')
     assert c.front() == 'A'
     c.dequeue()
     assert c.front() == 'B'
     c.dequeue()
     assert c.front() is None
     c.enqueue('C')
     assert c.front() == 'C'
Ejemplo 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
Ejemplo n.º 19
0
 def test_front(self):
     abcd = ['a', 1, ['dsf', 5, '324'], {'d': 1002}]
     cb = CircularBuffer(4)
     for i in range(4):
         cb.enqueue(abcd[i])
         assert cb.size == i + 1
     assert cb.front() == 'a'
     cb.dequeue()
     assert cb.front() == 1
     cb.dequeue()
     assert cb.front() == ['dsf', 5, '324']
     cb.dequeue()
     assert cb.front() == {'d': 1002}
     cb.dequeue()
Ejemplo n.º 20
0
 def test_dequeue(self):
     c = CircularBuffer(3)
     with self.assertRaises(ValueError):
         c.dequeue()
     c.enqueue('A')
     c.enqueue('B')
     c.enqueue('C')
     assert c.dequeue() == 'A'
     c.enqueue('D')
     assert c.dequeue() == 'B'
     assert c.dequeue() == 'C'
     c.enqueue('E')
     c.enqueue('F')
     assert c.dequeue() == 'D'
     assert c.dequeue() == 'E'
     assert c.dequeue() == 'F'
     with self.assertRaises(ValueError):
         c.dequeue()