forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.jl
378 lines (339 loc) · 11 KB
/
test.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
# This file is a part of Julia. License is MIT: https://julialang.org/license
# Test @test
@test true
@test 1 == 1
@test 1 != 2
@test strip("\t hi \n") == "hi"
@test strip("\t this should fail \n") != "hi"
# Test @test_broken with fail
@test_broken false
@test_broken 1 == 2
@test_broken 1 != 1
@test_broken strip("\t hi \n") != "hi"
@test_broken strip("\t this should fail \n") == "hi"
# Test @test_broken with errors
@test_broken error()
@test_broken absolute_nonsense
#Test @test_skip
@test_skip error()
@test_skip true
@test_skip false
@test_skip gobbeldygook
a = Array(Float64, 2, 2, 2, 2, 2)
a[1,1,1,1,1] = 10
@test a[1,1,1,1,1] == 10
@test a[1,1,1,1,1] != 2
@test rand() != rand()
# Test printing of Pass results
# Pass - constant
@test contains(sprint(show, @test true), "Expression: true")
# Pass - expression
@test contains(sprint(show, @test 10 == 2*5), "Evaluated: 10 == 10")
@test contains(sprint(show, @test !false), "Expression: !false")
# Pass - exception
@test contains(sprint(show, @test_throws ErrorException error()),
"Thrown: ErrorException")
# Test printing of Fail results
type NoThrowTestSet <: Base.Test.AbstractTestSet
results::Vector
NoThrowTestSet(desc) = new([])
end
Base.Test.record(ts::NoThrowTestSet, t::Base.Test.Result) = (push!(ts.results, t); t)
Base.Test.finish(ts::NoThrowTestSet) = ts.results
fails = @testset NoThrowTestSet begin
# Fail - wrong exception
@test_throws OverflowError error()
# Fail - no exception
@test_throws OverflowError 1 + 1
# Fail - comparison
@test 1+1 == 2+2
# Error - unexpected pass
@test_broken true
end
for i in 1:3
@test isa(fails[i], Base.Test.Fail)
end
@test contains(sprint(show, fails[1]), "Thrown: ErrorException")
@test contains(sprint(show, fails[2]), "No exception thrown")
@test contains(sprint(show, fails[3]), "Evaluated: 2 == 4")
@test contains(sprint(show, fails[4]), "Unexpected Pass")
# Test printing of a TestSetException
tse_str = sprint(show, Test.TestSetException(1,2,3,4))
@test contains(tse_str, "1 passed")
@test contains(tse_str, "2 failed")
@test contains(tse_str, "3 errored")
@test contains(tse_str, "4 broken")
@test Test.finish(Test.FallbackTestSet()) !== nothing
OLD_STDOUT = STDOUT
catch_out = IOStream("")
rd, wr = redirect_stdout()
# Check that the fallback test set throws immediately
@test_throws ErrorException (@test 1 == 2)
@testset "no errors" begin
@test true
@test 1 == 1
end
# Test entirely empty test set
@testset "outer" begin
@testset "inner" begin
end
end
try @testset "outer" begin
@testset "inner1" begin
@test true
@test false
@test 1 == 1
@test 2 == :foo
@test 3 == 3
@testset "d" begin
@test 4 == 4
end
@testset begin
@test :blank != :notblank
end
end
@testset "inner1" begin
@test 1 == 1
@test 2 == 2
@test 3 == :bar
@test 4 == 4
@test_throws ErrorException 1+1
@test_throws ErrorException error()
@test_throws RemoteException error()
@testset "errrrr" begin
@test "not bool"
@test error()
end
error("exceptions in testsets should be caught")
@test 1 == 1 # this test will not be run
end
@testset "loop with desc" begin
@testset "loop1 $T" for T in (Float32, Float64)
@test 1 == T(1)
end
end
@testset "loops without desc" begin
@testset for T in (Float32, Float64)
@test 1 == T(1)
end
@testset for T in (Float32, Float64), S in (Int32,Int64)
@test S(1) == T(1)
end
end
srand(123)
@testset "some loops fail" begin
@testset for i in 1:5
@test i <= rand(1:10)
end
# should add 3 errors and 3 passing tests
@testset for i in 1:6
iseven(i) || error("error outside of test")
@test true # only gets run if the above passed
end
end
end
# These lines shouldn't be called
redirect_stdout(OLD_STDOUT)
error("No exception was thrown!")
catch ex
@test isa(ex, Test.TestSetException)
@test ex.pass == 24
@test ex.fail == 6
@test ex.error == 6
@test ex.broken == 0
end
# Test @test_approx_eq
# TODO
@test isapprox(.1+.1+.1, .3)
@test !isapprox(.1+.1+.1, .4)
@test_throws ErrorException Test.test_approx_eq(ones(10),ones(11),1e-8,"a","b")
@test_throws ErrorException Test.test_approx_eq(ones(10),zeros(10),1e-8,"a","b")
# Test @test_approx_eq_eps
# TODO
ts = @testset "@testset should return the testset" begin
@test true
end
@test typeof(ts) == Base.Test.DefaultTestSet
@test ts.n_passed == 1
tss = @testset "@testset/for should return an array of testsets: $i" for i in 1:3
@test true
end
@test length(tss) == 3
@test typeof(tss[1]) == Base.Test.DefaultTestSet
@test tss[1].n_passed == 1
# Issue #17908 (return)
testset_depth17908 = Test.get_testset_depth()
@testset for i in 1:3
i > 1 && return
@test i == 1
end
# The return aborts the control flow so the expression above doesn't return a
# value. The only thing we can test is whether the testset is properly popped.
# Do not use `@test` since the issue this is testing will swallow the error.
@assert testset_depth17908 == Test.get_testset_depth()
# Issue #17462 and Issue #17908 (break, continue)
testset_depth17462 = Test.get_testset_depth()
counter_17462_pre = 0
counter_17462_post = 0
tss17462 = @testset for x in [1,2,3,4]
counter_17462_pre += 1
if x == 1
@test counter_17462_pre == x
continue
@test false
elseif x == 3
@test counter_17462_pre == x
break
@test false
elseif x == 4
@test false
else
@test counter_17462_pre == x
@test x == 2
@test counter_17462_post == 0
end
counter_17462_post += 1
end
# Do not use `@test` since the issue this is testing will swallow the error.
# Same for the `@assert` in the for loop below
@assert testset_depth17462 == Test.get_testset_depth()
@assert length(tss17462) == 3
for ts17462 in tss17462
@assert isa(ts17462, Base.Test.DefaultTestSet)
end
@test counter_17462_pre == 3
@test counter_17462_post == 1
# Issue #21008
ts = try
@testset "@test_broken and @test_skip should not give an exception" begin
@test_broken false
@test_skip true
@test_skip false
end
catch
nothing # Shouldn't get here
end
@test typeof(ts) == Base.Test.DefaultTestSet
# now we're done running tests with DefaultTestSet so we can go back to STDOUT
redirect_stdout(OLD_STDOUT)
# import the methods needed for defining our own testset type
import Base.Test: record, finish
using Base.Test: get_testset_depth, get_testset
using Base.Test: AbstractTestSet, Result, Pass, Fail, Error
immutable CustomTestSet <: Base.Test.AbstractTestSet
description::AbstractString
foo::Int
results::Vector
# constructor takes a description string and options keyword arguments
CustomTestSet(desc; foo=1) = new(desc, foo, [])
end
record(ts::CustomTestSet, child::AbstractTestSet) = push!(ts.results, child)
record(ts::CustomTestSet, res::Result) = push!(ts.results, res)
function finish(ts::CustomTestSet)
# just record if we're not the top-level parent
if get_testset_depth() > 0
record(get_testset(), ts)
end
ts
end
ts = @testset CustomTestSet "Testing custom testsets" begin
# this testset should inherit the parent testset type
@testset "custom testset inner 1" begin
@test true
@test false
@test error("this error will be reported as an error")
@test_throws ErrorException nothing
@test_throws ErrorException error("this error is a success")
end
# this testset has its own testset type
@testset CustomTestSet foo=4 "custom testset inner 2" begin
# this testset should inherit the type, but not the argument. If a particular
# testset type wants inheritance behavior they should implement it themselves
# using get_testset() in the constructor
@testset "custom testset inner 2 inner 1" begin
@test true
end
# make sure the RHS can use computed values, also tests options without
# specifying the testset type
@testset foo=(1+2) "custom testset inner 2 inner 2" begin
@test true
end
end
end
@test typeof(ts) == CustomTestSet
@test ts.foo == 1
@test ts.description == "Testing custom testsets"
@test typeof(ts.results[1]) == CustomTestSet
@test ts.results[1].description == "custom testset inner 1"
@test ts.results[1].foo == 1
@test typeof(ts.results[1].results[1]) == Pass
@test typeof(ts.results[1].results[2]) == Fail
@test typeof(ts.results[1].results[3]) == Error
@test typeof(ts.results[1].results[4]) == Fail
@test typeof(ts.results[1].results[5]) == Pass
@test typeof(ts.results[2]) == CustomTestSet
@test ts.results[2].description == "custom testset inner 2"
@test ts.results[2].foo == 4
@test typeof(ts.results[2].results[1]) == CustomTestSet
@test ts.results[2].results[1].foo == 1
@test typeof(ts.results[2].results[1].results[1]) == Pass
@test typeof(ts.results[2].results[2]) == CustomTestSet
@test ts.results[2].results[2].foo == 3
# test custom testset types on testset/for
tss = @testset CustomTestSet foo=3 "custom testset $i" for i in 1:6
@testset "inner testset $i-$j" for j in 1:3
@test iseven(i + j)
end
# make sure a testset within a testset/for works
@testset "inner testset $i" begin
@test iseven(i)
end
end
for i in 1:6
@test typeof(tss[i]) == CustomTestSet
@test tss[i].foo == 3
for j in 1:3
@test typeof(tss[i].results[j]) == CustomTestSet
@test tss[i].results[j].foo == 1
@test typeof(tss[i].results[j].results[1]) == (iseven(i+j) ? Pass : Fail)
end
@test typeof(tss[i].results[4]) == CustomTestSet
@test typeof(tss[i].results[4].results[1]) == (iseven(i) ? Pass : Fail)
end
# test @inferred
function uninferrable_function(i)
q = [1, "1"]
return q[i]
end
@test_throws ErrorException @inferred(uninferrable_function(1))
@test @inferred(identity(1)) == 1
# Ensure @inferred only evaluates the arguments once
inferred_test_global = 0
function inferred_test_function()
global inferred_test_global
inferred_test_global += 1
true
end
@test @inferred inferred_test_function()
@test inferred_test_global == 1
# Test that @inferred works with A[i] expressions
@test @inferred((1:3)[2]) == 2
immutable SillyArray <: AbstractArray{Float64,1} end
Base.getindex(a::SillyArray, i) = rand() > 0.5 ? 0 : false
test_result = @test_throws ErrorException @inferred(SillyArray()[2])
@test contains(test_result.value.msg, "Bool")
# Issue #14928
# Make sure abstract error type works.
@test_throws Exception error("")
# Issue #17105
# @inferred with kwargs
function inferrable_kwtest(x; y=1)
2x
end
function uninferrable_kwtest(x; y=1)
2x+y
end
@test @inferred(inferrable_kwtest(1)) == 2
@test @inferred(inferrable_kwtest(1; y=1)) == 2
@test @inferred(uninferrable_kwtest(1)) == 3
@test_throws ErrorException @inferred(uninferrable_kwtest(1; y=2)) == 2