Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
aviatesk committed Sep 14, 2020
1 parent cf07c7a commit f842bb1
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 74 deletions.
20 changes: 13 additions & 7 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ end
import Base:
Fix1, Fix2

import Base.Meta:
isexpr

import Core.Compiler:

Expand All @@ -34,15 +37,16 @@ const ERROR_REPORTS_FROM_SUM_OVER_STRING = let
interp.reports
end

function test_sum_over_string(ers::AbstractVector)
function test_sum_over_string(ers)
@test !isempty(ers)
for target in ERROR_REPORTS_FROM_SUM_OVER_STRING
@test any(ers) do er
return er.msg == target.msg && er.sig == target.sig
end
end
end
test_sum_over_string(res::TypeProfiler.VirtualProcessResult) = test_sum_over_string(res.inference_error_reports)
test_sum_over_string(res::TypeProfiler.VirtualProcessResult) =
test_sum_over_string(res.inference_error_reports)

function profile_toplevel!(s,
virtualmod = gen_virtualmod();
Expand All @@ -53,11 +57,13 @@ function profile_toplevel!(s,
return virtual_process!(s, filename, actualmodsym, virtualmod, interp)
end

function profile_file!(filename,
virtualmod = gen_virtualmod();
actualmodsym = :Main,
interp = TPInterpreter())
return virtual_process!(read(filename, String), filename, actualmodsym, virtualmod, interp)
profile_file!(filename, args...; kwargs...) =
profile_toplevel!(read(filename, String), args...; filename, kwargs...)

macro to_s(ex)
# TODO: remove this flattening on https://github.com/aviatesk/TypeProfiler.jl/issues/21
isexpr(ex, :block) && return join(string.(ex.args), '\n')
return string(ex)
end

# %% test body
Expand Down
90 changes: 90 additions & 0 deletions test/test_abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,96 @@ end
@test widenconst(get_virtual_globalvar(interp, vmod, :s)) == String
test_sum_over_string(res)
end

@testset "union assignment" begin
let
vmod = gen_virtualmod()
interp, frame = Core.eval(vmod, :($(profile_call)() do
global globalvar
if rand(Bool)
globalvar = "String"
else
globalvar = :Symbol
end
end))

@test get_virtual_globalvar(interp, vmod, :globalvar) === Union{String,Symbol}
end

let
vmod = gen_virtualmod()
s = """
if rand(Bool)
globalvar = "String"
else
globalvar = :Symbol
end
foo(s::AbstractString) = length(s)
foo(globalvar) # union-split no method matching error should be reported
"""
res, interp = profile_toplevel!(s, vmod)

@test get_virtual_globalvar(interp, vmod, :globalvar) === Union{String,Symbol}
@test length(res.inference_error_reports) === 1
er = first(res.inference_error_reports)
@test er isa NoMethodErrorReport &&
er.unionsplit # should be true
end

# sequential
let
vmod = gen_virtualmod()
s = """
if rand(Bool)
globalvar = "String"
else
globalvar = :Symbol
end
foo(s::AbstractString) = length(s)
foo(globalvar) # union-split no method matching error should be reported
globalvar = 10
foo(globalvar) # no method matching error should be reported
"""
res, interp = profile_toplevel!(s, vmod)

@test get_virtual_globalvar(interp, vmod, :globalvar) Int
@test length(res.inference_error_reports) === 2
let er = first(res.inference_error_reports)
@test er isa NoMethodErrorReport &&
er.unionsplit
end
let er = last(res.inference_error_reports)
@test er isa NoMethodErrorReport &&
!er.unionsplit
end
end
end

@testset "invalidate code cache" begin
let s = @to_s begin
foo(::Integer) = "good call, pal"
bar() = a

a = 1
foo(bar()) # no method error should NOT be reported

a = '1'
foo(bar()) # no method error should be reported

a = 1
foo(bar()) # no method error should NOT be reported
end
vmod = gen_virtualmod()
res, interp = profile_toplevel!(s, vmod)
@test length(res.inference_error_reports) === 1
er = first(res.inference_error_reports)
@test er isa NoMethodErrorReport &&
er.atype <: Tuple{Any,Char}
end
end
end

@testset "report `throw` calls" begin
Expand Down
67 changes: 0 additions & 67 deletions test/test_virtualprocess.jl
Original file line number Diff line number Diff line change
Expand Up @@ -499,73 +499,6 @@ end
@test !isnothing(get_virtual_globalvar(interp, vmod, :constvar))
end

@testset "union assignment" begin
let
vmod = gen_virtualmod()
interp, frame = Core.eval(vmod, :($(profile_call)() do
global globalvar
if rand(Bool)
globalvar = "String"
else
globalvar = :Symbol
end
end))

@test get_virtual_globalvar(interp, vmod, :globalvar) === Union{String,Symbol}
end

let
vmod = gen_virtualmod()
s = """
if rand(Bool)
globalvar = "String"
else
globalvar = :Symbol
end
foo(s::AbstractString) = length(s)
foo(globalvar) # union-split no method matching error should be reported
"""
res, interp = profile_toplevel!(s, vmod)

@test get_virtual_globalvar(interp, vmod, :globalvar) === Union{String,Symbol}
@test length(res.inference_error_reports) === 1
er = first(res.inference_error_reports)
@test er isa NoMethodErrorReport &&
er.unionsplit # should be true
end

# sequential
let
vmod = gen_virtualmod()
s = """
if rand(Bool)
globalvar = "String"
else
globalvar = :Symbol
end
foo(s::AbstractString) = length(s)
foo(globalvar) # union-split no method matching error should be reported
globalvar = 10
foo(globalvar) # no method matching error should be reported
"""
res, interp = profile_toplevel!(s, vmod)

@test get_virtual_globalvar(interp, vmod, :globalvar) Int
@test length(res.inference_error_reports) === 2
let er = first(res.inference_error_reports)
@test er isa NoMethodErrorReport &&
er.unionsplit
end
let er = last(res.inference_error_reports)
@test er isa NoMethodErrorReport &&
!er.unionsplit
end
end
end

@testset "scope" begin
# function
# --------
Expand Down

0 comments on commit f842bb1

Please sign in to comment.