Skip to content

Commit

Permalink
test foldability inference of basic iterator functions like eltype
Browse files Browse the repository at this point in the history
  • Loading branch information
nsajko committed Dec 14, 2024
1 parent 06f988b commit fe11fd8
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions test/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,43 @@ end
@test Base.IteratorSize(zip(1:5, (1,2,3)) ) == Base.HasLength() # for zip of ::HasShape and ::HasLength
end

@testset "foldability inference" begin
functions = (eltype, Base.IteratorSize, Base.IteratorEltype)
helper(type::UnionAll) = (type{n} for n 1:10) # helper for trying with multiple iterator counts
iterator_types = ( # each element here takes an iterator type as first parameter
Iterators.Reverse,
Iterators.Enumerate,
helper(Iterators.Zip{Tuple{Vararg{I, N}}} where {N, I})...,
Iterators.Filter{F, I} where {I, F},
Iterators.Accumulate{F, I} where {I, F},
Iterators.Rest,
Iterators.Count,
Iterators.Take,
Iterators.Drop,
Iterators.TakeWhile,
Iterators.DropWhile,
Iterators.Cycle,
Iterators.Repeated,
helper(Iterators.ProductIterator{Tuple{Vararg{I, N}}} where {N, I})...,
helper(Iterators.Flatten{Tuple{Vararg{I, N}}} where {N, I})...,
Iterators.PartitionIterator,
Iterators.Stateful,
)
simple_types = (Vector, NTuple, NamedTuple{X, Y} where {X, Y <: NTuple})
@testset "f: $f" for f functions
for V simple_types
@test Core.Compiler.is_foldable(Base.infer_effects(f, Tuple{V}))
@test Core.Compiler.is_foldable(Base.infer_effects(f, Tuple{Type{<:V}}))
for U iterator_types
S = U{<:V}
((U <: Iterators.Zip) || (U <: Iterators.Flatten)) && continue # partially broken
@test Core.Compiler.is_foldable(Base.infer_effects(f, Tuple{S}))
@test Core.Compiler.is_foldable(Base.infer_effects(f, Tuple{Type{<:S}}))
end
end
end
end

@testset "proper partition for non-1-indexed vector" begin
@test partition(IdentityUnitRange(11:19), 5) |> collect == [11:15,16:19] # IdentityUnitRange
end
Expand Down

0 comments on commit fe11fd8

Please sign in to comment.