From 23b2a3d4267bb689a98b7c9c60eb566dc749fcac Mon Sep 17 00:00:00 2001 From: daviehh <25255906+daviehh@users.noreply.github.com> Date: Tue, 23 Sep 2025 11:49:11 -0400 Subject: [PATCH 1/3] length check before return; also explicit error instead of `@assert` --- src/interpolation_utils.jl | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/interpolation_utils.jl b/src/interpolation_utils.jl index f04aa5b6..7beea72c 100644 --- a/src/interpolation_utils.jl +++ b/src/interpolation_utils.jl @@ -93,6 +93,8 @@ end # helper function for data manipulation function munge_data(u::AbstractVector, t::AbstractVector; check_sorted = t, sorted_arg_name = ("second", "t")) + length(t) == length(u) || error("`u`, `t` length mismatch: length(t) ≠ length(u)") + Tu = nonmissingtype(eltype(u)) Tt = nonmissingtype(eltype(t)) @@ -109,8 +111,6 @@ function munge_data(u::AbstractVector, t::AbstractVector; return u, t end - @assert length(t) == length(u) - non_missing_mask = map((ui, ti) -> !ismissing(ui) && !ismissing(ti), u, t) u = convert(AbstractVector{Tu}, u[non_missing_mask]) t = convert(AbstractVector{Tt}, t[non_missing_mask]) @@ -119,13 +119,14 @@ function munge_data(u::AbstractVector, t::AbstractVector; end function munge_data(U::AbstractMatrix, t::AbstractVector) + length(t) == size(U, 2) || error("`u`, `t` length mismatch: length(t) ≠ size(U, 2)") + TU = nonmissingtype(eltype(U)) Tt = nonmissingtype(eltype(t)) if TU === eltype(U) && Tt === eltype(t) return U, t end - @assert length(t) == size(U, 2) non_missing_mask = map( (uis, ti) -> !any(ismissing, uis) && !ismissing(ti), eachcol(U), t) U = convert(AbstractMatrix{TU}, U[:, non_missing_mask]) @@ -135,13 +136,14 @@ function munge_data(U::AbstractMatrix, t::AbstractVector) end function munge_data(U::AbstractArray{T, N}, t) where {T, N} + length(t) == size(U, N) || error("`u`, `t` length mismatch: length(t) ≠ size(U, N)") + TU = nonmissingtype(eltype(U)) Tt = nonmissingtype(eltype(t)) if TU === eltype(U) && Tt === eltype(t) return U, t end - @assert length(t) == size(U, N) non_missing_mask = map( (uis, ti) -> !any(ismissing, uis) && !ismissing(ti), eachslice(U; dims = N), t) U = convert(AbstractArray{TU, N}, copy(selectdim(U, N, non_missing_mask))) From c18ba703cc2dbe29a730a2ed3917d94bb19dae05 Mon Sep 17 00:00:00 2001 From: daviehh <25255906+daviehh@users.noreply.github.com> Date: Tue, 23 Sep 2025 12:00:33 -0400 Subject: [PATCH 2/3] ArgumentError, and test --- src/interpolation_utils.jl | 9 ++++++--- test/interpolation_tests.jl | 3 ++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/interpolation_utils.jl b/src/interpolation_utils.jl index 7beea72c..54cc60ac 100644 --- a/src/interpolation_utils.jl +++ b/src/interpolation_utils.jl @@ -93,7 +93,8 @@ end # helper function for data manipulation function munge_data(u::AbstractVector, t::AbstractVector; check_sorted = t, sorted_arg_name = ("second", "t")) - length(t) == length(u) || error("`u`, `t` length mismatch: length(t) ≠ length(u)") + length(t) == length(u) || + throw(ArgumentError("`u`, `t` length mismatch: length(t) ≠ length(u)")) Tu = nonmissingtype(eltype(u)) Tt = nonmissingtype(eltype(t)) @@ -119,7 +120,8 @@ function munge_data(u::AbstractVector, t::AbstractVector; end function munge_data(U::AbstractMatrix, t::AbstractVector) - length(t) == size(U, 2) || error("`u`, `t` length mismatch: length(t) ≠ size(U, 2)") + length(t) == size(U, 2) || + throw(ArgumentError("`u`, `t` length mismatch: length(t) ≠ size(U, 2)")) TU = nonmissingtype(eltype(U)) Tt = nonmissingtype(eltype(t)) @@ -136,7 +138,8 @@ function munge_data(U::AbstractMatrix, t::AbstractVector) end function munge_data(U::AbstractArray{T, N}, t) where {T, N} - length(t) == size(U, N) || error("`u`, `t` length mismatch: length(t) ≠ size(U, N)") + length(t) == size(U, N) || + throw(ArgumentError("`u`, `t` length mismatch: length(t) ≠ size(U, N)")) TU = nonmissingtype(eltype(U)) Tt = nonmissingtype(eltype(t)) diff --git a/test/interpolation_tests.jl b/test/interpolation_tests.jl index 33abdf79..ce6272be 100644 --- a/test/interpolation_tests.jl +++ b/test/interpolation_tests.jl @@ -1263,7 +1263,8 @@ end @testset "user error" begin @test_throws ArgumentError LinearInterpolation(rand(10), rand(10)) - @test_throws ArgumentError LinearInterpolation(0:10, rand(10)) + @test_throws ArgumentError LinearInterpolation(1:10, rand(10)) + @test_throws ArgumentError LinearInterpolation(1:3, 1:5) end @testset "Symbolic interpolation" begin From c1bbe8872312a110fae67e371d51088b9362cb6c Mon Sep 17 00:00:00 2001 From: daviehh <25255906+daviehh@users.noreply.github.com> Date: Tue, 23 Sep 2025 12:15:13 -0400 Subject: [PATCH 3/3] length mismatches in tests --- test/derivative_tests.jl | 4 ++-- test/integral_tests.jl | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/derivative_tests.jl b/test/derivative_tests.jl index 016ba8dc..9b35bf89 100644 --- a/test/derivative_tests.jl +++ b/test/derivative_tests.jl @@ -150,9 +150,9 @@ end @testset "Constant Interpolation" begin u = [0.0, 2.0, 1.0, 3.0, 2.0, 6.0, 5.5, 5.5, 2.7, 5.1, 3.0] - t = collect(0.0:11.0) + t = collect(0.0:10.0) A = ConstantInterpolation(u, t) - t2 = collect(0.0:10.0) + t2 = collect(0.0:9.0) @test all(isnan, derivative.(Ref(A), t)) @test all(derivative.(Ref(A), t2 .+ 0.1) .== 0.0) end diff --git a/test/integral_tests.jl b/test/integral_tests.jl index ea71082e..54c3d5e3 100644 --- a/test/integral_tests.jl +++ b/test/integral_tests.jl @@ -112,7 +112,7 @@ end args = [u, t, :Backward], name = "Quadratic Interpolation (Vector)") u = round.(rand(100), digits = 5) - t = 1.0collect(1:10) + t = 1.0collect(1:100) test_integral(QuadraticInterpolation; args = [u, t], name = "Quadratic Interpolation (Vector) with random points") end