Skip to content

Commit 4d26be0

Browse files
Fix string output of Base.format_bytes when binary=false (JuliaLang#52932)
Introduced in [50572](JuliaLang#50572) Before: ``` julia> Base.format_bytes(1, binary=false) "1 " julia> Base.format_bytes(11, binary=false) "11 s" ``` This PR: ``` julia> Base.format_bytes(1, binary=false) "1 byte" julia> Base.format_bytes(11, binary=false) "11 bytes" ``` Also add tests.
1 parent 081f045 commit 4d26be0

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

base/timing.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ function format_bytes(bytes; binary=true) # also used by InteractiveUtils
140140
factor = binary ? 1024 : 1000
141141
bytes, mb = prettyprint_getunits(bytes, length(units), Int64(factor))
142142
if mb == 1
143-
return string(Int(bytes), " ", units[mb], bytes==1 ? "" : "s")
143+
return string(Int(bytes), " ", _mem_units[mb], bytes==1 ? "" : "s")
144144
else
145145
return string(Ryu.writefixed(Float64(bytes), 3), binary ? " $(units[mb])" : "$(units[mb])B")
146146
end

test/misc.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,6 +1396,21 @@ end
13961396
@test (@allocations "a" * Base.inferencebarrier("b")) == 1
13971397
end
13981398

1399+
#TODO: merge with `@testset "Base/timing.jl"` once https://github.com/JuliaLang/julia/issues/52948 is resolved
1400+
@testset "Base/timing.jl2" begin
1401+
# Test the output of `format_bytes()`
1402+
inputs = [(factor * (Int64(1000)^e),binary) for binary in (false,true), factor in (1,2), e in 0:6][:]
1403+
expected_output = ["1 byte", "1 byte", "2 bytes", "2 bytes", "1000 bytes", "1000 bytes", "2.000 kB", "1.953 KiB",
1404+
"1000.000 kB", "976.562 KiB", "2.000 MB", "1.907 MiB", "1000.000 MB", "953.674 MiB",
1405+
"2.000 GB", "1.863 GiB", "1000.000 GB", "931.323 GiB", "2.000 TB", "1.819 TiB",
1406+
"1000.000 TB", "909.495 TiB", "2.000 PB", "1.776 PiB", "1000.000 PB", "888.178 PiB",
1407+
"2000.000 PB", "1776.357 PiB"]
1408+
1409+
for ((n, binary), expected) in zip(inputs, expected_output)
1410+
@test Base.format_bytes(n; binary) == expected
1411+
end
1412+
end
1413+
13991414
@testset "in_finalizer" begin
14001415
@test !GC.in_finalizer()
14011416

0 commit comments

Comments
 (0)