Skip to content

Commit

Permalink
fix #20
Browse files Browse the repository at this point in the history
  • Loading branch information
findmyway committed Mar 23, 2024
1 parent b3d49d2 commit 47377ea
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/CircularArrayBuffers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ Base.getindex(cb::CircularVectorBuffer, i, j) = getindex(cb.buffer, _buffer_fram
Base.setindex!(cb::CircularArrayBuffer{T,N}, v, i::Int) where {T,N} = setindex!(cb.buffer, v, _buffer_index(cb, i))
Base.setindex!(cb::CircularArrayBuffer{T,N}, v, I...) where {T,N} = setindex!(cb.buffer, v, Base.front(I)..., _buffer_frame(cb, Base.last(I)))

Base.view(cb::CircularArrayBuffer, i::Int) = view(cb.buffer, _buffer_index(cb, i))
Base.view(cb::CircularArrayBuffer, I...) = view(cb.buffer, Base.front(I)..., _buffer_frame(cb, Base.last(I)))
Base.view(cb::CircularArrayBuffer, i::Int) = (@boundscheck checkbounds(cb, i); view(cb.buffer, _buffer_index(cb, i)))
Base.view(cb::CircularArrayBuffer, I...) = (@boundscheck checkbounds(cb, I...); view(cb.buffer, Base.front(I)..., _buffer_frame(cb, Base.last(I))))

capacity(cb::CircularArrayBuffer{T,N}) where {T,N} = size(cb.buffer, N)
isfull(cb::CircularArrayBuffer) = cb.nframes == capacity(cb)
Expand Down
7 changes: 7 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ CUDA.allowscalar(false)
@test b[1] == 0
end

# https://github.com/JuliaReinforcementLearning/CircularArrayBuffers.jl/issues/20
@testset "bound check with view" begin
b = CircularArrayBuffer([1 2 3; 4 5 6])
@test_throws BoundsError @view b[:, 4]
@test_throws BoundsError @view b[:, 9]
end

@testset "1D vector" begin
b = CircularArrayBuffer([[1], [2, 3]])
push!(b, [4, 5, 6])
Expand Down

0 comments on commit 47377ea

Please sign in to comment.