From f842bb17fedba9277ba3110572cc3ba42e78d50c Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki Date: Mon, 14 Sep 2020 20:21:59 +0900 Subject: [PATCH] add test --- test/runtests.jl | 20 ++++--- test/test_abstractinterpretation.jl | 90 +++++++++++++++++++++++++++++ test/test_virtualprocess.jl | 67 --------------------- 3 files changed, 103 insertions(+), 74 deletions(-) diff --git a/test/runtests.jl b/test/runtests.jl index 4c9e8212c..7c7ae1536 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -20,6 +20,9 @@ end import Base: Fix1, Fix2 +import Base.Meta: + isexpr + import Core.Compiler: ⊑ @@ -34,7 +37,7 @@ 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 @@ -42,7 +45,8 @@ function test_sum_over_string(ers::AbstractVector) 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(); @@ -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 diff --git a/test/test_abstractinterpretation.jl b/test/test_abstractinterpretation.jl index f69d0ea1f..4f291ba44 100644 --- a/test/test_abstractinterpretation.jl +++ b/test/test_abstractinterpretation.jl @@ -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 diff --git a/test/test_virtualprocess.jl b/test/test_virtualprocess.jl index dc015ec29..fe8c07bff 100644 --- a/test/test_virtualprocess.jl +++ b/test/test_virtualprocess.jl @@ -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 # --------