Skip to content

Commit

Permalink
test: add tests for show methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sathvikbhagavan committed Oct 20, 2023
1 parent 8b955ae commit e385f5f
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ using DataInterpolations, Test
@testset "Regularization Smoothing" begin
include("regularization.jl")
end
@testset "Show methods" begin
include("show.jl")
end
end
75 changes: 75 additions & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Optim, StableRNGs
using RegularizationTools

t = [1.0, 2.0, 3.0, 4.0, 5.0]
x = [1.0, 2.0, 3.0, 4.0, 5.0]

@testset "Generic Cases" begin
function test_show_line(A)
@testset "$(nameof(typeof(A)))" begin
@test startswith(sprint(io -> show(io, MIME"text/plain"(), A)), "$(nameof(typeof(A))) with $(length(A.t)) points\n")
end
end
methods = [
LinearInterpolation(x, t),
AkimaInterpolation(x, t),
QuadraticSpline(x, t),
CubicSpline(x, t),
]
test_show_line.(methods)
end

@testset "Specific Cases" begin
@testset "QuadraticInterpolation" begin
A = QuadraticInterpolation(x, t)
@test startswith(sprint(io -> show(io, MIME"text/plain"(), A)), "QuadraticInterpolation with 5 points, Forward mode\n")
end
@testset "LagrangeInterpolation" begin
A = LagrangeInterpolation(x, t)
@test startswith(sprint(io -> show(io, MIME"text/plain"(), A)), "LagrangeInterpolation with 5 points, with order 4\n")
end
@testset "ConstantInterpolation" begin
A = ConstantInterpolation(x, t)
@test startswith(sprint(io -> show(io, MIME"text/plain"(), A)), "ConstantInterpolation with 5 points, in left direction\n")
end
@testset "BSplineInterpolation" begin
A = BSplineInterpolation(x, t, 3, :Uniform, :Uniform)
@test startswith(sprint(io -> show(io, MIME"text/plain"(), A)), "BSplineInterpolation with 5 points, with degree 3\n")
end
@testset "BSplineApprox" begin
A = BSplineApprox(x, t, 2, 4, :Uniform, :Uniform)
@test startswith(sprint(io -> show(io, MIME"text/plain"(), A)), "BSplineApprox with 5 points, with degree 2, number of control points 4\n")
end
end

@testset "CurveFit" begin
rng = StableRNG(12345)
model(x, p) = @. p[1] / (1 + exp(x - p[2]))
t = range(-10, stop = 10, length = 40)
u = model(t, [1.0, 2.0]) + 0.01 * randn(rng, length(t))
p0 = [0.5, 0.5]
A = Curvefit(u, t, model, p0, LBFGS())
@test startswith(sprint(io -> show(io, MIME"text/plain"(), A)), "Curvefit with 40 points, using LBFGS\n")
end

@testset "RegularizationSmooth" begin
npts = 50
xmin = 0.0
xspan = 3 / 2 * π
x = collect(range(xmin, xmin + xspan, length = npts))
rng = StableRNG(655)
x = x + xspan / npts * (rand(rng, npts) .- 0.5)
# select a subset randomly
idx = unique(rand(rng, collect(eachindex(x)), 20))
t = x[unique(idx)]
npts = length(t)
ut = sin.(t)
stdev = 1e-1 * maximum(ut)
u = ut + stdev * randn(rng, npts)
# data must be ordered if t̂ is not provided
idx = sortperm(t)
tₒ = t[idx]
uₒ = u[idx]
A = RegularizationSmooth(uₒ, tₒ; alg = :fixed)
@test startswith(sprint(io -> show(io, MIME"text/plain"(), A)), "RegularizationSmooth with 15 points, with regularization coefficient 1.0\n")
end

0 comments on commit e385f5f

Please sign in to comment.