Skip to content

Commit

Permalink
Merge pull request #427 from tynsh/master
Browse files Browse the repository at this point in the history
Fix confusion in circular buffer
  • Loading branch information
kmsquire committed Aug 24, 2018
2 parents f51d57b + 6bb0afa commit 1dd2322
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/circular_buffer.jl
Expand Up @@ -118,13 +118,11 @@ and overwrite back if full.
"""
function pushfirst!(cb::CircularBuffer, data)
# if full, decrement and overwrite, otherwise pushfirst
if length(cb) == cb.capacity
cb.first = (cb.first == 1 ? cb.capacity : cb.first - 1)
cb[1] = data
else
cb.first = (cb.first == 1 ? cb.capacity : cb.first - 1)
if length(cb) < cb.capacity
cb.length += 1
pushfirst!(cb.buffer, data)
end
cb.buffer[cb.first] = data
cb
end

Expand Down
10 changes: 10 additions & 0 deletions test/test_circular_buffer.jl
Expand Up @@ -57,6 +57,16 @@
end
end

@testset "Issue 429" begin
cb = CircularBuffer{Int}(5)
map(x -> pushfirst!(cb, x), 1:8)
pop!(cb)
pushfirst!(cb, 9)
@test length(cb.buffer) == cb.capacity
arr = convert(Array, cb)
@test arr == Int[9, 8, 7, 6, 5]
end

@testset "Issue 379" begin
cb = CircularBuffer{Int}(5)
pushfirst!(cb, 1)
Expand Down

0 comments on commit 1dd2322

Please sign in to comment.