Repository: JuliaLang/julia
File: test/iterators.jl (Julia)

Code before:
    @test nth(cycle_size_unknown, 20) == 9 # mod1(20, 5) = 5, wraps 4 times
    @test nth(finite_cycle_size_unknown, 2) == 3
    @test nth(finite_cycle_size_unknown, 20) == 9
    @test_throws BoundsError nth(finite_cycle_size_unknown, 30) # only wraps 5 times, max n is 5 * 5 = 25
end

@testset "Iterators docstrings" begin
    @test isempty(Docs.undocumented_names(Iterators))
end

# Filtered list comprehension (`Filter` construct) type inference
@test Base.infer_return_type((Vector{Any},)) do xs
    [x for x in xs if x isa Int]
end == Vector{Int}

@testset "issue #58922" begin
    # `last` short circuits correctly
    @test last(zip(1:10, 2:11)) == (10, 11)  # same length
    @test last(zip(1:3, 2:11)) == (3, 4)     # different length

    # Finite-guarded zip iterator: one iterator bounded and the other is not
    @test last(zip(1:3, Iterators.countfrom(2))) == (3, 4)
    @test last(zip(1:3, Iterators.cycle(('x', 'y')))) == (3, 'x')
    @test last(zip(1:3, Iterators.repeated('x'))) == (3, 'x')

    # TODO: test for other array types
    # @test last(zip(OffsetArray(1:10, 2), OffsetArray(1:10, 3))) == (10, 10)
    # @test last(collect(zip(OffsetArray(1:10, 2), OffsetArray(1:10, 3)))) == (10, 10)
end


Review comment (refactor):
```
isdefined(Main, :OffsetArrays) || @eval Main include("testhelpers/OffsetArrays.jl")
using .Main.OffsetArrays
```

is a pattern used in many test suites to get `OffsetArrays` in Base tests (put at the top

Code after:
    cycle_size_unknown = Iterators.cycle(it_size_unknown)
    finite_cycle_size_unknown = Iterators.cycle(it_size_unknown, 5)
    @test nth(cycle_size_unknown, 2) == 3
    @test nth(cycle_size_unknown, 20) == 9 # mod1(20, 5) = 5, wraps 4 times
    @test nth(finite_cycle_size_unknown, 2) == 3
    @test nth(finite_cycle_size_unknown, 20) == 9
    @test_throws BoundsError nth(finite_cycle_size_unknown, 30) # only wraps 5 times, max n is 5 * 5 = 25
end

@testset "Iterators docstrings" begin
    @test isempty(Docs.undocumented_names(Iterators))
end

# Filtered list comprehension (`Filter` construct) type inference
@test Base.infer_return_type((Vector{Any},)) do xs
    [x for x in xs if x isa Int]
end == Vector{Int}

@testset "issue #58922" begin
    # `last` short circuits correctly
    @test last(zip(1:10, 2:11)) == (10, 11)  # same length
    @test last(zip(1:3, 2:11)) == (3, 4)     # different length

    # Finite-guarded zip iterator: one iterator bounded and the other is not
    @test last(zip(1:3, Iterators.countfrom(2))) == (3, 4)
    @test last(zip(1:3, Iterators.cycle(('x', 'y')))) == (3, 'x')
    @test last(zip(1:3, Iterators.repeated('x'))) == (3, 'x')
    @test last(zip(OffsetArray(1:10, 2), OffsetArray(1:10, 3))) == (10, 10)

    # Cannot statically know length of zipped iterator if any of its components are of
    # unknown length
    @test_throws MethodError last(zip(1:3, Iterators.filter(x -> x > 0, -5:5))) # (3, 3)
    @test_throws MethodError last(zip(Iterators.filter(x -> x > 0, -5:5), 1:3)) # (3, 3)
    @test_throws MethodError last(zip(1:10, Iterators.filter(x -> x > 0, -5:5))) # (5, 5)

    # We also can't know the length of zipped iterators when all constituents are of an
    # unknown length.  In this test, the answer is (5, 4), but we can't know that without
    # a greedy algorithm
    @test_throws MethodError last(zip(Iterators.filter(x -> x > 0, -5:5), Iterators.filter(x -> x % 2 == 0, -5:5)))  # (5, 4)
end