|
| 1 | +using DataInterpolations |
| 2 | +using AllocCheck: @check_allocs |
| 3 | +using StaticArrays: SVector |
| 4 | + |
| 5 | +@check_allocs(test_allocs(itp, x) = itp(x)) # Reuse function definition to save on compilation time |
| 6 | + |
| 7 | +@testset "Allocation-free interpolation tests" begin |
| 8 | + @testset "LinearInterpolation" begin |
| 9 | + t = 1.0collect(1:10) |
| 10 | + x = 1:10 |
| 11 | + y = 2:4 |
| 12 | + u_ = x' .* y |
| 13 | + u = [u_[:, i] for i in 1:size(u_, 2)] |
| 14 | + u_s = [convert(SVector{length(y), eltype(u_)}, i) for i in u] |
| 15 | + A_s = LinearInterpolation(u_s, t; extrapolation = ExtrapolationType.Extension) |
| 16 | + @test_nowarn test_allocs(A_s, 0) |
| 17 | + end |
| 18 | + |
| 19 | + @testset "QuadraticInterpolation" begin |
| 20 | + t = [1.0, 2.0, 3.0, 4.0] |
| 21 | + u_ = [1.0, 4.0, 9.0, 16.0]' .* ones(5) |
| 22 | + u = [u_[:, i] for i in 1:size(u_, 2)] |
| 23 | + u_s = [convert(SVector{length(u[1])}, i) for i in u] |
| 24 | + A_s = QuadraticInterpolation(u_s, t; extrapolation = ExtrapolationType.Extension) |
| 25 | + @test_nowarn test_allocs(A_s, 0) |
| 26 | + end |
| 27 | + |
| 28 | + @testset "ConstantInterpolation" begin |
| 29 | + t = [1.0, 2.0, 3.0, 4.0] |
| 30 | + u = [[1.0, 2.0], [0.0, 1.0], [1.0, 2.0], [0.0, 1.0]] |
| 31 | + u_s = [convert(SVector{length(first(u))}, i) for i in u] |
| 32 | + A_s = ConstantInterpolation(u_s, t; extrapolation = ExtrapolationType.Extension) |
| 33 | + @test_nowarn test_allocs(A_s, 0) |
| 34 | + end |
| 35 | + |
| 36 | + @testset "SmoothedConstantInterpolation" begin |
| 37 | + u = [0.0, 2.0, 1.0, 3.0] |
| 38 | + t = [1.2, 2.5, 5.7, 8.7] |
| 39 | + d_max = 0.5 |
| 40 | + u_ = u' .* ones(5) |
| 41 | + uv = [u_[:, i] for i in 1:size(u_, 2)] |
| 42 | + u_s = [convert(SVector{length(uv[1])}, i) for i in uv] |
| 43 | + A_s = SmoothedConstantInterpolation(u_s, t; d_max) |
| 44 | + @test_nowarn test_allocs(A_s, 1.9) |
| 45 | + end |
| 46 | + |
| 47 | + @testset "QuadraticSpline" begin |
| 48 | + t = [-1.0, 0.0, 1.0] |
| 49 | + u_ = [0.0, 1.0, 3.0]' .* ones(4) |
| 50 | + u = [u_[:, i] for i in 1:size(u_, 2)] |
| 51 | + u_s = [convert(SVector{length(u[1])}, i) for i in u] |
| 52 | + A_s = QuadraticSpline(u_s, t; extrapolation = ExtrapolationType.Extension) |
| 53 | + @test_nowarn test_allocs(A_s, 0.7) |
| 54 | + end |
| 55 | + |
| 56 | + @testset "CubicSpline" begin |
| 57 | + t = [-1.0, 0.0, 1.0] |
| 58 | + u_ = [0.0, 1.0, 3.0]' .* ones(4) |
| 59 | + u = [u_[:, i] for i in 1:size(u_, 2)] |
| 60 | + u_s = [convert(SVector{length(first(u))}, i) for i in u] |
| 61 | + A_s = CubicSpline(u_s, t; extrapolation = ExtrapolationType.Extension) |
| 62 | + @test_nowarn test_allocs(A_s, 0) |
| 63 | + end |
| 64 | + |
| 65 | + @testset "CubicHermiteSpline" begin |
| 66 | + du = [-0.047, -0.058, 0.054, 0.012, -0.068, 0.0] |
| 67 | + u = [14.7, 11.51, 10.41, 14.95, 12.24, 11.22] |
| 68 | + t = [0.0, 62.25, 109.66, 162.66, 205.8, 252.3] |
| 69 | + u2 = [[u[i], u[i] + 1] for i in eachindex(u)] |
| 70 | + du2 = [[du[i], du[i]] for i in eachindex(du)] |
| 71 | + u2_s = [convert(SVector{length(u2[1])}, i) for i in u2] |
| 72 | + du2_s = [convert(SVector{length(du2[1])}, i) for i in du2] |
| 73 | + A2_s = CubicHermiteSpline( |
| 74 | + du2_s, u2_s, t; extrapolation = ExtrapolationType.Extension |
| 75 | + ) |
| 76 | + @test_nowarn test_allocs(A2_s, 0.7) |
| 77 | + end |
| 78 | + |
| 79 | + @testset "QuinticHermiteSpline" begin |
| 80 | + ddu = [0.0, -0.00033, 0.0051, -0.0067, 0.0029, 0.0] |
| 81 | + du = [-0.047, -0.058, 0.054, 0.012, -0.068, 0.0] |
| 82 | + u = [14.7, 11.51, 10.41, 14.95, 12.24, 11.22] |
| 83 | + t = [0.0, 62.25, 109.66, 162.66, 205.8, 252.3] |
| 84 | + u2 = [[u[i], u[i] + 1] for i in eachindex(u)] |
| 85 | + du2 = [[du[i], du[i]] for i in eachindex(du)] |
| 86 | + ddu2 = [[ddu[i], ddu[i]] for i in eachindex(ddu)] |
| 87 | + u2_s = [convert(SVector{length(u2[1])}, i) for i in u2] |
| 88 | + du2_s = [convert(SVector{length(du2[1])}, i) for i in du2] |
| 89 | + ddu2_s = [convert(SVector{length(du2[1])}, i) for i in ddu2] |
| 90 | + A2_s = QuinticHermiteSpline( |
| 91 | + ddu2_s, du2_s, u2_s, t; extrapolation = ExtrapolationType.Extension |
| 92 | + ) |
| 93 | + @test_nowarn test_allocs(A2_s, 0.7) |
| 94 | + end |
| 95 | +end |
0 commit comments