Skip to content

Commit

Permalink
add type tests
Browse files Browse the repository at this point in the history
  • Loading branch information
t-bltg committed Dec 12, 2021
1 parent 141ad94 commit c085880
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 32 deletions.
24 changes: 12 additions & 12 deletions src/ticks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,7 @@ function optimize_ticks(x_min::T, x_max::T; extend_ticks::Bool=false,
coverage_weight::Float64=1/3, niceness_weight::Float64=1/4,
strict_span=true, span_buffer=nothing, scale=nothing) where T

F = float(T)
Qv = [(F(q[1]), F(q[2])) for q in Q]
Qv = [(Float64(q[1]), Float64(q[2])) for q in Q]
optimize_ticks_typed(x_min, x_max, extend_ticks, Qv, k_min, k_max, k_ideal,
granularity_weight, simplicity_weight,
coverage_weight, niceness_weight, strict_span, span_buffer, scale)
Expand All @@ -147,9 +146,11 @@ function optimize_ticks_typed(x_min::T, x_max::T, extend_ticks,
granularity_weight::Float64, simplicity_weight::Float64,
coverage_weight::Float64, niceness_weight::Float64,
strict_span, span_buffer, scale) where T
xspan = x_max - x_min

F = float(T)
xspan < eps(F) && return fallback_ticks(x_min, x_max, k_min, k_max)
if (xspan = x_max - x_min) < eps(F)
return fallback_ticks(x_min, x_max, k_min, k_max)
end

n = length(Q)
is_log_scale = scale _logScales
Expand Down Expand Up @@ -192,11 +193,12 @@ function optimize_ticks_typed(x_min::T, x_max::T, extend_ticks,
isfinite(r) || continue
r = ceil(Int, r)

has_nice_scale = true
if is_log_scale && !isinteger(tickspan)
# try to favor integer exponents for log scales
has_nice_scale = false
nice_scale = false
qscore = 0
else
nice_scale = true
end

while r * tickspan <= x_min
Expand Down Expand Up @@ -245,7 +247,7 @@ function optimize_ticks_typed(x_min::T, x_max::T, extend_ticks,
has_zero = r <= 0 && abs(r) < k

# simplicity
s = has_zero && has_nice_scale ? 1 : 0
s = has_zero && nice_scale ? 1 : 0

# granularity
g = 0 < length(S) < 2k_ideal ? 1 - abs(length(S) - k_ideal) / k_ideal : 0
Expand Down Expand Up @@ -299,14 +301,12 @@ function optimize_ticks_typed(x_min::T, x_max::T, extend_ticks,
end


function optimize_ticks(x_min::Date, x_max::Date; extend_ticks::Bool=false,
optimize_ticks(x_min::Date, x_max::Date; extend_ticks::Bool=false,
k_min=nothing, k_max=nothing, scale=:auto,
granularity_weight=nothing, simplicity_weight=nothing,
coverage_weight=nothing, niceness_weight=nothing,
strict_span=true, span_buffer = nothing)
return optimize_ticks(convert(DateTime, x_min), convert(DateTime, x_max),
extend_ticks=extend_ticks, scale=scale)
end
strict_span=true, span_buffer = nothing) =
optimize_ticks(convert(DateTime, x_min), convert(DateTime, x_max), extend_ticks=extend_ticks, scale=scale)


function optimize_ticks(x_min::DateTime, x_max::DateTime; extend_ticks::Bool=false,
Expand Down
50 changes: 30 additions & 20 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,42 +131,52 @@ end
end
end

@testset "PlotUtils.jl/issues/86" begin
let x = -1.0, y = 13.0
test_ticks(x, y, optimize_ticks(x, y, k_min = 4, k_max = 8)[1])
end
end

@testset "digits" begin
@testset "digits $((10^n) - 1)*10^$i" for n in 1:9, i in -9:9
y0 = 10^n
x0 = y0 - 1
x, y = (x0, y0) .* 10.0^i
ticks = optimize_ticks(x, y)[1]
ticks, = optimize_ticks(x, y)
test_ticks(x, y, ticks)
end
end

@testset "Plots.jl/issues/3859" begin
x, y = extrema([-1.7055509600077687e307, -1.3055509600077687e307, -1.e300])
test_ticks(x, y, optimize_ticks(x, y, k_min = 4, k_max = 8)[1])
@testset "types" begin
for T in (Int32, Int64, Float16, Float32, Float64)
x, y = T(1), T(10)
ticks, = optimize_ticks(x, y)
@test eltype(ticks) <: AbstractFloat
@test eltype(ticks) == (T <: AbstractFloat ? T : float(T))
end
end

@testset "PlotUtils.jl/issues/114" begin
let x = -.1eps(), y = .1eps()
test_ticks(x, y, optimize_ticks(x, y)[1])
@testset "issues" begin
@testset "PlotUtils.jl/issues/86" begin
let x = -1.0, y = 13.0
test_ticks(x, y, optimize_ticks(x, y, k_min = 4, k_max = 8)[1])
end
end

@testset "Plots.jl/issues/3859" begin
x, y = extrema([-1.7055509600077687e307, -1.3055509600077687e307, -1.e300])
test_ticks(x, y, optimize_ticks(x, y, k_min = 4, k_max = 8)[1])
end
end

@testset "PlotUtils.jl/issues/116" begin
let x = 4.5, y = 5.5
test_ticks(x, y, optimize_ticks(x, y, scale=:log10)[1])
@testset "PlotUtils.jl/issues/114" begin
let x = -.1eps(), y = .1eps()
test_ticks(x, y, optimize_ticks(x, y)[1])
end
end
let x = 2.5, y = 3.5
test_ticks(x, y, optimize_ticks(x, y, scale=:log2)[1])

@testset "PlotUtils.jl/issues/116" begin
let x = 4.5, y = 5.5
test_ticks(x, y, optimize_ticks(x, y, scale = :log10)[1])
end
let x = 2.5, y = 3.5
test_ticks(x, y, optimize_ticks(x, y, scale = :log2)[1])
end
end
end

end

# ----------------------
Expand Down

0 comments on commit c085880

Please sign in to comment.