gpt4 book ai didi

testing - 如何测试 `if @generated` 的两边?

转载 作者:行者123 更新时间:2023-11-28 20:00:47 25 4
gpt4 key购买 nike

考虑我可能有一些我想测试的生成函数,如下所示。正在关注recommendation of the manual我让它们可选地生成

# This code is taken directly from Base. 
# https://github.com/JuliaLang/julia/blob/592748adb25301a45bd6edef3ac0a93eed069852/base/namedtuple.jl#L220-L231
# So importing some of the helpers
using Base: merge_names, merge_types, sym_in

function my_merge_fail1(a::NamedTuple{an}, b::NamedTuple{bn}) where {an, bn}
if @generated
names = merge_names(an, bn)
types = merge_types(names, a, b)
vals = Any[ :(getfield($(sym_in(n, bn) ? :b : :a), $(QuoteNode(n)))) for n in names ]
:(error("typo1"); NamedTuple{$names,$types}(($(vals...),)) )
else
names = merge_names(an, bn)
types = merge_types(names, typeof(a), typeof(b))
NamedTuple{names,types}(map(n->getfield(sym_in(n, bn) ? b : a, n), names))
end
end

function my_merge_fail2(a::NamedTuple{an}, b::NamedTuple{bn}) where {an, bn}
if @generated
names = merge_names(an, bn)
types = merge_types(names, a, b)
vals = Any[ :(getfield($(sym_in(n, bn) ? :b : :a), $(QuoteNode(n)))) for n in names ]
:(NamedTuple{$names,$types}(($(vals...),)) )
else
error("typo2")
names = merge_names(an, bn)
types = merge_types(names, typeof(a), typeof(b))
NamedTuple{names,types}(map(n->getfield(sym_in(n, bn) ? b : a, n), names))
end
end

现在,事实证明我在每一个中都犯了一个“错字”。我错误地将 error(...) 包含在它们中。在 my_merge_fail1 中,我在 @generated 分支中犯了错误,并在非生成分支中的 my_merge_fail2 中。

我的测试似乎只在一个分支中捕获它:

julia> using Test

julia> @test my_merge_fail1((a=1,), (b=2,)) == (a=1, b=2)
Error During Test at REPL[12]:1
Test threw exception
Expression: my_merge_fail1((a = 1,), (b = 2,)) == (a = 1, b = 2)
typo1
Stacktrace:
[1] error(::String) at ./error.jl:33
[2] macro expansion at ./REPL[6]:2 [inlined]
[3] my_merge_fail1(::NamedTuple{(:a,),Tuple{Int64}}, ::NamedTuple{(:b,),Tuple{Int64}}) at ./REPL[6]:2
[4] top-level scope at REPL[12]:1
[5] eval(::Module, ::Any) at ./boot.jl:331
[6] eval_user_input(::Any, ::REPL.REPLBackend) at /usr/local/src/julia/julia-master/usr/share/julia/stdlib/v1.4/REPL/src/REPL.jl:86
[7] macro expansion at /usr/local/src/julia/julia-master/usr/share/julia/stdlib/v1.4/REPL/src/REPL.jl:118 [inlined]
[8] (::REPL.var"#26#27"{REPL.REPLBackend})() at ./task.jl:333

ERROR: There was an error during testing

julia> @test my_merge_fail2((a=1,), (b=2,)) == (a=1, b=2)
Test Passed

我怎样才能改进我的测试?

最佳答案

可以通过从方法中提取降低的代码来实现这一点,您可以在其中指定是获取此方法的生成版本还是非生成版本的代码。此方法使用 evalBase.invokelatest,因此会产生一些开销,但对于测试来说应该没问题。

function _invoke(f, x...; generated)
sig = Core.Typeof(x)
ci = code_lowered(f, sig; generated)[1]
Meta.partially_inline!(
ci.code, [], Tuple{typeof(f),sig.parameters...}, Any[sig.parameters...],
0, 0, :propagate,
)
g = @eval @generated function $(gensym(:g))($([gensym() for _ in x]...))
return $(QuoteNode(ci))
end
Base.invokelatest(g, x...)
end

然后您可以选择调用生成的或非生成的方法版本,如下所示:

julia> f(x, y) = @generated() ? :x : y
f (generic function with 1 method)

julia> _invoke(f, 1, 2; generated=true)
1

julia> _invoke(f, 1, 2; generated=false)
2

关于testing - 如何测试 `if @generated` 的两边?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58612962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com