Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix extend-with-overflow #18

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions numpy_ringbuffer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ def __init__(self, capacity, dtype=float, allow_overwrite=True):
If false, throw an IndexError when trying to append to an already
full buffer
"""
# Check parameters
if not capacity >= 0:
raise ValueError('capacity must be non-negative')

self._arr = np.empty(capacity, dtype)
self._left_index = 0
self._right_index = 0
Expand Down Expand Up @@ -74,7 +78,7 @@ def append(self, value):
if self.is_full:
if not self._allow_overwrite:
raise IndexError('append to a full RingBuffer with overwrite disabled')
elif not len(self):
elif not self._capacity:
return
else:
self._left_index += 1
Expand All @@ -87,7 +91,7 @@ def appendleft(self, value):
if self.is_full:
if not self._allow_overwrite:
raise IndexError('append to a full RingBuffer with overwrite disabled')
elif not len(self):
elif not self._capacity:
return
else:
self._right_index -= 1
Expand Down Expand Up @@ -117,7 +121,7 @@ def extend(self, values):
if len(self) + lv > self._capacity:
if not self._allow_overwrite:
raise IndexError('extend a RingBuffer such that it would overflow, with overwrite disabled')
elif not len(self):
elif not self._capacity:
return
if lv >= self._capacity:
# wipe the entire array! - this may not be threadsafe
Expand All @@ -141,7 +145,7 @@ def extendleft(self, values):
if len(self) + lv > self._capacity:
if not self._allow_overwrite:
raise IndexError('extend a RingBuffer such that it would overflow, with overwrite disabled')
elif not len(self):
elif not self._capacity:
return
if lv >= self._capacity:
# wipe the entire array! - this may not be threadsafe
Expand Down
19 changes: 14 additions & 5 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
class TestAll(unittest.TestCase):
def test_dtype(self):
r = RingBuffer(5)
self.assertEqual(r.dtype, np.dtype(np.float64))
self.assertEqual(r.dtype, np.float64)

r = RingBuffer(5, dtype=np.bool)
self.assertEqual(r.dtype, np.dtype(np.bool))
r = RingBuffer(5, dtype=bool)
self.assertEqual(r.dtype, bool)

def test_sizes(self):
r = RingBuffer(5, dtype=(int, 2))
Expand Down Expand Up @@ -95,6 +95,15 @@ def test_extend(self):
r.extend([1, 2, 3, 4, 5, 6, 7])
np.testing.assert_equal(r, np.array([3, 4, 5, 6, 7]))

# test empty extend with overflow
r = RingBuffer(5)
r.extendleft([1, 2, 3, 4, 5, 6, 7])
np.testing.assert_equal(r, np.array([1, 2, 3, 4, 5]))

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

def test_pops(self):
r = RingBuffer(3)
r.append(1)
Expand All @@ -116,7 +125,7 @@ def test_pops(self):
empty.popleft()

def test_2d(self):
r = RingBuffer(5, dtype=(np.float, 2))
r = RingBuffer(5, dtype=(float, 2))

r.append([1, 2])
np.testing.assert_equal(r, np.array([[1, 2]]))
Expand Down Expand Up @@ -145,7 +154,7 @@ def test_iter(self):
self.assertEqual(i, j)

def test_repr(self):
r = RingBuffer(5, dtype=np.int)
r = RingBuffer(5, dtype=int)
for i in range(5):
r.append(i)

Expand Down