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

Support more array mutation methods #29

Merged
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
6 changes: 6 additions & 0 deletions src/CircularArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ Create a `CircularMatrix` of size `size` filled with value `def`.
CircularMatrix(def::T, size::NTuple{2, Integer}) where T = CircularMatrix{T}(fill(def, size))

Base.empty(::CircularVector{T}, ::Type{U}=T) where {T,U} = CircularVector{U}(U[])
Base.empty!(a::CircularVector) = (empty!(parent(a)); a)
Base.push!(a::CircularVector, x...) = (push!(parent(a), x...); a)
Base.append!(a::CircularVector, items) = (append!(parent(a), items); a)
Base.resize!(a::CircularVector, nl::Integer) = (resize!(parent(a), nl); a)
Base.pop!(a::CircularVector) = pop!(parent(a))
Base.sizehint!(a::CircularVector, sz::Integer) = (sizehint!(parent(a), sz); a)

function Base.deleteat!(a::CircularVector, i::Integer)
deleteat!(a.data, mod(i, eachindex(IndexLinear(), a.data)))
Expand Down
82 changes: 74 additions & 8 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ end

@testset "matrix construction" begin
data = zeros(Float64, 2, 2)
ref = CircularArray(data)
@test CircularArray{Float64}(data) == ref
ref = CircularMatrix(data)
@test CircularArray{Float64, 2}(data) == ref
@test CircularArray{Float64, 2, Array{Float64, 2}}(data) == ref
@test CircularArray{Float64, 2, Matrix{Float64}}(data) == ref
@test CircularMatrix{Float64}(data) == ref
@test CircularMatrix{Float64, Array{Float64, 2}}(data) == ref
@test CircularMatrix{Float64, Matrix{Float64}}(data) == ref
end
end

Expand Down Expand Up @@ -96,11 +96,77 @@ end

@test prod(v2) == "abcde"^5

@test_throws MethodError push!(v1, 15)
@testset "empty/empty!" begin
v1 = CircularVector([1,2,3])
@test empty(v1) isa CircularVector{Int64}
@test empty(v1, Float64) isa CircularVector{Float64}
v1 == CircularVector([])
# `isempty` can be used to implement `size` method.
@test isempty(empty(v1))

v2 = CircularVector("abcde", 5)
@test empty!(v2) isa CircularVector{String}
@test isempty(v2)
end

@testset "resize!" begin
@test_throws ArgumentError("new length must be ≥ 0") resize!(CircularVector([]), -2)
v = CircularVector([1,2,3,4,5,6,7])
resize!(v, 3)
@test length(v) == 3

# ensure defining `resize!` induces `push!` and `append!` methods
@testset "push!" begin
v = CircularVector([1,2,3])
push!(v, 42)
@test v == CircularVector([1,2,3,42])
push!(v, -9, -99, -999)
@test v == CircularVector([1,2,3,42, -9, -99, -999])
end

@testset "append!" begin
v1 = CircularVector([1,2,3])
append!(v1, [-9, -99, -999])
@test v1 == CircularVector([1,2,3, -9, -99, -999])

v2 = CircularVector([1,2,3])
append!(v2, CircularVector([-1,-2]))
@test v2 == CircularVector([1,2,3,-1,-2])

v3 = CircularVector([1,2,3])
append!(v3, [4, 5], [6])
@test v3 == CircularVector([1,2,3,4,5,6])

v4 = CircularVector([1,2,3])
o4 = OffsetVector([-1,-2,-3], -2:0)
append!(v4, o4)
@test v4 == CircularVector([1,2,3,-1,-2,-3])

v5 = CircularVector([1,2,3])
o5 = OffsetVector([-1,-2,-3], -2:0)
append!(v5, o5, -4)
@test v5 == CircularVector([1,2,3,-1,-2,-3,-4])
end
end

@testset "pop!" begin
v1 = CircularVector([1,2,3,42])
pop!(v1) == 42
@test v1 == CircularVector([1,2,3])

@test empty(v1) isa CircularVector{Int64}
@test empty(v1, Float64) isa CircularVector{Float64}
@test isempty(empty(v1))
v2 = CircularVector([1])
pop!(v2) == 1
@test v2 == CircularVector([])
@test isempty(v2)
@test_throws ArgumentError("array must be non-empty") pop!(v2)
end

@testset "sizehint!" begin
v = CircularVector([1,2,3,4,5,6,7])
resize!(v, 1)
sizehint!(v, 1)
@test length(v) == 1
end

@testset "deleteat!" begin
@test deleteat!(CircularVector([1, 2, 3]), 2) == CircularVector([1, 3])
Expand Down
Loading