Skip to content

Tests for CachedIterator #373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 8 additions & 24 deletions src/LinearAlgebra/helper.jl
Original file line number Diff line number Diff line change
Expand Up @@ -571,9 +571,11 @@ end

eltype(it::Type{<:CachedIterator{T}}) where {T} = T

Base.IteratorSize(::Type{<:CachedIterator{<:Any,IT}}) where {IT} = Base.IteratorSize(IT)
function Base.IteratorSize(::Type{<:CachedIterator{<:Any,IT}}) where {IT}
Base.IteratorSize(IT) isa Base.IsInfinite ? Base.IsInfinite() : Base.HasLength()
end

Base.keys(c::CachedIterator) = keys(c.iterator)
Base.keys(c::CachedIterator) = oneto(length(c))

iterate(it::CachedIterator) = iterate(it,1)
function iterate(it::CachedIterator,st::Int)
Expand All @@ -589,30 +591,12 @@ function getindex(it::CachedIterator, k)
if mx > length(it) || mx < 1
throw(BoundsError(it,k))
end
resize!(it,isempty(k) ? 0 : mx).storage[k]
end

function findfirst(f::Function,A::CachedIterator)
k=1
for c in A
if f(c)
return k
end
k+=1
end
return 0
v = resize!(it, mx)
v.storage[k]
end

function findfirst(A::CachedIterator,x)
k=1
for c in A
if c == x
return k
end
k+=1
end
return 0
end
@deprecate findfirst(A::CachedIterator, x) findfirst(x, A::CachedIterator)
findfirst(x::T, A::CachedIterator{T}) where {T} = findfirst(==(x), A)

length(A::CachedIterator) = length(A.iterator)

Expand Down
37 changes: 35 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ end
blk = ApproxFunBase.block(t, i)
@test i ∈ ApproxFunBase.blockrange(t, blk)
@test vi == t[i]
@test findfirst(t, vi) == i
@test findfirst(vi, t) == i
end
end
@testset "nD" begin
Expand All @@ -525,7 +525,7 @@ end
v = collect(Iterators.take(t, 16))
@test eltype(v) == eltype(t)
@testset for (i, vi) in enumerate(v)
@test findfirst(t, vi) == i
@test findfirst(vi, t) == i
end
end
@testset "cache" begin
Expand All @@ -539,5 +539,38 @@ end
end
end

@testset "CachedIterator" begin
v = ApproxFunBase.CachedIterator(1:4)
@test Base.IteratorSize(v) == Base.HasLength()
@test length(v) == 4
@test eltype(v) == Int
@test findfirst(3, v) == 3
vs = collect(v)
@test vs == [1:4;]
@test eltype(vs) == Int

v = ApproxFunBase.CachedIterator(Iterators.take(1:14, 4))
@test Base.IteratorSize(v) == Base.HasLength()
@test length(v) == 4
@test eltype(v) == Int
@test findfirst(3, v) == 3
vs = collect(v)
@test vs == [1:4;]
@test eltype(vs) == Int

v = ApproxFunBase.CachedIterator(1:∞)
@test Base.IteratorSize(v) == Base.IsInfinite()
@test isinf(length(v))
@test eltype(v) == Int
@test findfirst(3, v) == 3

s = PointSpace(1:4) + PointSpace(1:4)
b = ApproxFunBase.BlockInterlacer(s);
it = ApproxFunBase.CachedIterator(b)
# Test that the iterator isn't stateful
@test collect(it) == collect(Iterators.take(it, length(it)))
@test collect(it) == collect(b)
end

@time include("ETDRK4Test.jl")
include("show.jl")