Skip to content

Commit d39543f

Browse files
Merge pull request #472 from daviehh/master
length check before return; also explicit error instead of `@assert`
2 parents 9df9635 + c1bbe88 commit d39543f

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

src/interpolation_utils.jl

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ end
9393
# helper function for data manipulation
9494
function munge_data(u::AbstractVector, t::AbstractVector;
9595
check_sorted = t, sorted_arg_name = ("second", "t"))
96+
length(t) == length(u) ||
97+
throw(ArgumentError("`u`, `t` length mismatch: length(t) ≠ length(u)"))
98+
9699
Tu = nonmissingtype(eltype(u))
97100
Tt = nonmissingtype(eltype(t))
98101

@@ -109,8 +112,6 @@ function munge_data(u::AbstractVector, t::AbstractVector;
109112
return u, t
110113
end
111114

112-
@assert length(t) == length(u)
113-
114115
non_missing_mask = map((ui, ti) -> !ismissing(ui) && !ismissing(ti), u, t)
115116
u = convert(AbstractVector{Tu}, u[non_missing_mask])
116117
t = convert(AbstractVector{Tt}, t[non_missing_mask])
@@ -119,13 +120,15 @@ function munge_data(u::AbstractVector, t::AbstractVector;
119120
end
120121

121122
function munge_data(U::AbstractMatrix, t::AbstractVector)
123+
length(t) == size(U, 2) ||
124+
throw(ArgumentError("`u`, `t` length mismatch: length(t) ≠ size(U, 2)"))
125+
122126
TU = nonmissingtype(eltype(U))
123127
Tt = nonmissingtype(eltype(t))
124128
if TU === eltype(U) && Tt === eltype(t)
125129
return U, t
126130
end
127131

128-
@assert length(t) == size(U, 2)
129132
non_missing_mask = map(
130133
(uis, ti) -> !any(ismissing, uis) && !ismissing(ti), eachcol(U), t)
131134
U = convert(AbstractMatrix{TU}, U[:, non_missing_mask])
@@ -135,13 +138,15 @@ function munge_data(U::AbstractMatrix, t::AbstractVector)
135138
end
136139

137140
function munge_data(U::AbstractArray{T, N}, t) where {T, N}
141+
length(t) == size(U, N) ||
142+
throw(ArgumentError("`u`, `t` length mismatch: length(t) ≠ size(U, N)"))
143+
138144
TU = nonmissingtype(eltype(U))
139145
Tt = nonmissingtype(eltype(t))
140146
if TU === eltype(U) && Tt === eltype(t)
141147
return U, t
142148
end
143149

144-
@assert length(t) == size(U, N)
145150
non_missing_mask = map(
146151
(uis, ti) -> !any(ismissing, uis) && !ismissing(ti), eachslice(U; dims = N), t)
147152
U = convert(AbstractArray{TU, N}, copy(selectdim(U, N, non_missing_mask)))

test/derivative_tests.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@ end
150150

151151
@testset "Constant Interpolation" begin
152152
u = [0.0, 2.0, 1.0, 3.0, 2.0, 6.0, 5.5, 5.5, 2.7, 5.1, 3.0]
153-
t = collect(0.0:11.0)
153+
t = collect(0.0:10.0)
154154
A = ConstantInterpolation(u, t)
155-
t2 = collect(0.0:10.0)
155+
t2 = collect(0.0:9.0)
156156
@test all(isnan, derivative.(Ref(A), t))
157157
@test all(derivative.(Ref(A), t2 .+ 0.1) .== 0.0)
158158
end

test/integral_tests.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ end
112112
args = [u, t, :Backward],
113113
name = "Quadratic Interpolation (Vector)")
114114
u = round.(rand(100), digits = 5)
115-
t = 1.0collect(1:10)
115+
t = 1.0collect(1:100)
116116
test_integral(QuadraticInterpolation; args = [u, t],
117117
name = "Quadratic Interpolation (Vector) with random points")
118118
end

test/interpolation_tests.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1263,7 +1263,8 @@ end
12631263

12641264
@testset "user error" begin
12651265
@test_throws ArgumentError LinearInterpolation(rand(10), rand(10))
1266-
@test_throws ArgumentError LinearInterpolation(0:10, rand(10))
1266+
@test_throws ArgumentError LinearInterpolation(1:10, rand(10))
1267+
@test_throws ArgumentError LinearInterpolation(1:3, 1:5)
12671268
end
12681269

12691270
@testset "Symbolic interpolation" begin

0 commit comments

Comments
 (0)