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

fix doctests, and make them less prone to errors #26275

Merged
merged 2 commits into from
Mar 4, 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
16 changes: 8 additions & 8 deletions base/abstractdict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ return the elements in the same order.

# Examples
```jldoctest
julia> a = Dict('a'=>2, 'b'=>3)
julia> D = Dict('a'=>2, 'b'=>3)
Dict{Char,Int64} with 2 entries:
'b' => 3
'a' => 2
'b' => 3

julia> collect(keys(a))
julia> collect(keys(D))
2-element Array{Char,1}:
'b'
'a'
'b'
```
"""
keys(a::AbstractDict) = KeySet(a)
Expand All @@ -115,15 +115,15 @@ return the elements in the same order.

# Examples
```jldoctest
julia> a = Dict('a'=>2, 'b'=>3)
julia> D = Dict('a'=>2, 'b'=>3)
Dict{Char,Int64} with 2 entries:
'b' => 3
'a' => 2
'b' => 3

julia> collect(values(a))
julia> collect(values(D))
2-element Array{Int64,1}:
3
2
3
```
"""
values(a::AbstractDict) = ValueIterator(a)
Expand Down
46 changes: 15 additions & 31 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1523,8 +1523,7 @@ julia> A = [false, false, true, false]
julia> findnext(A, 1)
3

julia> findnext(A, 4) == nothing
true
julia> findnext(A, 4) # returns nothing, but not printed in the REPL

julia> A = [false false; true false]
2×2 Array{Bool,2}:
Expand Down Expand Up @@ -1575,8 +1574,7 @@ julia> A = [false, false, true, false]
julia> findfirst(A)
3

julia> findfirst(falses(3)) == nothing
true
julia> findfirst(falses(3)) # returns nothing, but not printed in the REPL

julia> A = [false false; true false]
2×2 Array{Bool,2}:
Expand Down Expand Up @@ -1615,23 +1613,14 @@ and [`pairs(A)`](@ref).

# Examples
```jldoctest
A = [1, 4, 2, 2]
4-element Array{Int64,1}:
1
4
2
2
julia> A = [1, 4, 2, 2];

julia> findnext(isodd, A, 1)
1

julia> findnext(isodd, A, 2) == nothing
true
julia> findnext(isodd, A, 2) # returns nothing, but not printed in the REPL

julia> A = [1 4; 2 2]
2×2 Array{Int64,2}:
1 4
2 2
julia> A = [1 4; 2 2];

julia> findnext(isodd, A, CartesianIndex(1, 1))
CartesianIndex(1, 1)
Expand Down Expand Up @@ -1670,8 +1659,7 @@ julia> A = [1, 4, 2, 2]
julia> findfirst(iseven, A)
2

julia> findfirst(x -> x>10, A) == nothing
true
julia> findfirst(x -> x>10, A) # returns nothing, but not printed in the REPL

julia> findfirst(equalto(4), A)
2
Expand Down Expand Up @@ -1717,8 +1705,7 @@ julia> A = [false, false, true, true]
julia> findprev(A, 3)
3

julia> findprev(A, 1) == nothing
true
julia> findprev(A, 1) # returns nothing, but not printed in the REPL

julia> A = [false false; true true]
2×2 Array{Bool,2}:
Expand Down Expand Up @@ -1767,8 +1754,7 @@ julia> findlast(A)

julia> A = falses(2,2);

julia> findlast(A) == nothing
true
julia> findlast(A) # returns nothing, but not printed in the REPL

julia> A = [true false; true false]
2×2 Array{Bool,2}:
Expand Down Expand Up @@ -1814,8 +1800,7 @@ julia> A = [4, 6, 1, 2]
1
2

julia> findprev(isodd, A, 1) == nothing
true
julia> findprev(isodd, A, 1) # returns nothing, but not printed in the REPL

julia> findprev(isodd, A, 3)
3
Expand Down Expand Up @@ -1859,8 +1844,7 @@ julia> A = [1, 2, 3, 4]
julia> findlast(isodd, A)
3

julia> findlast(x -> x > 5, A) == nothing
true
julia> findlast(x -> x > 5, A) # returns nothing, but not printed in the REPL

julia> A = [1 2; 3 4]
2×2 Array{Int64,2}:
Expand Down Expand Up @@ -1965,9 +1949,9 @@ julia> A = [true false; false true]
false true

julia> findall(A)
2-element Array{Int64,1}:
1
4
2-element Array{CartesianIndex{2},1}:
CartesianIndex(1, 1)
CartesianIndex(2, 2)

julia> findall(falses(3))
0-element Array{Int64,1}
Expand Down Expand Up @@ -2137,9 +2121,9 @@ array contains `nothing` wherever `a` is not a member of `b`.

# Examples
```jldoctest
julia> a = ['a', 'b', 'c', 'b', 'd', 'a']
julia> a = ['a', 'b', 'c', 'b', 'd', 'a'];

