Skip to content

Commit 4a2298d

Browse files
committed
Merge pull request #13090 from JuliaLang/ird/testref
Tweaks to tests (mmap robustness, don't use at-test_throw return type)
2 parents b6ba692 + a3a2fd0 commit 4a2298d

File tree

3 files changed

+69
-45
lines changed

3 files changed

+69
-45
lines changed

test/core.jl

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3109,15 +3109,25 @@ Base.convert(::Type{Foo11874},x::Int) = float(x)
31093109

31103110
# issue #9233
31113111
let
3112-
err = @test_throws TypeError NTuple{Int, 1}
3113-
@test err.func == :NTuple
3114-
@test err.expected == Int
3115-
@test err.got == Int
3116-
3117-
err = @test_throws TypeError NTuple{0x1, Int}
3118-
@test err.func == :NTuple
3119-
@test err.expected == Int
3120-
@test err.got == 0x1
3112+
try
3113+
NTuple{Int, 1}
3114+
@test false
3115+
catch err
3116+
@test isa(err, TypeError)
3117+
@test err.func == :NTuple
3118+
@test err.expected == Int
3119+
@test err.got == Int
3120+
end
3121+
3122+
try
3123+
NTuple{0x1, Int}
3124+
@test false
3125+
catch err
3126+
@test isa(err, TypeError)
3127+
@test err.func == :NTuple
3128+
@test err.expected == Int
3129+
@test err.got == 0x1
3130+
end
31213131
end
31223132

31233133
# 11996

test/misc.jl

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ let res = assert(true)
3434
@test res === nothing
3535
end
3636
let
37-
ex = @test_throws AssertionError begin
37+
try
3838
assert(false)
3939
error("unexpected")
40+
catch ex
41+
@test isa(ex, AssertionError)
42+
@test isempty(ex.msg)
4043
end
41-
@test isempty(ex.msg)
4244
end
4345

4446
# test @assert macro
@@ -48,44 +50,54 @@ end
4850
@test_throws AssertionError (@assert false "this is a test" "another test")
4951
@test_throws AssertionError (@assert false :a)
5052
let
51-
ex = @test_throws AssertionError begin
53+
try
5254
@assert 1 == 2
5355
error("unexpected")
56+
catch ex
57+
@test isa(ex, AssertionError)
58+
@test contains(ex.msg, "1 == 2")
5459
end
55-
@test contains(ex.msg, "1 == 2")
5660
end
5761
# test @assert message
5862
let
59-
ex = @test_throws AssertionError begin
63+
try
6064
@assert 1 == 2 "this is a test"
6165
error("unexpected")
66+
catch ex
67+
@test isa(ex, AssertionError)
68+
@test ex.msg == "this is a test"
6269
end
63-
@test ex.msg == "this is a test"
6470
end
6571
# @assert only uses the first message string
6672
let
67-
ex = @test_throws AssertionError begin
73+
try
6874
@assert 1 == 2 "this is a test" "this is another test"
6975
error("unexpected")
76+
catch ex
77+
@test isa(ex, AssertionError)
78+
@test ex.msg == "this is a test"
7079
end
71-
@test ex.msg == "this is a test"
7280
end
7381
# @assert calls string() on second argument
7482
let
75-
ex = @test_throws AssertionError begin
83+
try
7684
@assert 1 == 2 :random_object
7785
error("unexpected")
86+
catch ex
87+
@test isa(ex, AssertionError)
88+
@test !contains(ex.msg, "1 == 2")
89+
@test contains(ex.msg, "random_object")
7890
end
79-
@test !contains(ex.msg, "1 == 2")
80-
@test contains(ex.msg, "random_object")
8191
end
8292
# if the second argument is an expression, c
8393
let deepthought(x, y) = 42
84-
ex = @test_throws AssertionError begin
94+
try
8595
@assert 1 == 2 string("the answer to the ultimate question: ",
8696
deepthought(6, 9))
97+
catch ex
98+
@test isa(ex, AssertionError)
99+
@test ex.msg == "the answer to the ultimate question: 42"
87100
end
88-
@test ex.msg == "the answer to the ultimate question: 42"
89101
end
90102

91103
let # test the process title functions, issue #9957

test/mmap.jl

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,20 @@ s = open(file, "w") do f
66
end
77
t = "Hello World".data
88
@test Mmap.mmap(file, Array{UInt8,3}, (11,1,1)) == reshape(t,(11,1,1))
9-
gc()
9+
gc(); gc()
1010
@test Mmap.mmap(file, Array{UInt8,3}, (1,11,1)) == reshape(t,(1,11,1))
11-
gc()
11+
gc(); gc()
1212
@test Mmap.mmap(file, Array{UInt8,3}, (1,1,11)) == reshape(t,(1,1,11))
13-
gc()
13+
gc(); gc()
1414
@test_throws ArgumentError Mmap.mmap(file, Array{UInt8,3}, (11,0,1)) # 0-dimension results in len=0
1515
@test Mmap.mmap(file, Vector{UInt8}, (11,)) == t
16-
gc()
16+
gc(); gc()
1717
@test Mmap.mmap(file, Array{UInt8,2}, (1,11)) == t'
18-
gc()
18+
gc(); gc()
1919
@test_throws ArgumentError Mmap.mmap(file, Array{UInt8,2}, (0,12))
2020
m = Mmap.mmap(file, Array{UInt8,3}, (1,2,1))
2121
@test m == reshape("He".data,(1,2,1))
22-
m=nothing; gc()
22+
finalize(m); m=nothing; gc()
2323

2424
# constructors
2525
@test length(Mmap.mmap(file)) == 12
@@ -45,15 +45,15 @@ s = open(file)
4545
@test length(Mmap.mmap(s, Vector{Int8}, 12, 0; shared=false)) == 12
4646
close(s)
4747
@test_throws ErrorException Mmap.mmap(file, Vector{Ref}) # must be bit-type
48-
gc()
48+
gc(); gc()
4949

5050
s = open(f->f,file,"w")
5151
@test_throws ArgumentError Mmap.mmap(file) # requested len=0 on empty file
5252
@test_throws ArgumentError Mmap.mmap(file,Vector{UInt8},0)
5353
m = Mmap.mmap(file,Vector{UInt8},12)
5454
m[:] = "Hello World\n".data
5555
Mmap.sync!(m)
56-
m=nothing; gc()
56+
finalize(m); m=nothing; gc()
5757
@test open(readall,file) == "Hello World\n"
5858

5959
s = open(file, "r")
@@ -70,30 +70,30 @@ close(s)
7070
for i = 0x01:0x0c
7171
@test length(Mmap.mmap(file, Vector{UInt8}, i)) == Int(i)
7272
end
73-
gc()
73+
gc(); gc()
7474

7575
sz = filesize(file)
7676
m = Mmap.mmap(file, Vector{UInt8}, sz+1)
7777
@test length(m) == sz+1 # test growing
7878
@test m[end] == 0x00
79-
m=nothing; gc()
79+
finalize(m); m=nothing; gc()
8080
sz = filesize(file)
8181
m = Mmap.mmap(file, Vector{UInt8}, 1, sz)
8282
@test length(m) == 1
8383
@test m[1] == 0x00
84-
m=nothing; gc()
84+
finalize(m); m=nothing; gc()
8585
sz = filesize(file)
8686
# test where offset is actually > than size of file; file is grown with zeroed bytes
8787
m = Mmap.mmap(file, Vector{UInt8}, 1, sz+1)
8888
@test length(m) == 1
8989
@test m[1] == 0x00
90-
m=nothing; gc()
90+
finalize(m); m=nothing; gc()
9191

9292
# Uncomment out once #11351 is resolved
9393
# s = open(file, "r")
9494
# m = Mmap.mmap(s)
9595
# @test_throws ReadOnlyMemoryError m[5] = Vector{UInt8}('x') # tries to setindex! on read-only array
96-
# m=nothing; gc()
96+
# finalize(m); m=nothing; gc()
9797

9898
s = open(file, "w") do f
9999
write(f, "Hello World\n")
@@ -102,7 +102,7 @@ end
102102
s = open(file, "r")
103103
m = Mmap.mmap(s)
104104
close(s)
105-
m=nothing; gc()
105+
finalize(m); m=nothing; gc()
106106
m = Mmap.mmap(file)
107107
s = open(file, "r+")
108108
c = Mmap.mmap(s)
@@ -112,6 +112,7 @@ Mmap.sync!(c)
112112
close(s)
113113
@test m[1] == UInt8('J')
114114
@test d[1] == UInt8('J')
115+
finalize(m); finalize(c); finalize(d)
115116
m=nothing; c=nothing; d=nothing; gc()
116117

117118
s = open(file, "w") do f
@@ -122,10 +123,10 @@ s = open(file, "r")
122123
@test isreadonly(s) == true
123124
c = Mmap.mmap(s, Vector{UInt8}, (11,))
124125
@test c == "Hello World".data
125-
c=nothing; gc()
126+
finalize(c); c=nothing; gc()
126127
c = Mmap.mmap(s, Vector{UInt8}, (UInt16(11),))
127128
@test c == "Hello World".data
128-
c=nothing; gc()
129+
finalize(c); c=nothing; gc()
129130
@test_throws ArgumentError Mmap.mmap(s, Vector{UInt8}, (Int16(-11),))
130131
@test_throws ArgumentError Mmap.mmap(s, Vector{UInt8}, (typemax(UInt),))
131132
close(s)
@@ -139,22 +140,22 @@ s = open(file, "r")
139140
str = readline(s)
140141
close(s)
141142
@test startswith(str, "Hellx World")
142-
c=nothing; gc()
143+
finalize(c); c=nothing; gc()
143144

144145
c = Mmap.mmap(file)
145146
@test c == "Hellx World\n".data
146-
c=nothing; gc()
147+
finalize(c); c=nothing; gc()
147148
c = Mmap.mmap(file, Vector{UInt8}, 3)
148149
@test c == "Hel".data
149-
c=nothing; gc()
150+
finalize(c); c=nothing; gc()
150151
s = open(file, "r")
151152
c = Mmap.mmap(s, Vector{UInt8}, 6)
152153
@test c == "Hellx ".data
153154
close(s)
154-
c=nothing; gc()
155+
finalize(c); c=nothing; gc()
155156
c = Mmap.mmap(file, Vector{UInt8}, 5, 6)
156157
@test c == "World".data
157-
c=nothing; gc()
158+
finalize(c); c=nothing; gc()
158159

159160
s = open(file, "w")
160161
write(s, "Hello World\n")
@@ -167,7 +168,7 @@ for i = 1:12
167168
@test m[i] == t.data[i]
168169
end
169170
@test_throws BoundsError m[13]
170-
m=nothing; gc()
171+
finalize(m); m=nothing; gc()
171172

172173
m = Mmap.mmap(file,Vector{UInt8},6)
173174
@test m[1] == "H".data[1]
@@ -177,7 +178,7 @@ m = Mmap.mmap(file,Vector{UInt8},6)
177178
@test m[5] == "o".data[1]
178179
@test m[6] == " ".data[1]
179180
@test_throws BoundsError m[7]
180-
m=nothing; gc()
181+
finalize(m); m=nothing; gc()
181182

182183
m = Mmap.mmap(file,Vector{UInt8},2,6)
183184
@test m[1] == "W".data[1]
@@ -283,3 +284,4 @@ n = similar(m, (2,2))
283284
n = similar(m, 12)
284285
@test length(n) == 12
285286
@test size(n) == (12,)
287+
finalize(m); m = nothing; gc()

0 commit comments

Comments
 (0)