Skip to content

Commit

Permalink
Test some untested codepaths so that coverage doesn't drop
Browse files Browse the repository at this point in the history
Also change some errors to ArgumentErrors
  • Loading branch information
simonster committed May 13, 2015
1 parent 3e4283a commit 3d114e8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
12 changes: 6 additions & 6 deletions src/Filters/coefficients.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ PolynomialRatio{T<:Number}(b::Poly{T}, a::Poly{T}) = PolynomialRatio{T}(b, a)
# convention is lowest power first.
function PolynomialRatio{T<:Number,S<:Number}(b::Union(T,Vector{T}), a::Union(S,Vector{S}))
if findfirst(b) == 0 || findfirst(a) == 0
error("filter must have non-zero numerator and denominator")
throw(ArgumentError("filter must have non-zero numerator and denominator"))
end
PolynomialRatio{promote_type(T,S)}(Poly(b[end:-1:findfirst(b)]), Poly(a[end:-1:findfirst(a)]))
end
Expand Down Expand Up @@ -98,9 +98,9 @@ function Base.convert{T}(::Type{Biquad}, f::PolynomialRatio{T})
elseif xs == 1
Biquad(b[0], zero(T), zero(T), zero(T), zero(T))
elseif xs == 0
error("cannot convert an empty PolynomialRatio to Biquad")
throw(ArgumentError("cannot convert an empty PolynomialRatio to Biquad"))
else
error("cannot convert a filter of length > 3 to Biquad")
throw(ArgumentError("cannot convert a filter of length > 3 to Biquad"))
end
end

Expand Down Expand Up @@ -199,13 +199,13 @@ function Base.convert{Z,P}(::Type{SecondOrderSections}, f::ZeroPoleGain{Z,P})
p = f.p
nz = length(z)
n = length(p)
nz > n && error("ZeroPoleGain must not have more zeros than poles")
nz > n && throw(ArgumentError("ZeroPoleGain must not have more zeros than poles"))

# Split real and complex poles
(complexz, realz, matched) = split_real_complex(z)
matched || error("complex zeros could not be matched to their conjugates")
matched || throw(ArgumentError("complex zeros could not be matched to their conjugates"))
(complexp, realp, matched) = split_real_complex(p)
matched || error("complex poles could not be matched to their conjugates")
matched || throw(ArgumentError("complex poles could not be matched to their conjugates"))

# Sort poles according to distance to unit circle (nearest first)
sort!(complexp, by=x->abs(abs(x) - 1))
Expand Down
2 changes: 1 addition & 1 deletion test/filt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ for n = 1:6
if n <= 2
bq = convert(Biquad, zpk)
else
@test_throws ErrorException convert(Biquad, zpk)
@test_throws ArgumentError convert(Biquad, zpk)
end
sos = convert(SecondOrderSections, zpk)

Expand Down
21 changes: 21 additions & 0 deletions test/filter_conversion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,24 @@ for proto in (Butterworth(3), Chebyshev1(3, 1), Chebyshev2(3, 1))
end
end
end

# Test some otherwise untested code paths
b = Biquad(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 2)
@test b.b0 === 0.0
@test b.b1 === 2*1/3
@test b.b2 === 2*2/3
@test b.a1 === 4/3
@test b.a2 === 5/3
f = convert(PolynomialRatio, Biquad(2.0, 0.0, 0.0, 0.0, 0.0))
@test coefb(f) == [2.0]
@test coefa(f) == [1.0]
@test convert(Biquad, PolynomialRatio([4.0], [2.0])) == Biquad(2.0, 0.0, 0.0, 0.0, 0.0)
@test Biquad(2.0, 0.0, 0.0, 0.0, 0.0)*2 == Biquad(4.0, 0.0, 0.0, 0.0, 0.0)

@test_throws ArgumentError PolynomialRatio(Float64[], Float64[])
f = PolynomialRatio(Float64[1.0], Float64[1.0])
empty!(f.b.a)
empty!(f.a.a)
@test_throws ArgumentError convert(Biquad, f)
@test_throws ArgumentError convert(SOSFilter, ZPKFilter([0.5 + 0.5im, 0.5 + 0.5im], [0.5 + 0.5im, 0.5 - 0.5im], 1))
@test_throws ArgumentError convert(SOSFilter, ZPKFilter([0.5 + 0.5im, 0.5 - 0.5im], [0.5 + 0.5im, 0.5 + 0.5im], 1))

0 comments on commit 3d114e8

Please sign in to comment.