Skip to content

Commit af19042

Browse files
committed
Update tests for isbits union optimizations
1 parent 4e7e711 commit af19042

File tree

1 file changed

+42
-19
lines changed

1 file changed

+42
-19
lines changed

test/core.jl

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5080,16 +5080,10 @@ module UnionOptimizations
50805080

50815081
using Base.Test
50825082

5083-
const testuniontypes = []
5084-
for i = 1:128
5085-
@eval struct $(Symbol("TestUnionType$i")); val::Int8; end
5086-
@eval push!(testuniontypes, $(Symbol("TestUnionType$i")))
5087-
end
5088-
5089-
const boxedunions = [Union{}, Union{String, Void}, Union{testuniontypes...}]
5083+
const boxedunions = [Union{}, Union{String, Void}]
50905084
const unboxedunions = [Union{Int8, Void}, Union{Int8, Float16, Void},
50915085
Union{Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128},
5092-
Union{Char, Date, Number}]
5086+
Union{Char, Date, Int}]
50935087

50945088
initvalue(::Type{Void}) = nothing
50955089
initvalue(::Type{Char}) = '\0'
@@ -5101,16 +5095,28 @@ initvalue2(::Type{Char}) = Char(0x01)
51015095
initvalue2(::Type{Date}) = Date(1)
51025096
initvalue2(::Type{T}) where {T <: Number} = T(1)
51035097

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])
51045110

51055111
for U in boxedunions
51065112
for N in (1, 2, 3, 4)
5107-
A = Array{U, N}(0)
5113+
A = Array{U}(ntuple(x->0, N)...)
51085114
@test isempty(A)
51095115
@test Core.sizeof(A) == 0
51105116

5111-
A = Array{U, N}(100)
5112-
@test length(A) == 100
5113-
@test Core.sizeof(A) == 800
5117+
A = Array{U}(ntuple(x->10, N)...)
5118+
@test length(A) == 10^N
5119+
@test Core.sizeof(A) == 8 * (10^N)
51145120
@test !isassigned(A, 1)
51155121
end
51165122
end
@@ -5123,20 +5129,26 @@ A5 = [1 2 3; 4 5 6]
51235129

51245130
for U in unboxedunions
51255131
for N in (1, 2, 3, 4)
5126-
A = Array{U, N}(0)
5132+
A = Array{U}(ntuple(x->0, N)...)
51275133
@test isempty(A)
51285134
@test Core.sizeof(A) == 0
51295135

51305136
len = ntuple(x->10, N)
51315137
mxsz = maximum(sizeof, Base.uniontypes(U))
5132-
A = Array{U, N}(len)
5138+
@time A = Array{U}(len)
51335139
@test length(A) == prod(len)
51345140
@test Core.sizeof(A) == prod(len) * mxsz
51355141
@test isassigned(A, 1)
51365142
@test isassigned(A, length(A))
51375143

51385144
# arrayref / arrayset
51395145
F = Base.uniontypes(U)[1]
5146+
@show F
5147+
s = ccall(:jl_elsize2, Csize_t, (Any,), A)
5148+
l = ccall(:jl_array_len2, Csize_t, (Any,), A)
5149+
@show s, l
5150+
@show A[1]
5151+
# @show A
51405152
@test A[1] === initvalue(F)
51415153
A[1] = initvalue2(F)
51425154
@test A[1] === initvalue2(F)
@@ -5151,9 +5163,8 @@ for U in unboxedunions
51515163
end
51525164

51535165
# serialize / deserialize
5154-
#TODO
51555166
io = IOBuffer()
5156-
serialize(io, v)
5167+
serialize(io, A)
51575168
seekstart(io)
51585169
A2 = deserialize(io)
51595170
@test A == A2
@@ -5164,6 +5175,10 @@ for U in unboxedunions
51645175
@test isassigned(A, 1)
51655176
@test A[1] === initvalue2(F)
51665177

5178+
# copy
5179+
A4 = copy(A)
5180+
@test A == A4
5181+
51675182
if N == 1
51685183
## Dequeue functions
51695184
# pop!
@@ -5203,7 +5218,7 @@ for U in unboxedunions
52035218
# deleteat!
52045219
F = Base.uniontypes(U)[1]
52055220
A = U[rand(F(1):F(len)) for i = 1:len]
5206-
deleteat!(A, sort!(unique(A[1:4])))
5221+
deleteat!(A, map(Int, sort!(unique(A[1:4]))))
52075222
A = U[initvalue2(F2) for i = 1:len]
52085223
deleteat!(A, 1:2)
52095224
@test length(A) == len - 2
@@ -5236,7 +5251,7 @@ for U in unboxedunions
52365251
@test A[2] === initvalue2(F)
52375252
end
52385253

5239-
# push! / append! / prepend!
5254+
# push! / append! / prepend! TODO
52405255
A = U[initvalue2(F2) for i = 1:len]
52415256
push!(A, initvalue2(F))
52425257
@test A[end] === initvalue2(F)
@@ -5249,11 +5264,19 @@ for U in unboxedunions
52495264
@test A[2] === initvalue2(F)
52505265
@test A[1] === initvalue(F)
52515266

5252-
# insert! TODO
5267+
# insert!
52535268
A = U[initvalue2(F2) for i = 1:len]
52545269
insert!(A, 2, initvalue2(F))
5270+
@test A[2] === initvalue2(F)
5271+
@test A[1] === initvalue2(F2)
5272+
@test A[3] === initvalue2(F2)
5273+
@test A[end] === initvalue2(F2)
52555274
A = U[initvalue2(F2) for i = 1:len]
52565275
insert!(A, 8, initvalue2(F))
5276+
@test A[8] === initvalue2(F)
5277+
@test A[7] === initvalue2(F2)
5278+
@test A[9] === initvalue2(F2)
5279+
@test A[end] === initvalue2(F2)
52575280

52585281
# splice!
52595282
A = U[initvalue2(F2) for i = 1:len]

0 commit comments

Comments
 (0)