Skip to content

Commit

Permalink
Fix get-last index out of bounds issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Informaticore committed Apr 5, 2024
1 parent 77262a4 commit 3b2ef66
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ringbuffer.toit
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,4 @@ class RingBuffer:
return math.sqrt variance / size_

get_last -> float:
return buffer[head - 1]
return buffer[(head - 1 + size_) % size_]
8 changes: 8 additions & 0 deletions test/test_ringbuffer.toit
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class TestRingbuffer implements TestCase:
test_ringbuffer_max
test_ringbuffer_limit
test_ringbuffer_get_last
test_ringbuffer_get_last_when_full
test_ringbuffer_std_deviation

name -> string:
Expand Down Expand Up @@ -105,6 +106,13 @@ class TestRingbuffer implements TestCase:
last = ringbuffer.get_last
assertEquals 2.0 last

test_ringbuffer_get_last_when_full:
ringbuffer := RingBuffer 5
5.repeat:
ringbuffer.append (it + 1).to-float
last := ringbuffer.get_last
assertEquals 5.0 last

test_ringbuffer_std_deviation:
ringbuffer := RingBuffer 5

Expand Down

0 comments on commit 3b2ef66

Please sign in to comment.