Example #1
0
def test_degenerate():
    r = RingBuffer(0)
    np.testing.assert_equal(r, np.array([]))

    # this does not error with deque(maxlen=0), so should not error here
    try:
        r.append(0)
        r.append_left(0)
    except IndexError:
        assert True is False
Example #2
0
def test_append_left():
    r = RingBuffer(5)

    r.append_left(1)
    np.testing.assert_equal(r, np.array([1]))
    assert len(r) == 1

    r.append_left(2)
    np.testing.assert_equal(r, np.array([2, 1]))
    assert len(r) == 2

    r.append_left(3)
    r.append_left(4)
    r.append_left(5)
    np.testing.assert_equal(r, np.array([5, 4, 3, 2, 1]))
    assert len(r) == 5

    r.append_left(6)
    np.testing.assert_equal(r, np.array([6, 5, 4, 3, 2]))
    assert len(r) == 5
Example #3
0
def test_2d():
    r = RingBuffer(5, dtype=(np.float, 2))

    r.append([1, 2])
    np.testing.assert_equal(r, np.array([[1, 2]]))
    assert len(r) == 1
    assert np.shape(r), (1 == 2)

    r.append([3, 4])
    np.testing.assert_equal(r, np.array([[1, 2], [3, 4]]))
    assert len(r) == 2
    assert np.shape(r), (2 == 2)

    r.append_left([5, 6])
    np.testing.assert_equal(r, np.array([[5, 6], [1, 2], [3, 4]]))
    assert len(r) == 3
    assert np.shape(r), (3 == 2)

    np.testing.assert_equal(r[0], [5, 6])
    np.testing.assert_equal(r[0, :], [5, 6])
    np.testing.assert_equal(r[:, 0], [5, 1, 3])