Skip to content

Commit

Permalink
Refine tests and fix append when both inputs are the same array
Browse files Browse the repository at this point in the history
  • Loading branch information
quinnj committed Aug 29, 2016
1 parent 8c3dc95 commit 82a9345
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 12 deletions.
8 changes: 4 additions & 4 deletions src/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,7 @@ function _levels!(A::CatOrdArray, newlevels::Vector; nullok=false)
levelsmap = indexin(oldindex, index(A.pool))

@inbounds for (i, x) in enumerate(A.refs)
j = levelsmap[x]
x > 0 && (A.refs[i] = j)
x > 0 && (A.refs[i] = levelsmap[x])
end
end

Expand Down Expand Up @@ -200,9 +199,10 @@ end
function Base.append!(A::CatOrdArray, B::CatOrdArray)
levels!(A, union(levels(A), levels(B)))
len = length(A.refs)
len2 = length(B.refs)
resize!(A.refs, len + length(B.refs))
for (i, item) in enumerate(B)
A[len + i] = item
for i = 1:len2
A[len + i] = B[i]
end
return A
end
Expand Down
59 changes: 54 additions & 5 deletions test/11_array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -146,23 +146,35 @@ for (A, V, M) in ((NominalArray, NominalVector, NominalMatrix),
@test length(x) == 6
@test x[1] == x[end]
@test levels(x) == ["e", "a", "b", "c", "zz"]
# ["c", "a", "a", "a", "zz", "c"]

y = V{String, R}(a)
append!(x, y)
@test length(x) == 9
append!(x, x)
@test length(x) == 12
@test x[1] == "c"
@test x[2] == "a"
@test x[3] == "a"
@test x[4] == "a"
@test x[5] == "zz"
@test x[6] == "c"
@test x[7] == "c"
@test x[8] == "a"
@test x[9] == "a"
@test x[10] == "a"
@test x[11] == "zz"
@test x[12] == "c"

b = ["z","y","x"]
y = V{String, R}(b)
append!(x, y)
@test length(x) == 12
@test length(x) == 15
@test x[end-2] == "z"
@test x[end-1] == "y"
@test x[end] == "x"
@test levels(x) == ["e", "a", "b", "c", "zz", "z", "y", "x"]


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

# Vector created from range (i.e. non-Array AbstractArray),
# direct conversion to a vector with different eltype
Expand Down Expand Up @@ -271,6 +283,43 @@ for (A, V, M) in ((NominalArray, NominalVector, NominalMatrix),
@test x[4] === x.pool.valindex[4]
@test levels(x) == vcat(unique(a), -1)

push!(x, 2.0)
@test length(x) == 5
@test x[end] == 2.0
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0]

push!(x, x[1])
@test length(x) == 6
@test x[1] == x[end]
@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] == -1.0
@test x[2] == -1.0
@test x[3] == 1.0
@test x[4] == 1.5
@test x[5] == 2.0
@test x[6] == -1.0
@test x[7] == -1.0
@test x[8] == -1.0
@test x[9] == 1.0
@test x[10] == 1.5
@test x[11] == 2.0
@test x[12] == -1.0

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

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

# Matrix
a = ["a" "b" "c"; "b" "a" "c"]
Expand Down
44 changes: 41 additions & 3 deletions test/12_nullablearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,23 +166,24 @@ for (A, V, M) in ((NullableNominalArray, NullableNominalVector, NullableNominalM
@test isnull(x[end])
@test levels(x) == ["e", "c", "zz"]

y = V{String, R}(a)
y = V{String, R}(levels(x))
append!(x, y)
@test length(x) == 10

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

push!(y, eltype(y)())
append!(x, y)
@test isnull(x[end])
@test levels(x) == ["e", "c", "zz", "a", "b", "z", "y", "x"]
@test levels(x) == ["e", "c", "zz", "z", "y", "x"]

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


Expand Down Expand Up @@ -399,6 +400,43 @@ for (A, V, M) in ((NullableNominalArray, NullableNominalVector, NullableNominalM
@test x[4] === Nullable(x.pool.valindex[4])
@test levels(x) == vcat(unique(a), -1)

push!(x, 2.0)
@test length(x) == 5
@test get(x[end]) == 2.0
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0]

push!(x, x[1])
@test length(x) == 6
@test x[1] == x[end]
@test levels(x) == [0.0, 0.5, 1.0, 1.5, -1.0, 2.0]

append!(x, x)
@test length(x) == 12
@test get(x[1]) == -1.0
@test get(x[2]) == -1.0
@test get(x[3]) == 1.0
@test get(x[4]) == 1.5
@test get(x[5]) == 2.0
@test get(x[6]) == -1.0
@test get(x[7]) == -1.0
@test get(x[8]) == -1.0
@test get(x[9]) == 1.0
@test get(x[10]) == 1.5
@test get(x[11]) == 2.0
@test get(x[12]) == -1.0

b = [2.5, 3.0, -3.5]
y = V{Float64, R}(b)
append!(x, y)
@test length(x) == 15
@test get(x[end-2]) == 2.5
@test get(x[end-1]) == 3.0
@test get(x[end]) == -3.5
@test levels(x) == [0.0,0.5,1.0,1.5,-1.0,2.0,2.5,3.0,-3.5]

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

# Matrix with no null values
for a in (["a" "b" "c"; "b" "a" "c"],
Expand Down

0 comments on commit 82a9345

Please sign in to comment.