@@ -5080,16 +5080,10 @@ module UnionOptimizations
5080
5080
5081
5081
using Base. Test
5082
5082
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}]
5090
5084
const unboxedunions = [Union{Int8, Void}, Union{Int8, Float16, Void},
5091
5085
Union{Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128},
5092
- Union{Char, Date, Number }]
5086
+ Union{Char, Date, Int }]
5093
5087
5094
5088
initvalue (:: Type{Void} ) = nothing
5095
5089
initvalue (:: Type{Char} ) = ' \0 '
@@ -5101,16 +5095,28 @@ initvalue2(::Type{Char}) = Char(0x01)
5101
5095
initvalue2 (:: Type{Date} ) = Date (1 )
5102
5096
initvalue2 (:: Type{T} ) where {T <: Number } = T (1 )
5103
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])
5104
5110
5105
5111
for U in boxedunions
5106
5112
for N in (1 , 2 , 3 , 4 )
5107
- A = Array {U, N} ( 0 )
5113
+ A = Array {U} ( ntuple (x -> 0 , N) ... )
5108
5114
@test isempty (A)
5109
5115
@test Core. sizeof (A) == 0
5110
5116
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)
5114
5120
@test ! isassigned (A, 1 )
5115
5121
end
5116
5122
end
@@ -5123,20 +5129,26 @@ A5 = [1 2 3; 4 5 6]
5123
5129
5124
5130
for U in unboxedunions
5125
5131
for N in (1 , 2 , 3 , 4 )
5126
- A = Array {U, N} ( 0 )
5132
+ A = Array {U} ( ntuple (x -> 0 , N) ... )
5127
5133
@test isempty (A)
5128
5134
@test Core. sizeof (A) == 0
5129
5135
5130
5136
len = ntuple (x-> 10 , N)
5131
5137
mxsz = maximum (sizeof, Base. uniontypes (U))
5132
- A = Array {U, N } (len)
5138
+ @time A = Array {U} (len)
5133
5139
@test length (A) == prod (len)
5134
5140
@test Core. sizeof (A) == prod (len) * mxsz
5135
5141
@test isassigned (A, 1 )
5136
5142
@test isassigned (A, length (A))
5137
5143
5138
5144
# arrayref / arrayset
5139
5145
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
5140
5152
@test A[1 ] === initvalue (F)
5141
5153
A[1 ] = initvalue2 (F)
5142
5154
@test A[1 ] === initvalue2 (F)
@@ -5151,9 +5163,8 @@ for U in unboxedunions
5151
5163
end
5152
5164
5153
5165
# serialize / deserialize
5154
- # TODO
5155
5166
io = IOBuffer ()
5156
- serialize (io, v )
5167
+ serialize (io, A )
5157
5168
seekstart (io)
5158
5169
A2 = deserialize (io)
5159
5170
@test A == A2
@@ -5164,6 +5175,10 @@ for U in unboxedunions
5164
5175
@test isassigned (A, 1 )
5165
5176
@test A[1 ] === initvalue2 (F)
5166
5177
5178
+ # copy
5179
+ A4 = copy (A)
5180
+ @test A == A4
5181
+
5167
5182
if N == 1
5168
5183
# # Dequeue functions
5169
5184
# pop!
@@ -5203,7 +5218,7 @@ for U in unboxedunions
5203
5218
# deleteat!
5204
5219
F = Base. uniontypes (U)[1 ]
5205
5220
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 ]) )))
5207
5222
A = U[initvalue2 (F2) for i = 1 : len]
5208
5223
deleteat! (A, 1 : 2 )
5209
5224
@test length (A) == len - 2
@@ -5236,7 +5251,7 @@ for U in unboxedunions
5236
5251
@test A[2 ] === initvalue2 (F)
5237
5252
end
5238
5253
5239
- # push! / append! / prepend!
5254
+ # push! / append! / prepend! TODO
5240
5255
A = U[initvalue2 (F2) for i = 1 : len]
5241
5256
push! (A, initvalue2 (F))
5242
5257
@test A[end ] === initvalue2 (F)
@@ -5249,11 +5264,19 @@ for U in unboxedunions
5249
5264
@test A[2 ] === initvalue2 (F)
5250
5265
@test A[1 ] === initvalue (F)
5251
5266
5252
- # insert! TODO
5267
+ # insert!
5253
5268
A = U[initvalue2 (F2) for i = 1 : len]
5254
5269
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)
5255
5274
A = U[initvalue2 (F2) for i = 1 : len]
5256
5275
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)
5257
5280
5258
5281
# splice!
5259
5282
A = U[initvalue2 (F2) for i = 1 : len]
0 commit comments