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

Code before:
        @testset "r: $r" for r ∈ mappers1
            @testset "f: $f" for f ∈ maps
                @test_throws MethodError r(f)
                @test !applicable(r, f)
                @test !hasmethod(r, Tuple{typeof(f)})
            end
        end
    end

    @testset "mapreduce" begin
        @testset "mr: $mr" for mr ∈ mappers2
            @testset "f: $f" for f ∈ maps
                @testset "g: $g" for g ∈ maps
                    @test_throws MethodError mr(f, g)
                    @test !applicable(mr, f, g)
                    @test !hasmethod(mr, Tuple{typeof(f),typeof(g)})
                end
            end
        end
    end
end

@testset "nth" begin
    Z = Array{Int,0}(undef)
    Z[] = 17
    itrs = (collect(1:1000), 10:6:1000, "∀ϵ>0", (1, 3, 5, 10, 78), reshape(1:30, (5, 6)),
        Z, 3, true, 'x', 4 => 5, view(Z), view(reshape(1:30, (5, 6)), 2:4, 2:6),
        (x^2 for x in 1:10), Iterators.Filter(isodd, 1:10), Iterators.flatten((1:10, 50:60)),
        pairs(50:60), zip(1:10, 21:30, 51:60), Iterators.product(1:3, 10:12),
        Iterators.repeated(3.14159, 5), (a=2, b=3, c=5, d=7, e=11), Iterators.cycle(collect(1:100)),
        Iterators.cycle([1, 2, 3, 4, 5], 5))
    ns = (
        234, 123, 3, 2, 21, 1, 1, 1, 1, 1, 1, 10, 9, 3, 15, 7, 6, 3, 4, 4, 99999, 25
    )
    expected = (
        234, 742, '>', 3, 21, 17, 3, true, 'x', 4, 17, 22, 81, 5, 54, (7 => 56), (6, 26, 56), (3, 10), 3.14159, 7, 99, 5
    )
    @test length(itrs) == length(ns) == length(expected)
    testset = zip(itrs, ns, expected)
    @testset "iter: $IT" for (IT, n, exp) in testset
        @test exp == nth(IT, n)
        IT isa Cycle && continue # cycles are infinite so never OOB
        @test nth(IT, 999999999) === nothing
    end
end

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


Review comment (performance):
I would put these in a `Vector{Any}` - no need to create specialized code for long tuples here. Since the test only runs once, using a tuple will be much slower.

Code after:
    end
end

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

@testset "Iterators.peel" begin
    @test Iterators.peel([]) === nothing
    @test Iterators.peel(1:10)[1] == 1
    @test Iterators.peel(1:10)[2] |> collect == 2:10
    @test Iterators.peel(x^2 for x in 2:4)[1] == 4
    @test Iterators.peel(x^2 for x in 2:4)[2] |> collect == [9, 16]
end

@testset "last for iterators" begin
    @test last(Iterators.map(identity, 1:3)) == 3
    @test last(Iterators.filter(iseven, (Iterators.map(identity, 1:3)))) == 2
end

@testset "isempty and isdone for Generators" begin
    itr = eachline(IOBuffer("foo\n"))
    gen = (x for x in itr)
    @test !(@inferred isempty(gen))
    @test !Base.isdone(gen)
    @test collect(gen) == ["foo"]
end

@testset "empty product iterators" begin
    v = nothing
    for (z,) in zip(Iterators.product())
        v = z
    end
    @test v == ()
end

@testset "collect partition substring" begin
    @test collect(Iterators.partition(lstrip("01111", '0'), 2)) == ["11", "11"]
end

@testset "IterableStringPairs" begin
    for s in ["", "a", "abcde", "γ", "∋γa"]
        for T in (String, SubString, GenericString)
            sT = T(s)
            p = pairs(sT)
            @test collect(p) == [k=>v for (k,v) in zip(keys(sT), sT)]
            rv = Iterators.reverse(p)
            @test collect(rv) == reverse([k=>v for (k,v) in zip(keys(sT), sT)])
            rrv = Iterators.reverse(rv)
            @test collect(rrv) == collect(p)
        end