julia> b = ['a', 'b', 'c']
julia> b = ['a', 'b', 'c'];

julia> indexin(a, b)
6-element Array{Union{Nothing, Int64},1}:
Expand Down
4 changes: 2 additions & 2 deletions base/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ would be the broadcast `inds`). The shape of the output is equal to the shape of
element of `indsb`.

# Examples
```jldoctest
```jldoctest bc_getindex
julia> A = [11 12; 21 22]
2×2 Array{Int64,2}:
11 12
Expand Down Expand Up @@ -729,7 +729,7 @@ julia> broadcast_getindex(A, 1:2, 2:-1:1)
Because the indices are all vectors, these calls are like `[A[i[k], j[k]] for k = 1:2]`
where `i` and `j` are the two index vectors.

```jldoctest
```jldoctest bc_getindex
julia> broadcast_getindex(A, 1:2, (1:2)')
2×2 Array{Int64,2}:
11 12
Expand Down
16 changes: 8 additions & 8 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -530,15 +530,15 @@ end
Determine whether a collection has a mapping for a given key.

```jldoctest
julia> a = Dict('a'=>2, 'b'=>3)
julia> D = Dict('a'=>2, 'b'=>3)
Dict{Char,Int64} with 2 entries:
'b' => 3
'a' => 2
'b' => 3

julia> haskey(a,'a')
julia> haskey(D, 'a')
true

julia> haskey(a,'c')
julia> haskey(D, 'c')
false
```
"""
Expand All @@ -551,15 +551,15 @@ in(key, v::KeySet{<:Any, <:Dict}) = (ht_keyindex(v.dict, key) >= 0)
Return the key matching argument `key` if one exists in `collection`, otherwise return `default`.

```jldoctest
julia> a = Dict('a'=>2, 'b'=>3)
julia> D = Dict('a'=>2, 'b'=>3)
Dict{Char,Int64} with 2 entries:
'b' => 3
'a' => 2
'b' => 3

julia> getkey(a,'a',1)
julia> getkey(D, 'a', 1)
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)

julia> getkey(a,'d','a')
julia> getkey(D, 'd', 'a')
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
```
"""
Expand Down
15 changes: 9 additions & 6 deletions base/docs/basedocs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -924,25 +924,28 @@ OutOfMemoryError
An indexing operation into an array, `a`, tried to access an out-of-bounds element at index `i`.

# Examples
```jldoctest
```jldoctest; filter = r"Stacktrace:(\\n \\[[0-9]+\\].*)*"
julia> A = fill(1.0, 7);

julia> A[8]
ERROR: BoundsError: attempt to access 7-element Array{Float64,1} at index [8]
Stacktrace:
[1] getindex(::Array{Float64,1}, ::Int64) at ./array.jl:758
[1] getindex(::Array{Float64,1}, ::Int64) at ./array.jl:660
[2] top-level scope

julia> B = fill(1.0, (2,3));

julia> B[2, 4]
ERROR: BoundsError: attempt to access 2×3 Array{Float64,2} at index [2, 4]
Stacktrace:
[1] getindex(::Array{Float64,2}, ::Int64, ::Int64) at ./array.jl:759
[1] getindex(::Array{Float64,2}, ::Int64, ::Int64) at ./array.jl:661
[2] top-level scope

julia> B[9]
ERROR: BoundsError: attempt to access 2×3 Array{Float64,2} at index [9]
Stacktrace:
[1] getindex(::Array{Float64,2}, ::Int64) at ./array.jl:758
[1] getindex(::Array{Float64,2}, ::Int64) at ./array.jl:660
[2] top-level scope
```
"""
BoundsError
Expand All @@ -955,9 +958,9 @@ Cannot exactly convert `val` to type `T` in a method of function `name`.
# Examples
```jldoctest
julia> convert(Float64, 1+2im)
ERROR: InexactError: convert(Float64, 1 + 2im)
ERROR: InexactError: Float64(Float64, 1 + 2im)
Stacktrace:
[1] convert(::Type{Float64}, ::Complex{Int64}) at ./complex.jl:37
[...]
```
"""
InexactError
Expand Down
20 changes: 11 additions & 9 deletions base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ julia> convert(Int, 3.0)
3

julia> convert(Int, 3.5)
ERROR: InexactError: convert(Int64, 3.5)
ERROR: InexactError: Int64(Int64, 3.5)
Stacktrace:
[1] convert(::Type{Int64}, ::Float64) at ./float.jl:703
[...]
```

If `T` is a [`AbstractFloat`](@ref) or [`Rational`](@ref) type,
Expand Down Expand Up @@ -414,22 +414,24 @@ Annotates the expression `blk` as a bounds checking block, allowing it to be eli
its caller in order for `@inbounds` to have effect.

# Examples
```jldoctest
```jldoctest; filter = r"Stacktrace:(\\n \\[[0-9]+\\].*)*"
julia> @inline function g(A, i)
@boundscheck checkbounds(A, i)
return "accessing (\$A)[\$i]"
end
f1() = return g(1:2, -1)
f2() = @inbounds return g(1:2, -1)
f2 (generic function with 1 method)
end;

julia> f1() = return g(1:2, -1);

julia> f2() = @inbounds return g(1:2, -1);

julia> f1()
ERROR: BoundsError: attempt to access 2-element UnitRange{Int64} at index [-1]
Stacktrace:
[1] throw_boundserror(::UnitRange{Int64}, ::Tuple{Int64}) at ./abstractarray.jl:435
[2] checkbounds at ./abstractarray.jl:399 [inlined]
[1] throw_boundserror(::UnitRange{Int64}, ::Tuple{Int64}) at ./abstractarray.jl:455
[2] checkbounds at ./abstractarray.jl:420 [inlined]
[3] g at ./none:2 [inlined]
[4] f1() at ./none:1
[5] top-level scope

julia> f2()
"accessing (1:2)[-1]"
Expand Down
2 changes: 1 addition & 1 deletion base/floatfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ julia> round(pi, 3, base = 2)
rounded to 1.2.

# Examples
```jldoctest
```jldoctest; setup = :(using Printf)
julia> x = 1.15
1.15

Expand Down
8 changes: 4 additions & 4 deletions base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,9 @@ julia> open("my_file.txt", "w") do io
57

julia> readuntil("my_file.txt", 'L')
"JuliaL"
"Julia"

julia> readuntil("my_file.txt", '.')
julia> readuntil("my_file.txt", '.', keep = true)
"JuliaLang is a GitHub organization."

julia> rm("my_file.txt")
Expand Down Expand Up @@ -1003,7 +1003,7 @@ end with the EOL, matching the length returned by [`eachline`](@ref) and [`readl

# Examples
```jldoctest
julia> io = IOBuffer("JuliaLang is a GitHub organization.\n");
julia> io = IOBuffer("JuliaLang is a GitHub organization.\\n");

julia> countlines(io)
1
Expand All @@ -1014,7 +1014,7 @@ julia> countlines(io)
1

julia> countlines(io, eol = '.')
1
0
```
"""
function countlines(io::IO; eol::Char='\n')
Expand Down
8 changes: 4 additions & 4 deletions base/iostream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ iswritable(s::IOStream) = ccall(:ios_get_writable, Cint, (Ptr{Cvoid},), s.ios)!=
isreadable(s::IOStream) = ccall(:ios_get_readable, Cint, (Ptr{Cvoid},), s.ios)!=0

"""
truncate(file,n)
truncate(file, n)

Resize the file or buffer given by the first argument to exactly `n` bytes, filling
previously unallocated space with '\\0' if the file or buffer is grown.
Expand All @@ -73,7 +73,7 @@ julia> write(io, "JuliaLang is a GitHub organization.");
julia> truncate(io, 40);

julia> String(take!(io))
"JuliaLang is a GitHub organization.\0\0\0\0\0"
"JuliaLang is a GitHub organization.\\0\\0\\0\\0\\0"
```
"""
function truncate(s::IOStream, n::Integer)
Expand Down Expand Up @@ -354,8 +354,8 @@ descriptor upon completion.
# Examples
```jldoctest
julia> open("myfile.txt", "w") do io
write(io, "Hello world!");
end
write(io, "Hello world!")
end;

julia> open(f->read(f, String), "myfile.txt")
"Hello world!"
Expand Down
2 changes: 1 addition & 1 deletion base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ See [`Base.filter`](@ref) for an eager implementation of filtering for arrays.
# Examples
```jldoctest
julia> f = Iterators.filter(isodd, [1, 2, 3, 4, 5])
Base.Iterators.Filter{Base.#isodd,Array{Int64,1}}(isodd, [1, 2, 3, 4, 5])
Base.Iterators.Filter{typeof(isodd),Array{Int64,1}}(isodd, [1, 2, 3, 4, 5])

julia> foreach(println, f)
1
Expand Down
2 changes: 1 addition & 1 deletion base/meta.jl
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ julia> Meta.parse("x = ")
:($(Expr(:incomplete, "incomplete: premature end of input")))

julia> Meta.parse("1.0.2")
ERROR: ParseError("invalid numeric constant \\\"1.0.\\\"")
ERROR: Base.Meta.ParseError("invalid numeric constant \\\"1.0.\\\"")
Stacktrace:
[...]

Expand Down
Loading