diff --git a/test/test_pretty_printing.jl b/test/test_pretty_printing.jl index 7752ab7..d2729c9 100644 --- a/test/test_pretty_printing.jl +++ b/test/test_pretty_printing.jl @@ -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 ┤■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■ 0.94 \n └ ┘ " +str = String(take!(iob)) +@test occursin("SparseCat", str) +@test occursin("", str) # test that it doesn't print 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("", str) diff --git a/test/test_sparse_cat.jl b/test/test_sparse_cat.jl index 17891fc..9d6cd81 100644 --- a/test/test_sparse_cat.jl +++ b/test/test_sparse_cat.jl @@ -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