forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtuple.jl
429 lines (363 loc) · 14.5 KB
/
tuple.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# This file is a part of Julia. License is MIT: https://julialang.org/license
struct BitPerm_19352
p::NTuple{8,UInt8}
function BitPerm(p::NTuple{8,UInt8})
sort(collect(p)) != 0:7 && error("$p is not a permutation of 0:7")
new(p)
end
BitPerm_19352(xs::Vararg{Any,8}) = BitPerm(map(UInt8, xs))
end
@testset "conversion and construction" begin
@test convert(Tuple, ()) === ()
@test convert(Tuple, (1, 2)) === (1, 2)
@test convert(Tuple, (1.0, 2)) === (1.0, 2)
@test convert(NTuple, ()) === ()
@test convert(Tuple{}, ()) === ()
@test convert(Tuple{Vararg{Int}}, ()) === ()
@test convert(Tuple{Vararg{T}} where T<:Integer, ()) === ()
@test convert(NTuple{3, Int}, (1, 2, 3)) === (1, 2, 3)
@test convert(NTuple, (1, 2, 3)) === (1, 2, 3)
@test convert(Tuple{Vararg{Int}}, (1, 2, 3)) === (1, 2, 3)
@test convert(Tuple{Int, Vararg{Int}}, (1, 2, 3)) === (1, 2, 3)
@test convert(Tuple{Vararg{T}} where T<:Integer, (1, 2, 3)) === (1, 2, 3)
@test convert(Tuple{T, Vararg{T}} where T<:Integer, (1, 2, 3)) === (1, 2, 3)
@test convert(Tuple{Int, Int, Float64}, (1, 2, 3)) === (1, 2, 3.0)
@test convert(Tuple{Float64, Int, UInt8}, (1.0, 2, 0x3)) === (1.0, 2, 0x3)
@test convert(NTuple, (1.0, 2, 0x3)) === (1.0, 2, 0x3)
@test convert(Tuple{Vararg{Int}}, (1.0, 2, 0x3)) === (1, 2, 3)
@test convert(Tuple{Int, Vararg{Int}}, (1.0, 2, 0x3)) === (1, 2, 3)
@test convert(Tuple{Vararg{T}} where T<:Integer, (1.0, 2, 0x3)) === (1, 2, 0x3)
@test convert(Tuple{T, Vararg{T}} where T<:Integer, (1.0, 2, 0x3)) === (1, 2, 0x3)
@test convert(NTuple{3, Int}, (1.0, 2, 0x3)) === (1, 2, 3)
@test convert(Tuple{Int, Int, Float64}, (1.0, 2, 0x3)) === (1, 2, 3.0)
@test_throws MethodError convert(Tuple{Int}, ())
@test_throws MethodError convert(Tuple{Any}, ())
@test_throws MethodError convert(Tuple{Int, Vararg{Int}}, ())
@test_throws MethodError convert(Tuple{}, (1, 2, 3))
@test_throws MethodError convert(Tuple{}, (1.0, 2, 3))
@test_throws MethodError convert(NTuple{3, Int}, ())
@test_throws MethodError convert(NTuple{3, Int}, (1, 2))
@test_throws MethodError convert(NTuple{3, Int}, (1, 2, 3, 4))
@test_throws MethodError convert(Tuple{Int, Int, Float64}, ())
@test_throws MethodError convert(Tuple{Int, Int, Float64}, (1, 2))
@test_throws MethodError convert(Tuple{Int, Int, Float64}, (1, 2, 3, 4))
# #17198
@test_throws MethodError convert(Tuple{Int}, (1.0, 2.0, 3.0))
# #21238
@test_throws MethodError convert(Tuple{Int, Int, Int}, (1, 2))
# issue #26589
@test_throws MethodError convert(NTuple{4}, (1.0,2.0,3.0,4.0,5.0))
# PR #15516
@test Tuple{Char,Char}("za") === ('z','a')
@test_throws ArgumentError Tuple{Char,Char}("z")
@test NTuple{20,Int}(Iterators.countfrom(2)) === (2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21)
@test NTuple{20,Float64}(Iterators.countfrom(2)) === (2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.,13.,14.,15.,16.,17.,18.,19.,20.,21.)
@test_throws ArgumentError NTuple{20,Int}([1,2])
@test Tuple{Vararg{Float32}}(Float64[1,2,3]) === (1.0f0, 2.0f0, 3.0f0)
@test Tuple{Int,Vararg{Float32}}(Float64[1,2,3]) === (1, 2.0f0, 3.0f0)
@test Tuple{Int,Vararg{Any}}(Float64[1,2,3]) === (1, 2.0, 3.0)
@test Tuple(fill(1.,5)) === (1.0,1.0,1.0,1.0,1.0)
@test_throws MethodError convert(Tuple, fill(1.,5))
@testset "ambiguity between tuple constructors #20990" begin
Tuple16Int = Tuple{Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int,Int}
tuple16int = (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
@test Tuple16Int(tuple16int) isa Tuple16Int
end
@testset "side effect in tuple constructor #19352" begin
@test BitPerm_19352(0,2,4,6,1,3,5,7).p[2] == 0x02
end
@testset "ninitialized" begin
@test Tuple{Int,Any}.ninitialized == 2
@test Tuple.ninitialized == 0
@test Tuple{Int,Vararg{Any}}.ninitialized == 1
@test Tuple{Any,Any,Vararg{Any}}.ninitialized == 2
end
@test empty((1, 2.0, "c")) === ()
end
@testset "size" begin
@test length(()) == 0
@test length((1,)) === 1
@test length((1,2)) === 2
@test_throws ArgumentError Base.front(())
@test_throws ArgumentError first(())
@test lastindex(()) === 0
@test lastindex((1,)) === 1
@test lastindex((1,2)) === 2
@test size((), 1) === 0
@test size((1,), 1) === 1
@test size((1,2), 1) === 2
# @test_throws ArgumentError size((), 2)
# @test_throws ArgumentError size((1,), 2)
# @test_throws ArgumentError size((1,2), 2)
end
@testset "indexing" begin
@test getindex((1,), 1) === 1
@test getindex((1,2), 2) === 2
@test_throws BoundsError getindex((), 1)
@test_throws BoundsError getindex((1,2), 0)
@test_throws BoundsError getindex((1,2), -1)
@test getindex((1,), 1.0) === 1
@test getindex((1,2), 2.0) === 2
@test_throws BoundsError getindex((), 1.0)
@test_throws BoundsError getindex((1,2), 0.0)
@test_throws BoundsError getindex((1,2), -1.0)
@test getindex((5,6,7,8), [1,2,3]) === (5,6,7)
@test_throws BoundsError getindex((1,2), [3,4])
@test getindex((5,6,7,8), [true, false, false, true]) === (5,8)
@test_throws BoundsError getindex((5,6,7,8), [true, false, false, true, true])
@test getindex((5,6,7,8), []) === ()
@test getindex((1,9,9,3),:) === (1,9,9,3)
@test getindex((),:) === ()
@test getindex((1,),:) === (1,)
@testset "boolean arrays" begin
# issue #19719
@test_throws BoundsError (1,2,3)[falses(4)]
@test_throws BoundsError (1,2,3)[[false,false,true,true]]
@test_throws BoundsError (1,2,3)[trues(2)]
@test_throws BoundsError (1,2,3)[falses(2)]
@test_throws BoundsError ()[[false]]
@test_throws BoundsError ()[[true]]
end
@testset "Multidimensional indexing (issue #20453)" begin
@test_throws MethodError (1,)[]
@test_throws MethodError (1,1,1)[1,1]
end
end
@testset "fill to length" begin
## filling to specified length
@test @inferred(Base.fill_to_length((1,2,3), -1, Val(5))) == (1,2,3,-1,-1)
@test_throws ArgumentError Base.fill_to_length((1,2,3), -1, Val(2))
end
@testset "iterating" begin
@test iterate(()) === nothing
t = (1,2,3)
y1 = iterate(t)
y2 = iterate(t, y1[2])
y3 = iterate(t, y2[2])
@test y3 !== nothing
@test iterate(t, y3[2]) === nothing
@test eachindex((2,5,"foo")) === Base.OneTo(3)
@test eachindex((2,5,"foo"), (1,2,5,7)) === Base.OneTo(4)
end
@testset "element type" begin
@test eltype((1,2,3)) === Int
@test eltype((1.0,2.0,3.0)) <: AbstractFloat
@test eltype((true, false)) === Bool
@test eltype((1, 2.0, false)) === typejoin(Int, Float64, Bool)
@test eltype(()) === Union{}
@test eltype(Tuple{Int, Float64, Vararg{Bool}}) === typejoin(Int, Float64, Bool)
@test eltype(Tuple{Int, T, Vararg{Bool}} where T <: AbstractFloat) ===
typejoin(Int, AbstractFloat, Bool)
@test eltype(Tuple{Int, Bool, Vararg{T}} where T <: AbstractFloat) ===
typejoin(Int, AbstractFloat, Bool)
@test eltype(Union{Tuple{Int, Float64}, Tuple{Vararg{Bool}}}) ===
typejoin(Int, Float64, Bool)
@test eltype(Tuple{Int, Missing}) === Union{Missing, Int}
@test eltype(Tuple{Int, Nothing}) === Union{Nothing, Int}
end
@testset "map with Nothing and Missing" begin
for T in (Nothing, Missing)
x = [(1, T()), (1, 2)]
y = map(v -> (v[1], v[2]), [(1, T()), (1, 2)])
@test y isa Vector{Tuple{Int, Any}}
@test isequal(x, y)
end
y = map(v -> (v[1], v[1] + v[2]), [(1, missing), (1, 2)])
@test y isa Vector{Tuple{Int, Any}}
@test isequal(y, [(1, missing), (1, 3)])
end
@testset "mapping" begin
foo() = 2
foo(x) = 2x
foo(x, y) = x + y
foo(x, y, z) = x + y + z
longtuple = ntuple(identity, 20)
@testset "1 argument" begin
@test map(foo, ()) === ()
@test map(foo, (1,)) === (2,)
@test map(foo, (1,2)) === (2,4)
@test map(foo, (1,2,3,4)) === (2,4,6,8)
@test map(foo, longtuple) === ntuple(i->2i,20)
end
@testset "2 arguments" begin
@test map(foo, (), ()) === ()
@test map(foo, (1,), (1,)) === (2,)
@test map(foo, (1,2), (1,2)) === (2,4)
@test map(foo, (1,2,3,4), (1,2,3,4)) === (2,4,6,8)
@test map(foo, longtuple, longtuple) === ntuple(i->2i,20)
@test_throws BoundsError map(foo, (), (1,))
@test_throws BoundsError map(foo, (1,), ())
end
@testset "n arguments" begin
@test map(foo, (), (), ()) === ()
@test map(foo, (1,), (1,), (1,)) === (3,)
@test map(foo, (1,2), (1,2), (1,2)) === (3,6)
@test map(foo, (1,2,3,4), (1,2,3,4), (1,2,3,4)) === (3,6,9,12)
@test map(foo, longtuple, longtuple, longtuple) === ntuple(i->3i,20)
@test_throws BoundsError map(foo, (), (1,), (1,))
@test_throws BoundsError map(foo, (1,), (1,), ())
end
end
@testset "comparison and hash" begin
@test isequal((), ())
@test isequal((1,2,3), (1,2,3))
@test !isequal((1,2,3), (1,2,4))
@test !isequal((1,2,3), (1,2))
@test ==((), ())
@test ==((1,2,3), (1,2,3))
@test !==((1,2,3), (1,2,4))
@test !==((1,2,3), (1,2))
@test (1,2) < (1,3)
@test (1,) < (1,2)
@test !((1,2) < (1,2))
@test (2,1) > (1,2)
@test isless((1,2), (1,3))
@test isless((1,), (1,2))
@test !isless((1,2), (1,2))
@test !isless((2,1), (1,2))
@test hash(()) === Base.tuplehash_seed
@test hash((1,)) === hash(1, Base.tuplehash_seed)
@test hash((1,2)) === hash(1, hash(2, Base.tuplehash_seed))
# Test Any16 methods
t = ntuple(identity, 16)
@test isequal((t...,1,2,3), (t...,1,2,3))
@test !isequal((t...,1,2,3), (t...,1,2,4))
@test !isequal((t...,1,2,3), (t...,1,2))
@test ==((t...,1,2,3), (t...,1,2,3))
@test !==((t...,1,2,3), (t...,1,2,4))
@test !==((t...,1,2,3), (t...,1,2))
@test (t...,1,2) < (t...,1,3)
@test (t...,1,) < (t...,1,2)
@test !((t...,1,2) < (t...,1,2))
@test (t...,2,1) > (t...,1,2)
@test isless((t...,1,2), (t...,1,3))
@test isless((t...,1,), (t...,1,2))
@test !isless((t...,1,2), (t...,1,2))
@test !isless((t...,2,1), (t...,1,2))
@test hash(t) === foldr(hash, [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,(),UInt(0)])
end
@testset "functions" begin
@test isempty(())
@test !isempty((1,))
@test reverse(()) === ()
@test reverse((1,2,3)) === (3,2,1)
end
# issue #21697
@test_throws ArgumentError ntuple(identity, -1)
@testset "specialized reduction" begin
@test sum((1,2,3)) === 6
@test prod(()) === 1
@test prod((1,2,3)) === 6
@testset "all" begin
@test all(()) === true
@test all((false,)) === false
@test all((true,)) === true
@test all((true, true)) === true
@test all((true, false)) === false
@test all((false, false)) === false
end
@testset "any" begin
@test any(()) === false
@test any((true,)) === true
@test any((false,)) === false
@test any((true, true)) === true
@test any((true, false)) === true
@test any((false, false)) === false
@test any((false,false,false)) === false
@test any((true,false,false)) === true
@test any((false,true,false)) === true
@test any((false,false,true)) === true
@test any((true,true,false)) === true
@test any((true,false,true)) === true
@test any((true,true,false)) === true
@test any((true,true,true)) === true
end
end
@testset "ntuple" begin
nttest1(x::NTuple{n, Int}) where {n} = n
@test nttest1(()) == 0
@test nttest1((1, 2)) == 2
@test NTuple <: Tuple
@test (NTuple{T, Int32} where T) <: Tuple{Vararg{Int32}}
@test !((NTuple{T, Int32} where T) <: Tuple{Int32, Vararg{Int32}})
@test Tuple{Vararg{Int32}} <: (NTuple{T, Int32} where T)
@test Tuple{Int32, Vararg{Int32}} <: (NTuple{T, Int32} where T)
@test @inferred(ntuple(abs2, Val(0))) == ()
@test @inferred(ntuple(abs2, Val(2))) == (1, 4)
@test @inferred(ntuple(abs2, Val(3))) == (1, 4, 9)
@test @inferred(ntuple(abs2, Val(4))) == (1, 4, 9, 16)
@test @inferred(ntuple(abs2, Val(5))) == (1, 4, 9, 16, 25)
@test @inferred(ntuple(abs2, Val(6))) == (1, 4, 9, 16, 25, 36)
# issue #21697
@test_throws ArgumentError ntuple(abs2, Val(-1))
# issue #12854
@test_throws TypeError ntuple(identity, Val(1:2))
for n = 0:20
t = ntuple(identity, n)
@test length(t) == n
for i = 1:n
@test t[i] == i
end
end
# issue #21697
@test_throws ArgumentError ntuple(identity, -1)
# PR #21446
for n = 0:15
@test ntuple(identity, Val(n)) == ntuple(identity, n)
end
end
struct A_15703{N}
keys::NTuple{N, Int}
end
struct B_15703
x::A_15703
end
@testset "issue #15703" begin
function bug_15703(xs...)
[x for x in xs]
end
function test_15703()
s = (1,)
a = A_15703(s)
ss = B_15703(a).x.keys
@test ss === s
bug_15703(ss...)
end
test_15703()
end
@testset "#21026" begin
# https://github.com/JuliaLang/julia/issues/21026#issuecomment-317113307
VecTuple21026{T} = Tuple{VecElement{T}}
@test convert(VecTuple21026, (1,)) === (VecElement(1),)
@test convert(Tuple{Complex{T}, Complex{T}} where T<:Real, (1, 2)) ===
(Complex(1), Complex(2))
@test convert(Tuple{Complex{T}, Complex{T}} where T<:Real, (1, 2.0)) ===
(Complex(1), Complex(2.0))
@test convert(Tuple{Complex, Complex}, (1, 2)) ===
(Complex(1), Complex(2))
@test convert(Tuple{Complex, Complex}, (1, 2.0)) ===
(Complex(1), Complex(2.0))
end
@testset "issue 24707" begin
@test eltype(Tuple{Vararg{T}} where T<:Integer) >: Integer
end
@testset "find" begin
@test findall(isequal(1), (1, 2)) == [1]
@test findall(isequal(1), (1, 1)) == [1, 2]
@test isempty(findall(isequal(1), ()))
@test isempty(findall(isequal(1), (2, 3)))
@test findfirst(isequal(1), (1, 2)) == 1
@test findlast(isequal(1), (1, 2)) == 1
@test findfirst(isequal(1), (1, 1)) == 1
@test findlast(isequal(1), (1, 1)) == 2
@test findfirst(isequal(1), ()) === nothing
@test findlast(isequal(1), ()) === nothing
@test findfirst(isequal(1), (2, 3)) === nothing
@test findlast(isequal(1), (2, 3)) === nothing
@test findnext(isequal(1), (1, 2), 1) == 1
@test findprev(isequal(1), (1, 2), 2) == 1
@test findnext(isequal(1), (1, 1), 2) == 2
@test findprev(isequal(1), (1, 1), 1) == 1
@test findnext(isequal(1), (2, 3), 1) === nothing
@test findprev(isequal(1), (2, 3), 2) === nothing
end