From d5f37d556a613a1704c9280230a75db05f5ba171 Mon Sep 17 00:00:00 2001 From: Guillem Jover Date: Tue, 20 Dec 2022 14:18:57 +0100 Subject: [PATCH] MT#56236 Support decimal and binary unit suffixes in humanize_bytes() This switches the output to the traditional SI binary unit suffixes, and to the decimal metric system unit suffixes. Change-Id: Icf3b6a0c074ad89014884eed3a9643085ee802cf (cherry picked from commit cb86f01b3305153175b88bbe25ae0febe2e9de89) --- lib/NGCP/BulkProcessor/Utils.pm | 24 ++++++--- t/Utils.t | 94 ++++++++++++++++----------------- 2 files changed, 64 insertions(+), 54 deletions(-) diff --git a/lib/NGCP/BulkProcessor/Utils.pm b/lib/NGCP/BulkProcessor/Utils.pm index 5c9586b..2027c21 100644 --- a/lib/NGCP/BulkProcessor/Utils.pm +++ b/lib/NGCP/BulkProcessor/Utils.pm @@ -489,19 +489,29 @@ sub getscriptpath { } -my @unit_suffix = qw( - Bytes - kBytes - MBytes - GBytes - TBytes - PBytes +my @dec_unit_suffix = qw( + bytes + kB + MB + GB + TB + PB +); + +my @bin_unit_suffix = qw( + bytes + KiB + MiB + GiB + TiB + PiB ); sub humanize_bytes { my ($number, $base, $round_integer) = @_; $base = 1024 if $base <= 0; + my @unit_suffix = $base == 1024 ? @bin_unit_suffix : @dec_unit_suffix; my $unit = 0; while ($unit < @unit_suffix && $number >= $base) { diff --git a/t/Utils.t b/t/Utils.t index 6b12df1..f0985ce 100644 --- a/t/Utils.t +++ b/t/Utils.t @@ -43,68 +43,68 @@ is(zerofill(25, 4), '0025'); is(zerofill(1000, 4), '1000'); # Unit conversion -is(humanize_bytes(1), '1 Bytes'); -is(humanize_bytes(1024), '1 kBytes'); -is(humanize_bytes(1024 ** 2), '1 MBytes'); -is(humanize_bytes(1024 ** 3), '1 GBytes'); -is(humanize_bytes(1024 ** 4), '1 TBytes'); -is(humanize_bytes(1024 ** 5), '1 PBytes'); +is(humanize_bytes(1), '1 bytes'); +is(humanize_bytes(1024), '1 KiB'); +is(humanize_bytes(1024 ** 2), '1 MiB'); +is(humanize_bytes(1024 ** 3), '1 GiB'); +is(humanize_bytes(1024 ** 4), '1 TiB'); +is(humanize_bytes(1024 ** 5), '1 PiB'); -is(humanize_bytes(2), '2 Bytes'); -is(humanize_bytes(2 * 1024), '2 kBytes'); -is(humanize_bytes(2 * (1024 ** 2) + 1), '2 MBytes'); -is(humanize_bytes(2 * (1024 ** 3) + 1024 + 1), '2 GBytes'); -is(humanize_bytes(2 * (1024 ** 4) + (1024 ** 2) + 1024 + 1), '2 TBytes'); +is(humanize_bytes(2), '2 bytes'); +is(humanize_bytes(2 * 1024), '2 KiB'); +is(humanize_bytes(2 * (1024 ** 2) + 1), '2 MiB'); +is(humanize_bytes(2 * (1024 ** 3) + 1024 + 1), '2 GiB'); +is(humanize_bytes(2 * (1024 ** 4) + (1024 ** 2) + 1024 + 1), '2 TiB'); -is(humanize_bytes(920), '920 Bytes'); -is(humanize_bytes(920 * 1024), '920 kBytes'); -is(humanize_bytes(942080 * 1024), '920 MBytes'); -is(humanize_bytes(964689920 * 1024), '920 GBytes'); +is(humanize_bytes(920), '920 bytes'); +is(humanize_bytes(920 * 1024), '920 KiB'); +is(humanize_bytes(942080 * 1024), '920 MiB'); +is(humanize_bytes(964689920 * 1024), '920 GiB'); -is(humanize_bytes(920, 1000), '920 Bytes'); -is(humanize_bytes(920000, 1000), '920 kBytes'); -is(humanize_bytes(920000000, 1000), '920 MBytes'); -is(humanize_bytes(920000000000, 1000), '920 GBytes'); +is(humanize_bytes(920, 1000), '920 bytes'); +is(humanize_bytes(920000, 1000), '920 kB'); +is(humanize_bytes(920000000, 1000), '920 MB'); +is(humanize_bytes(920000000000, 1000), '920 GB'); -is(humanize_bytes(942172), '920.08 kBytes'); -is(humanize_bytes(965632092), '920.89 MBytes'); +is(humanize_bytes(942172), '920.08 KiB'); +is(humanize_bytes(965632092), '920.89 MiB'); -is(humanize_bytes(920920, 1000), '920.92 kBytes'); -is(humanize_bytes(920920920, 1000), '920.92 MBytes'); +is(humanize_bytes(920920, 1000), '920.92 kB'); +is(humanize_bytes(920920920, 1000), '920.92 MB'); -is(humanize_bytes(942172, 1024, 1), '920 kBytes'); -is(humanize_bytes(965632092, 1024, 1), '920 MBytes'); +is(humanize_bytes(942172, 1024, 1), '920 KiB'); +is(humanize_bytes(965632092, 1024, 1), '920 MiB'); -is(humanize_bytes(920920, 1000, 1), '920 kBytes'); -is(humanize_bytes(920920920, 1000,1 ), '920 MBytes'); +is(humanize_bytes(920920, 1000, 1), '920 kB'); +is(humanize_bytes(920920920, 1000,1 ), '920 MB'); -is(kbytes2gigs(1), '1 kBytes'); -is(kbytes2gigs(1024), '1 MBytes'); -is(kbytes2gigs(1024 ** 2), '1 GBytes'); +is(kbytes2gigs(1), '1 KiB'); +is(kbytes2gigs(1024), '1 MiB'); +is(kbytes2gigs(1024 ** 2), '1 GiB'); -is(kbytes2gigs(2), '2 kBytes'); -is(kbytes2gigs(2 * 1024 + 1), '2 MBytes'); -is(kbytes2gigs(2 * 1024 ** 2 + 1024 + 1), '2 GBytes'); +is(kbytes2gigs(2), '2 KiB'); +is(kbytes2gigs(2 * 1024 + 1), '2 MiB'); +is(kbytes2gigs(2 * 1024 ** 2 + 1024 + 1), '2 GiB'); -is(kbytes2gigs(920), '920 kBytes'); -is(kbytes2gigs(920 * 1024), '920 MBytes'); -is(kbytes2gigs(920 * 1024 ** 2), '920 GBytes'); +is(kbytes2gigs(920), '920 KiB'); +is(kbytes2gigs(920 * 1024), '920 MiB'); +is(kbytes2gigs(920 * 1024 ** 2), '920 GiB'); -is(kbytes2gigs(920, 1000), '920 kBytes'); -is(kbytes2gigs(920 * 1000, 1000), '920 MBytes'); -is(kbytes2gigs(920 * 1000 ** 2, 1000), '920 GBytes'); +is(kbytes2gigs(920, 1000), '920 kB'); +is(kbytes2gigs(920 * 1000, 1000), '920 MB'); +is(kbytes2gigs(920 * 1000 ** 2, 1000), '920 GB'); -is(kbytes2gigs(92 + 920 * 1024), '920.08 MBytes'); -is(kbytes2gigs(92 + 920 * 1024 + 920 * 1024 ** 2), '920.89 GBytes'); +is(kbytes2gigs(92 + 920 * 1024), '920.08 MiB'); +is(kbytes2gigs(92 + 920 * 1024 + 920 * 1024 ** 2), '920.89 GiB'); -is(kbytes2gigs(920920, 1000), '920.92 MBytes'); -is(kbytes2gigs(920920920, 1000), '920.92 GBytes'); +is(kbytes2gigs(920920, 1000), '920.92 MB'); +is(kbytes2gigs(920920920, 1000), '920.92 GB'); -is(kbytes2gigs(92 + 920 * 1024, 1024, 1), '920 MBytes'); -is(kbytes2gigs(92 + 920 * 1024 + 920 * 1024 ** 2, 1024, 1), '920 GBytes'); +is(kbytes2gigs(92 + 920 * 1024, 1024, 1), '920 MiB'); +is(kbytes2gigs(92 + 920 * 1024 + 920 * 1024 ** 2, 1024, 1), '920 GiB'); -is(kbytes2gigs(920920, 1000, 1), '920 MBytes'); -is(kbytes2gigs(920920920, 1000,1 ), '920 GBytes'); +is(kbytes2gigs(920920, 1000, 1), '920 MB'); +is(kbytes2gigs(920920920, 1000,1 ), '920 GB'); # secs_to_years() is(secs_to_years(1), '1 second');