Skip to content

Commit 93107b0

Browse files
authored
Merge pull request #107 from Tokazama/range-tests
Better test coverage for ranges
2 parents 514e3e8 + c387858 commit 93107b0

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ArrayInterface"
22
uuid = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
3-
version = "2.14.13"
3+
version = "2.14.14"
44

55
[deps]
66
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/ranges.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ struct OptionallyStaticUnitRange{F<:Integer,L<:Integer} <: AbstractUnitRange{Int
8585
if step(x) == 1
8686
return OptionallyStaticUnitRange(static_first(x), static_last(x))
8787
else
88-
throw(ArgumentError("step must be 1, got $(step(r))"))
88+
throw(ArgumentError("step must be 1, got $(step(x))"))
8989
end
9090
end
9191

9292
function OptionallyStaticUnitRange(x::AbstractRange)
9393
if step(x) == 1
9494
return OptionallyStaticUnitRange(static_first(x), static_last(x))
9595
else
96-
throw(ArgumentError("step must be 1, got $(step(r))"))
96+
throw(ArgumentError("step must be 1, got $(step(x))"))
9797
end
9898
end
9999
end
@@ -251,6 +251,7 @@ unsafe_isempty_unit_range(fst, lst) = fst > lst
251251
unsafe_length_one_to(lst::Int) = lst
252252
unsafe_length_one_to(::StaticInt{L}) where {L} = L
253253

254+
# TODO this should probably be renamed because the point is that it is safe
254255
@inline function unsafe_length_step_range(start::Int, step::Int, stop::Int)
255256
if step > 1
256257
return Base.checked_add(Int(div(unsigned(stop - start), step)), 1)

test/runtests.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,20 @@ end
199199
@test @inferred(1:2:StaticInt(10)) == 1:2:10
200200
@test @inferred(1:StaticInt(2):10) == 1:2:10
201201
@test @inferred(StaticInt(1):2:10) == 1:2:10
202+
@test @inferred(StaticInt(1):UInt(10)) === StaticInt(1):10
203+
@test @inferred(UInt(1):StaticInt(1):StaticInt(10)) === 1:StaticInt(10)
204+
@test @inferred(ArrayInterface.OptionallyStaticUnitRange{Int,Int}(1:10)) == 1:10
205+
@test @inferred(ArrayInterface.OptionallyStaticUnitRange(1:10)) == 1:10
206+
207+
@inferred(ArrayInterface.OptionallyStaticUnitRange(1:10))
208+
209+
@test @inferred(ArrayInterface.OptionallyStaticStepRange(StaticInt(1), 1, UInt(10))) == StaticInt(1):1:10
210+
@test @inferred(ArrayInterface.OptionallyStaticStepRange(UInt(1), 1, StaticInt(10))) == StaticInt(1):1:10
211+
@test @inferred(ArrayInterface.OptionallyStaticStepRange(1:10)) == 1:1:10
212+
213+
@test_throws ArgumentError ArrayInterface.OptionallyStaticUnitRange(1:2:10)
214+
@test_throws ArgumentError ArrayInterface.OptionallyStaticUnitRange{Int,Int}(1:2:10)
215+
@test_throws ArgumentError ArrayInterface.OptionallyStaticStepRange(1, 0, 10)
202216

203217
@test @inferred(StaticInt(1):StaticInt(1):StaticInt(10)) === ArrayInterface.OptionallyStaticUnitRange(StaticInt(1), StaticInt(10))
204218
@test @inferred(StaticInt(1):StaticInt(1):10) === ArrayInterface.OptionallyStaticUnitRange(StaticInt(1), 10)
@@ -226,13 +240,16 @@ end
226240
@test isnothing(@inferred(ArrayInterface.known_first(typeof(1:4))))
227241
@test isone(@inferred(ArrayInterface.known_first(Base.OneTo(4))))
228242
@test isone(@inferred(ArrayInterface.known_first(typeof(Base.OneTo(4)))))
243+
@test isone(@inferred(ArrayInterface.known_first(typeof(StaticInt(1):2:10))))
229244

230245
@test isnothing(@inferred(ArrayInterface.known_last(1:4)))
231246
@test isnothing(@inferred(ArrayInterface.known_last(typeof(1:4))))
247+
@test isone(@inferred(ArrayInterface.known_last(typeof(StaticInt(-1):StaticInt(2):StaticInt(1)))))
232248

233249
@test isnothing(@inferred(ArrayInterface.known_step(typeof(1:0.2:4))))
234250
@test isone(@inferred(ArrayInterface.known_step(1:4)))
235251
@test isone(@inferred(ArrayInterface.known_step(typeof(1:4))))
252+
@test isone(@inferred(ArrayInterface.known_step(typeof(Base.Slice(1:4)))))
236253

237254
@testset "length" begin
238255
@test @inferred(length(ArrayInterface.OptionallyStaticUnitRange(1, 0))) == 0
@@ -244,6 +261,16 @@ end
244261

245262
@test @inferred(length(StaticInt(1):StaticInt(2):StaticInt(0))) == 0
246263
@test @inferred(length(StaticInt(0):StaticInt(-2):StaticInt(1))) == 0
264+
265+
@test @inferred(ArrayInterface.known_length(typeof(ArrayInterface.OptionallyStaticStepRange(StaticInt(1), 2, 10)))) === nothing
266+
@test @inferred(ArrayInterface.known_length(typeof(ArrayInterface.OptionallyStaticStepRange(StaticInt(1), StaticInt(1), StaticInt(10))))) === 10
267+
@test @inferred(ArrayInterface.known_length(typeof(ArrayInterface.OptionallyStaticStepRange(StaticInt(2), StaticInt(1), StaticInt(10))))) === 9
268+
@test @inferred(ArrayInterface.known_length(typeof(ArrayInterface.OptionallyStaticStepRange(StaticInt(2), StaticInt(2), StaticInt(10))))) === 5
269+
270+
@test @inferred(length(ArrayInterface.OptionallyStaticStepRange(StaticInt(1), 2, 10))) == 5
271+
@test @inferred(length(ArrayInterface.OptionallyStaticStepRange(StaticInt(1), StaticInt(1), StaticInt(10)))) == 10
272+
@test @inferred(length(ArrayInterface.OptionallyStaticStepRange(StaticInt(2), StaticInt(1), StaticInt(10)))) == 9
273+
@test @inferred(length(ArrayInterface.OptionallyStaticStepRange(StaticInt(2), StaticInt(2), StaticInt(10)))) == 5
247274
end
248275

249276
@test @inferred(getindex(ArrayInterface.OptionallyStaticUnitRange(StaticInt(1), 10), 1)) == 1

0 commit comments

Comments
 (0)