Skip to content

Commit cb1aae9

Browse files
authored
a bunch of random doctests (#20608)
* add more doctests * fixup bugged doctests * fixup line numbers and printing * fix indices [ci skip] * fix type -> mutable struct [ci skip]
1 parent a3ebe1a commit cb1aae9

24 files changed

+259
-48
lines changed

base/abstractarray.jl

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,22 @@ if they need to provide custom bounds checking behaviors; however, in
301301
many cases one can rely on `A`'s indices and [`checkindex`](@ref).
302302
303303
See also [`checkindex`](@ref).
304+
305+
```jldoctest
306+
julia> A = rand(3, 3);
307+
308+
julia> checkbounds(Bool, A, 2)
309+
true
310+
311+
julia> checkbounds(Bool, A, 3, 4)
312+
false
313+
314+
julia> checkbounds(Bool, A, 1:3)
315+
true
316+
317+
julia> checkbounds(Bool, A, 1:3, 2:4)
318+
false
319+
```
304320
"""
305321
function checkbounds(::Type{Bool}, A::AbstractArray, I...)
306322
@_inline_meta
@@ -346,6 +362,8 @@ bounds-check for a single dimension of the array.
346362
There are two important exceptions to the 1-1 rule: linear indexing and
347363
CartesianIndex{N}, both of which may "consume" more than one element
348364
of `IA`.
365+
366+
See also [`checkbounds`](@ref).
349367
"""
350368
function checkbounds_indices(::Type{Bool}, IA::Tuple, I::Tuple)
351369
@_inline_meta
@@ -443,25 +461,31 @@ default is an `Array{element_type}(dims...)`.
443461
For example, `similar(1:10, 1, 4)` returns an uninitialized `Array{Int,2}` since ranges are
444462
neither mutable nor support 2 dimensions:
445463
446-
julia> similar(1:10, 1, 4)
447-
1×4 Array{Int64,2}:
448-
4419743872 4374413872 4419743888 0
464+
```julia
465+
julia> similar(1:10, 1, 4)
466+
1×4 Array{Int64,2}:
467+
4419743872 4374413872 4419743888 0
468+
```
449469
450470
Conversely, `similar(trues(10,10), 2)` returns an uninitialized `BitVector` with two
451471
elements since `BitArray`s are both mutable and can support 1-dimensional arrays:
452472
453-
julia> similar(trues(10,10), 2)
454-
2-element BitArray{1}:
455-
false
456-
false
473+
```julia
474+
julia> similar(trues(10,10), 2)
475+
2-element BitArray{1}:
476+
false
477+
false
478+
```
457479
458480
Since `BitArray`s can only store elements of type `Bool`, however, if you request a
459481
different element type it will create a regular `Array` instead:
460482
461-
julia> similar(falses(10), Float64, 2, 4)
462-
2×4 Array{Float64,2}:
463-
2.18425e-314 2.18425e-314 2.18425e-314 2.18425e-314
464-
2.18425e-314 2.18425e-314 2.18425e-314 2.18425e-314
483+
```julia
484+
julia> similar(falses(10), Float64, 2, 4)
485+
2×4 Array{Float64,2}:
486+
2.18425e-314 2.18425e-314 2.18425e-314 2.18425e-314
487+
2.18425e-314 2.18425e-314 2.18425e-314 2.18425e-314
488+
```
465489
466490
"""
467491
similar{T}(a::AbstractArray{T}) = similar(a, T)
@@ -675,6 +699,17 @@ Make a mutable copy of an array or iterable `a`. For `a::Array`,
675699
this is equivalent to `copy(a)`, but for other array types it may
676700
differ depending on the type of `similar(a)`. For generic iterables
677701
this is equivalent to `collect(a)`.
702+
703+
```jldoctest
704+
julia> tup = (1, 2, 3)
705+
(1, 2, 3)
706+
707+
julia> Base.copymutable(tup)
708+
3-element Array{Int64,1}:
709+
1
710+
2
711+
3
712+
```
678713
"""
679714
function copymutable(a::AbstractArray)
680715
@_propagate_inbounds_meta
@@ -1629,7 +1664,7 @@ needed, for example in `foreach(println, array)`.
16291664
```jldoctest
16301665
julia> a = 1:3:7;
16311666
1632-
julia> foreach(x->println(x^2),a)
1667+
julia> foreach(x -> println(x^2), a)
16331668
1
16341669
16
16351670
49
@@ -1783,7 +1818,7 @@ Transform collection `c` by applying `f` to each element. For multiple collectio
17831818
apply `f` elementwise.
17841819
17851820
```jldoctest
1786-
julia> map((x) -> x * 2, [1, 2, 3])
1821+
julia> map(x -> x * 2, [1, 2, 3])
17871822
3-element Array{Int64,1}:
17881823
2
17891824
4
@@ -1823,6 +1858,18 @@ end
18231858
18241859
Like [`map`](@ref), but stores the result in `destination` rather than a new
18251860
collection. `destination` must be at least as large as the first collection.
1861+
1862+
```jldoctest
1863+
julia> x = zeros(3);
1864+
1865+
julia> map!(x -> x * 2, x, [1, 2, 3]);
1866+
1867+
julia> x
1868+
3-element Array{Float64,1}:
1869+
2.0
1870+
4.0
1871+
6.0
1872+
```
18261873
"""
18271874
map!{F}(f::F, dest::AbstractArray, As::AbstractArray...) = map_n!(f, dest, As)
18281875

base/array.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,8 @@ julia> deleteat!([6, 5, 4, 3, 2, 1], [true, false, true, false, true, false])
838838
julia> deleteat!([6, 5, 4, 3, 2, 1], (2, 2))
839839
ERROR: ArgumentError: indices must be unique and sorted
840840
Stacktrace:
841-
[1] deleteat!(::Array{Int64,1}, ::Tuple{Int64,Int64}) at ./array.jl:808
841+
[1] _deleteat!(::Array{Int64,1}, ::Tuple{Int64,Int64}) at ./array.jl:858
842+
[2] deleteat!(::Array{Int64,1}, ::Tuple{Int64,Int64}) at ./array.jl:845
842843
```
843844
"""
844845
deleteat!(a::Vector, inds) = _deleteat!(a, inds)

base/associative.jl

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,20 @@ end
122122
123123
Update collection with pairs from the other collections.
124124
See also [`merge`](@ref).
125+
126+
```jldoctest
127+
julia> d1 = Dict(1 => 2, 3 => 4);
128+
129+
julia> d2 = Dict(1 => 4, 4 => 5);
130+
131+
julia> merge!(d1, d2);
132+
133+
julia> d1
134+
Dict{Int64,Int64} with 3 entries:
135+
4 => 5
136+
3 => 4
137+
1 => 4
138+
```
125139
"""
126140
function merge!(d::Associative, others::Associative...)
127141
for other in others
@@ -145,6 +159,11 @@ end
145159
keytype(type)
146160
147161
Get the key type of an associative collection type. Behaves similarly to [`eltype`](@ref).
162+
163+
```jldoctest
164+
julia> keytype(Dict(Int32(1) => "foo"))
165+
Int32
166+
```
148167
"""
149168
keytype{K,V}(::Type{Associative{K,V}}) = K
150169
keytype(a::Associative) = keytype(typeof(a))
@@ -154,6 +173,11 @@ keytype{A<:Associative}(::Type{A}) = keytype(supertype(A))
154173
valtype(type)
155174
156175
Get the value type of an associative collection type. Behaves similarly to [`eltype`](@ref).
176+
177+
```jldoctest
178+
julia> valtype(Dict(Int32(1) => "foo"))
179+
String
180+
```
157181
"""
158182
valtype{K,V}(::Type{Associative{K,V}}) = V
159183
valtype{A<:Associative}(::Type{A}) = valtype(supertype(A))

base/base64.jl

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@ Returns a new write-only I/O stream, which converts any bytes written to it into
2121
base64-encoded ASCII bytes written to `ostream`.
2222
Calling [`close`](@ref) on the `Base64EncodePipe` stream
2323
is necessary to complete the encoding (but does not close `ostream`).
24+
25+
```jldoctest
26+
julia> io = IOBuffer();
27+
28+
julia> iob64_encode = Base64EncodePipe(io);
29+
30+
julia> write(iob64_encode, "Hello!")
31+
6
32+
33+
julia> close(iob64_encode);
34+
35+
julia> str = String(take!(io))
36+
"SGVsbG8h"
37+
38+
julia> String(base64decode(str))
39+
"Hello!"
40+
```
2441
"""
2542
mutable struct Base64EncodePipe <: IO
2643
io::IO
@@ -171,6 +188,8 @@ Given a [`write`](@ref)-like function `writefunc`, which takes an I/O stream as
171188
string, and returns the string. `base64encode(args...)` is equivalent to `base64encode(write, args...)`:
172189
it converts its arguments into bytes using the standard [`write`](@ref) functions and returns the
173190
base64-encoded string.
191+
192+
See also [`base64decode`](@ref).
174193
"""
175194
function base64encode(f::Function, args...)
176195
s = IOBuffer()
@@ -187,6 +206,20 @@ base64encode(x...) = base64encode(write, x...)
187206
Base64DecodePipe(istream)
188207
189208
Returns a new read-only I/O stream, which decodes base64-encoded data read from `istream`.
209+
210+
```jldoctest
211+
julia> io = IOBuffer();
212+
213+
julia> iob64_decode = Base64DecodePipe(io);
214+
215+
julia> write(io, "SGVsbG8h")
216+
0x0000000000000008
217+
218+
julia> seekstart(io);
219+
220+
julia> String(read(iob64_decode))
221+
"Hello!"
222+
```
190223
"""
191224
mutable struct Base64DecodePipe <: IO
192225
io::IO
@@ -225,6 +258,22 @@ close(b::Base64DecodePipe) = nothing
225258
base64decode(string)
226259
227260
Decodes the base64-encoded `string` and returns a `Vector{UInt8}` of the decoded bytes.
261+
262+
See also [`base64encode`](@ref)
263+
264+
```jldoctest
265+
julia> b = base64decode("SGVsbG8h")
266+
6-element Array{UInt8,1}:
267+
0x48
268+
0x65
269+
0x6c
270+
0x6c
271+
0x6f
272+
0x21
273+
274+
julia> String(b)
275+
"Hello!"
276+
```
228277
"""
229278
function base64decode(s)
230279
b = IOBuffer(s)

base/bool.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ false
2626
julia> !false
2727
true
2828
29-
julia> ![true false true]
29+
julia> .![true false true]
3030
1×3 BitArray{2}:
3131
false true false
3232
```

base/cartesian.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,22 @@ export @nloops, @nref, @ncall, @nexprs, @nextract, @nall, @nany, @ntuple, @nif
1313
1414
Generate `N` nested loops, using `itersym` as the prefix for the iteration variables.
1515
`rangeexpr` may be an anonymous-function expression, or a simple symbol `var` in which case
16-
the range is `1:size(var,d)` for dimension `d`.
16+
the range is `indices(var, d)` for dimension `d`.
1717
1818
Optionally, you can provide "pre" and "post" expressions. These get executed first and last,
1919
respectively, in the body of each loop. For example:
2020
21-
@nloops 2 i A d->j_d=min(i_d,5) begin
21+
@nloops 2 i A d -> j_d = min(i_d, 5) begin
2222
s += @nref 2 A j
2323
end
2424
2525
would generate:
2626
27-
for i_2 = 1:size(A, 2)
27+
for i_2 = indices(A, 2)
2828
j_2 = min(i_2, 5)
29-
for i_1 = 1:size(A, 1)
29+
for i_1 = indices(A, 1)
3030
j_1 = min(i_1, 5)
31-
s += A[j_1,j_2]
31+
s += A[j_1, j_2]
3232
end
3333
end
3434

base/docs/helpdb/Base.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ If `T` does not have a specific size, an error is thrown.
341341
julia> sizeof(Base.LinAlg.LU)
342342
ERROR: argument is an abstract type; size is indeterminate
343343
Stacktrace:
344-
[1] sizeof(::Type{T} where T) at ./essentials.jl:147
344+
[1] sizeof(::Type{T} where T) at ./essentials.jl:160
345345
```
346346
"""
347347
sizeof(::Type)

base/essentials.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ end
215215
Eliminates array bounds checking within expressions.
216216
217217
In the example below the bound check of array A is skipped to improve performance.
218+
218219
```julia
219220
function sum(A::AbstractArray)
220221
r = zero(eltype(A))
@@ -224,6 +225,7 @@ function sum(A::AbstractArray)
224225
return r
225226
end
226227
```
228+
227229
!!! Warning
228230
229231
Using `@inbounds` may return incorrect results/crashes/corruption
@@ -281,6 +283,25 @@ getindex(v::SimpleVector, I::AbstractArray) = Core.svec(Any[ v[i] for i in I ]..
281283
282284
Tests whether the given array has a value associated with index `i`. Returns `false`
283285
if the index is out of bounds, or has an undefined reference.
286+
287+
```jldoctest
288+
julia> isassigned(rand(3, 3), 5)
289+
true
290+
291+
julia> isassigned(rand(3, 3), 3 * 3 + 1)
292+
false
293+
294+
julia> mutable struct Foo end
295+
296+
julia> v = similar(rand(3), Foo)
297+
3-element Array{Foo,1}:
298+
#undef
299+
#undef
300+
#undef
301+
302+
julia> isassigned(v, 1)
303+
false
304+
```
284305
"""
285306
function isassigned end
286307

base/expr.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ Small functions typically do not need the `@inline` annotation,
117117
as the compiler does it automatically. By using `@inline` on bigger functions,
118118
an extra nudge can be given to the compiler to inline it.
119119
This is shown in the following example:
120+
120121
```julia
121122
@inline function bigfunction(x)
122123
#=
@@ -137,6 +138,7 @@ Prevents the compiler from inlining a function.
137138
Small functions are typically inlined automatically.
138139
By using `@noinline` on small functions, auto-inlining can be
139140
prevented. This is shown in the following example:
141+
140142
```julia
141143
@noinline function smallfunction(x)
142144
#=

base/int.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ resulting in the return of a negative value. This overflow occurs only
9494
when `abs` is applied to the minimum representable value of a signed
9595
integer. That is, when `x == typemin(typeof(x))`, `abs(x) == x < 0`,
9696
not `-x` as might be expected.
97+
98+
```jldoctest
99+
julia> abs(-3)
100+
3
101+
102+
julia> abs(1 + im)
103+
1.4142135623730951
104+
105+
julia> abs(typemin(Int64))
106+
-9223372036854775808
107+
```
97108
"""
98109
function abs end
99110

0 commit comments

Comments
 (0)