Skip to content

Commit 12698af

Browse files
nsajkofingolfin
andauthored
test foldability inference of basic iterator functions like eltype (#56838)
Co-authored-by: Max Horn <max@quendi.de>
1 parent e485be8 commit 12698af

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

test/iterators.jl

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,57 @@ end
998998
@test (@inferred Base.IteratorSize(zip(1:5, (1,2,3)) )) == Base.HasLength() # for zip of ::HasShape and ::HasLength
999999
end
10001000

1001+
@testset "foldability inference" begin
1002+
functions = (eltype, Base.IteratorSize, Base.IteratorEltype)
1003+
helper(type::UnionAll) = (type{n} for n 1:10) # helper for trying with multiple iterator counts
1004+
iterator_types = ( # each element here takes an iterator type as first parameter
1005+
Base.Generator,
1006+
Iterators.Reverse,
1007+
Iterators.Enumerate,
1008+
Iterators.Filter{F, I} where {I, F},
1009+
Iterators.Accumulate{F, I} where {I, F},
1010+
Iterators.Rest,
1011+
Iterators.Count,
1012+
Iterators.Take,
1013+
Iterators.Drop,
1014+
Iterators.TakeWhile,
1015+
Iterators.DropWhile,
1016+
Iterators.Cycle,
1017+
Iterators.Repeated,
1018+
Iterators.PartitionIterator,
1019+
Iterators.Stateful,
1020+
helper(Iterators.ProductIterator{Tuple{Vararg{I, N}}} where {N, I})...,
1021+
)
1022+
iterator_types_extra = (
1023+
iterator_types...,
1024+
helper(Iterators.Zip{Tuple{Vararg{I, N}}} where {N, I})...,
1025+
helper(Iterators.Flatten{Tuple{Vararg{I, N}}} where {N, I})...,
1026+
)
1027+
simple_types = (Vector, NTuple, NamedTuple{X, Y} where {X, Y <: NTuple})
1028+
example_type = Tuple{Bool, Int8, Vararg{Int16, 20}}
1029+
function test_foldability_inference(f, S::Type)
1030+
@test Core.Compiler.is_foldable(Base.infer_effects(f, Tuple{S}))
1031+
@test Core.Compiler.is_foldable(Base.infer_effects(f, Tuple{Type{<:S}}))
1032+
end
1033+
@testset "concrete" begin # weaker test, only checks foldability for certain concrete types
1034+
@testset "f: $f" for f functions
1035+
for U iterator_types_extra
1036+
test_foldability_inference(f, U{example_type})
1037+
end
1038+
end
1039+
end
1040+
@testset "nonconcrete" begin # stronger test, checks foldability for large families of types
1041+
@testset "f: $f" for f functions
1042+
for V simple_types
1043+
test_foldability_inference(f, V) # sanity check
1044+
for U iterator_types
1045+
test_foldability_inference(f, U{<:V})
1046+
end
1047+
end
1048+
end
1049+
end
1050+
end
1051+
10011052
@testset "proper partition for non-1-indexed vector" begin
10021053
@test partition(IdentityUnitRange(11:19), 5) |> collect == [11:15,16:19] # IdentityUnitRange
10031054
end

0 commit comments

Comments
 (0)