Skip to content

Commit

Permalink
fix append! for SubArray (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins authored and nalimilan committed Nov 20, 2018
1 parent e468ddc commit 8e5fc12
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 65 deletions.
8 changes: 4 additions & 4 deletions src/array.jl
Expand Up @@ -694,11 +694,11 @@ function Base.push!(A::CategoricalVector, item)
A
end

function Base.append!(A::CategoricalVector, B::CategoricalArray)
function Base.append!(A::CategoricalVector, B::CatArrOrSub)
levels!(A, union(levels(A), levels(B)))
len = length(A.refs)
len2 = length(B.refs)
resize!(A.refs, len + length(B.refs))
len = length(A)
len2 = length(B)
resize!(A.refs, len + len2)
for i = 1:len2
A[len + i] = B[i]
end
Expand Down
31 changes: 2 additions & 29 deletions test/11_array.jl
Expand Up @@ -207,22 +207,9 @@ using CategoricalArrays: DefaultRefType, catvaluetype, leveltype
@test x.pool.index == x2.pool.index
@test x.pool.invindex == x2.pool.invindex

append!(x, x)
@test length(x) == 12
@test x == ["c", "b", "b", "a", "zz", "c", "c", "b", "b", "a", "zz", "c"]
@test isordered(x) === false
@test levels(x) == ["e", "a", "b", "c", "zz"]

b = ["z","y","x"]
y = CategoricalVector{String, R}(b)
append!(x, y)
@test length(x) == 15
@test x == ["c", "b", "b", "a", "zz", "c", "c", "b", "b", "a", "zz", "c", "z", "y", "x"]
@test levels(x) == ["e", "a", "b", "c", "zz", "x", "y", "z"]

empty!(x)
@test length(x) == 0
@test levels(x) == ["e", "a", "b", "c", "zz", "x", "y", "z"]
@test levels(x) == ["e", "a", "b", "c", "zz"]

@testset "Vector created from range" begin
# (i.e. non-Array AbstractArray),
Expand Down Expand Up @@ -387,24 +374,10 @@ using CategoricalArrays: DefaultRefType, catvaluetype, leveltype
@test isordered(x) === ordered
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0]

append!(x, x)
@test length(x) == 12
@test x == [-1.0, -1.0, 1.0, 1.5, 2.0, -1.0, -1.0, -1.0, 1.0, 1.5, 2.0, -1.0]
@test isordered(x) === ordered
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0]

b = [2.5, 3.0, -3.5]
y = CategoricalVector{Float64, R}(b, ordered=ordered)
append!(x, y)
@test length(x) == 15
@test x == [-1.0, -1.0, 1.0, 1.5, 2.0, -1.0, -1.0, -1.0, 1.0, 1.5, 2.0, -1.0, 2.5, 3.0, -3.5]
@test isordered(x) === ordered
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0, -3.5, 2.5, 3.0]

empty!(x)
@test length(x) == 0
@test isordered(x) === ordered
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0, -3.5, 2.5, 3.0]
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0]
end

@testset "Matrix" begin
Expand Down
32 changes: 2 additions & 30 deletions test/12_missingarray.jl
Expand Up @@ -224,24 +224,10 @@ const ≅ = isequal
@test x ["c", missing, missing, "e", "zz", "c", missing]
@test levels(x) == ["e", "c", "zz"]

append!(x, x)
@test x ["c", missing, missing, "e", "zz", "c", missing, "c", missing, missing, "e", "zz", "c", missing]
@test levels(x) == ["e", "c", "zz"]
@test isordered(x) === false
@test length(x) == 14

b = ["z","y","x"]
y = CategoricalVector{Union{String, Missing}, R}(b)
append!(x, y)
@test length(x) == 17
@test isordered(x) === false
@test levels(x) == ["e", "c", "zz", "x", "y", "z"]
@test x ["c", missing, missing, "e", "zz", "c", missing, "c", missing, missing, "e", "zz", "c", missing, "z", "y", "x"]

empty!(x)
@test isordered(x) === false
@test length(x) == 0
@test levels(x) == ["e", "c", "zz", "x", "y", "z"]
@test levels(x) == ["e", "c", "zz"]
end
end

Expand Down Expand Up @@ -568,24 +554,10 @@ const ≅ = isequal
@test isordered(x) === ordered
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0]

append!(x, x)
@test length(x) == 12
@test x == [-1.0, -1.0, 1.0, 1.5, 2.0, -1.0, -1.0, -1.0, 1.0, 1.5, 2.0, -1.0]
@test isordered(x) === ordered
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0]

b = [2.5, 3.0, -3.5]
y = CategoricalVector{Union{Float64, Missing}, R}(b)
append!(x, y)
@test length(x) == 15
@test x == [-1.0, -1.0, 1.0, 1.5, 2.0, -1.0, -1.0, -1.0, 1.0, 1.5, 2.0, -1.0, 2.5, 3.0, -3.5]
@test isordered(x) === ordered
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0, -3.5, 2.5, 3.0]

empty!(x)
@test length(x) == 0
@test isordered(x) === ordered
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0, -3.5, 2.5, 3.0]
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0]
end

@testset "Matrix $(typeof(a)) with no missing values" for a in
Expand Down
119 changes: 117 additions & 2 deletions test/13_arraycommon.jl
Expand Up @@ -974,9 +974,124 @@ end
end

