Skip to content

Commit 5080788

Browse files
committed
Add testset for format "g/G"
g/G currently lacks a testset. This testset is currently non-compilant to Python's format_spec as trailing zeros are retained. Added FIXME to remind future maintainers.
1 parent 783608a commit 5080788

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

test/fmtspec.jl

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,58 @@ end
344344
@test pyfmt(".1e", 0.0006) == "6.0e-04"
345345
end
346346

347+
# This testset is currently non-compliant to Python's format_spec
348+
# Trailing insignificant zeros should be removed, however it is not done in
349+
# the current implementation.
350+
# FIXME: Make g/G compliant with Python's behavior
351+
@testset "Format general (g/G)" begin
352+
# Default precision (6 significant digits)
353+
@test pyfmt("g", 123.456789) == "123.457"
354+
@test pyfmt("g", 1.234567e-5) == "1.23457e-05"
355+
356+
# Switch between fixed and scientific depending on precision
357+
# and exponent -> Issue #91 (Format.jl)
358+
@test pyfmt(".3g", 0.0012345) == "0.00123"
359+
@test pyfmt(".3g", 1234567.0) == "1.23e+06"
360+
@test pyfmt(".2g", 0.000012345) == "1.2e-05"
361+
@test pyfmt(".2g", 1e-4) == "0.00010"
362+
@test pyfmt("g", 1e5) == "100000"
363+
@test pyfmt("g", 1e6) == "1.00000e+06"
364+
365+
# Trailing zero and decimal point trimming
366+
@test pyfmt(".6g", 1200.0) == "1200.00"
367+
@test pyfmt(".4g", 12.0) == "12.00"
368+
369+
# Alternate form '#' keeps trailing zeros and decimal point
370+
@test pyfmt("#.4g", 12.0) == "12.00"
371+
@test pyfmt("#.3g", 1.0e-5) == "1.00e-05"
372+
373+
# Width, alignment, and zero-padding
374+
@test pyfmt(">8.3g", 13.89) == " 13.9"
375+
@test pyfmt("⋆<8.3g", 13.89) == "13.9⋆⋆⋆⋆"
376+
@test pyfmt("⋆^8.3g", 13.89) == "⋆⋆13.9⋆⋆"
377+
@test pyfmt("010.3g", 13.89) == "00000013.9"
378+
@test pyfmt("010.3g", -13.89) == "-0000013.9"
379+
@test pyfmt("+010.3g", 13.89) == "+0000013.9"
380+
381+
# Rounding across powers of ten
382+
@test_broken pyfmt(".3g", 9.999) == "10.0"
383+
@test pyfmt(".3g", 9.999e9) == "1.00e+10"
384+
@test pyfmt(".3g", -9.999e-5) == "-1.00e-04"
385+
@test pyfmt(".3g", -9.999e-6) == "-1.00e-05"
386+
387+
# 'G' behaves like 'g' but uses 'E'
388+
@test pyfmt(".3G", 1234567.0) == "1.23E+06"
389+
@test pyfmt(".3G", 0.000012345) == "1.23E-05"
390+
@test pyfmt("G", 12.0) == "12.0000"
391+
@test pyfmt("#.4G", 12.0) == "12.00"
392+
@test pyfmt("G", 1e6) == "1.00000E+06"
393+
394+
# Zeros, from regression of (04baaf3)
395+
@test pyfmt("g", 0.0) == "0.00000"
396+
@test pyfmt("g", -0.0) == "-0.00000"
397+
end
398+
347399
@testset "Format percentage (%)" begin
348400
@test pyfmt("8.2%", 0.123456) == " 12.35%"
349401
@test pyfmt("<8.2%", 0.123456) == "12.35% "

0 commit comments

Comments
 (0)