Skip to content

Commit f90dd8c

Browse files
authored
Merge pull request #780 from robert-wright/master
Proposed bug fix for bad state when using CircularBuffer push!
2 parents 2e74620 + 4503128 commit f90dd8c

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/circular_buffer.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,18 @@ end
101101
102102
Add an element to the back and overwrite front if full.
103103
"""
104-
@inline function Base.push!(cb::CircularBuffer, data)
104+
@inline function Base.push!(cb::CircularBuffer{T}, data) where T
105+
106+
# As per the behaviour of Base.push!
107+
data_converted = convert(T, data)
108+
105109
# if full, increment and overwrite, otherwise push
106110
if cb.length == cb.capacity
107111
cb.first = (cb.first == cb.capacity ? 1 : cb.first + 1)
108112
else
109113
cb.length += 1
110114
end
111-
@inbounds cb.buffer[_buffer_index(cb, cb.length)] = data
115+
@inbounds cb.buffer[_buffer_index(cb, cb.length)] = data_converted
112116
return cb
113117
end
114118

test/test_circular_buffer.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,15 @@
165165
end
166166
end
167167

168+
@testset "Issue 754" begin
169+
cb = CircularBuffer{Int}(5)
170+
@test size(cb) == (0,)
171+
@test_throws InexactError push!(cb, 1.5)
172+
@test size(cb) == (0,)
173+
push!(cb, 1.0)
174+
@test size(cb) == (1,)
175+
end
176+
168177
@testset "resize!" begin
169178
@testset "resize an empty buffer" begin
170179
cb = CircularBuffer{Int}(3)

0 commit comments

Comments
 (0)