Skip to content

Commit 8e5fc12

Browse files
bkaminsnalimilan
authored andcommitted
fix append! for SubArray (#171)
1 parent e468ddc commit 8e5fc12

File tree

4 files changed

+125
-65
lines changed

4 files changed

+125
-65
lines changed

src/array.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -694,11 +694,11 @@ function Base.push!(A::CategoricalVector, item)
694694
A
695695
end
696696

697-
function Base.append!(A::CategoricalVector, B::CategoricalArray)
697+
function Base.append!(A::CategoricalVector, B::CatArrOrSub)
698698
levels!(A, union(levels(A), levels(B)))
699-
len = length(A.refs)
700-
len2 = length(B.refs)
701-
resize!(A.refs, len + length(B.refs))
699+
len = length(A)
700+
len2 = length(B)
701+
resize!(A.refs, len + len2)
702702
for i = 1:len2
703703
A[len + i] = B[i]
704704
end

test/11_array.jl

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -207,22 +207,9 @@ using CategoricalArrays: DefaultRefType, catvaluetype, leveltype
207207
@test x.pool.index == x2.pool.index
208208
@test x.pool.invindex == x2.pool.invindex
209209

210-
append!(x, x)
211-
@test length(x) == 12
212-
@test x == ["c", "b", "b", "a", "zz", "c", "c", "b", "b", "a", "zz", "c"]
213-
@test isordered(x) === false
214-
@test levels(x) == ["e", "a", "b", "c", "zz"]
215-
216-
b = ["z","y","x"]
217-
y = CategoricalVector{String, R}(b)
218-
append!(x, y)
219-
@test length(x) == 15
220-
@test x == ["c", "b", "b", "a", "zz", "c", "c", "b", "b", "a", "zz", "c", "z", "y", "x"]
221-
@test levels(x) == ["e", "a", "b", "c", "zz", "x", "y", "z"]
222-
223210
empty!(x)
224211
@test length(x) == 0
225-
@test levels(x) == ["e", "a", "b", "c", "zz", "x", "y", "z"]
212+
@test levels(x) == ["e", "a", "b", "c", "zz"]
226213

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

390-
append!(x, x)
391-
@test length(x) == 12
392-
@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]
393-
@test isordered(x) === ordered
394-
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0]
395-
396-
b = [2.5, 3.0, -3.5]
397-
y = CategoricalVector{Float64, R}(b, ordered=ordered)
398-
append!(x, y)
399-
@test length(x) == 15
400-
@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]
401-
@test isordered(x) === ordered
402-
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0, -3.5, 2.5, 3.0]
403-
404377
empty!(x)
405378
@test length(x) == 0
406379
@test isordered(x) === ordered
407-
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0, -3.5, 2.5, 3.0]
380+
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0]
408381
end
409382

410383
@testset "Matrix" begin

test/12_missingarray.jl

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -224,24 +224,10 @@ const ≅ = isequal
224224
@test x ["c", missing, missing, "e", "zz", "c", missing]
225225
@test levels(x) == ["e", "c", "zz"]
226226

227-
append!(x, x)
228-
@test x ["c", missing, missing, "e", "zz", "c", missing, "c", missing, missing, "e", "zz", "c", missing]
229-
@test levels(x) == ["e", "c", "zz"]
230-
@test isordered(x) === false
231-
@test length(x) == 14
232-
233-
b = ["z","y","x"]
234-
y = CategoricalVector{Union{String, Missing}, R}(b)
235-
append!(x, y)
236-
@test length(x) == 17
237-
@test isordered(x) === false
238-
@test levels(x) == ["e", "c", "zz", "x", "y", "z"]
239-
@test x ["c", missing, missing, "e", "zz", "c", missing, "c", missing, missing, "e", "zz", "c", missing, "z", "y", "x"]
240-
241227
empty!(x)
242228
@test isordered(x) === false
243229
@test length(x) == 0
244-
@test levels(x) == ["e", "c", "zz", "x", "y", "z"]
230+
@test levels(x) == ["e", "c", "zz"]
245231
end
246232
end
247233

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

571-
append!(x, x)
572-
@test length(x) == 12
573-
@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]
574-
@test isordered(x) === ordered
575-
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0]
576-
577-
b = [2.5, 3.0, -3.5]
578-
y = CategoricalVector{Union{Float64, Missing}, R}(b)
579-
append!(x, y)
580-
@test length(x) == 15
581-
@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]
582-
@test isordered(x) === ordered
583-
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0, -3.5, 2.5, 3.0]
584-
585557
empty!(x)
586558
@test length(x) == 0
587559
@test isordered(x) === ordered
588-
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0, -3.5, 2.5, 3.0]
560+
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0]
589561
end
590562

591563
@testset "Matrix $(typeof(a)) with no missing values" for a in

test/13_arraycommon.jl

Lines changed: 117 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -974,9 +974,124 @@ end
974974
end
975975

