Tests that the ring buffer correctly tracks recently inserted values, up to a fixed size, and can be cleared to reset its state.
()
| 4 | |
| 5 | |
| 6 | def test_ring_buffer(): |
| 7 | """ |
| 8 | Tests that the ring buffer correctly tracks recently inserted values, up to |
| 9 | a fixed size, and can be cleared to reset its state. |
| 10 | """ |
| 11 | buffer = RingBuffer(maxlen=2) |
| 12 | assert_equal(buffer.maxlen, 2) |
| 13 | assert_equal(len(buffer), 0) |
| 14 | |
| 15 | obj1 = object() |
| 16 | obj2 = object() |
| 17 | |
| 18 | # Insert obj1, and test that the length and peek functions return the |
| 19 | # correct values: 1 item, and peek at the next slot should return None |
| 20 | # since we haven't set it yet. |
| 21 | buffer.append(obj1) |
| 22 | assert_equal(len(buffer), 1) |
| 23 | assert_(buffer.peek() is None) |
| 24 | |
| 25 | # Append obj2, and check that the buffer now has two items. Peeking at |
| 26 | # the next slot should wrap around, back to obj1, since we can store only |
| 27 | # two items. |
| 28 | buffer.append(obj2) |
| 29 | assert_equal(len(buffer), 2) |
| 30 | assert_(buffer.peek() is obj1) |
| 31 | |
| 32 | # Skip the next slot. This should not remove anything, but should cause |
| 33 | # ``peek()`` to now point to the slot occupied by obj2. |
| 34 | buffer.skip() |
| 35 | assert_equal(len(buffer), 2) |
| 36 | assert_(buffer.peek() is obj2) |
| 37 | |
| 38 | # Clearing the buffer should reset its entire state. |
| 39 | buffer.clear() |
| 40 | assert_equal(buffer.maxlen, 2) |
| 41 | assert_equal(len(buffer), 0) |
nothing calls this directly
no test coverage detected