Skip to content

Commit 603096d

Browse files
authored
Tests for CachedIterator (#373)
* Tests for CachedIterator * Tests with BlockInterlacer
1 parent fea3939 commit 603096d

File tree

2 files changed

+43
-26
lines changed

2 files changed

+43
-26
lines changed

src/LinearAlgebra/helper.jl

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -571,9 +571,11 @@ end
571571

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

574-
Base.IteratorSize(::Type{<:CachedIterator{<:Any,IT}}) where {IT} = Base.IteratorSize(IT)
574+
function Base.IteratorSize(::Type{<:CachedIterator{<:Any,IT}}) where {IT}
575+
Base.IteratorSize(IT) isa Base.IsInfinite ? Base.IsInfinite() : Base.HasLength()
576+
end
575577

576-
Base.keys(c::CachedIterator) = keys(c.iterator)
578+
Base.keys(c::CachedIterator) = oneto(length(c))
577579

578580
iterate(it::CachedIterator) = iterate(it,1)
579581
function iterate(it::CachedIterator,st::Int)
@@ -589,30 +591,12 @@ function getindex(it::CachedIterator, k)
589591
if mx > length(it) || mx < 1
590592
throw(BoundsError(it,k))
591593
end
592-
resize!(it,isempty(k) ? 0 : mx).storage[k]
593-
end
594-
595-
function findfirst(f::Function,A::CachedIterator)
596-
k=1
597-
for c in A
598-
if f(c)
599-
return k
600-
end
601-
k+=1
602-
end
603-
return 0
594+
v = resize!(it, mx)
595+
v.storage[k]
604596
end
605597

606-
function findfirst(A::CachedIterator,x)
607-
k=1
608-
for c in A
609-
if c == x
610-
return k
611-
end
612-
k+=1
613-
end
614-
return 0
615-
end
598+
@deprecate findfirst(A::CachedIterator, x) findfirst(x, A::CachedIterator)
599+
findfirst(x::T, A::CachedIterator{T}) where {T} = findfirst(==(x), A)
616600

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

test/runtests.jl

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ end
505505
blk = ApproxFunBase.block(t, i)
506506
@test i ApproxFunBase.blockrange(t, blk)
507507
@test vi == t[i]
508-
@test findfirst(t, vi) == i
508+
@test findfirst(vi, t) == i
509509
end
510510
end
511511
@testset "nD" begin
@@ -525,7 +525,7 @@ end
525525
v = collect(Iterators.take(t, 16))
526526
@test eltype(v) == eltype(t)
527527
@testset for (i, vi) in enumerate(v)
528-
@test findfirst(t, vi) == i
528+
@test findfirst(vi, t) == i
529529
end
530530
end
531531
@testset "cache" begin
@@ -539,5 +539,38 @@ end
539539
end
540540
end
541541

542+
@testset "CachedIterator" begin
543+
v = ApproxFunBase.CachedIterator(1:4)
544+
@test Base.IteratorSize(v) == Base.HasLength()
545+
@test length(v) == 4
546+
@test eltype(v) == Int
547+
@test findfirst(3, v) == 3
548+
vs = collect(v)
549+
@test vs == [1:4;]
550+
@test eltype(vs) == Int
551+
552+
v = ApproxFunBase.CachedIterator(Iterators.take(1:14, 4))
553+
@test Base.IteratorSize(v) == Base.HasLength()
554+
@test length(v) == 4
555+
@test eltype(v) == Int
556+
@test findfirst(3, v) == 3
557+
vs = collect(v)
558+
@test vs == [1:4;]
559+
@test eltype(vs) == Int
560+
561+
v = ApproxFunBase.CachedIterator(1:∞)
562+
@test Base.IteratorSize(v) == Base.IsInfinite()
563+
@test isinf(length(v))
564+
@test eltype(v) == Int
565+
@test findfirst(3, v) == 3
566+
567+
s = PointSpace(1:4) + PointSpace(1:4)
568+
b = ApproxFunBase.BlockInterlacer(s);
569+
it = ApproxFunBase.CachedIterator(b)
570+
# Test that the iterator isn't stateful
571+
@test collect(it) == collect(Iterators.take(it, length(it)))
572+
@test collect(it) == collect(b)
573+
end
574+
542575
@time include("ETDRK4Test.jl")
543576
include("show.jl")

0 commit comments

Comments
 (0)