Пример #1
0
    def test_addresses(self):
        r = RingBuffer(5)
        r.extend([1, 2, 3])
        np.testing.assert_equal(r, np.array([1, 2, 3]))
        self.assertNotEqual(r.current_address, r.unwrap_address)

        r.popleft()
        r.extend([4, 5, 6])
        np.testing.assert_equal(r, np.array([2, 3, 4, 5, 6]))
        self.assertEqual(r.current_address, r.unwrap_address)

        r.extendleft([0, 1])
        np.testing.assert_equal(r, np.array([0, 1, 2, 3, 4]))
        self.assertEqual(r.current_address, r.unwrap_address)

        r.extendleft([1, 2, 3, 4, 5, 6, 7])
        np.testing.assert_equal(r, np.array([1, 2, 3, 4, 5]))
        self.assertEqual(r.current_address, r.unwrap_address)

        r.extend([1, 2, 3, 4, 5, 6, 7])
        np.testing.assert_equal(r, np.array([3, 4, 5, 6, 7]))
        self.assertEqual(r.current_address, r.unwrap_address)

        r.clear()
        np.testing.assert_equal(r, np.array([]))
        np.testing.assert_equal(len(r), 0)
        self.assertNotEqual(r.current_address, r.unwrap_address)
Пример #2
0
    def test_getitem(self):
        r = RingBuffer(5)
        r.extend([1, 2, 3])
        r.extendleft([4, 5])
        expected = np.array([4, 5, 1, 2, 3])
        np.testing.assert_equal(r, expected)

        for i in range(r.maxlen):
            self.assertEqual(expected[i], r[i])

        ii = [0, 4, 3, 1, 2]
        np.testing.assert_equal(r[ii], expected[ii])
Пример #3
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()
Пример #4
0
    def test_extend(self):
        r = RingBuffer(5)

        r.extend([1, 2, 3])
        np.testing.assert_equal(r, np.array([1, 2, 3]))

        r.popleft()
        r.extend([4, 5, 6])
        np.testing.assert_equal(r, np.array([2, 3, 4, 5, 6]))

        r.extendleft([0, 1])
        np.testing.assert_equal(r, np.array([0, 1, 2, 3, 4]))

        r.extendleft([1, 2, 3, 4, 5, 6, 7])
        np.testing.assert_equal(r, np.array([1, 2, 3, 4, 5]))

        r.extend([1, 2, 3, 4, 5, 6, 7])
        np.testing.assert_equal(r, np.array([3, 4, 5, 6, 7]))
Пример #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]))