Skip to content

Commit

Permalink
add examples and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet committed Jun 15, 2017
1 parent 97daba3 commit 1ff58f5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
12 changes: 12 additions & 0 deletions base/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1479,6 +1479,18 @@ from `chars` if specified, and of upper- and lower-case letters and
the digits 0-9 otherwise. The optional `rng` argument specifies a
random number generator, see [Random Numbers](@ref).
# Examples
```julia-repl
julia> randstring()
"c03rgKi1"
julia> randstring(MersenneTwister(0), 'a':'z', 6)
"wijzek"
julia> randstring("gcat")
"tgtcaatc"
```
!!! note
The collection `chars` can be any collection of characters
(of type `Char` or `UInt8`)
Expand Down
18 changes: 12 additions & 6 deletions test/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -539,15 +539,21 @@ let g = Base.Random.GLOBAL_RNG,
end

# test randstring API
let b = ['0':'9';'A':'Z';'a':'z'],
c = 'a':'z'
let b = ['0':'9';'A':'Z';'a':'z']
for rng = [[], [MersenneTwister(0)]]
@test length(randstring(rng...)) == 8
@test length(randstring(rng..., 20)) == 20
@test issubset(randstring(rng...), b)
@test issubset(randstring(rng..., c), c)
s = randstring(rng..., c, 20)
@test length(s) == 20
@test issubset(s, c)
for c = ['a':'z', "qwèrtï", Set(Vector{UInt8}("gcat"))],
len = [8, 20]
s = len == 8 ? randstring(rng..., c) : randstring(rng..., c, len)
@test length(s) == len
if eltype(c) == Char
@test issubset(s, c)
else # UInt8
@test issubset(s, map(Char, c))
end
end
end
@test randstring(MersenneTwister(0)) == randstring(MersenneTwister(0), b)
end

0 comments on commit 1ff58f5

Please sign in to comment.