@testset "broadcast" for x in (CategoricalArray(1:3),
CategoricalArray{Union{Int,Missing}}(1:3))
CategoricalArray{Union{Int,Missing}}(1:3),
CategoricalArray(["a", "b", "c"]))
x[1:2] .= x[3]
@test x == [3, 3, 3]
@test x == fill(get(x[3]), 3)
end

@testset "append! ordered=$ordered" for ordered in (false, true)
@testset "append! String" begin
a = ["a", "b", "c"]
x = CategoricalVector{String}(a, ordered=ordered)

append!(x, x)
@test length(x) == 6
@test x == ["a", "b", "c", "a", "b", "c"]
@test isordered(x) === ordered
@test levels(x) == ["a", "b", "c"]

b = ["z","y","x"]
y = CategoricalVector{String}(b)
append!(x, y)
@test isordered(x) === ordered
@test length(x) == 9
@test x == ["a", "b", "c", "a", "b", "c", "z", "y", "x"]
@test levels(x) == ["a", "b", "c", "x", "y", "z"]

z1 = view(CategoricalVector{String}(["ex1", "ex2"]), 1)
z2 = view(CategoricalVector{String}(["ex3", "ex4"]), 1:1)
append!(x, z1)
append!(x, z2)
@test isordered(x) === ordered
@test length(x) == 11
@test x == ["a", "b", "c", "a", "b", "c", "z", "y", "x", "ex1", "ex3"]
@test levels(x) == ["a", "b", "c", "x", "y", "z", "ex1", "ex2", "ex3", "ex4"]
end

@testset "append! Float64" begin
a = [-1.0, 0.0, 1.0]
x = CategoricalVector{Float64}(a, ordered=ordered)

append!(x, x)
@test length(x) == 6
@test x == [-1.0, 0.0, 1.0, -1.0, 0.0, 1.0]
@test isordered(x) === ordered
@test levels(x) == [-1.0, 0.0, 1.0]

b = [2.5, 3.0, 3.5]
y = CategoricalVector{Float64}(b, ordered=ordered)
append!(x, y)
@test length(x) == 9
@test x == [-1.0, 0.0, 1.0, -1.0, 0.0, 1.0, 2.5, 3.0, 3.5]
@test isordered(x) === ordered
@test levels(x) == [-1.0, 0.0, 1.0, 2.5, 3.0, 3.5]

z1 = view(CategoricalVector{Float64}([100.0, 101.0]), 1)
z2 = view(CategoricalVector{Float64}([102.0, 103.0]), 1:1)
append!(x, z1)
append!(x, z2)
@test length(x) == 11
@test x == [-1.0, 0.0, 1.0, -1.0, 0.0, 1.0, 2.5, 3.0, 3.5, 100.0, 102.0]
@test isordered(x) === ordered
@test levels(x) == [-1.0, 0.0, 1.0, 2.5, 3.0, 3.5, 100.0, 101.0, 102.0, 103.0]
end
end

@testset "append! ordered=$ordered" for ordered in (false, true)
cases = (["b", "a", missing], Union{String, Missing}["b", "a", "b"])
@testset "String, has missing: $(any(ismissing.(a)))" for a in cases
x = CategoricalVector{Union{String, Missing}}(a, ordered=ordered)

append!(x, x)
@test x [a; a]
@test levels(x) == ["a", "b"]
@test isordered(x) === ordered
@test length(x) == 6

b = ["x","y",missing]
y = CategoricalVector{Union{String, Missing}}(b)
append!(x, y)
@test length(x) == 9
@test isordered(x) === ordered
@test levels(x) == ["a", "b", "x", "y"]
@test x [a; a; b]
z1 = view(CategoricalVector{Union{String, Missing}}([missing, "ex2"]), 1)
z2 = view(CategoricalVector{Union{String, Missing}}(["ex3", "ex4"]), 1:1)
append!(x, z1)
append!(x, z2)
@test length(x) == 11
@test isordered(x) === ordered
@test levels(x) == ["a", "b", "x", "y", "ex2", "ex3", "ex4"]
@test x [a; a; b; missing; "ex3"]
end

@testset "Float64" begin
a = 0.0:0.5:1.0
x = CategoricalVector{Union{Float64, Missing}}(a, ordered=ordered)

append!(x, x)
@test length(x) == 6
@test x == [a; a]
@test isordered(x) === ordered
@test levels(x) == [0.0, 0.5, 1.0]

b = [2.5, 3.0, missing]
y = CategoricalVector{Union{Float64, Missing}}(b)
append!(x, y)
@test length(x) == 9
@test x [a; a; b]
@test isordered(x) === ordered
@test levels(x) == [0.0, 0.5, 1.0, 2.5, 3.0]
z1 = view(CategoricalVector{Union{Float64, Missing}}([missing, 101.0]), 1)
z2 = view(CategoricalVector{Union{Float64, Missing}}([102.0, 103.0]), 1:1)
append!(x, z1)
append!(x, z2)
@test length(x) == 11
@test x [a; a; b; missing; 102.0]
@test isordered(x) === ordered
@test levels(x) == [0.0, 0.5, 1.0, 2.5, 3.0, 101.0, 102.0, 103.0]
end
end

end

0 comments on commit 8e5fc12

Please sign in to comment.