Skip to content

Commit

Permalink
fixed pretty printing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zsunberg committed May 4, 2021
1 parent 54bfd6a commit ba3ab10
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
10 changes: 7 additions & 3 deletions test/test_pretty_printing.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
d = SparseCat([1,2], [0.5, 0.5])
@test sprint(showdistribution, d) == " SparseCat{Array{Int64,1},Array{Float64,1}} distribution\n ┌ ┐ \n 1 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 0.5 \n 2 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 0.5 \n └ ┘ "
@test occursin("SparseCat", sprint(showdistribution, d))

d = SparseCat(1:50, fill(1/50, 50))
iob = IOBuffer()
io = IOContext(iob, :limit=>true, :displaysize=>(10, 7))
showdistribution(io, d)
@test String(take!(iob)) == " SparseCat{UnitRange{Int64},Array{Float64,1}} distribution\n ┌ ┐ \n 1 ┤■ 0.02 \n 2 ┤■ 0.02 \n 3 ┤■ 0.02 \n <everything else> ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 0.94 \n └ ┘ "
str = String(take!(iob))
@test occursin("SparseCat", str)
@test occursin("<everything else>", str)

# test that it doesn't print <everything else> when there are enough lines
d = SparseCat([:a], 1.0)
iob = IOBuffer()
io = IOContext(iob, :limit=>true, :displaysize=>(10, 7))
showdistribution(io, d)
@test String(take!(iob)) == " SparseCat{Array{Symbol,1},Float64} distribution\n ┌ ┐ \n :a ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 1.0 \n └ ┘ "
str = String(take!(iob))
@test occursin("SparseCat", str)
@test !occursin("<everything else>", str)
12 changes: 12 additions & 0 deletions test/test_sparse_cat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,19 @@ let
@test Random.gentype(dt) == Symbol
@test Random.gentype(typeof(dt)) == Symbol
@inferred rand(Random.GLOBAL_RNG, dt)

# rand(::SparseCat)
samples = Symbol[]
N = 100_000
@time for i in 1:N
push!(samples, rand(d))
end
@test isapprox(count(samples.==:a)/N, pdf(d,:a), atol=0.005)
@test isapprox(count(samples.==:b)/N, pdf(d,:b), atol=0.005)
@test isapprox(count(samples.==:c)/N, pdf(d,:c), atol=0.005)
@test isapprox(count(samples.==:d)/N, pdf(d,:d), atol=0.005)

# rand(rng, ::SparseCat)
rng = MersenneTwister(14)
samples = Symbol[]
N = 100_000
Expand Down

2 comments on commit ba3ab10

@zsunberg
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/36049

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.7 -m "<description of version>" ba3ab10864bdbefd0e3d4bee6aa0ba523c935877
git push origin v0.3.7

Please sign in to comment.