From e385f5fd3b0bbc8107479b3039366c7bb89a0b70 Mon Sep 17 00:00:00 2001 From: Sathvik Bhagavan Date: Fri, 20 Oct 2023 10:02:13 +0530 Subject: [PATCH] test: add tests for show methods --- test/runtests.jl | 3 ++ test/show.jl | 75 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 test/show.jl diff --git a/test/runtests.jl b/test/runtests.jl index eebeda2a..39447ed6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -19,4 +19,7 @@ using DataInterpolations, Test @testset "Regularization Smoothing" begin include("regularization.jl") end + @testset "Show methods" begin + include("show.jl") + end end diff --git a/test/show.jl b/test/show.jl new file mode 100644 index 00000000..bd8ecd17 --- /dev/null +++ b/test/show.jl @@ -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 \ No newline at end of file