Skip to content
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

additional tests for take/drop iterators #12029

Merged
merged 2 commits into from
Mar 17, 2018
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
8 changes: 8 additions & 0 deletions base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,10 @@ IteratorSize(::Type{<:Count}) = IsInfinite()
struct Take{I}
xs::I
n::Int
function Take(xs::I, n::Integer) where {I}
n < 0 && throw(ArgumentError("Take length must be nonnegative"))
return new{I}(xs, n)
end
end

"""
Expand Down Expand Up @@ -574,6 +578,10 @@ end
struct Drop{I}
xs::I
n::Int
function Drop(xs::I, n::Integer) where {I}
n < 0 && throw(ArgumentError("Drop length must be nonnegative"))
return new{I}(xs, n)
end
end

"""
Expand Down
7 changes: 6 additions & 1 deletion test/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ end
# take
# ----
let t = take(0:2:8, 10), i = 0
@test length(collect(t)) == 5
@test length(collect(t)) == 5 == length(t)

for j = t
@test j == i*2
Expand All @@ -101,6 +101,8 @@ let i = 0
@test i == 10
end

@test isempty(take(0:2:8, 0))
@test_throws ArgumentError take(0:2:8, -1)
@test length(take(1:3,typemax(Int))) == 3
@test length(take(countfrom(1),3)) == 3
@test length(take(1:6,3)) == 3
Expand All @@ -115,6 +117,9 @@ let i = 0
@test i == 4
end

@test isempty(drop(0:2:10, 100))
@test isempty(collect(drop(0:2:10, 100)))
@test_throws ArgumentError drop(0:2:8, -1)
@test length(drop(1:3,typemax(Int))) == 0
@test Base.IteratorSize(drop(countfrom(1),3)) == Base.IsInfinite()
@test_throws MethodError length(drop(countfrom(1), 3))
Expand Down