Пример #1
0
    def test_degenerate(self):
        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.appendleft(0)
            r.extend([0])
            r.extendleft([0])
        except IndexError:
            self.fail()
Пример #2
0
    def test_pops(self):
        r = RingBuffer(3)
        r.append(1)
        r.appendleft(2)
        r.append(3)
        np.testing.assert_equal(r, np.array([2, 1, 3]))

        self.assertEqual(r.pop(), 3)
        np.testing.assert_equal(r, np.array([2, 1]))

        self.assertEqual(r.popleft(), 2)
        np.testing.assert_equal(r, np.array([1]))

        # test empty pops
        empty = RingBuffer(1)
        with self.assertRaisesRegex(IndexError, "empty"):
            empty.pop()
        with self.assertRaisesRegex(IndexError, "empty"):
            empty.popleft()
Пример #3
0
    def test_appendleft(self):
        r = RingBuffer(5)

        r.appendleft(1)
        np.testing.assert_equal(r, np.array([1]))
        self.assertEqual(len(r), 1)

        r.appendleft(2)
        np.testing.assert_equal(r, np.array([2, 1]))
        self.assertEqual(len(r), 2)

        r.appendleft(3)
        r.appendleft(4)
        r.appendleft(5)
        np.testing.assert_equal(r, np.array([5, 4, 3, 2, 1]))
        self.assertEqual(len(r), 5)

        r.appendleft(6)
        np.testing.assert_equal(r, np.array([6, 5, 4, 3, 2]))
        self.assertEqual(len(r), 5)
Пример #4
0
    def test_2d(self):
        r = RingBuffer(5, dtype=(float, 2))

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

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

        r.appendleft([5, 6])
        np.testing.assert_equal(r, np.array([[5, 6], [1, 2], [3, 4]]))
        self.assertEqual(len(r), 3)
        self.assertEqual(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])
Пример #5
0
    def test_no_overwrite(self):
        r = RingBuffer(3, allow_overwrite=False)
        r.append(1)
        r.append(2)
        r.appendleft(3)
        with self.assertRaisesRegex(IndexError, "overwrite"):
            r.appendleft(4)
        with self.assertRaisesRegex(IndexError, "overwrite"):
            r.extendleft([4])
        r.extendleft([])

        np.testing.assert_equal(r, np.array([3, 1, 2]))
        with self.assertRaisesRegex(IndexError, "overwrite"):
            r.append(4)
        with self.assertRaisesRegex(IndexError, "overwrite"):
            r.extend([4])
        r.extend([])

        # works fine if we pop the surplus
        r.pop()
        r.append(4)
        np.testing.assert_equal(r, np.array([3, 1, 4]))