976976
@testset "broadcast" for x in (CategoricalArray(1:3),
977-
CategoricalArray{Union{Int,Missing}}(1:3))
977+
CategoricalArray{Union{Int,Missing}}(1:3),
978+
CategoricalArray(["a", "b", "c"]))
978979
x[1:2] .= x[3]
979-
@test x == [3, 3, 3]
980+
@test x == fill(get(x[3]), 3)
981+
end
982+
983+
@testset "append! ordered=$ordered" for ordered in (false, true)
984+
@testset "append! String" begin
985+
a = ["a", "b", "c"]
986+
x = CategoricalVector{String}(a, ordered=ordered)
987+
988+
append!(x, x)
989+
@test length(x) == 6
990+
@test x == ["a", "b", "c", "a", "b", "c"]
991+
@test isordered(x) === ordered
992+
@test levels(x) == ["a", "b", "c"]
993+
994+
b = ["z","y","x"]
995+
y = CategoricalVector{String}(b)
996+
append!(x, y)
997+
@test isordered(x) === ordered
998+
@test length(x) == 9
999+
@test x == ["a", "b", "c", "a", "b", "c", "z", "y", "x"]
1000+
@test levels(x) == ["a", "b", "c", "x", "y", "z"]
1001+
1002+
z1 = view(CategoricalVector{String}(["ex1", "ex2"]), 1)
1003+
z2 = view(CategoricalVector{String}(["ex3", "ex4"]), 1:1)
1004+
append!(x, z1)
1005+
append!(x, z2)
1006+
@test isordered(x) === ordered
1007+
@test length(x) == 11
1008+
@test x == ["a", "b", "c", "a", "b", "c", "z", "y", "x", "ex1", "ex3"]
1009+
@test levels(x) == ["a", "b", "c", "x", "y", "z", "ex1", "ex2", "ex3", "ex4"]
1010+
end
1011+
1012+
@testset "append! Float64" begin
1013+
a = [-1.0, 0.0, 1.0]
1014+
x = CategoricalVector{Float64}(a, ordered=ordered)
1015+
1016+
append!(x, x)
1017+
@test length(x) == 6
1018+
@test x == [-1.0, 0.0, 1.0, -1.0, 0.0, 1.0]
1019+
@test isordered(x) === ordered
1020+
@test levels(x) == [-1.0, 0.0, 1.0]
1021+
1022+
b = [2.5, 3.0, 3.5]
1023+
y = CategoricalVector{Float64}(b, ordered=ordered)
1024+
append!(x, y)
1025+
@test length(x) == 9
1026+
@test x == [-1.0, 0.0, 1.0, -1.0, 0.0, 1.0, 2.5, 3.0, 3.5]
1027+
@test isordered(x) === ordered
1028+
@test levels(x) == [-1.0, 0.0, 1.0, 2.5, 3.0, 3.5]
1029+
1030+
z1 = view(CategoricalVector{Float64}([100.0, 101.0]), 1)
1031+
z2 = view(CategoricalVector{Float64}([102.0, 103.0]), 1:1)
1032+
append!(x, z1)
1033+
append!(x, z2)
1034+
@test length(x) == 11
1035+
@test x == [-1.0, 0.0, 1.0, -1.0, 0.0, 1.0, 2.5, 3.0, 3.5, 100.0, 102.0]
1036+
@test isordered(x) === ordered
1037+
@test levels(x) == [-1.0, 0.0, 1.0, 2.5, 3.0, 3.5, 100.0, 101.0, 102.0, 103.0]
1038+
end
1039+
end
1040+
1041+
@testset "append! ordered=$ordered" for ordered in (false, true)
1042+
cases = (["b", "a", missing], Union{String, Missing}["b", "a", "b"])
1043+
@testset "String, has missing: $(any(ismissing.(a)))" for a in cases
1044+
x = CategoricalVector{Union{String, Missing}}(a, ordered=ordered)
1045+
1046+
append!(x, x)
1047+
@test x [a; a]
1048+
@test levels(x) == ["a", "b"]
1049+
@test isordered(x) === ordered
1050+
@test length(x) == 6
1051+
1052+
b = ["x","y",missing]
1053+
y = CategoricalVector{Union{String, Missing}}(b)
1054+
append!(x, y)
1055+
@test length(x) == 9
1056+
@test isordered(x) === ordered
1057+
@test levels(x) == ["a", "b", "x", "y"]
1058+
@test x [a; a; b]
1059+
z1 = view(CategoricalVector{Union{String, Missing}}([missing, "ex2"]), 1)
1060+
z2 = view(CategoricalVector{Union{String, Missing}}(["ex3", "ex4"]), 1:1)
1061+
append!(x, z1)
1062+
append!(x, z2)
1063+
@test length(x) == 11
1064+
@test isordered(x) === ordered
1065+
@test levels(x) == ["a", "b", "x", "y", "ex2", "ex3", "ex4"]
1066+
@test x [a; a; b; missing; "ex3"]
1067+
end
1068+
1069+
@testset "Float64" begin
1070+
a = 0.0:0.5:1.0
1071+
x = CategoricalVector{Union{Float64, Missing}}(a, ordered=ordered)
1072+
1073+
append!(x, x)
1074+
@test length(x) == 6
1075+
@test x == [a; a]
1076+
@test isordered(x) === ordered
1077+
@test levels(x) == [0.0, 0.5, 1.0]
1078+
1079+
b = [2.5, 3.0, missing]
1080+
y = CategoricalVector{Union{Float64, Missing}}(b)
1081+
append!(x, y)
1082+
@test length(x) == 9
1083+
@test x [a; a; b]
1084+
@test isordered(x) === ordered
1085+
@test levels(x) == [0.0, 0.5, 1.0, 2.5, 3.0]
1086+
z1 = view(CategoricalVector{Union{Float64, Missing}}([missing, 101.0]), 1)
1087+
z2 = view(CategoricalVector{Union{Float64, Missing}}([102.0, 103.0]), 1:1)
1088+
append!(x, z1)
1089+
append!(x, z2)
1090+
@test length(x) == 11
1091+
@test x [a; a; b; missing; 102.0]
1092+
@test isordered(x) === ordered
1093+
@test levels(x) == [0.0, 0.5, 1.0, 2.5, 3.0, 101.0, 102.0, 103.0]
1094+
end
9801095
end
9811096

9821097
end

0 commit comments

Comments
 (0)