Skip to content

Commit fcc891e

Browse files
committed
Add tests for isbits union optimizations
1 parent 034c425 commit fcc891e

File tree

1 file changed

+220
-0
lines changed

1 file changed

+220
-0
lines changed

test/core.jl

+220
Original file line numberDiff line numberDiff line change
@@ -5075,3 +5075,223 @@ f_isdefined_cl_6() = (local x; () -> @isdefined x)
50755075
@test !f_isdefined_cl_4()
50765076
@test f_isdefined_cl_5()()
50775077
@test !f_isdefined_cl_6()()
5078+
5079+
module UnionOptimizations
5080+
5081+
using Base.Test
5082+
5083+
const boxedunions = [Union{}, Union{String, Void}]
5084+
const unboxedunions = [Union{Int8, Void}, Union{Int8, Float16, Void},
5085+
Union{Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128},
5086+
Union{Char, Date, Int}]
5087+
5088+
initvalue(::Type{Void}) = nothing
5089+
initvalue(::Type{Char}) = '\0'
5090+
initvalue(::Type{Date}) = Date(0, 12, 31)
5091+
initvalue(::Type{T}) where {T <: Number} = T(0)
5092+
5093+
initvalue2(::Type{Void}) = nothing
5094+
initvalue2(::Type{Char}) = Char(0x01)
5095+
initvalue2(::Type{Date}) = Date(1)
5096+
initvalue2(::Type{T}) where {T <: Number} = T(1)
5097+
5098+
U = unboxedunions[1]
5099+
5100+
mutable struct UnionField
5101+
u::U
5102+
end
5103+
5104+
x = UnionField(initvalue(Base.uniontypes(U)[1]))
5105+
@test x.u === initvalue(Base.uniontypes(U)[1])
5106+
x.u = initvalue2(Base.uniontypes(U)[1])
5107+
@test x.u === initvalue2(Base.uniontypes(U)[1])
5108+
x.u = initvalue(Base.uniontypes(U)[2])
5109+
@test x.u === initvalue(Base.uniontypes(U)[2])
5110+
5111+
for U in boxedunions
5112+
for N in (1, 2, 3, 4)
5113+
A = Array{U}(ntuple(x->0, N)...)
5114+
@test isempty(A)
5115+
@test Core.sizeof(A) == 0
5116+
5117+
A = Array{U}(ntuple(x->10, N)...)
5118+
@test length(A) == 10^N
5119+
@test Core.sizeof(A) == sizeof(Int) * (10^N)
5120+
@test !isassigned(A, 1)
5121+
end
5122+
end
5123+
5124+
# unsafe_wrap
5125+
A4 = [1, 2, 3]
5126+
@test_throws ArgumentError unsafe_wrap(Array, convert(Ptr{Union{Int, Void}}, pointer(A4)), 3)
5127+
A5 = [1 2 3; 4 5 6]
5128+
@test_throws ArgumentError unsafe_wrap(Array, convert(Ptr{Union{Int, Void}}, pointer(A5)), 6)
5129+
5130+
for U in unboxedunions
5131+
for N in (1, 2, 3, 4)
5132+
A = Array{U}(ntuple(x->0, N)...)
5133+
@test isempty(A)
5134+
@test Core.sizeof(A) == 0
5135+
5136+
len = ntuple(x->10, N)
5137+
mxsz = maximum(sizeof, Base.uniontypes(U))
5138+
A = Array{U}(len)
5139+
@test length(A) == prod(len)
5140+
@test Core.sizeof(A) == prod(len) * mxsz
5141+
@test isassigned(A, 1)
5142+
@test isassigned(A, length(A))
5143+
5144+
# arrayref / arrayset
5145+
F = Base.uniontypes(U)[1]
5146+
@test A[1] === initvalue(F)
5147+
A[1] = initvalue2(F)
5148+
@test A[1] === initvalue2(F)
5149+
5150+
F2 = Base.uniontypes(U)[2]
5151+
A[2] = initvalue(F2)
5152+
@test A[2] === initvalue(F2)
5153+
5154+
for (i, U2) in enumerate(Base.uniontypes(U))
5155+
A[i] = initvalue2(U2)
5156+
@test A[i] === initvalue2(U2)
5157+
end
5158+
5159+
# serialize / deserialize
5160+
io = IOBuffer()
5161+
serialize(io, A)
5162+
seekstart(io)
5163+
A2 = deserialize(io)
5164+
@test A == A2
5165+
5166+
# reshape
5167+
A3 = reshape(A, (div(prod(len), 2), 2))
5168+
@test Core.sizeof(A) == prod(len) * mxsz
5169+
@test isassigned(A, 1)
5170+
@test A[1] === initvalue2(F)
5171+
5172+
# copy
5173+
A4 = copy(A)
5174+
@test A == A4
5175+
5176+
if N == 1
5177+
## Dequeue functions
5178+
# pop!
5179+
F2 = Base.uniontypes(U)[2]
5180+
len = len[1]
5181+
A = U[initvalue2(F2) for i = 1:len]
5182+
for i = 1:len
5183+
@test A[end] === initvalue2(F2)
5184+
v = pop!(A)
5185+
@test v === initvalue2(F2)
5186+
end
5187+
@test isempty(A)
5188+
5189+
# shift!
5190+
A = U[initvalue2(F2) for i = 1:len]
5191+
for i = 1:len
5192+
@test A[1] === initvalue2(F2)
5193+
shift!(A)
5194+
end
5195+
@test isempty(A)
5196+
5197+
# empty!
5198+
A = U[initvalue2(F2) for i = 1:len]
5199+
empty!(A)
5200+
@test isempty(A)
5201+
5202+
# resize!
5203+
A = U[initvalue2(F2) for i = 1:len]
5204+
resize!(A, 1)
5205+
@test length(A) === 1
5206+
@test A[1] === initvalue2(F2)
5207+
resize!(A, len)
5208+
@test length(A) === len
5209+
@test A[1] === initvalue2(F2)
5210+
@test typeof(A[end]) === F
5211+
5212+
# deleteat!
5213+
F = Base.uniontypes(U)[1]
5214+
A = U[rand(F(1):F(len)) for i = 1:len]
5215+
deleteat!(A, map(Int, sort!(unique(A[1:4]))))
5216+
A = U[initvalue2(F2) for i = 1:len]
5217+
deleteat!(A, 1:2)
5218+
@test length(A) == len - 2
5219+
@test all(A .== initvalue2(F2))
5220+
deleteat!(A, 1:2)
5221+
@test length(A) == len - 4
5222+
@test all(A .== initvalue2(F2))
5223+
A = U[initvalue2(F2) for i = 1:len]
5224+
deleteat!(A, length(A)-1:length(A))
5225+
@test length(A) == len - 2
5226+
@test all(A .== initvalue2(F2))
5227+
deleteat!(A, length(A)-1:length(A))
5228+
@test length(A) == len - 4
5229+
@test all(A .== initvalue2(F2))
5230+
A = U[initvalue2(F2) for i = 1:len]
5231+
deleteat!(A, 2:3)
5232+
@test length(A) == len - 2
5233+
@test all(A .== initvalue2(F2))
5234+
A = U[initvalue2(F2) for i = 1:len]
5235+
deleteat!(A, length(A)-2:length(A)-1)
5236+
@test length(A) == len - 2
5237+
@test all(A .== initvalue2(F2))
5238+
5239+
# unshift!
5240+
A = U[initvalue2(F2) for i = 1:len]
5241+
for i = 1:5
5242+
unshift!(A, initvalue2(F))
5243+
unshift!(A, initvalue(F2))
5244+
@test A[1] === initvalue(F2)
5245+
@test A[2] === initvalue2(F)
5246+
end
5247+
5248+
# push! / append! / prepend!
5249+
A = U[initvalue2(F2) for i = 1:len]
5250+
push!(A, initvalue2(F))
5251+
@test A[end] === initvalue2(F)
5252+
push!(A, initvalue2(F2))
5253+
@test A[end] === initvalue2(F2)
5254+
append!(A, [initvalue(F), initvalue2(F)])
5255+
@test A[end] === initvalue2(F)
5256+
@test A[end-1] === initvalue(F)
5257+
prepend!(A, [initvalue(F), initvalue2(F)])
5258+
@test A[2] === initvalue2(F)
5259+
@test A[1] === initvalue(F)
5260+
5261+
# insert!
5262+
A = U[initvalue2(F2) for i = 1:len]
5263+
insert!(A, 2, initvalue2(F))
5264+
@test A[2] === initvalue2(F)
5265+
@test A[1] === initvalue2(F2)
5266+
@test A[3] === initvalue2(F2)
5267+
@test A[end] === initvalue2(F2)
5268+
A = U[initvalue2(F2) for i = 1:len]
5269+
insert!(A, 8, initvalue2(F))
5270+
@test A[8] === initvalue2(F)
5271+
@test A[7] === initvalue2(F2)
5272+
@test A[9] === initvalue2(F2)
5273+
@test A[end] === initvalue2(F2)
5274+
5275+
# splice!
5276+
A = U[initvalue2(F2) for i = 1:len]
5277+
V = splice!(A, 1:2)
5278+
@test length(A) == len - 2
5279+
@test length(V) == 2
5280+
@test V[1] == initvalue2(F2)
5281+
@test V[2] == initvalue2(F2)
5282+
@test A[1] == initvalue2(F2)
5283+
@test A[end] == initvalue2(F2)
5284+
5285+
A = U[initvalue2(F2) for i = 1:len]
5286+
V = splice!(A, 4:5)
5287+
@test length(A) == len - 2
5288+
@test length(V) == 2
5289+
@test V[1] == initvalue2(F2)
5290+
@test V[2] == initvalue2(F2)
5291+
@test A[1] == initvalue2(F2)
5292+
@test A[end] == initvalue2(F2)
5293+
end
5294+
end
5295+
end
5296+
5297+
end # module UnionOptimizations

0 commit comments

Comments
 (0)