Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Put some guards on types allowed in TaylorTangents #220

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/tangent.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,14 @@ struct ExplicitTangent{P <: Tuple} <: AbstractTangentSpace
partials::P
end


@eval struct TaylorTangent{C <: Tuple} <: AbstractTangentSpace
coeffs::C
TaylorTangent(coeffs) = $(Expr(:new, :(TaylorTangent{typeof(coeffs)}), :coeffs))
function TaylorTangent(coeffs)
bad_tangent_type = Union{DataType, Symbol, String, Nothing} # protect against obvious mistakes
any(c->isa(c, bad_tangent_type), coeffs) && throw(DomainError(coeffs, "Nonvector-space partial type"))
$(Expr(:new, :(TaylorTangent{typeof(coeffs)}), :coeffs))
end
end

"""
Expand Down
10 changes: 9 additions & 1 deletion test/tangent.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module tagent
module tangent
using Diffractor
using Diffractor: AbstractZeroBundle, ZeroBundle, DNEBundle
using Diffractor: TaylorBundle, TaylorTangentIndex
Expand Down Expand Up @@ -54,4 +54,12 @@ end
@test truncate(et, Val(1)) == TaylorTangent((1.0,))
end


@testset "Bad Partial Types" begin
@test_throws DomainError TaylorBundle{1}(1.5, (ZeroTangent,)) # mistakenly passing a type rather than a value
@test_throws DomainError TaylorBundle{1}(1.5, (:a,))
@test_throws DomainError TaylorBundle{1}(1.5, (nothing,))
@test_throws DomainError TaylorBundle{1}(1.5, ("x",))
end

end # module
Loading