Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests #8

Merged
merged 3 commits into from
Apr 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*.jl.cov
*.jl.*.cov
*.jl.mem
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ julia:
- nightly
notifications:
email: false
after_success:
- julia -e 'cd(Pkg.dir("Showoff")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder())';
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Showoff

[![Build
Status](https://travis-ci.org/JuliaGraphics/Showoff.jl.svg?branch=master)](https://travis-ci.org/JuliaGraphics/Showoff.jl)
[![Showoff](http://pkg.julialang.org/badges/Showoff_0.5.svg)](http://pkg.julialang.org/?pkg=Showoff)
[![Showoff](http://pkg.julialang.org/badges/Showoff_0.6.svg)](http://pkg.julialang.org/?pkg=Showoff)
[![Build Status](https://travis-ci.org/JuliaGraphics/Showoff.jl.svg?branch=master)](https://travis-ci.org/JuliaGraphics/Showoff.jl)
[![Coverage Status](https://coveralls.io/repos/github/JuliaGraphics/Showoff.jl/badge.svg?branch=master)](https://coveralls.io/github/JuliaGraphics/Showoff.jl?branch=master)

Showoff provides an interface for consistently formatting an array of n things,
e.g. numbers, dates, unitful values. It's used in Gadfly to
Expand Down Expand Up @@ -58,4 +60,4 @@ trailing the `.`, and look nice when right-aligned.

When no specialized `showoff` is defined, it falls back on the `show` function.


This package was originally written by [Daniel C. Jones](https://github.com/dcjones).
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
julia 0.5
Compat 0.9.5
Compat 0.18.0
187 changes: 86 additions & 101 deletions src/Showoff.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
VERSION >= v"0.4.0-dev+6641" && __precompile__()
__precompile__()

module Showoff

using Compat
if VERSION >= v"0.6.0-dev.1015"
import Base.Iterators: drop
end
import Compat.Iterators: drop
Copy link
Contributor

Choose a reason for hiding this comment

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

need a higher min Compat version for this

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call, thanks


export showoff

Expand All @@ -17,23 +15,13 @@ end


function grisu(v::AbstractFloat, mode, requested_digits)
if VERSION < v"0.4-dev"
if isa(v, Float32) && mode == Base.Grisu.SHORTEST
mode = Base.Grisu.SHORTEST_SINGLE
end
@grisu_ccall v mode requested_digits
return Base.Grisu.LEN[1], Base.Grisu.POINT[1], Base.Grisu.NEG, Base.Grisu.DIGITS
elseif VERSION < v"0.5.0-dev+2094"
return Base.Grisu.grisu(v, mode, requested_digits)
else
return tuple(Base.Grisu.grisu(v, mode, requested_digits)..., Base.Grisu.DIGITS)
end
return tuple(Base.Grisu.grisu(v, mode, requested_digits)..., Base.Grisu.DIGITS)
end


# Fallback
function showoff(xs::AbstractArray, style=:none)
result = Array(AbstractString, length(xs))
result = Vector{String}(length(xs))
buf = IOBuffer()
for (i, x) in enumerate(xs)
show(buf, x)
Expand All @@ -48,7 +36,7 @@ end

function concrete_minimum(xs)
if done(xs, start(xs))
error("argument must not be empty")
throw(ArgumentError("argument must not be empty"))
end

x_min = first(xs)
Expand All @@ -70,7 +58,7 @@ end

function concrete_maximum(xs)
if done(xs, start(xs))
error("argument must not be empty")
throw(ArgumentError("argument must not be empty"))
end

x_max = first(xs)
Expand Down Expand Up @@ -103,7 +91,7 @@ end

function scientific_precision_heuristic{T <: AbstractFloat}(xs::AbstractArray{T})
ys = [x == 0.0 ? 0.0 : x / 10.0^floor(log10(abs(x)))
for x in filter(isfinite, xs)]
for x in xs if isfinite(x)]
return plain_precision_heuristic(ys) + 1
end

Expand All @@ -115,7 +103,7 @@ function showoff{T <: AbstractFloat}(xs::AbstractArray{T}, style=:auto)
x_max = Float64(Float32(x_max))

if !isfinite(x_min) || !isfinite(x_max)
error("At least one finite value must be provided to formatter.")
throw(ArgumentError("At least one finite value must be provided to formatter."))
end

if style == :auto
Expand All @@ -128,17 +116,17 @@ function showoff{T <: AbstractFloat}(xs::AbstractArray{T}, style=:auto)

if style == :plain
precision = plain_precision_heuristic(xs)
return AbstractString[format_fixed(x, precision) for x in xs]
return String[format_fixed(x, precision) for x in xs]
elseif style == :scientific
precision = scientific_precision_heuristic(xs)
return AbstractString[format_fixed_scientific(x, precision, false)
return String[format_fixed_scientific(x, precision, false)
for x in xs]
elseif style == :engineering
precision = scientific_precision_heuristic(xs)
return AbstractString[format_fixed_scientific(x, precision, true)
return String[format_fixed_scientific(x, precision, true)
for x in xs]
else
error("$(style) is not a recongnized number format")
throw(ArgumentError("$(style) is not a recongnized number format"))
end
end

Expand Down Expand Up @@ -274,91 +262,88 @@ function format_fixed_scientific(x::AbstractFloat, precision::Integer,
end



if VERSION >= v"0.4-dev"
function showoff{T <: (Union{Date, DateTime})}(ds::AbstractArray{T}, style=:none)
years = Set()
months = Set()
days = Set()
hours = Set()
minutes = Set()
seconds = Set()
for d in ds
push!(years, Dates.year(d))
push!(months, Dates.month(d))
push!(days, Dates.day(d))
push!(hours, Dates.hour(d))
push!(minutes, Dates.minute(d))
push!(seconds, Dates.second(d))
end
all_same_year = length(years) == 1
all_one_month = length(months) == 1 && 1 in months
all_one_day = length(days) == 1 && 1 in days
all_zero_hour = length(hours) == 1 && 0 in hours
all_zero_minute = length(minutes) == 1 && 0 in minutes
all_zero_seconds = length(minutes) == 1 && 0 in minutes
all_zero_milliseconds = length(minutes) == 1 && 0 in minutes

# first label format
label_months = false
label_days = false
f1 = "u d, yyyy"
f2 = ""
if !all_zero_seconds
f2 = "HH:MM:SS.sss"
elseif !all_zero_seconds
f2 = "HH:MM:SS"
elseif !all_zero_hour || !all_zero_minute
f2 = "HH:MM"
else
if !all_one_day
first_label_format = "u d yyyy"
elseif !all_one_month
first_label_format = "u yyyy"
elseif !all_one_day
first_label_format = "yyyy"
end
end
if f2 != ""
first_label_format = string(f1, " ", f2)
else
first_label_format = f1
function showoff{T <: (Union{Date, DateTime})}(ds::AbstractArray{T}, style=:none)
years = Set()
months = Set()
days = Set()
hours = Set()
minutes = Set()
seconds = Set()
for d in ds
push!(years, Dates.year(d))
push!(months, Dates.month(d))
push!(days, Dates.day(d))
push!(hours, Dates.hour(d))
push!(minutes, Dates.minute(d))
push!(seconds, Dates.second(d))
end
all_same_year = length(years) == 1
all_one_month = length(months) == 1 && 1 in months
all_one_day = length(days) == 1 && 1 in days
all_zero_hour = length(hours) == 1 && 0 in hours
all_zero_minute = length(minutes) == 1 && 0 in minutes
all_zero_seconds = length(minutes) == 1 && 0 in minutes
all_zero_milliseconds = length(minutes) == 1 && 0 in minutes

# first label format
label_months = false
label_days = false
f1 = "u d, yyyy"
f2 = ""
if !all_zero_seconds
f2 = "HH:MM:SS.sss"
elseif !all_zero_seconds
f2 = "HH:MM:SS"
elseif !all_zero_hour || !all_zero_minute
f2 = "HH:MM"
else
if !all_one_day
first_label_format = "u d yyyy"
elseif !all_one_month
first_label_format = "u yyyy"
elseif !all_one_day
first_label_format = "yyyy"
end
end
if f2 != ""
first_label_format = string(f1, " ", f2)
else
first_label_format = f1
end

labels = Array(AbstractString, length(ds))
labels[1] = Dates.format(ds[1], first_label_format)
d_last = ds[1]
for (i, d) in enumerate(ds[2:end])
if Dates.year(d) != Dates.year(d_last)
if all_one_day && all_one_month
f1 = "yyyy"
elseif all_one_day && !all_one_month
f1 = "u yyyy"
else
f1 = "u d, yyyy"
end
elseif Dates.month(d) != Dates.month(d_last)
f1 = all_one_day ? "u" : "u d"
elseif Dates.day(d) != Dates.day(d_last)
f1 = "d"
else
f1 = ""
end

if f2 != ""
f = string(f1, " ", f2)
elseif f1 != ""
f = f1
labels = Vector{String}(length(ds))
labels[1] = Dates.format(ds[1], first_label_format)
d_last = ds[1]
for (i, d) in enumerate(ds[2:end])
if Dates.year(d) != Dates.year(d_last)
if all_one_day && all_one_month
f1 = "yyyy"
elseif all_one_day && !all_one_month
f1 = "u yyyy"
else
f = first_label_format
f1 = "u d, yyyy"
end
elseif Dates.month(d) != Dates.month(d_last)
f1 = all_one_day ? "u" : "u d"
elseif Dates.day(d) != Dates.day(d_last)
f1 = "d"
else
f1 = ""
end

labels[i+1] = Dates.format(d, f)
d_last = d
if f2 != ""
f = string(f1, " ", f2)
elseif f1 != ""
f = f1
else
f = first_label_format
end

return labels
labels[i+1] = Dates.format(d, f)
d_last = d
end

return labels
end


Expand Down
47 changes: 45 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,48 @@
using Showoff
using Base.Test

# write your own tests here
@test 1 == 1
@testset "Internals" begin
@test Showoff.@grisu_ccall(1, 2, 3) === nothing
@test Showoff.grisu(1.0, Base.Grisu.SHORTEST, 2) == (1, 1, false, Base.Grisu.DIGITS)

let x = [1.0, Inf, 2.0, NaN]
@test Showoff.concrete_minimum(x) == 1.0
@test Showoff.concrete_maximum(x) == 2.0
end

@test_throws ArgumentError Showoff.concrete_minimum([])
@test_throws ArgumentError Showoff.concrete_maximum([])

let x = [1.12345, 4.5678]
@test Showoff.plain_precision_heuristic(x) == 5
@test Showoff.scientific_precision_heuristic(x) == 6
end
end

@testset "Formatting" begin
@test Showoff.format_fixed(-10.0, 0) == "-10"
@test Showoff.format_fixed(0.012345, 3) == "0.012"
@test Showoff.format_fixed(Inf, 1) == "∞"
@test Showoff.format_fixed(-Inf, 1) == "-∞"
@test Showoff.format_fixed(NaN, 1) == "NaN"
@test Showoff.format_fixed_scientific(0.0, 1, false) == "0"
@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.34568×10⁻³"
@test Showoff.format_fixed_scientific(0.012345678, 4, false) == "1.234568×10⁻²"
@test Showoff.format_fixed_scientific(-10.0, 4, false) == "-1.000×10¹"
end

@testset "Showoff" begin
x = [1.12345, 4.5678]
@test showoff(x) == ["1.12345", "4.56780"]
@test showoff([0.0, 50000.0]) == ["0", "5×10⁴"]
@test showoff(x, :plain) == ["1.12345", "4.56780"]
@test showoff(x, :scientific) == ["1.12345×10⁰", "4.56780×10⁰"]
@test showoff(x, :engineering) == ["1.12345×10⁰", "4.56780×10⁰"]
@test showoff([Dates.DateTime("2017-04-11", "yyyy-mm-dd")]) == ["Apr 11, 2017"]
@test showoff(["a", "b"]) == ["\"a\"", "\"b\""]
@test_throws ArgumentError showoff(x, :nevergonnagiveyouup)
@test_throws ArgumentError showoff([Inf, Inf, NaN])
end