Skip to content

Commit 3e79857

Browse files
authored
Prettier tests (#608)
* iterate over x one_buffer * improve tests * jacobsen, sinpi, cospi * triple quotes restore double newline * test_mtspec, atol=eps * remove more DSP. * diric DomainError * Apply suggestions from code review - foreach - move test_quinn - eps -> atol
1 parent f33b262 commit 3e79857

File tree

12 files changed

+142
-180
lines changed

12 files changed

+142
-180
lines changed

src/diric.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ julia> diric(0, 4)
3636
```
3737
"""
3838
function diric::T, n::Integer) where T <: AbstractFloat
39-
n > 0 || throw(ArgumentError("n=$n not positive"))
39+
n > 0 || throw(DomainError(n, "n not positive"))
4040
sign = one(T)
4141
if isodd(n)
4242
Ω = rem2pi(Ω, RoundNearest) # [-π,π)

test/FilterTestHelpers.jl

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ function zpkfilter_eq(f1, f2)
2727
@test map(Float64, f1.k) map(Float64, f2.k)
2828
end
2929

30-
function zpkfilter_eq(f1, f2, eps)
30+
function zpkfilter_eq(f1, f2, atol)
3131
if !isempty(f1.z) || !isempty(f2.z)
32-
@test (map(ComplexF64, sort(f1.z; lt)), map(ComplexF64, sort(f2.z; lt)); atol=eps)
32+
@test (map(ComplexF64, sort(f1.z; lt)), map(ComplexF64, sort(f2.z; lt)); atol)
3333
end
34-
@test (map(ComplexF64, sort(f1.p; lt)), map(ComplexF64, sort(f2.p; lt)); atol=eps)
35-
@test (map(Float64, f1.k), map(Float64, f2.k); atol=eps)
34+
@test (map(ComplexF64, sort(f1.p; lt)), map(ComplexF64, sort(f2.p; lt)); atol)
35+
@test (map(Float64, f1.k), map(Float64, f2.k); atol)
3636
end
3737

3838
loss(x::Real, y::Real) = abs(float(x) - float(y))/eps(float(x))
@@ -58,33 +58,21 @@ function tffilter_accuracy(f1, f2, accurate_f)
5858
accuracy_check(loss(a1, accurate_a), loss(a2, accurate_a), "a")
5959
end
6060

61-
function zpkfilter_accuracy(f1, f2, accurate_f; relerr=1, compare_gain_at=nothing, eps=nothing)
61+
function zpkfilter_accuracy(f1, f2, accurate_f; relerr=1, compare_gain_at=nothing, atol=0.0)
6262
z1, p1 = sort(f1.z; lt), sort(f1.p; lt)
6363
z2, p2 = sort(f2.z; lt), sort(f2.p; lt)
6464
accurate_z, accurate_p = sort(accurate_f.z; lt), sort(accurate_f.p; lt)
6565
if !isempty(z1) || !isempty(z2) || !isempty(accurate_z)
66-
if eps !== nothing
67-
@test (z1, accurate_z; atol=eps)
68-
@test (z2, accurate_z; atol=eps)
69-
else
70-
@test z1 accurate_z
71-
@test z2 accurate_z
72-
end
66+
@test (z1, accurate_z; atol)
67+
@test (z2, accurate_z; atol)
7368
accuracy_check(loss(z1, accurate_z), loss(z2, accurate_z), "z", relerr)
7469
end
75-
if eps !== nothing
76-
@test (p1, accurate_p; atol=eps)
77-
@test (p2, accurate_p; atol=eps)
78-
@test (f1.k, accurate_f.k; atol=eps)
79-
@test (f2.k, accurate_f.k; atol=eps)
80-
else
81-
@test p1 accurate_p
82-
@test p2 accurate_p
83-
@test f1.k accurate_f.k
84-
@test f2.k accurate_f.k
85-
end
70+
@test (p1, accurate_p; atol)
71+
@test (p2, accurate_p; atol)
72+
@test (f1.k, accurate_f.k; atol)
73+
@test (f2.k, accurate_f.k; atol)
8674
accuracy_check(loss(p1, accurate_p), loss(p2, accurate_p), "p", relerr)
87-
if compare_gain_at === nothing
75+
if isnothing(compare_gain_at)
8876
accuracy_check(loss(f1.k, accurate_f.k), loss(f2.k, accurate_f.k), "k", relerr)
8977
else
9078
= compare_gain_at*im

test/diric.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# diric.jl Dirichlet kernel tests
22

33
@testset "diric" begin
4-
@test_throws ArgumentError diric(0, -2)
4+
@test_throws DomainError diric(0, -2)
55
@test @inferred(diric(0, 4)) 1
66
@test @inferred(diric(0 // 1, 5)) == 1
77
@test @inferred(diric(4π, 4)) 1

test/estimation.jl

Lines changed: 32 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -22,77 +22,59 @@ end
2222

2323
@testset "jacobsen" begin
2424
fs = 100
25-
t = range(0, 5, step = 1/fs)
25+
t = range(0, 5; step=1/fs)
26+
function test_complex_jacobsen(fs, fc, f=0, t=t)
27+
sc = cispi.(2 * fc * t .+ f)
28+
f_est_complex = jacobsen(sc, fs)
29+
isapprox(f_est_complex, fc; atol=1e-5)
30+
end
2631
# test at two arbitrary frequencies
27-
fc = -40.3
28-
sc = cis.(2π*fc*t .+ π/1.4)
29-
f_est_complex = jacobsen(sc, fs)
30-
@test isapprox(f_est_complex, fc, atol = 1e-5)
31-
fc = 14.3
32-
sc = cis.(2π*fc*t .+ π/3)
33-
f_est_complex = jacobsen(sc, fs)
34-
@test isapprox(f_est_complex, fc, atol = 1e-5)
32+
@test test_complex_jacobsen(fs, -40.3, 1 / 1.4)
33+
@test test_complex_jacobsen(fs, 14.3, 1 / 3)
3534
# test near fs/2
36-
fc = 49.90019
37-
sc = cis.(2π*fc*t)
38-
f_est_complex = jacobsen(sc, fs)
39-
@test isapprox(f_est_complex, fc, atol = 1e-5)
35+
@test test_complex_jacobsen(fs, 49.90019)
4036
# test near -fs/2
41-
fc = -49.90019
42-
sc = cis.(2π*fc*t)
43-
f_est_complex = jacobsen(sc, fs)
44-
@test isapprox(f_est_complex, fc, atol = 1e-5)
37+
@test test_complex_jacobsen(fs, -49.90019)
4538
# test near +zero
46-
fc = 0.04
47-
sc = cis.(2π*fc*t)
48-
f_est_complex = jacobsen(sc, fs)
49-
@test isapprox(f_est_complex, fc, atol = 1e-5)
39+
@test test_complex_jacobsen(fs, 0.04)
5040
# test near -zero
51-
fc = -0.1
52-
sc = cis.(2π*fc*t)
53-
f_est_complex = jacobsen(sc, fs)
54-
@test isapprox(f_est_complex, fc, atol = 1e-5)
41+
@test test_complex_jacobsen(fs, -0.1)
5542
# tests for real signals: test only around fs/4, where the
5643
# expected error is small.
5744
fr = 28.3
58-
sr = cos.(2π*fr*t .+ π/4.2)
45+
sr = cospi.(2 * fr * t .+ 1 / 4.2)
5946
f_est_real = jacobsen(sr, fs)
60-
@test isapprox(f_est_real, fr, atol = 1e-5)
47+
@test isapprox(f_est_real, fr; atol=1e-5)
6148
fr = 23.45
62-
sr = sin.(2π*fr*t .+ 3π/2.2)
49+
sr = sinpi.(2 * fr * t .+ 3 / 2.2)
6350
f_est_real = jacobsen(sr, fs)
64-
@test isapprox(f_est_real, fr, atol = 1e-5)
51+
@test isapprox(f_est_real, fr; atol=1e-5)
6552
end
6653

6754
@testset "quinn" begin
55+
function test_quinn(f, s, args...)
56+
(f_est_real, maxiter) = quinn(s, args...)
57+
@test maxiter == false
58+
@test isapprox(f_est_real, f; atol=1e-3)
59+
return nothing
60+
end
6861
### real input
6962
fs = 100
70-
t = range(0, 5, step = 1/fs)
63+
t = range(0, 5; step=1/fs)
7164
fr = 28.3
72-
sr = cos.(2π*fr*t .+ π/4.2)
73-
(f_est_real, maxiter) = quinn(sr, 50, fs)
74-
@test maxiter == false
75-
@test isapprox(f_est_real, fr, atol = 1e-3)
65+
sr = cospi.(2 * fr * t .+ 1 / 4.2)
66+
test_quinn(fr, sr, 50, fs)
7667
# use default initial guess
77-
(f_est_real, maxiter) = quinn(sr, fs) # initial guess given by Jacobsen
78-
@test maxiter == false
79-
@test isapprox(f_est_real, fr, atol = 1e-3)
68+
test_quinn(fr, sr, fs) # initial guess given by Jacobsen
8069
# use default fs
81-
(f_est_real, maxiter) = quinn(sr) # fs = 1.0, initial guess given by Jacobsen
82-
@test maxiter == false
83-
@test isapprox(f_est_real, fr/fs, atol = 1e-3)
70+
test_quinn(fr / fs, sr) # fs = 1.0, initial guess given by Jacobsen
71+
8472
### complex input
8573
fc = -40.3
86-
sc = cis.(2π*fc*t .+ π/1.4)
87-
(f_est_real, maxiter) = quinn(sc, -20, fs)
88-
@test maxiter == false
89-
@test isapprox(f_est_real, fc, atol = 1e-3)
74+
sc = cispi.(2 * fc * t .+ 1 / 1.4)
75+
test_quinn(fc, sc, -20, fs)
9076
# use default initial guess
91-
(f_est_real, maxiter) = quinn(sc, fs) # initial guess given by Jacobsen
92-
@test maxiter == false
93-
@test isapprox(f_est_real, fc, atol = 1e-3)
77+
test_quinn(fc, sc, fs) # initial guess given by Jacobsen
9478
# use default fs
95-
(f_est_real, maxiter) = quinn(sc) # fs = 1.0, initial guess by Jacobsen
96-
@test maxiter == false
97-
@test isapprox(f_est_real, fc/fs, atol = 1e-3)
79+
test_quinn(fc / fs, sc) # fs = 1.0, initial guess by Jacobsen
9880
end

test/filt.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,13 @@ end
293293
@testset "filtfilt SOS" begin
294294
x = readdlm(joinpath(dirname(@__FILE__), "data", "spectrogram_x.txt"),'\t')
295295

296-
f = DSP.digitalfilter(DSP.Lowpass(0.2), DSP.Butterworth(4))
296+
f = digitalfilter(Lowpass(0.2), Butterworth(4))
297297
@test filtfilt(convert(SecondOrderSections, f), x) filtfilt(convert(PolynomialRatio, f), x)
298298

299-
f = DSP.digitalfilter(DSP.Highpass(0.1), DSP.Butterworth(6))
299+
f = digitalfilter(Highpass(0.1), Butterworth(6))
300300
@test filtfilt(convert(SecondOrderSections, f), x) filtfilt(convert(PolynomialRatio, f), x)
301301

302-
f = DSP.digitalfilter(DSP.Bandpass(0.1, 0.3), DSP.Butterworth(2))
302+
f = digitalfilter(Bandpass(0.1, 0.3), Butterworth(2))
303303
@test filtfilt(convert(SecondOrderSections, f), x) filtfilt(convert(PolynomialRatio, f), x)
304304
end
305305

0 commit comments

Comments
 (0)