-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
core.jl
2805 lines (2488 loc) · 62.7 KB
/
core.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# test core language features
const Bottom = Union()
# basic type relationships
@test Int8 <: Integer
@test Int32 <: Integer
@test Tuple{Int8,Int8} <: Tuple{Integer,Integer}
@test !(AbstractArray{Float64,2} <: AbstractArray{Number,2})
@test !(AbstractArray{Float64,1} <: AbstractArray{Float64,2})
@test Tuple{Integer,Vararg{Integer}} <: Tuple{Integer,Vararg{Real}}
@test Tuple{Integer,Float64,Vararg{Integer}} <: Tuple{Integer,Vararg{Number}}
@test Tuple{Integer,Float64} <: Tuple{Integer,Vararg{Number}}
@test Tuple{Int32,} <: Tuple{Vararg{Number}}
@test Tuple{} <: Tuple{Vararg{Number}}
@test !(Tuple{Vararg{Int32}} <: Tuple{Int32,})
@test !(Tuple{Vararg{Int32}} <: Tuple{Number,Integer})
@test !(Tuple{Vararg{Integer}} <: Tuple{Integer,Integer,Vararg{Integer}})
@test !(Array{Int8,1} <: Array{Any,1})
@test !(Array{Any,1} <: Array{Int8,1})
@test Array{Int8,1} <: Array{Int8,1}
@test !(Type{Bottom} <: Type{Int32})
@test !(Vector{Float64} <: Vector{Union(Float64,Float32)})
@test is(Bottom, typeintersect(Vector{Float64},Vector{Union(Float64,Float32)}))
@test !isa(Array,Type{Any})
@test Type{Complex} <: DataType
@test isa(Complex,Type{Complex})
@test !(Type{Ptr{Bottom}} <: Type{Ptr})
@test !(Type{Rational{Int}} <: Type{Rational})
let T = TypeVar(:T,true)
@test !is(Bottom, typeintersect(Array{Bottom},AbstractArray{T}))
@test is(Bottom, typeintersect(Tuple{Type{Ptr{UInt8}},Ptr{Bottom}},
Tuple{Type{Ptr{T}},Ptr{T}}))
@test !(Type{T} <: TypeVar)
@test isequal(typeintersect(Tuple{Range{Int},Tuple{Int,Int}},Tuple{AbstractArray{T},Dims}),
Tuple{Range{Int},Tuple{Int,Int}})
@test isequal(typeintersect(Tuple{T, AbstractArray{T}}, Tuple{Number, Array{Int,1}}),
Tuple{Int, Array{Int,1}})
@test isequal(typeintersect(Tuple{T, AbstractArray{T}}, Tuple{Int, Array{Number,1}}),
Tuple{Int, Array{Number,1}})
@test isequal(typeintersect(Tuple{T, AbstractArray{T}},Tuple{Any, Array{Number,1}}),
Tuple{Number, Array{Number,1}})
@test !is(Bottom, typeintersect(Tuple{Array{T}, Array{T}}, Tuple{Array, Array{Any}}))
f47{T}(x::Vector{Vector{T}}) = 0
@test_throws MethodError f47(Array(Vector,0))
@test f47(Array(Vector{Int},0)) == 0
@test typeintersect(Tuple{T,T}, Tuple{Union(Float64,Int64),Int64}) == Tuple{Int64,Int64}
@test typeintersect(Tuple{T,T}, Tuple{Int64,Union(Float64,Int64)}) == Tuple{Int64,Int64}
TT = TypeVar(:T)
S = TypeVar(:S,true); N = TypeVar(:N,true); SN = TypeVar(:S,Number,true)
@test typeintersect(Type{TypeVar(:T,Array{TT,1})},Type{Array{SN,N}}) == Type{Array{SN,1}}
# issue #5359
@test typeintersect(Tuple{Type{Array{T,1}},Array{T,1}},
Tuple{Type{AbstractVector},Vector{Int}}) === Bottom
# issue #5559
@test typeintersect(Tuple{Type{Vector{Complex128}}, AbstractVector},
Tuple{Type{Array{T,N}}, Array{S,N}}) == Tuple{Type{Vector{Complex128}},Vector}
@test typeintersect(Tuple{Type{Vector{Complex128}}, AbstractArray},
Tuple{Type{Array{T,N}}, Array{S,N}}) == Tuple{Type{Vector{Complex128}},Vector}
@test typeintersect(Type{Array{T}}, Type{AbstractArray{T}}) === Bottom
@test typeintersect(Type{Tuple{Bool,Vararg{Int}}}, Type{Tuple{Vararg{T}}}) === Bottom
@test typeintersect(Type{Tuple{Bool,Vararg{Int}}}, Type{Tuple{T,Vararg{T}}}) === Bottom
@test typeintersect(Tuple{Rational{T},T}, Tuple{Rational{Integer},Int}) === Tuple{Rational{Integer},Int}
@test typeintersect(Pair{T,Ptr{T}}, Pair{Ptr{S},S}) === Bottom
@test typeintersect(Tuple{T,Ptr{T}}, Tuple{Ptr{S},S}) === Bottom
end
let N = TypeVar(:N,true)
@test isequal(typeintersect(Tuple{NTuple{N,Integer},NTuple{N,Integer}},
Tuple{Tuple{Integer,Integer}, Tuple{Vararg{Integer}}}),
Tuple{Tuple{Integer,Integer}, Tuple{Integer,Integer}})
@test isequal(typeintersect(Tuple{NTuple{N,Integer},NTuple{N,Integer}},
Tuple{Tuple{Vararg{Integer}}, Tuple{Integer,Integer}}),
Tuple{Tuple{Integer,Integer}, Tuple{Integer,Integer}})
local A = typeintersect(Tuple{NTuple{N,Any},Array{Int,N}},
Tuple{Tuple{Int,Vararg{Int}},Array})
local B = Tuple{Tuple{Int,Vararg{Int}},Array{Int,N}}
@test A<:B && B<:A
@test isequal(typeintersect(Tuple{NTuple{N,Any},Array{Int,N}},
Tuple{Tuple{Int,Vararg{Int}},Array{Int,2}}),
Tuple{Tuple{Int,Int}, Array{Int,2}})
end
@test is(Bottom, typeintersect(Type{Any},Type{Complex}))
@test is(Bottom, typeintersect(Type{Any},Type{TypeVar(:T,Real)}))
@test !(Type{Array{Integer}} <: Type{AbstractArray{Integer}})
@test !(Type{Array{Integer}} <: Type{Array{TypeVar(:T,Integer)}})
@test is(Bottom, typeintersect(Type{Function},UnionType))
@test is(Type{Int32}, typeintersect(Type{Int32},DataType))
@test !(Type <: TypeVar)
@test !is(Bottom, typeintersect(DataType, Type))
@test !is(Bottom, typeintersect(UnionType, Type))
@test !is(Bottom, typeintersect(DataType, Type{Int}))
@test !is(Bottom, typeintersect(DataType, Type{TypeVar(:T,Int)}))
@test !is(Bottom, typeintersect(DataType, Type{TypeVar(:T,Integer)}))
@test typeintersect(Tuple{Vararg{Int}}, Tuple{Vararg{Bool}}) === Tuple{}
@test typeintersect(Type{Tuple{Vararg{Int}}}, Type{Tuple{Vararg{Bool}}}) === Bottom
@test typeintersect(Tuple{Bool,Vararg{Int}}, Tuple{Vararg{Bool}}) === Tuple{Bool,}
let T = TypeVar(:T,Union(Float32,Float64))
@test typeintersect(AbstractArray, Matrix{T}) == Matrix{T}
end
let T = TypeVar(:T,Union(Float32,Float64),true)
@test typeintersect(AbstractArray, Matrix{T}) == Matrix{T}
end
@test isa(Int,Type{TypeVar(:T,Number)})
@test !isa(DataType,Type{TypeVar(:T,Number)})
@test DataType <: Type{TypeVar(:T,Type)}
@test isa(Tuple{},Type{Tuple{}})
@test !(Tuple{Int,} <: Type{TypeVar(:T,Tuple)})
@test isa(Tuple{Int,},Type{TypeVar(:T,Tuple)})
@test !isa(Type{Tuple{Int,Int}},Tuple)
@test !(Type{Tuple{Int,Int}} <: Tuple)
@test Tuple{Type{Int}} <: Tuple{DataType}
@test !issubtype(Type{Array{TypeVar(:T,true)}}, Type{Array})
@test () != Type{Tuple{}}
# issue #6561
@test issubtype(Array{Tuple}, Array{NTuple})
@test issubtype(Array{Tuple{Vararg{Any}}}, Array{NTuple})
@test !issubtype(Array{Tuple{Vararg{Int}}}, Array{NTuple})
@test !issubtype(Array{Tuple{Int,Int}}, Array{NTuple})
@test !issubtype(Type{Tuple{Void}}, Tuple{Type{Void}})
# this is fancy: know that any type T<:Number must be either a DataType or a UnionType
@test Type{TypeVar(:T,Number)} <: Union(DataType,UnionType)
@test !(Type{TypeVar(:T,Number)} <: DataType)
@test !(Type{TypeVar(:T,Tuple)} <: Union(Tuple,UnionType))
@test Type{TypeVar(:T,Tuple)} <: Union(DataType,UnionType)
# issue #2997
let T = TypeVar(:T,Union(Float64,Array{Float64,1}),true)
@test typeintersect(T,Real) === Float64
end
# join
@test typejoin(Int8,Int16) === Signed
@test typejoin(Int,AbstractString) === Any
@test typejoin(Array{Float64},BitArray) <: AbstractArray
@test typejoin(Array{Bool},BitArray) <: AbstractArray{Bool}
@test typejoin(Tuple{Int,Int8},Tuple{Int8,Float64}) === Tuple{Signed,Real}
@test Base.typeseq(typejoin(Tuple{ASCIIString,ASCIIString},Tuple{UTF8String,ASCIIString},
Tuple{ASCIIString,UTF8String},Tuple{Int,ASCIIString,Int}),
Tuple{Any,AbstractString,Vararg{Int}})
@test Base.typeseq(typejoin(Tuple{Int8,Vararg{Int}},Tuple{Int8,Int8}),
Tuple{Int8,Vararg{Signed}})
@test Base.typeseq(typejoin(Tuple{Int8,Vararg{Int}},Tuple{Int8,Vararg{Int8}}),
Tuple{Int8,Vararg{Signed}})
@test Base.typeseq(typejoin(Tuple{Int8,UInt8,Vararg{Int}},Tuple{Int8,Vararg{Int8}}),
Tuple{Int8,Vararg{Integer}})
@test Base.typeseq(typejoin(Union(Int,AbstractString),Int), Union(Int,AbstractString))
@test Base.typeseq(typejoin(Union(Int,AbstractString),Int8), Any)
# typejoin associativity
abstract Foo____{K}
type Wow____{K,V} <: Foo____{K} end
type Bar____{K,V} <: Foo____{K} end
let
a = Wow____{Int64, Int64}
b = Wow____{Int64, Float64}
c = Bar____{Int64, Int64}
@test typejoin(typejoin(b,c), a) == typejoin(typejoin(b,a), c) == Foo____{Int64}
end
@test promote_type(Bool,Bottom) === Bool
# ntuples
nttest1{n}(x::NTuple{n,Int}) = n
@test nttest1(()) == 0
@test nttest1((1,2)) == 2
@test NTuple <: Tuple
@test NTuple{TypeVar(:T),Int32} <: Tuple{Vararg{Int32}}
@test !(NTuple{TypeVar(:T),Int32} <: Tuple{Int32,Vararg{Int32}})
@test Tuple{Vararg{Int32}} <: NTuple{TypeVar(:T),Int32}
@test Tuple{Int32,Vararg{Int32}} <: NTuple{TypeVar(:T),Int32}
# type declarations
abstract Sup_{A,B}
abstract Qux_{T} <: Sup_{Qux_{Int},T}
@test Qux_{Int}.super <: Sup_
@test is(Qux_{Int}, Qux_{Int}.super.parameters[1])
@test is(Qux_{Int}.super.parameters[2], Int)
@test Qux_{Char}.super <: Sup_
@test is(Qux_{Int}, Qux_{Char}.super.parameters[1])
@test is(Qux_{Char}.super.parameters[2], Char)
@test Qux_.super.parameters[1].super <: Sup_
@test is(Qux_{Int}, Qux_.super.parameters[1].super.parameters[1])
@test is(Int, Qux_.super.parameters[1].super.parameters[2])
type Foo_{T} x::Foo_{Int} end
@test is(Foo_.types[1], Foo_{Int})
@test is(Foo_.types[1].types[1], Foo_{Int})
type Circ_{T} x::Circ_{T} end
@test is(Circ_{Int}, Circ_{Int}.types[1])
# issue #3890
type A3890{T1}
x::Matrix{Complex{T1}}
end
@test A3890{Float64}.types[1] === Array{Complex{Float64},2}
# make sure the field type Matrix{Complex{T1}} isn't cached
type B3890{T2}
x::Matrix{Complex{T2}}
end
@test B3890{Float64}.types[1] === Array{Complex{Float64},2}
# issue #786
type Node{T}
v::Vector{Node}
end
@test is(Node{Int}.types[1].parameters[1], Node)
type Node2{T}
v::Vector{Node2{T}}
end
@test is(Node2{Int}.types[1].parameters[1], Node2{Int})
type FooFoo{A,B} y::FooFoo{A} end
@test FooFoo{Int} <: FooFoo{Int,AbstractString}.types[1]
x = (2,3)
@test +(x...) == 5
# bits types
if WORD_SIZE == 64
@test isa((()->Intrinsics.box(Ptr{Int8},Intrinsics.unbox(Int64,0)))(), Ptr{Int8})
else
@test isa((()->Intrinsics.box(Ptr{Int8},Intrinsics.unbox(Int32,0)))(), Ptr{Int8})
end
@test isa(convert(Char,65), Char)
# conversions
function fooo()
local x::Int8
x = 100
x
end
@test fooo() === convert(Int8,100)
function fooo_2()
local x::Int8
x = 100
end
@test fooo_2() === 100
function fooo_3()
local x::Int8
y = x = 100
@test isa(x,Int8)
y
end
@test fooo_3() === 100
function foo()
local x::Int8
function bar()
x = 100
end
bar()
x
end
@test foo() === convert(Int8,100)
function bar{T}(x::T)
local z::Complex{T}
z = x
z
end
@test bar(3.0) == Complex(3.0,0.0)
z = convert(Complex{Float64},2)
@test z == Complex(2.0,0.0)
# misc
fib(n) = n < 2 ? n : fib(n-1) + fib(n-2)
@test fib(20) == 6765
# static parameters
sptest1{T}(x::T, y::T) = 42
sptest1{T,S}(x::T, y::S) = 43
@test sptest1(1,2) == 42
@test sptest1(1,"b") == 43
sptest2{T}(x::T) = T
@test is(sptest2(:a),Symbol)
sptest3{T}(x::T) = y->T
let m = sptest3(:a)
@test is(m(0),Symbol)
end
# closures
function clotest()
c = 0
function inc()
c += 1
end
function dec()
c -= 1
end
inc(); inc()
@test c == 2
dec()
@test c == 1
@test (()->c)() == 1
fibb(n) = n < 2 ? n : fibb(n-1)+fibb(n-2)
@test fibb(10) == 55
return (n->(c+=n), ()->c)
end
let T = clotest()
(inc, C) = T
inc(11)
@test C() == 12
end
Yc(f) = (h->f(x->h(h)(x)))(h->f(x->h(h)(x)))
yfib = Yc(fib->(n->(n < 2 ? n : fib(n-1) + fib(n-2))))
@test yfib(20) == 6765
# variable scope, globals
glob_x = 23
function glotest()
global glob_x
glob_x = 24
loc_x = 8
function inner()
global loc_x = 10
glob_x = 88
end
function inner2()
local glob_x # override
global loc_x
glob_x = 2
@test glob_x == 2
@test loc_x == 10
end
inner()
inner2()
@test glob_x == 88
@test loc_x == 8
end
glotest()
@test glob_x == 88
@test loc_x == 10
# issue #7272
@test expand(parse("let
global x = 2
local x = 1
end")) == Expr(:error, "variable \"x\" declared both local and global")
# let - new variables, including undefinedness
function let_undef()
first = true
for i = 1:2
let x
if first; x=1; first=false; end
x+1
end
end
end
@test_throws UndefVarError let_undef()
# const implies local in a local scope block
function const_implies_local()
let
x = 1
local y
let
const x = 0
y = x
end
x, y
end
end
@test const_implies_local() === (1, 0)
a = cell(3)
for i=1:3
let ii = i
a[i] = x->x+ii
end
end
@test a[1](10) == 11
@test a[2](10) == 12
@test a[3](10) == 13
# ? syntax
@test (true ? 1 : false ? 2 : 3) == 1
# issue #7252
begin
local a
1 > 0 ? a=2 : a=3
@test a == 2
1 < 0 ? a=2 : a=3
@test a == 3
end
# tricky space sensitive syntax cases
@test [-1 ~1] == [(-1) (~1)]
# undefinedness
type UndefField
field
UndefField() = new()
end
begin
local a
a = cell(2)
@test !isdefined(a,1) && !isdefined(a,2)
a[1] = 1
@test isdefined(a,1) && !isdefined(a,2)
a = Array(Float64,1)
@test isdefined(a,1)
@test isdefined(a)
@test !isdefined(a,2)
a = UndefField()
@test !isdefined(a, :field)
@test !isdefined(a, :foo)
@test !isdefined(2, :a)
@test isdefined("a",:data)
@test isdefined("a", 1)
@test !isdefined("a", 2)
@test_throws TypeError isdefined(2)
end
# dispatch
begin
local foo, bar, baz
foo(x::Tuple{Vararg{Any}})=0
foo(x::Tuple{Vararg{Integer}})=1
@test foo((:a,))==0
@test foo(( 2,))==1
bar{T}(x::Tuple{T,T,T,T})=1
bar(x::Tuple{Any,Any,Any,Any})=2
@test bar((1,1,1,1)) == 1
@test bar((1,1,1,"a")) == 2
@test bar((:a,:a,:a,:a)) == 1
baz(::Type{Rational}) = 1
baz{T}(::Type{Rational{T}}) = 2
@test baz(Rational) == 1
@test baz(Rational{Int}) == 2
end
begin
local mytype
function mytype(vec)
convert(Vector{Tuple{ASCIIString, DataType}}, vec)
end
some_data = Any[("a", Int32), ("b", Int32)]
@test isa(mytype(some_data),Vector{Tuple{ASCIIString, DataType}})
end
type MyArray{N} <: AbstractArray{Int, N}
end
begin
local x
x = MyArray{1}()
foob(x::AbstractArray)=0
foob{T}(x::AbstractVector{T})=1
@test foob(x) == 1
end
begin
local f, g, a
f{T}(a::Vector{Vector{T}}) = a
g{T}(a::Vector{Vector{T}}) = a
a = Vector{Int}[]
@test is(f(a), a)
@test is(g(a), a)
end
type _AA{T}; a::T; end
typealias _AoA{T} _AA{_AA{T}}
begin
local g, a
g{T}(a::_AA{_AA{T}}) = a
a = _AA(_AA(1))
@test is(g(a),a)
end
# dispatch using Val{T}. See discussion in #9452 for instances vs types
begin
local firstlast
firstlast(::Type{Val{true}}) = "First"
firstlast(::Type{Val{false}}) = "Last"
@test firstlast(Val{true}) == "First"
@test firstlast(Val{false}) == "Last"
end
# x::Vararg{Any} declarations
begin
local f1, f2, f3
f1(x...) = [x...]
f2(x::Vararg{Any}) = [x...]
f3(x::Vararg) = [x...]
@test f1(1,2,3) == [1,2,3]
@test f2(1,2,3) == [1,2,3]
@test f3(1,2,3) == [1,2,3]
end
# try/finally
begin
after = 0
b = try
1+2
finally
after = 1
end
@test b == 3
@test after == 1
after = 0
gothere = 0
try
try
error(" ")
finally
after = 1
end
gothere = 1
end
@test after == 1
@test gothere == 0
after = 0
b = try
error(" ")
catch
42
finally
after = 1
end
@test b == 42
@test after == 1
glo = 0
function retfinally()
try
return 5
finally
glo = 18
end
end
@test retfinally() == 5
@test glo == 18
@test try error() end === nothing
end
# finalizers
let
A = [1]
x = 0
finalizer(A, a->(x+=1))
finalize(A)
@test x == 1
A = 0
gc(); gc()
@test x == 1
end
# Module() constructor
@test names(Module(:anonymous), true, true) != [:anonymous]
@test names(Module(:anonymous, false), true, true) == [:anonymous]
# issue #7307
function test7307(a, ret)
try
try
ret && return a
finally
push!(a, "inner")
end
finally
push!(a, "outer")
end
return a
end
@test test7307([], true) == ["inner","outer"]
@test test7307([], false) == ["inner","outer"]
# issue #8277
function test8277(a)
i = 0
for j=1:2
try
if i == 0
push!(a,0)
end
i += 1
error()
catch
end
end
end
let a = []
test8277(a)
@test length(a) == 1
end
# chained and multiple assignment behavior (issue #2913)
begin
local x, a, b, c, d, e
x = (a,b,b,b,e) = (1,2,3,4,5)
@test x === (1,2,3,4,5)
@test a == 1
@test b == 4
@test e == 5
x = (a,b,b,e) = (1,2,3,4,5)
@test x === (1,2,3,4,5)
@test a == 1
@test b == 3
@test e == 4
a = complex(1,2)
b = 3
b, a = a.re, b
@test b == 1
@test a == 3
a = complex(1,2)
b = 3
a, b = b, a.re
@test a == 3
@test b == 1
end
# accessing fields by index
begin
local z = complex(3, 4)
v = Int[0,0]
for i=1:2
v[i] = getfield(z, i)
end
@test v == [3,4]
@test_throws BoundsError getfield(z, -1)
@test_throws BoundsError getfield(z, 0)
@test_throws BoundsError getfield(z, 3)
strct = LoadError("", 0, "")
setfield!(strct, 2, 8)
@test strct.line == 8
setfield!(strct, 3, "hi")
@test strct.error == "hi"
setfield!(strct, 1, "yo")
@test strct.file == "yo"
@test_throws BoundsError getfield(strct, 10)
@test_throws BoundsError setfield!(strct, 0, "")
@test_throws BoundsError setfield!(strct, 4, "")
end
# allow typevar in Union to match as long as the arguments contain
# sufficient information
# issue #814
begin
local MatOrNot, my_func, M
typealias MatOrNot{T} Union(AbstractMatrix{T}, Vector{Union()})
my_func{T<:Real}(A::MatOrNot{T}, B::MatOrNot{T}, C::MatOrNot{T}) = 0
M = [ 2. 1. ; 1. 1. ]
@test my_func(Union()[], M, M) == 0
end
begin
local my_func, a, c
my_func{T}(P::Vector{T}, Q::Vector{T}) = 0
my_func{T}(x::T, P::Vector{T}) = 1
# todo: this gives an ambiguity warning
#my_func{T}(P::Vector{T}, x::T) = 2
a = Int[3]
c = Vector[a]
@test my_func(c,c)==0
@test my_func(a,c)==1
end
begin
local baar, foor, boor
# issue #1131
baar(x::DataType) = 0
baar(x::UnionType) = 1
baar(x::TypeConstructor) = 2
@test baar(StridedArray) == 2
@test baar(StridedArray.body) == 1
@test baar(Vector) == 2
@test baar(Vector.body) == 0
boor(x) = 0
boor(x::UnionType) = 1
@test boor(StridedArray) == 0
@test boor(StridedArray.body) == 1
# issue #1202
foor(x::UnionType) = 1
@test_throws MethodError foor(StridedArray)
@test foor(StridedArray.body) == 1
@test_throws MethodError foor(StridedArray)
end
# issue #1153
type SI{m, s, kg}
value::FloatingPoint
end
import Base.*
*{m1, m2, s1, s2, kg1, kg2}(x::SI{m1, s1, kg1}, y::SI{m2, s2, kg2}) = SI{m1 + m2, s1 + s2, kg1 + kg2}(x.value * y.value)
begin
local a,b
a = SI{0,0,1}(1.0) * SI{1,2,0}(2.0)
b = SI{0,0,1}(1.0) * SI{1,-2,0}(2.0)
@test typeof(a) === SI{1,2,1}
@test typeof(b) === SI{1,-2,1}
end
# pointer arithmetic
begin
local a,b,c
a = C_NULL
b = C_NULL + 1
c = C_NULL - 1
@test a != b != c
@test UInt(a) == 0
@test UInt(b) == 1
@test UInt(c) == typemax(UInt)
@test b - a == -(a - b) == 1
@test c - a == -(a - c) == typemax(UInt)
@test c - b == -(b - c) == typemax(UInt) - 1
@test a < b < c
end
# pull request 1270
begin
local a,p, a2,p2
a = [11,12,13]
p = pointer(a)
@test unsafe_load(p, 1) == 11
unsafe_store!(p, 99, 2)
@test a == [11,99,13]
a2 = Any[101,102,103]
p2 = pointer(a2)
@test unsafe_load(p2) == 101
unsafe_store!(p2, 909, 3)
@test a2 == [101,102,909]
end
@test unsafe_pointer_to_objref(ccall(:jl_call1, Ptr{Void}, (Any,Any),
x -> x+1, 314158)) == 314159
@test unsafe_pointer_to_objref(pointer_from_objref(e+pi)) == e+pi
begin
local a, aa
a = [1,2,3]
aa = pointer_to_array(pointer(a), length(a))
@test aa == a
aa = pointer_to_array(pointer(a), (length(a),))
@test aa == a
aa = pointer_to_array(pointer(a), UInt(length(a)))
@test aa == a
aa = pointer_to_array(pointer(a), UInt16(length(a)))
@test aa == a
@test_throws ErrorException pointer_to_array(pointer(a), -3)
end
immutable FooBar
foo::Int
bar::Int
end
begin
local X, p
X = FooBar[ FooBar(3,1), FooBar(4,4) ]
p = pointer(X)
@test unsafe_load(p, 2) == FooBar(4,4)
unsafe_store!(p, FooBar(7,3), 1)
@test X[1] == FooBar(7,3)
end
# issue #1287, combinations of try, catch, return
begin
local f, g
function f()
try
return 1
end
end
@test f() == 1
function g()
try
error("badness")
catch
return 2
end
end
@test g() == 2
end
# issue #1442
type S1442{T}
end
begin
local f1442
f1442(::DataType) = 1
f1442{T}(::Type{S1442{T}}) = 2
@test f1442(S1442{Int}) == 2
@test f1442(DataType) == 1
end
# issue #1727
abstract Component
type Transform <: Component
x
y
z
Transform() = new(0, 0, 0)
end
type Body <: Component
vel
curr_force
Body() = new(0, 0)
end
function NewEntity{T<:Component}(components::Type{T}...)
map((c)->c(), components)
end
@test_throws MethodError NewEntity(Transform, Transform, Body, Body)
@test isa(NewEntity(Transform, Transform), Tuple{Transform, Transform})
@test_throws MethodError NewEntity(Transform, Transform, Body, Body)
# issue #1826
let
a = (1,2)
a,b = a
@test a==1 && b==2
end
# issue #1876
let
tst = 1
m1(i) = (tst+=1;i-1)
x = [1:4;]
x[1:end] *= 2
@test x == [2:2:8;]
x[m1(end)] += 3
@test x == [2,4,9,8]
@test tst == 2
# issue #1886
X = [1:4;]
r = Array(UnitRange{Int},1)
r[1] = 2:3
X[r...] *= 2
@test X == [1,4,6,4]
end
# issue #1632
let
f1632{R,S}(::R, ::S) = 1
f1632{T}( ::T, ::T) = 2
@test f1632(1, 2) == 2
@test f1632(:a, 2) == 1
g1632{T}( ::T, ::T) = 2
g1632{R,S}(::R, ::S) = 1
@test g1632(1, 2) == 2
@test g1632(:a, 2) == 1
end
# issue #1628
type I1628{X}
x::X
end
let
# here the potential problem is that the run-time value of static
# parameter X in the I1628 constructor is (DataType,DataType),
# but type inference will track it more accurately as
# (Type{Integer}, Type{Int}).
f1628() = I1628((Integer,Int))
@test isa(f1628(), I1628{Tuple{DataType,DataType}})
end
let
fT{T}(x::T) = T
@test fT(Any) === DataType
@test fT(Int) === DataType
@test fT(Type{Any}) === DataType
@test fT(Type{Int}) === DataType
ff{T}(x::Type{T}) = T
@test ff(Type{Any}) === Type{Any}
@test ff(Type{Int}) === Type{Int}
@test ff(Any) === Any
@test ff(Int) === Int
end
# issue #2098
let
i2098() = begin
c = Any[2.0]
[1:1:c[1];]
end
@test isequal(i2098(), [1.0,2.0])
end
# issue #2161
let
i2161_1() = promote(2,2,2.0,2)
i2161_2() = i2161_1()[1]
@test i2161_2() === 2.0
end
# issue #2169
let
i2169{T}(a::Array{T}) = typemin(T)
@test invoke(i2169,(Array,),Int8[1]) === Int8(-128)
end
# issue #2365
type B2365{T}
v::Union(T, Void)
end
@test B2365{Int}(nothing).v === nothing
@test B2365{Int}(0).v === 0
# issue #2352
begin
local Sum, n
Sum=0.0; for n=1:2:10000
Sum += -1/n + 1/(n+1)
end
@test Sum < -0.69
end
include("test_sourcepath.jl")
# issue #2509
immutable Foo2509; foo::Int; end
@test Foo2509(1) != Foo2509(2)
@test Foo2509(42) == Foo2509(42)
# issue #2517
immutable Foo2517; end
@test repr(Foo2517()) == "Foo2517()"
@test repr(Array(Foo2517,1)) == "[Foo2517()]"
@test Foo2517() === Foo2517()
# issue #1474
type X1474{a,b} end
begin
local Y
Y{A,B}(::Type{X1474{A,B}}) = 1
Y{A}(::Type{X1474{A}}) = 2
Y(::Type{X1474}) = 3
@test Y(X1474) == 3
@test Y(X1474{Int}) == 2
@test Y(X1474{Int,AbstractString}) == 1
end
# issue #2562
type Node2562{T}
value::T