Skip to content

Commit

Permalink
Allow tests to vary by Grisu/Ryu
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Feb 22, 2021
1 parent 43e9505 commit 8798a8d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Showoff.jl
Expand Up @@ -74,7 +74,7 @@ function concrete_maximum(xs)
end

function scientific_precision_heuristic(xs::AbstractArray{<:AbstractFloat})
ys = [x == 0.0 ? 0.0 : x / 10.0^floor(log10(abs(x)))
ys = [x == 0.0 ? 0.0 : round(10.0 ^ (z = log10(abs(Float64(x))); z - floor(z)); sigdigits=15)
for x in xs if isfinite(x)]
return plain_precision_heuristic(ys) + 1
end
Expand Down
26 changes: 15 additions & 11 deletions test/runtests.jl
Expand Up @@ -3,6 +3,8 @@ using Test
using Dates
using Printf

const drops0s = !isdefined(Base, :Ryu)

@testset "Internals" begin
@test Showoff.@grisu_ccall(1, 2, 3) === nothing
if isdefined(Showoff, :Grisu)
Expand Down Expand Up @@ -33,25 +35,27 @@ end
@test Showoff.format_fixed_scientific(Inf, 1, false) == ""
@test Showoff.format_fixed_scientific(-Inf, 1, false) == "-∞"
@test Showoff.format_fixed_scientific(NaN, 1, false) == "NaN"
@test Showoff.format_fixed_scientific(0.012345678, 4, true) == "12.346×10⁻³"
@test Showoff.format_fixed_scientific(0.012345678, 4, false) == "1.2346×10⁻²"
@test Showoff.format_fixed_scientific(-10.0, 4, false) == "-1.0000×10¹"
@test Showoff.format_fixed_scientific(-10.0, 4, true) == "-10.000×10⁰"
@test Showoff.format_fixed_scientific(-10.0, 4, false)[1:end-5] == @sprintf("%0.4e", -10.0)[1:end-4]
@test Showoff.format_fixed_scientific(1.23456e7, 3, false)[1:end-5] == @sprintf("%0.3e", 1.23456e7)[1:end-4]
@test Showoff.format_fixed_scientific(2.99999999999999956E-16, 2, false) == "3.00×10⁻¹⁶"
if isdefined(Base, :Ryu)
@test Showoff.format_fixed_scientific(0.012345678, 4, true) == "12.346×10⁻³"
@test Showoff.format_fixed_scientific(0.012345678, 4, false) == "1.2346×10⁻²"
@test Showoff.format_fixed_scientific(-10.0, 4, false) == "-1.0000×10¹"
@test Showoff.format_fixed_scientific(-10.0, 4, true) == "-10.000×10⁰"
@test Showoff.format_fixed_scientific(-10.0, 4, false)[1:end-5] == @sprintf("%0.4e", -10.0)[1:end-4]
@test Showoff.format_fixed_scientific(1.23456e7, 3, false)[1:end-5] == @sprintf("%0.3e", 1.23456e7)[1:end-4]
@test Showoff.format_fixed_scientific(2.99999999999999956E-16, 2, false) == "3.00×10⁻¹⁶"
end
end

@testset "Showoff" begin
x = [1.12345, 4.5678]
@test showoff(x) == ["1.12345", "4.56780"]
@test showoff([0.0, 50000.0]) == ["0", "5.0×10⁴"]
@test showoff([0.0, 50000.0]) == (drops0s ? ["0", "5×10⁴"] : ["0", "5.0×10⁴"])
@test showoff(x, :plain) == ["1.12345", "4.56780"]
@test showoff(x, :scientific) == ["1.123450×10⁰", "4.567800×10⁰"]
@test showoff(x, :engineering) == ["1.123450×10⁰", "4.567800×10⁰"]
@test showoff(x, :scientific) == (drops0s ? ["1.12345×10⁰", "4.56780×10⁰"] : ["1.123450×10⁰", "4.567800×10⁰"])
@test showoff(x, :engineering) == showoff(x, :scientific)
@test showoff([DateTime("2017-04-11", "yyyy-mm-dd")]) == ["Apr 11, 2017"]
@test showoff(["a", "b"]) == ["\"a\"", "\"b\""]
@test showoff([1, 1e39]) == ["1.0×10⁰", "1.0×10³⁹"]
@test showoff([1, 1e39]) == (drops0s ? ["1×10⁰", "1×10³⁹"] : ["1.0×10⁰", "1.0×10³⁹"])
@test_throws ArgumentError showoff(x, :nevergonnagiveyouup)
@test_throws ArgumentError showoff([Inf, Inf, NaN])
end

0 comments on commit 8798a8d

Please sign in to comment.